Python Data Types
In this page:
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
price = 19.99
print(type(age))
print(type(price))
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])
print(name[1:4])
print(name.upper())
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
print(is_ready)
print(True + True)
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]
numbers.append(4)
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"])
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: