Skip to main content

Exercices for Variables

Exercise Set 1: Variable Basics

  1. Name Game Write a program that:

    • Creates variables for first name, last name, and age
    • Follows Python naming conventions (snake_case)
    • Prints all variables
    • Includes comments explaining your naming choices
  2. Variable Swap Create a program that:

    • Creates two variables with different values
    • Swaps their values
    • Prints the values before and after swapping

Exercise Set 2: Variable Naming Rules

  1. Identify Valid Names Look at these variable names and comment which are valid and which are invalid:

    # Add comments to explain why each is valid or invalid
    first_name = "John"
    1st_name = "John"
    firstName = "John"
    first-name = "John"
    _first_name = "John"
    first name = "John"
  2. Fix Bad Names Improve these variable names according to Python conventions:

    x = "John Smith"
    a = 25
    thing = "Developer"
    DATA = True

Exercise Set 3: Variable Reassignment

  1. Value Tracking Write a program that:
    • Creates a variable called counter
    • Assigns it different values five times
    • Prints the value after each assignment
    • Comments showing what you expect the output to be