Difference between revisions of "Python Problems and Solutions"

From rbachwiki
Jump to navigation Jump to search
Line 8: Line 8:
  for num in range(0,11,2):
  for num in range(0,11,2):
   print(num)
   print(num)
Or using Modulos
for i in range(0,11)
  if i%2 ==0:
  print(i)
Go through the string below and if the length of a word is even print "even!"
st = 'Print every word in this sentence that has an even number of letters'
for i in st.split():
  if len(i) % 2 ==0:
  print(i + "is even")
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".

Revision as of 21:35, 2 February 2019

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

Use Range() to print all even numbers from 0 to 10

for num in range(0,11,2):
  print(num)

Or using Modulos

for i in range(0,11)
 if i%2 ==0:
 print(i)

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

st = 'Print every word in this sentence that has an even number of letters'
for i in st.split():
 if len(i) % 2 ==0:
  print(i + "is even")

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".