Python Documentation

Python Data Types

Python Data Types

Python has several built-in data types to store different kinds of values. Here are the main data types in Python:

Numeric Types

Python has three numeric types: int, float, and complex.

x = 5 # int
y = 2.5 # float
z = 1+2j # complex

Sequence Types

Python has three sequence types: list, tuple, and range.

my_list = [1, 2, 3] # list
my_tuple = (1, 2, 3) # tuple
my_range = range(6) # range

Modifying Lists

my_list.append(4) # Add an item
my_list[0] = 0 # Modify an item
del my_list[1] # Remove an item

Modifying Tuples

Tuples are immutable, but you can create a new tuple based on the existing one.

my_tuple = my_tuple + (4,) # Create a new tuple with an additional item

Text Type

Python has one text type: str (string).

my_string = "Hello, World!"

Mapping Type

Python has one mapping type: dict (dictionary).

my_dict = {"name": "John", "age": 30}

Modifying Dictionaries

my_dict["city"] = "New York" # Add or modify an item
del my_dict["age"] # Remove an item

Set Types

Python has two set types: set and frozenset.

my_set = {1, 2, 3} # set
my_frozenset = frozenset([1, 2, 3]) # frozenset

Boolean Type

Python has one boolean type: bool.

x = True
y = False