22 min read
Data Types
int, float, str and bool — what each stores, how to check a type with type(), and the char and double types you may see in other languages.
The Four Basic Types
| Python's basic data types | ||
|---|---|---|
Checking types with type()Python
quantity = 3price = 280.50item = "Red rice"in_stock = True print(type(quantity))print(type(price))print(type(item))print(type(in_stock))Expected output
<class 'int'> <class 'float'> <class 'str'> <class 'bool'>
Choosing the Right Type
String or number?Python
print("5" + "3")print(5 + 3)Expected output
53 8
Types in Other Languages: char and double
Multiple-choice questions sometimes use type names from languages such as C and Java:
| Term | Meaning | Python equivalent |
|---|---|---|
| char | A single character, e.g. 'A' | A str of length 1 |
| string | A sequence of characters | str |
| integer (int) | A whole number | int |
| float / real | A number with a decimal point | float |
| double | A decimal number stored with double precision (more digits) | float (Python's float is double precision) |
| Boolean | True or False | bool |
Quotes matter in C and Java. Single quotes make a char and double quotes make a string: '6' is a char, "6" is a string, 6 is an int and 5.10 is a float or double. In Python, both kinds of quotes make a str.
Practice
Which data type is best for a phone number like 0771234567?
More lessons in Python Programming · Next: Arithmetic Operators · Previous: Variables and Identifiers
