Python Practice Activities

--First, solve the problem. Then, write the code -- John Johnson





Try these activities in Trinket and then change them to see the effects of different variables.
Practice looping through lists:
1. Write a for loop to print only the odd numbers from this list
Comment your work!
list = [3,4,7,13,54,32,653,256,1,41,65,83,92,31]

Dictionaries: Put this data from this table into a dictionary and print it out:

Name Number
Arnold Schwarzenegger 8
Oprah Winfrey 40
Michael Jordan 23
Justin Beiber 31
Taylor Swift 24
Halle Berry 19
Stephen Curry 30
2. Looping through lists numbers = [2, 4, 244, 24, 13, 44, 53] Write a for loop to print out each number squared Write a for loop to print out three times the numbers in the list Write a for loop to print out “My new favorite number is ” and then each number Try to figure out the code that resulted in the following output: 3. Hint: lists What is your name? David hello David it is nice to meet you hello David it is nice to meet you hello David it is nice to meet you hello David it is nice to meet you hello David it is nice to meet you hello David it is nice to meet you hello David it is nice to meet you hello David it is nice to meet you hello David it is nice to meet you 4. Hint: loop with counter when I was 0 my favorite color was red when I was 1 my favorite color was orange when I was 2 my favorite color was gold when I was 3 my favorite color was blue when I was 4 my favorite color was green when I was 5 my favorite color was purple when I was 6 my favorite color was crimson when I was 7 my favorite color was yellow 5.Conditionals: Use both input and if...else statements to create a trivia quiz. Let the user know if they get the right answer. Print a message depending on the answer the user selected. Example: grace_hopper = input("Grace Hopper coined what Computer Science term? A) Byte, B) Bug, C) Iterate, D) Function") if grace_hopper.lower() == "b": print ("You remembered!") else: print ("No! check out her biography at http://www.biography.com/people/grace-hopper-21406809.") Practice these other if statements: --Given a number x, print "even" if x is even, and "odd" if x is odd: x=4 --Given two numbers, a, b, print "even" if the sum of a and b is even, otherwise print "odd": a=5 b=6 --Given a day of the week, print "weekday" if it is a weekday, or print "weekend" otherwise. Solve this problem with only an if-else statement. day="Monday" --now try the day of the week program with user input 6. While loops: Write code that counts down from any number. Start like this: n= input("What number would you like me to start at? \n") Write a program called Countdown: It takes any number as input, starts counting down and ends with a BOOM! It should look like this: Enter number: 10 10 9 8 7 6 5 4 3 2 1 BOOM! Write the code that has the following output: What number month would you like to know the name of? 3 That month is called March hint: This code uses an input,a list and conditionals 7. FizzBuzz! Print the numbers 1 → 100 except… For multiples of 3, print “Fizz” instead of the number For multiples of 5, print “Buzz” instead of the number For multiples of 3 & 5, print “FizzBuzz” instead of the number Start here: for x in range(1,101): print x # Hint:How do you check if a number is divisible by 3? what would the remainder be? It should look like this: 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 8. Functions: Maximum Value--Write a function, max_value, that takes in an integer and prints the numbers from 1 to that number inclusively. Stars--Write a function, swapping_stars, that will print out the following: * - * - * - - * - * - * * - * - * - - * - * - * * - * - * - - * - * - * is_even--Define a function is_even that will take a number x as input: If x is even, then return True. Otherwise, return False. is_int--Define a function is_int that takes a number x as an input. Have it return True if the number is an integer (as defined above) and False otherwise.

If you are stuck, the solution to 1 and 2 is here; 1 dictionary solution is here and 3 and 4 is here. The solution for 5 is here; the solution for 6 is here and the months code is here; the solution for #7 FizzBuzz. #8:Swapping stars.The 8 functions: Function Practice Now challenge yourself by building the functions at at 46 Simple Python Exercises. Next try some of the activities on the online book Invent Games with Python