Python Variables, Types and Input
Use variables, strings, integers and floats, read user input and convert it to numbers with int().
In Lesson 1 you printed text and numbers directly. Real programs need to remember things: a name, a score, a price. That’s what variables are for. In this lesson you’ll create variables, learn the three basic data types and write a program that talks back to the user.
Variables: names for values
A variable is a name that refers to a value. You create one with =:
name = "Aisha"
age = 19
print(name)
print(age)
Read = as “is set to”, not “equals”. The line age = 19 means “set age to 19”. You can change it later:
score = 0
score = score + 10
print(score) # 10
The second line takes the current value of score (0), adds 10 and stores the result back in score.
Naming rules: variable names can contain letters, digits and underscores, but can’t start with a digit or contain spaces. By convention, Python programmers use lowercase words joined by underscores, such as total_price or first_name. Names are case-sensitive, so Score and score are different variables.
Three basic types
Every value in Python has a type. You can check it with type():
>>> type("hello")
<class 'str'>
>>> type(42)
<class 'int'>
>>> type(3.5)
<class 'float'>
| Type | Name | Examples |
|---|---|---|
str |
string (text) | "hello", '42', "" |
int |
integer (whole number) | 0, 7, -3 |
float |
decimal number | 3.5, 0.1, -2.0 |
Types matter because they decide what operations do:
print(3 + 4) # 7 (int + int adds)
print("3" + "4") # 34 (str + str joins)
print("ha" * 3) # hahaha
And some combinations don’t work at all:
print("Age: " + 19)
TypeError: can only concatenate str (not "int") to str
Python won’t guess whether you meant to join text or add numbers. You need to convert one side, for example with str(19).
f-strings: the easy way to mix text and values
Instead of joining pieces with +, put an f before the quotes and write variables inside curly brackets:
name = "Aisha"
age = 19
print(f"{name} is {age} years old.")
Aisha is 19 years old.
f-strings convert values to text automatically and are the most readable option in modern Python.
Getting input from the user
The input() function shows a prompt, waits for the user to type something and press Enter, then gives you what they typed:
name = input("What's your name? ")
print(f"Nice to meet you, {name}!")
There’s one important catch: input() always returns a string, even if the user types a number.
age = input("How old are you? ")
print(age + 1) # TypeError!
To do maths with it, convert it with int() (for whole numbers) or float() (for decimals):
age = int(input("How old are you? "))
print(f"Next year you'll be {age + 1}.")
If the user types something that isn’t a number, like nineteen, int() raises a ValueError. That’s fine for now. You’ll learn to handle it with a loop in Lesson 3.
Practice: a simple tip calculator
Put it all together:
# Tip calculator
bill = float(input("Bill amount: "))
percent = int(input("Tip percent (e.g. 15): "))
tip = bill * percent / 100
total = bill + tip
print(f"Tip: {tip:.2f}")
print(f"Total: {total:.2f}")
If you enter 40 and 15, it prints:
Tip: 6.00
Total: 46.00
The :.2f inside the curly brackets formats a number with exactly two decimal places, which is handy for money.
Key takeaways
- A variable is a name that refers to a value;
=sets it. - The basic types are
str(text),int(whole numbers) andfloat(decimals). - The same operator can behave differently by type:
+adds numbers but joins strings. - f-strings like
f"Hi {name}"are the easiest way to mix text and values. input()always returns a string; convert it withint()orfloat()before doing maths.
Source checked: the official Python Tutorial, section “An Informal Introduction to Python”.
In Lesson 3 your programs will start making decisions and repeating actions.
Progress is saved in this browser only.