Estimated time: 15–20 minutes
In this module, you will learn how Python stores information using variables and how programs can receive input from a user and display output.
Learning Goals
By the end of this module, you should be able to:
- Create and use variables
- Store basic values such as text and numbers
- Display information using
print() - Get information from a user using
input() - Use variables together with input and output
1. Variables
A variable is used to store information that can be used later in a program.
For example:
name = "Alex"
age = 20
Here:
namestores the text"Alex"agestores the number20
You can then use those variables later in your program.
name = "Alex"
print(name)
Output:
Alex
Variables can store different types of information.
name = "Alex"
age = 20
score = 95.5
2. Output with print()
The print() function displays information to the user.
print("Hello!")
Output:
Hello!
You can also print variables:
name = "Alex"
print(name)
Or combine text and variables:
name = "Alex"
print("Hello", name)
Output:
Hello Alex
3. Input with input()
The input() function allows the user to enter information into the program.
name = input("What is your name? ")
print("Hello", name)
If the user enters:
Maya
The program displays:
Hello Maya
The value entered by the user is stored inside the name variable.
Watch and Learn
Watch this short video for another explanation of Python variables and input/output.
You can use the video, the written explanation above, or both.
Try It Yourself
Try creating this simple program:
name = input("What is your name? ")
age = input("How old are you? ")
print("Hello", name)
print("You are", age, "years old.")
Run the program and enter your own information.
Then try changing it so it also asks for something else, such as:
- Favourite game
- Favourite sport
- Favourite food
- Favourite programming interest
Choose an Activity Based on Your Interests
Choose one of the activities below. They all practise the same concepts: variables, input, and output.
General Programming
Create a simple profile program.
name = input("Enter your name: ")
hobby = input("Enter your hobby: ")
print("Name:", name)
print("Hobby:", hobby)
Cybersecurity
Create a program that asks the user for a username and displays it back to them.
Example:
username = input("Enter your username: ")
print("Username entered:", username)
Try adding another variable such as a website or account name.
Game Development
Create a program that asks for a player’s name and starting score.
Example:
player = input("Enter your player name: ")
score = input("Enter your score: ")
print("Player:", player)
print("Score:", score)
Data Analysis
Create a program that asks the user to enter two pieces of information that could be stored as data.
Example:
city = input("Enter a city: ")
temperature = input("Enter the temperature: ")
print("City:", city)
print("Temperature:", temperature)
Automation
Create a program that asks the user for a task they want to complete.
task = input("Enter a task: ")
print("Task added:", task)
Check Your Understanding
Complete the short quiz below before moving on.
What Should You Do Next?
If you understood most of the questions and completed the coding activity successfully, continue to:
If you found this module difficult, review the examples above and try another coding activity before moving forward.
You can also use the Questions & Help channel in our Discord if you need help.
Proudly powered by WordPress