Python Coding Exercises

Python Coding Exercises

1. Variable Declarations and Data Types

a) Declare variables for each of the following data types: integer, float, string, and Boolean. Assign appropriate values to each.

b) Print out each variable and its type using the type() function.

2. Lists, Tuples, Sets, and Dictionaries

a) Create a list of your favorite foods.

b) Create a tuple containing the names of three countries.

c) Create a set of unique numbers.

d) Create a dictionary that represents a person with keys for name, age, and occupation.

e) Print each of these data structures and their types.

3. For Loops

a) Use a for loop to iterate through your list of favorite foods and print each one.

b) Create a list of numbers from 1 to 10, then use a for loop to print the square of each number.

4. While Loops

a) Use a while loop to print numbers from 1 to 5.

b) Create a guessing game where the user has to guess a number between 1 and 10. Use a while loop to allow multiple guesses until the correct number is guessed.

5. Conditional Statements

a) Write a program that asks the user for their age and prints whether they are a child (0-12), teenager (13-19), adult (20-64), or senior (65+).

b) Modify your guessing game from exercise 4b to give “higher” or “lower” hints after each incorrect guess.

6. Combining Concepts

a) Create a dictionary where the keys are names and the values are ages. Use a for loop to iterate through the dictionary and print “{name} is {age} years old” for each person.

b) Write a program that creates a list of 10 random numbers between 1 and 100. Then, use a for loop and conditional statements to separate the numbers into two lists: one for even numbers and one for odd numbers.

7. Advanced Exercise

Create a simple address book program that does the following:

  1. Uses a dictionary to store names and phone numbers.
  2. Provides a menu with options to:
    • Add a new contact
    • Look up a phone number
    • Delete a contact
    • Quit the program
  3. Uses a while loop to keep the program running until the user chooses to quit.
  4. Uses conditional statements to perform the correct action based on the user’s choice.
  5. Uses appropriate data types for storing names and phone numbers.