In this lesson, we will dive deeper into the Data Types in Python, which are crucial for managing data in your programs. Understanding these data types will help you write more efficient and effective Python code.
Python provides several built-in data types that can be categorized as follows:
Let’s explore the most commonly used data types in detail.
Python supports three numeric types: int, float, and complex.
Integer numbers are whole numbers, positive or negative, without decimals.
x = 10 # int type
y = -5 # int type
Floating-point numbers represent real numbers with a decimal point.
a = 3.14 # float type
b = -2.5 # float type
Complex numbers have a real and an imaginary part, represented as a + bj
.
z = 2 + 3j # complex type
print(z.real) # Output: 2.0
print(z.imag) # Output: 3.0
Strings are sequences of characters enclosed in either single or double quotes.
name = "Alice" # string type
greeting = 'Hello, world!' # string type
You can perform operations on strings, such as concatenation and slicing:
message = "Hello" + " " + "Alice"
print(message) # Output: Hello Alice
# Slicing
substring = message[0:5]
print(substring) # Output: Hello
Booleans represent logical values, either True or False.
is_active = True # boolean type
is_logged_in = False # boolean type
Booleans are often used in conditions to control program flow.
if is_active:
print("The user is active.")
Lists are ordered, mutable collections of items that can be of different types.
fruits = ["apple", "banana", "cherry"] # list type
numbers = [1, 2, 3, 4] # list type
# Modifying a list
fruits[0] = "orange"
print(fruits) # Output: ['orange', 'banana', 'cherry']
Tuples are ordered, immutable collections of items.
coordinates = (10, 20) # tuple type
Tuples are useful when you want a collection of items that should not change.
Ranges represent a sequence of numbers, commonly used in loops.
for i in range(5):
print(i) # Output: 0, 1, 2, 3, 4
Sets are unordered collections of unique elements.
unique_numbers = {1, 2, 3, 4} # set type
# Automatically removes duplicates
numbers = {1, 2, 3, 3, 4}
print(numbers) # Output: {1, 2, 3, 4}
Frozensets are like sets but immutable.
immutable_set = frozenset([1, 2, 3])
Dictionaries are collections of key-value pairs, enclosed in curly braces.
person = {"name": "Alice", "age": 25} # dictionary type
# Accessing values
print(person["name"]) # Output: Alice
# Modifying a dictionary
person["age"] = 26
print(person) # Output: {'name': 'Alice', 'age': 26}
byte_data = b"Hello"
print(byte_data) # Output: b'Hello'
mutable_byte_data = bytearray(b"Hello")
mutable_byte_data[0] = 72 # ASCII value of 'H'
print(mutable_byte_data) # Output: bytearray(b'Hello')
Allows manipulation of binary data without copying it.
mv = memoryview(bytearray(b"Hello"))
print(mv[0]) # Output: 72
None
is a special data type in Python used to represent the absence of a value or a null value.
x = None # NoneType
print(x) # Output: None
None
is often used to indicate that a variable has no value assigned or as a placeholder for optional arguments in functions.
You can convert between different data types using built-in functions such as int()
, float()
, str()
, etc.
num_str = "123"
num_int = int(num_str)
print(num_int) # Output: 123
num = 456
num_str = str(num)
print(num_str) # Output: "456"
float_num = 3.14
int_num = int(float_num)
print(int_num) # Output: 3