Statements Assessment Test

Lets test your knowledge!


Use for, split(), and if to create a Statement that will print out words that start with 's':


In [1]:
st = 'Print only the words that start with s in this sentence'

In [3]:
#Code here
st = 'Print only the words that start with s in this sentence'
for word in st.split():
    if word[0] == 's':
        print(word )


start
s
sentence

Use range() to print all the even numbers from 0 to 10.


In [6]:
#Code Here
for number in range(0,11):
    if number % 2 == 0:
        print(number)


0
2
4
6
8
10

Use List comprehension to create a list of all numbers between 1 and 50 that are divisible by 3.


In [9]:
#Code in this cell
l = [number for number in range(1,51) if number % 3 == 0]
print(l)


[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48]

Go through the string below and if the length of a word is even print "even!"


In [6]:
st = 'Print every word in this sentence that has an even number of letters'

In [11]:
#Code in this cell
st = 'Print every word in this sentence that has an even number of letters'
for word in st.split():
    if len(word) % 2 == 0:
        print(word)


word
in
this
sentence
that
an
even
number
of

Write a program that prints the integers from 1 to 100. But for multiples of three print "Fizz" instead of the number, and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".


In [15]:
#Code in this cell
l = range(1,101)
for val in l:
    if val % 3 == 0 and val % 5 == 0:
        print ('FizzBuzz num ' + str(val))
    elif val % 3 == 0:
        print('Fizz num ' + str(val))
    elif val % 5 ==0 :
        print('Buzz num ' + str(val))


Fizz num 3
Buzz num 5
Fizz num 6
Fizz num 9
Buzz num 10
Fizz num 12
FizzBuzz num 15
Fizz num 18
Buzz num 20
Fizz num 21
Fizz num 24
Buzz num 25
Fizz num 27
FizzBuzz num 30
Fizz num 33
Buzz num 35
Fizz num 36
Fizz num 39
Buzz num 40
Fizz num 42
FizzBuzz num 45
Fizz num 48
Buzz num 50
Fizz num 51
Fizz num 54
Buzz num 55
Fizz num 57
FizzBuzz num 60
Fizz num 63
Buzz num 65
Fizz num 66
Fizz num 69
Buzz num 70
Fizz num 72
FizzBuzz num 75
Fizz num 78
Buzz num 80
Fizz num 81
Fizz num 84
Buzz num 85
Fizz num 87
FizzBuzz num 90
Fizz num 93
Buzz num 95
Fizz num 96
Fizz num 99
Buzz num 100

Use List Comprehension to create a list of the first letters of every word in the string below:


In [8]:
st = 'Create a list of the first letters of every word in this string'

In [21]:
#Code in this cell
st = 'Create a list of the first letters of every word in this string'
l = []
for word in st.split():
    l.append(word[0])
print(l)


['C', 'a', 'l', 'o', 't', 'f', 'l', 'o', 'e', 'w', 'i', 't', 's']

Great Job!