Python — Variables and data types

Lesson 5 of 26 · Basic Python

Welcome to this lesson of the Basic Python course! Today you will learn how Python remembers data, how to use different types of values (numbers and text), and how to get input from the user with input().


1. What are variables?

A variable is a "box" in which we keep a value. It has a name and a value, for example:

x = 10
name = "Ana"
pi = 3.14

The left side (x, name, pi) is the name of the variable, and the right side is the value we keep.


2. Basic data types

Python has several commonly used types:

  • int – a whole number (10, -5, 2024)
  • float – a decimal number (3.14, 2.5, -0.1)
  • str (string) – text ("Hello", "Python")
  • bool – logical values (True, False)

You can check the type of a variable with the function type():

x = 42
print(type(x))       # <class 'int'>

y = 3.14
print(type(y))       # <class 'float'>

name = "Ana"
print(type(name))    # <class 'str'>

active = True
print(type(active))  # <class 'bool'>

3. Entering data — input()

The function input() stops the program and waits for the user to type something on the keyboard.

Example:

name = input("What is your name? ")
print("Hello,", name)

IMPORTANT: input() ALWAYS returns a string. If you need a number, you have to convert it:

age = int(input("How old are you? "))
print("In 10 years you will be:", age + 10)

4. Formatting text

Python gives you several ways to join text and variables nicely. The easiest way is an f-string (a formatted string):

name = "Ana"
age = 14

print(f"{name} is {age} years old.")

It can also be done the older way:

print("{} is {} years old.".format(name, age))

5. MINI PROJECT — A program for age / birthday

In this mini project the user enters their year of birth, and the program calculates how old they are.

from datetime import date

birth_year = int(input("Enter your year of birth: "))

current_year = date.today().year   # the current year
age = current_year - birth_year

print(f"You are {age} years old.")

if age < 18:
    print("You are a minor.")
else:
    print("You are an adult.")

You can extend the program to also ask for the month of birth or to calculate how many days old you are. Let your imagination run free!




6. Practical examples of entering data

Now that you know how the function input() works and how to convert data into numbers, let us look at a few useful, real examples that you can try straight away.


Example 1: Converting currencies (EUR → RSD)

The user enters an amount in euros, and the program converts it into Serbian dinars at a given exchange rate.


rate = 117.3   # an example exchange rate

euros = float(input("Enter an amount in euros: "))
dinars = euros * rate

print(f"{euros} EUR is about {dinars} RSD.")

Example 2: Converting an angle (degrees → radians)

Radians are often used in mathematics. The formula is:
rad = degrees × π / 180


import math

degrees = float(input("Enter an angle in degrees: "))
radians = degrees * math.pi / 180

print(f"{degrees}° is {radians} radians.")

Example 3: The average of three numbers (great for practice)

The user enters three numbers, and the program calculates their arithmetic mean.


a = float(input("Enter the first number: "))
b = float(input("Enter the second number: "))
c = float(input("Enter the third number: "))

average = (a + b + c) / 3

print(f"The average of the entered numbers is: {average}")

Recommendation for further practice

If you want more tasks for working with variables, data types and entering numbers, I recommend that you continue here:

➡ Examples of tasks for independent work (data and input)

On that page there are more tasks of a similar level that will help you master entering data, calculating and working with numbers.

7. Advanced tasks — for students who want more

If you have mastered the basic examples of entering data, try the following tasks. They combine more knowledge: entering data, mathematical operations, logic and formatting.


Task 1 — An average speed calculator

Write a program that calculates the average speed of a vehicle. The user enters the distance travelled (in kilometres) and the driving time (in hours and minutes).

Requirements:

  • the user enters the kilometres as a float,
  • enters the hours and minutes as whole numbers,
  • the program converts everything into hours,
  • calculates the average speed: v = distance / time,
  • shows the result with two decimal places.

distance = float(input("Enter the distance travelled (km): "))
hours = int(input("Enter the time — hours: "))
minutes = int(input("Enter the time — minutes: "))

total_hours = hours + minutes / 60
speed = distance / total_hours

print(f"The average speed is {speed:.2f} km/h.")

Task 2 — A program that separates the digits of a number and calculates a special value

The user enters a three-digit number. The program should:

  • separate the units, tens and hundreds digits,
  • calculate the sum of the digits,
  • calculate the number in reverse order (for example 483 → 384),
  • print all the results.

This task uses the operators // and % and is an excellent introduction to working with digits.


n = int(input("Enter a three-digit number: "))

hundreds = n // 100
tens = (n // 10) % 10
units = n % 10

digit_sum = hundreds + tens + units
reversed_number = units * 100 + tens * 10 + hundreds

print("Hundreds:", hundreds)
print("Tens:", tens)
print("Units:", units)
print("Sum of the digits:", digit_sum)
print("Reversed number:", reversed_number)
Enter a three-digit number: 483
Hundreds: 4
Tens: 8
Units: 3
Sum of the digits: 15
Reversed number: 384

If you want more challenging tasks, look at the page with examples: Examples for independent work — data

Hidden mini quiz — variables, types and entering data

Click to check your knowledge of variables, types and the function input()!

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 = 5
y = "3"
print(x + int(y))



3. Which is a correctly formatted f-string?




4. What does the function type() do?




5. What will the program do?


name = input("Enter a name: ")
print("Hi,", name)