Practice – Printing, Variables, Math, Input

Work on the following tasks to build your Python programming skills. The tasks are arranged from easier to more challenging, so try them in order. Copy and paste the provided starter code into your code editor where needed.

See the Python Basics notes first for examples.

  1. Write a program that adds 5 to a number, then multiplies the result by 2. Print the final result.
    number = 10
    # add 5 and multiply by 2, then print the result
    
  2. Print the following text, using an escape character to handle the quotes:
    She said, "Hello!"
  3. Create a program that concatenates two strings and prints the result. Use two variables to store the strings and then combine them.
    first_name = "Harry"
    last_name = "Potter"
    full_name = # add code here
    print(full_name)
    
  4. Create three variables: x, y, and z. Assign the values 10, 5.5, and "Hello" to them, respectively. Print each variable on a new line.
  5. Write a program that calculates the area of a rectangle. The width and height should be stored in variables width and height, and the area should be stored in the variable area. Print the area.
    width = 8
    height = 5
    area = # calculate the area
    print("The area of the rectangle is", area)
    
  6. Create a program that takes a number stored in a variable and adds 10 to it using the += operator. Print the result.
    number = 15
    # add 10
    print("The new number is", number)
    
  7. Create a program that prints a sentence combining literal text with variable values. Include the name and age in a sentence. Hint: You can use str() function to convert a number so it can be concatenated with another string using + operator.
    name = "Alice"
    age = 30
    print(# complete this line to include the name and age)
    
  8. Ask the user to enter their favorite color and store it in a variable. Use input(). Then, print a message that includes the entered color.
    color = # get the user's input
    # print the message with the color
    
  9. Add comments to explain each line of code in the following program:
    num1 = 5  
    num2 = 7 
    sum = num1 + num2 
    print("The sum is", sum)
    
  10. Create a program that finds the minimum of two numbers and prints the result. Use the min() function.
    a = 9
    b = 3
    smallest = # find the smallest number
    
  11. Write a program that calculates and prints the absolute value of a given number. Use the abs() function.
    number = -20
    absolute_value = # find the absolute value
    
  12. Ask the user to enter a number. Convert this input to an integer and multiply it by 10. Print the result.
    user_input = input("Enter a number: ")
    number = # convert user_input to an integer
    result = number * 10
    print("The result is", result)
    
  13. Ask the user to enter a word. Convert the word to all uppercase letters and print it.
    word = input("Enter a word: ")
    upper_word = # convert word to uppercase
    
  14. Write a program that divides two numbers and prints the quotient. Use the // operator to perform integer division.
    numerator = 20
    denominator = 3
    quotient = # calculate the quotient
    print("The quotient is", quotient)
    
  15. Ask the user to enter two numbers, then multiply them together. Print the product. Hint: you will need to use the int() function to convert the input() function’s output string to a number.
    num1 = input("Enter the first number: ")
    num2 = input("Enter the second number: ")
    # multiply the numbers and print the result
    
  16. Create a program that calculates the remainder when one number is divided by another. Use the modulus operator %.
    num1 = 17
    num2 = 5
    remainder = # calculate the remainder
    print("The remainder is", remainder)
    
  17. Write a program that prints the type of the variable my_var. Use the type() function.
    my_var = 12.34
    print("The type of my_var is", # enter code here)
    
  18. Ask the user to enter their name and convert it to lowercase. Print the lowercase name.
    name = # use input() function
    lower_name = # convert name to lowercase
    print(# print the lowercase name)
    
  19. Ask the user for two numbers, then print which number is larger using the max() function. Hint: use int() function to convert the user strings to numbers.
    num1 = input("Enter the first number: ")
    num2 = input("Enter the second number: ")
    larger_number = # find the larger number
    print("The larger number is", larger_number)
    
  20. Write a program that asks the user for a string and an integer. Print the string repeated as many times as the integer. Hint: you can multiply a string by an integer.
    text = input("Enter a word: ")
    times = int(input("How many times should it be repeated? "))
    result = # repeat the string
    print(result)
    
  21. Write a program that prints the square of a number using the exponent operator **.
    number = 4
    # calculate the square and print it
    
  22. Write a program that calculates and prints the result of the following expression: (2+3)^3 + (9 \times 4)
    # calculate the result and print it
    
  23. Ask the user to enter a sentence. Print the sentence with each word on a new line using the \n escape character. Provide only the sentence, not the print() statement or the \n character.
    sentence = input("Enter a sentence: ")
    # split the sentence and print each word on a new line
    
  24. Ask the user to enter the price of an item, then convert this input to a float and calculate the total cost for 3 items. Print the total cost.
    price = input("Enter the price of one item: ")
    # convert to float and calculate total cost for 3 items
    
  25. Create a program that asks the user for a number and then prints that number three times in a row on the same line, separated by spaces. Do not provide the print statement or the separator.
    number = input("Enter a number: ")
    # print the number three times on the same line
    
  26. Write a program that asks the user to enter their name, then prints the name with a greeting on three separate lines. Use the \n character to create the line breaks.
    name = input("Enter your name: ")
    # print the greeting with line breaks
    
  27. Ask the user to enter a number, then subtract 5 from it and print the result.
    number = int(input("Enter a number: "))
    # subtract 5 and print the result
    
  28. Ask the user to enter a number. Print it three times in a row on the same line, separated by spaces.
    number = input("Enter a number: ")
    # print the number three times on the same line with spaces
    
  29. Write a program that asks the user for two numbers, adds them together, and prints the result.
    num1 = input("Enter the first number: ")
    num2 = input("Enter the second number: ")
    # add the numbers and print the result