Python Data Types
In Python, every value has a data type. Python’s dynamic nature means that the data type of a variable is inferred when a value is assigned to it. Understanding Python's various data types helps in writing efficient and effective code. Python supports different types of numbers, collections, sequences, and more.
Common Python Data Types
- Numbers (Integers and Floats)
- Complex Numbers
- Strings
- Booleans
- Lists
- Tuples
- Dictionaries
- Sets
- Ranges
- None Type
Numbers (Integers and Floats)
Python supports integers (whole numbers) and floating-point numbers (numbers with decimals). Integers do not have a decimal point, while floats include a decimal.
Example
# Working with integers and floats
x = 10 # Integer
y = 3.14 # Float
print(x)
print(y)
Output
10
3.14
Explanation: The variable x
holds an integer value, and y
holds a floating-point number. Python automatically assigns the correct type based on the value provided.
Complex Numbers
Python natively supports complex numbers. A complex number consists of a real part and an imaginary part. The imaginary part is denoted with a j
.
Example
# Working with complex numbers
z = 2 + 3j
print(z)
print(z.real)
print(z.imag)
Output
(2+3j)
2.0
3.0
Explanation: The variable z
holds a complex number. Using z.real
and z.imag
retrieves the real and imaginary parts of the complex number.
Strings
Strings are sequences of characters enclosed in single, double, or triple quotes. Python strings are immutable, meaning once created, they cannot be modified.
Example
# Working with strings
name = "Python"
print(name)
long_text = """This is a multi-line
string in Python."""
print(long_text)
Output
Python
This is a multi-line
string in Python.
Explanation: Strings can be defined with single, double, or triple quotes. Triple quotes allow multi-line strings, which are useful for longer text blocks.
Booleans
Booleans represent truth values: either True
or False
. They are commonly used in conditions and comparisons.
Example
# Working with booleans
is_python_fun = True
print(is_python_fun)
print(10 > 5) # Comparison returns a boolean
Output
True
True
Explanation: Boolean variables hold values of True
or False
. Comparisons like 10 > 5
return boolean values as well.
Lists
Lists are ordered, mutable collections of items. They are defined using square brackets and can hold any type of data, including numbers, strings, or even other lists.
Example
# Working with lists
fruits = ["Apple", "Banana", "Cherry"]
print(fruits)
fruits.append("Mango")
print(fruits)
Output
["Apple", "Banana", "Cherry"]
["Apple", "Banana", "Cherry", "Mango"]
Explanation: Lists are mutable, meaning you can change, add, or remove elements after the list has been created. The append()
method adds an item to the list.
Tuples
Tuples are similar to lists, but they are immutable, meaning their values cannot be changed after they are created. They are defined using parentheses.
Example
# Working with tuples
coordinates = (10, 20)
print(coordinates)
Output
(10, 20)
Explanation: The variable coordinates
holds a tuple with two values, 10 and 20. Tuples are immutable, so once created, their values cannot be modified.
Dictionaries
Dictionaries are collections of key-value pairs. They are unordered, mutable, and defined using curly braces. You can access values by using their corresponding keys.
Example
# Working with dictionaries
person = {"name": "Alice", "age": 30}
print(person["name"])
print(person["age"])
Output
Alice
30
Explanation: The variable person
is a dictionary that holds the key-value pairs for a person's name and age. You can access the values by using their keys, such as person["name"]
.
Sets
Sets are unordered collections of unique items. They are defined using curly braces or the set()
function. Duplicate values are automatically removed in sets.
Example
# Working with sets
unique_numbers = {1, 2, 3, 3, 2, 1}
print(unique_numbers)
Output
{1, 2, 3}
Explanation: The variable unique_numbers
is a set, and any duplicate values are removed automatically. In this case, {1, 2, 3}
remains after removing duplicates.
Ranges
The range()
function returns a sequence of numbers. It is often used in loops and can take three parameters: start, stop, and step.
Example
# Working with range
numbers = range(1, 6)
for number in numbers:
print(number)
Output
1
2
3
4
5
Explanation: The range()
function generates a sequence of numbers from 1 to 5. It is often used in loops to iterate over a series of numbers.
None Type
The None
data type represents the absence of a value. It is commonly used to signify that a variable has no value or to return a null value from a function.
Example
# Using None
x = None
print(x)
Output
None
Explanation: The variable x
is assigned the value None
, which signifies that it holds no value. None
is often used to indicate the absence of a value.