← Back to Python Course | Chapter 1: Basics | Lesson 8 of 14

Python Data Types

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

python
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

python
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

python
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

python
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

python
user = {"name": "Alex", "age": 30}
print(user["name"])

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.