Python Variables and Data Types (str, int, float, bool)
Give values a name and let Python work out their type. Meet the four types you will use in almost every program.
- Creating and updating variables
- str, int, float and bool
- Checking a type with type() and converting with int() or str()
name"Ada" → strage36 → intheight1.68 → floatis_adminFalse → boolA name that points to a value
The problem
A program has to remember things: a name typed by a user, a price, whether a box is ticked. Writing the same value again and again is slow and breaks as soon as the value changes. A variable stores it once under a name you can reuse everywhere.
Think of it like this
A variable is like a label stuck on a storage box. The label (the name) stays the same while you can put different things inside. The kind of thing in the box decides what you can do with it: you can add numbers, but not add a word to a number.
name = "Ada"🏷️ A box labeled name holding a word (str)age = 36🔢 A box holding a whole number (int)height = 1.68📏 A box holding a decimal number (float)is_student = False✅ A yes-or-no switch (bool)age = age + 1🔄 Take the value out, change it, put it backA variable is a name that refers to a value, and the value's data type (such as str, int, float or bool) decides what operations you can use on it.
Key points
name = "Ada"creates a variable. You never write the type; Python works it out from the value.type(age)shows the type, which is why the second line prints<class 'int'>forage.age = age + 1reads the old value, adds 1 and stores the result under the same name.int("42"),str(3.5)andfloat(7)convert between types, which you need when text must become a number.
When should you use it?
- Storing anything the program needs again later, such as a user's name or a running total.
- Giving a clear name to a value, so
tax_rateexplains itself where0.1would not. - Converting input from text to numbers before doing arithmetic with it.
When not to use it
- Keeping many related values such as 30 scores: use a list instead of
score1,score2, ... - Keeping several facts about one thing: use a dictionary or a class so they stay together.
Example code
# A variable is a name that refers to a value. Python works out the type for you.
name = "Ada" # str: text
age = 36 # int: whole number
height = 1.68 # float: decimal number
is_student = False # bool: True or False
print(name, age, height, is_student)
print(type(name), type(age), type(height), type(is_student))
# Variables can be reassigned, even to a value of another type.
age = age + 1
print("Next year:", age)
# Convert between types with int(), float(), str() and bool().
print(int("42") + 1)
print(str(3.5) + " stars")
print(float(7) / 2)
Result of running the code
Ada 36 1.68 False
<class 'str'> <class 'int'> <class 'float'> <class 'bool'>
Next year: 37
43
3.5 stars
3.5
Code explanation
type() shows a value's type. int(), float() and str() convert between types, which you need when text such as "42" must become a number.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Python data types at a glance
age = 30 | int: whole numbers |
price = 9.99 | float: decimal numbers |
name = "Ada" | str: text |
active = True | bool: True or False |
result = None | None: no value |
[1, 2], (1, 2), {"a": 1}, {1, 2} | list, tuple, dict and set |
type(age), isinstance(age, int) | Check a value's type |
int("42"), float("3.5"), str(42) | Convert between types |
a, b = 1, 2 | Assign several variables at once |
a, b = b, a | Swap two variables |
Common mistakes with Python variables and types
Joining a string and a number with +
Python does not convert numbers to text automatically. Use an f-string or str().
age = 30
print("Age: " + age) # TypeError
age = 30
print(f"Age: {age}")
Forgetting that input() returns a string
input() always returns text, so convert it before doing math.
age = input("Age: ")
print(age + 1) # TypeError
age = int(input("Age: "))
print(age + 1)
Comparing floats with ==
Decimal numbers are stored in binary, so 0.1 + 0.2 is not exactly 0.3. Use math.isclose() or round().
print(0.1 + 0.2 == 0.3) # False
import math
print(math.isclose(0.1 + 0.2, 0.3)) # True
Frequently asked questions
What are the basic data types in Python?
The built-in types you use most are int, float, str, bool and None, plus the collections list, tuple, dict and set.
Do I need to declare a variable's type in Python?
No. Python is dynamically typed: a variable gets its type from the value you assign. You can add type hints such as age: int = 30 for readability and tools, but Python does not enforce them.
How do I check the type of a variable?
Use type(x) to see the type, or isinstance(x, int) to test it. isinstance is usually better because it also accepts subclasses.
What is the difference between mutable and immutable types?
Mutable values, such as lists, dicts and sets, can change in place. Immutable values, such as int, float, str and tuple, cannot; operations on them create new values.
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
- Rectangle Perimeter Compute the perimeter of a rectangle from its sides. Easy
- Average of Three Average three whole numbers. Easy