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 )
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)
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)
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)
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))
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)