Lesson 3
Input & Output
Learn how to get information from the user and display results with Python.
What is input and output?
Programs often need to communicate with the user. They can receive information from the user and display information back to them.
Input is information that a program receives from the user.
Output is information that a program displays to the user.
In Python, we can use input() to get
information from the user and print()
to display information.
Getting input from the user
The input() function allows your program
to ask the user for information.
For example, you can ask the user for their name:
name = input("What is your name? ")
print(name)
When the program runs, Python waits for the user to type something and press Enter.
The information entered by the user is stored in the
name variable.
Combining input with text
You can use the information from input()
together with other text.
name = input("What is your name? ")
print("Hello,", name)
If the user enters Alex, the program displays:
Hello, Alex
You can also ask for other information and store it in different variables.
name = input("What is your name? ")
hobby = input("What is your hobby? ")
print("Your name is", name)
print("Your hobby is", hobby)
Working with numbers
The input() function returns the user's
answer as a string, even when the user enters a number.
age = input("How old are you? ")
print(age)
If the user enters 15, Python stores
"15" as text, not as an integer.
To convert this text into a whole number, we can use
int().
age = int(input("How old are you? "))
print(age + 1)
If the user enters 15, the output will be:
16
We use float() when we need a number
that can contain a decimal part.
price = float(input("Enter the price: "))
print(price)