Python Data Types
In this page:
variable = value # int, float, str, bool, list, tuple, dict, set
type(variable)
Numbers
Python has two built-in numeric types for whole and fractional values: int for integers of arbitrary size, and float for numbers with a decimal point.
The interpreter infers which one you meant purely from whether you wrote a decimal point when you assigned the value.
Example: Numbers
age = 25 # whole number, Python infers this as an int
price = 19.99 # has a decimal point, Python infers this as a float
print(type(age)) # shows <class 'int'>
print(type(price)) # shows <class 'float'>
Strings
A str is an ordered sequence of characters, created with either single or double quotes, and used for anything text-based — names, messages, file paths, parsed data.
Strings support indexing, slicing, and a large set of built-in methods for manipulation.
Example: Strings
name = "Python"
print(name[0]) # first character, indexing starts at 0
print(name[1:4]) # slice: characters from index 1 up to (not including) 4
print(name.upper()) # built-in method returns an uppercase copy
Booleans
bool has exactly two values, True and False (capitalized, unlike most languages), and is what every comparison and logical operator returns. Under the hood, True and False behave as 1 and 0, so they can even participate in arithmetic.
Example: Booleans
is_ready = True # capitalized True/False are the only bool values
print(is_ready)
print(True + True) # True behaves like 1, so this adds up to 2
Lists
A list is an ordered collection you can grow, shrink, and modify after creation, written with square brackets like [1, 2, 3].
Because lists preserve insertion order and allow duplicates, they're the default choice whenever you need a general-purpose sequence.
Example: Lists
numbers = [1, 2, 2, 3] # list can hold duplicate values
numbers.append(4) # add an item to the end of the list
print(numbers)
Dictionaries
A dict maps unique keys to values using {key: value} syntax, giving near-instant lookup by key instead of scanning a list one item at a time.
Dictionaries are the natural fit whenever your data is naturally described as pairs, like a username mapped to a user record.
Example: Dictionaries
user = {"name": "Alex", "age": 30}
print(user["name"])
- Assuming
input()returns a number:input("Age: ") + 1fails because the value is astr, so wrap it inint(). - Expecting
0.1 + 0.2 == 0.3to beTrue; floats are binary approximations, so it isFalse(useround()ormath.isclose). - Trying to change a string or tuple item in place, like
s[0] = H, which raisesTypeErrorbecause they are immutable.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: