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 [2]:
st = 'Print only the words that start with s in this sentence'

In [7]:
#Code here

# to note: a for in for a string iterates through letters, not numbers
for word in st.split():
    letter = word[0].lower()
    if letter == 's':
        print word


start
s
sentence

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


In [9]:
#Code Here
range(0, 11, 2)


Out[9]:
[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 [10]:
#Code in this cell
[num for num in range(1, 50) if num % 3 == 0]


Out[10]:
[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 [17]:
st = 'Print every word in this sentence that has an even number of letters'

In [18]:
#Code in this cell
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 [13]:
#Code in this cell

def fizzbuzz(start, end):
    
    for i in range(start, end):
        is_fizzy = i % 3 == 0
        is_buzzy = i % 5 == 0
        
        if is_fizzy and not is_buzzy:
            print "Fizz"
        elif is_buzzy and not is_fizzy:
            print "Buzz"
        elif is_fizzy and is_buzzy:
            print "FizzBuzz"
        else:
            print i
            
fizzbuzz(0, 100)


FizzBuzz
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz

In [ ]:


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


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

In [16]:
#Code in this cell
running_list = []
for word in st.split():
    running_list.append(word[0])
print running_list


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

Great Job!