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

Python Data Types

Data types are the different kinds of things a program can hold, like numbers, words, and yes-or-no answers. It is like sorting toys into different boxes because each kind is used differently.
Syntax
python
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

python
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

python
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

python
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

python
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

python
user = {"name": "Alex", "age": 30}
print(user["name"])
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Assuming input() returns a number: input("Age: ") + 1 fails because the value is a str, so wrap it in int().
  2. Expecting 0.1 + 0.2 == 0.3 to be True; floats are binary approximations, so it is False (use round() or math.isclose).
  3. Trying to change a string or tuple item in place, like s[0] = H, which raises TypeError because they are immutable.

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.