PY-101 ยท Variables and data types
Data types
โฑ 9 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
A data type tells Python what kind of value something is and what you can do with it. The core types are int for whole numbers, float for decimals, str for text, bool for True or False, and None for "no value". type(x) tells you which one you have.
- Recognise int, float, str, bool and None
- Check a value's type with type()
- Pick the right type for real data
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
count = 10price = 99.5name = "Python"is_active = Truenothing = Noneprint(type(count))print(type(price))print(type(name))print(type(is_active))print(type(nothing))print(0.1 + 0.2)
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
<class 'int'>
<class 'float'>
<class 'str'>
<class 'bool'>
<class 'NoneType'>
0.30000000000000004 ๐ฏ Create temperature as a float with the value 31.5 and is_raining as a bool with the value False. Then print both types.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
Booleans are written True and False, with no quotes.
- The problemA form asks for your age. You type 29. The program adds 1 and shows 291. The program treated 29 as text, not as a number.
- The Python idea
type(value)A data type tells Python what kind of value something is and what you can do with it. - In real lifeA phone number looks like a number but should be a str: you never add phone numbers, and int would drop a leading zero.
Start with a question
A form asks for your age. You type 29. The program adds 1 and shows 291. The program treated 29 as text, not as a number.
Types decide what is allowed. "5" + 5 is an error but 5 + 5 is 10. Most beginner bugs, and plenty of bugs in AI-generated code, are a value with the wrong type.
Explain it like I'm 10
Different things need different kinds of box. Numbers you can add, words you can spell out, and yes/no answers are True or False. Python labels every value with its kind, so it knows what is allowed.
The technical explanation
int is a whole number of any size: 42, -7, 10_000_000. float has a decimal point: 3.14, 2.0.
str is text in quotes: "Chennai". bool is True or False, with a capital first letter.
None means "nothing here yet". It is the value of a variable that has been created but not filled.
Floats are stored in binary, so some decimals are approximate: 0.1 + 0.2 prints 0.30000000000000004. For money, use whole paise or cents, or the decimal module.
| Type | Example | Use it for |
|---|---|---|
int | count = 10 | Counts, ages, IDs |
float | price = 99.5 | Measurements, averages |
str | name = "Asha" | Names, messages, codes |
bool | is_paid = True | Yes/no flags |
None | result = None | Not set yet |
Syntax
type(value)
isinstance(value, int) Real-world scenario
A phone number looks like a number but should be a str: you never add phone numbers, and int would drop a leading zero.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Type conversion โQuestions people ask
What are the basic data types in Python?
int (whole numbers), float (decimals), str (text), bool (True/False) and None. The collection types list, tuple, dict and set build on these.
Why is 0.1 + 0.2 not 0.3 in Python?
Floats are stored in binary, and 0.1 cannot be represented exactly in binary, so a tiny error appears. Every language that uses IEEE 754 floats does the same. Use round() for display, or the decimal module for money.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner What are the basic data types in Python?
int (whole numbers), float (decimals), str (text), bool (True/False) and None. The collection types list, tuple, dict and set build on these.
beginner Why is 0.1 + 0.2 not 0.3 in Python?
Floats are stored in binary, and 0.1 cannot be represented exactly in binary, so a tiny error appears. Every language that uses IEEE 754 floats does the same. Use round() for display, or the decimal module for money.
intermediate What is type(7.0)?
float. Any number with a decimal point is a float, even 7.0.
intermediate Which value is a bool?
True. Booleans are True and False with a capital letter. "True" in quotes is a str.
Quick revision
Remember
- Recognise int, float, str, bool and None
- Check a value's type with type()
- Pick the right type for real data
- When something behaves strangely, add
print(type(x)). A huge share of beginner bugs are a str where you expected an int.
Syntax
type(value)
isinstance(value, int) Official documentation
- Built-in types (docs.python.org)
- Floating point arithmetic (docs.python.org)