Worksheet: Inputs and Outputs in Python
Examples
1. Example 1: Printing a Message
The following code prints the message "Hello, World!" to the screen.
print("Hello, World!")
2. Example 2: Storing and Printing a Number
The number 7 is stored in the variable number, which is then printed.
number = 7
print(number)
3. Example 3: Taking User Input
This code takes the user's input and prints it with a message.
user_input = input("Please enter your name: ")
print("Your name is", user_input)
4. Example 4: Adding Two Numbers from User Input
The program takes two numbers as input, adds them, and prints the result.
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
total = num1 + num2
print("The sum is", total)
5. Example 5: Concatenating Strings
This program takes the first and last name from the user and prints the full name.
first_name = input("Enter your first name: ")
last_name = input("Enter your last name: ")
full_name = first_name + " " + last_name
print("Your full name is", full_name)
Questions
1. Question 1
Create a program that asks the user for their favorite color and prints a message with
that color.
Write your code here.
2. Question 2
Write a program that takes two numbers from the user, subtracts the second number
from the first, and prints the result.
Write your code here.
3. Question 3
Create a program that takes the user's name and age as input. Print a message saying,
"Your name is [name] and you are [age] years old."
Write your code here.
4. Question 4
Write a program that takes two numbers from the user, multiplies them, and prints the
product.
Write your code here.
5. Question 5
Is the following variable name correct? Why or why not?
USER_NAME
Explain your answer.