Python input() — how to enter data
In this lesson you will learn how Python receives data from the user with the function input(). This is one of the most important functions for beginners — it makes interaction between the user and the program possible.
1. How does input() work?
The function input() stops the program and waits for the user to type something and press Enter.
The result the user enters is kept in a variable.
name = input("What is your name? ")
print("Hello,", name)
The entered text is kept as a string.
2. Everything the user enters is a string
Important: input() ALWAYS returns a string.
Even if the user enters a number, Python sees it as text.
age = input("How old are you? ")
print(age + 1) # error! string + number
That is why we have to convert numbers from a string into a number by hand.
3. Converting input into a number
For a whole number we use int(), and for a decimal number float().
age = int(input("How old are you? "))
print(age + 1)
temperature = float(input("Enter the temperature: "))
print("In 10 minutes the temperature will be:", temperature + 0.5)
4. Examples
Entering a name
name = input("Enter a name: ")
print("Hello,", name)
Entering an age
age = int(input("How old are you? "))
print("In a year you will be:", age + 1)
Entering two numbers and adding them
a = float(input("First number: "))
b = float(input("Second number: "))
total = a + b
print("The sum is:", total)
5. Mini tasks
- Write a program that asks the user for their first and last name and prints a welcome message.
- Write a program that asks for the year of birth and prints how old the user is.
- Write a program that reads three numbers and prints their sum.
Congratulations! Now you know how to use input() and convert input into numbers — which is the basis for real small programs.
A mini Python game — Fill in the code
Practise basic Python code by filling in the empty places. This game comes before learning selection (if) and is used for learning variables, addition and printing.
Game 1: Showing a name
Fill in the empty place so that the user's name is printed:
name = "Ana"
print(______)
Game 2: Adding numbers
Fill in the empty place so that the sum of a and b is printed:
a = 5
b = 3
print(______) # should show 8
6. More advanced examples with input()
Now we will see how user input can be used in more concrete problems. In these examples we combine int, float and string input and basic calculations.
Example 1: Calculating an average mark (int)
# Entering the marks
mark1 = int(input("Enter the first mark: "))
mark2 = int(input("Enter the second mark: "))
mark3 = int(input("Enter the third mark: "))
# Calculating the average
average = (mark1 + mark2 + mark3) / 3
print("The average is:", average)
Here we enter whole numbers and use them to calculate the average.
Without int(), adding would not work as we expect.
Example 2: Converting money (float)
# Entering an amount in euros
eur = float(input("Enter an amount in euros: "))
rate = 117.5
# Converting into Serbian dinars
rsd = eur * rate
print(eur, "EUR is", rsd, "dinars")
We use float() because money can be a decimal number.
Example 3: Creating a simple message (string)
name = input("Enter a name: ")
city = input("Enter a city: ")
message = f"Hello {name}! How is it in {city}?"
print(message)
Here we combine two string inputs and form a message using an f-string.
7. Practice tasks
Test your knowledge with these tasks. The first three have an example solution, and the next four are for independent work.
Tasks with a solution
- Calculating the perimeter of a rectangle (int)
# Entering the lengths of the sides a = int(input("Enter the length of side a: ")) b = int(input("Enter the length of side b: ")) # The perimeter of the rectangle perimeter = 2 * (a + b) print("The perimeter of the rectangle is:", perimeter) - Calculating fuel consumption (float)
# Entering the distance travelled and the consumption per kilometre km = float(input("Enter the distance travelled (km): ")) consumption_per_km = float(input("Enter the consumption per kilometre (l/km): ")) # Total consumption total = km * consumption_per_km print("The total fuel consumption is:", total, "litres") - Making a user greeting (string)
name = input("Enter your first name: ") surname = input("Enter your last name: ") message = "Hello, " + name + " " + surname + "!" print(message)
Tasks for independent work
- Write a program that asks the user for three numbers and prints their product.
- Write a program that asks the user for the height and the base of a triangle and calculates the area.
- Write a program that asks the user for their name and year of birth, and then prints a message saying how old they are.
- Write a program that asks the user for the name of a product and the price, and then prints the message: "Product X costs Y dinars".
Now you are ready to combine data input with mathematical and text operations. Practise these tasks and try to solve them on your own!
8. Bonus challenges: combining int, float and string input
These tasks are a bit more demanding because they combine different data types and several calculation steps. They are excellent for practising logic and controlling input.
Example 1: Calculating a discount on a purchase
# Entering the user's name and the price of the product
name = input("Enter your name: ")
price = float(input("Enter the price of the product: "))
discount = int(input("Enter the discount in percent: "))
# Calculating the price after the discount
discounted_price = price * (1 - discount / 100)
print(f"{name}, the price after the discount is: {discounted_price:.2f} dinars")
Here we use a string for the name, a float for the price and an int for the discount percentage.
We apply the discount formula and format the result with two decimal places.
Example 2: Checking height and categorising
name = input("Enter a name: ")
height = float(input("Enter your height in cm: "))
if height < 150:
category = "short"
elif height <= 180:
category = "medium"
else:
category = "tall"
print(f"{name}, your height is {height} cm and you belong to the category: {category}.")
We combine string and float input and use the if-elif-else structure for categorising.
Example 3: A simple calculator
print("A simple calculator")
number1 = float(input("Enter the first number: "))
number2 = float(input("Enter the second number: "))
operation = input("Choose an operation (+, -, *, /): ")
if operation == "+":
result = number1 + number2
elif operation == "-":
result = number1 - number2
elif operation == "*":
result = number1 * number2
elif operation == "/":
if number2 != 0:
result = number1 / number2
else:
result = "Error: division by zero!"
else:
result = "Unknown operation"
print("Result:", result)
This example combines two float inputs and a string input for choosing the operation.
It gives good insight into controlling the flow of a program and working with different data types.
Bonus tasks for independent work
- Write a program that asks the user for their name, the number of hours worked and the price per hour, and then prints the pay.
- Write a program that reads a student's name and three marks, calculates the average mark and prints a message with the name and the average.
- Write a program that asks the user for the name of a product, the quantity and the price per piece, and then prints the total price.
- Write a program that asks the user for their name and year of birth, and then prints a message saying whether they are an adult or a minor.
These tasks are excellent practice for applying everything we have learned about input(), type conversion and working with different values in Python.
Mini quiz — The input() function and type conversion
Click to check your knowledge of data input and working with types in Python!
Open the quiz
Mini quiz: Have you mastered the lesson?
1. Which data type does the function input() return?
2. What will be the result of the following code?
x = 7
y = "2"
print(x + int(y))3. Which line correctly combines a string and a number in a message using an f-string?
4. Which function is used to convert a string into a decimal number?
5. What will the program do?
name = input("Enter a name: ")
print("Hello,", name)
6. Which statement correctly adds two numbers entered with input?
