Skip to content

Latest commit

 

History

History
24 lines (21 loc) · 2.24 KB

File metadata and controls

24 lines (21 loc) · 2.24 KB

Part 2: For Loops

Do the following exercises in a file called for_fun_loops.py in the 02-range-and-loops folder.
You will probably want to pull your changes to your local clone before beginning these exercises to avoid merging later.

Remember to commit early and often!

  1. Using a for­ loop, print out the numbers from 1 to 20.
  2. Using a for­ loop, print out the numbers from 1 to 20, but if the number is 13, print the word "hello" instead of the number.
  3. Using a for­ loop, print out the numbers from 0 to 100 (including the number 100) in increments of 10. (e.g. 0, 10, 20, 30, … , 100)
  4. Using a for­ loop, print out the odd numbers from 0 to 10.
  5. Using a for­ loop, print out the numbers from 10 to 0 (including 0).
  6. Modify this for loop to print out "Blastoff!" instead of printing 0.
  7. Create a list called fruits that contains "apples", "oranges", and "bananas"
  8. Using a for­ loop, and not using the range function, go through the list, and print out each fruit.
  9. Using a for­ loop, and using the range function, go through the list and print out each fruit. Hint: Recall how to find out how many items are in a list from last time.
  10. Create a function called sum_nums that takes in a number called num. sum_nums should add up all of the numbers from 0 until (but not including) num. sum_nums should return this sum.
    Example: print sum_nums(3)3
  11. Modify sum_nums to add up all the numbers from 0 to num, including num.
    Example: print sum_nums(3)6
  12. Write a function called sum_nums2 that takes one parameter. If the parameter is negative, sum_nums2 should add up all of the numbers from 0 to the negative number and return that sum. If the parameter num is positive, sum_nums2 should work the same as sum_nums above.
    Example: print sum_nums2(-3)-6
  13. Write a function called fizz_buzz that prints the numbers 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".

Remember to push your changes when you're done! (Hopefully you've already been committing after each exercise!)