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!
- Using a
for
loop, print out the numbers from 1 to 20. - 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. - 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) - Using a
for
loop, print out the odd numbers from 0 to 10. - Using a
for
loop, print out the numbers from 10 to 0 (including 0). - Modify this for loop to print out
"Blastoff!"
instead of printing 0. - Create a list called fruits that contains
"apples"
,"oranges"
, and"bananas"
- Using a
for
loop, and not using the range function, go through the list, and print out each fruit. - Using a
for
loop, and using therange
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. - Create a function called
sum_nums
that takes in a number callednum
.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
- Modify sum_nums to add up all the numbers from 0 to
num
, includingnum
.
Example:print sum_nums(3)
⇒6
- 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 parameternum
is positive,sum_nums2
should work the same assum_nums
above.
Example:print sum_nums2(-3)
⇒-6
- 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!)