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.
- 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
- Print the following text, using an escape character to handle the quotes:
She said, "Hello!"
- 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)
- Create three variables:
x
,y
, andz
. Assign the values10
,5.5
, and"Hello"
to them, respectively. Print each variable on a new line. - Write a program that calculates the area of a rectangle. The width and height should be stored in variables
width
andheight
, and the area should be stored in the variablearea
. Print the area.width = 8 height = 5 area = # calculate the area print("The area of the rectangle is", area)
- 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)
- 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)
- 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
- Add comments to explain each line of code in the following program:
num1 = 5 num2 = 7 sum = num1 + num2 print("The sum is", sum)
- 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
- 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
- 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)
- 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
- 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)
- 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 theinput()
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
- 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)
- Write a program that prints the type of the variable
my_var
. Use thetype()
function.my_var = 12.34 print("The type of my_var is", # enter code here)
- 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)
- Ask the user for two numbers, then print which number is larger using the
max()
function. Hint: useint()
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)
- 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)
- Write a program that prints the square of a number using the exponent operator
**
.number = 4 # calculate the square and print it
- Write a program that calculates and prints the result of the following expression:
# calculate the result and print it
- 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 theprint()
statement or the\n
character.sentence = input("Enter a sentence: ") # split the sentence and print each word on a new line
- 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
- 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
- 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
- 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
- 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
- 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