Understanding Python Data Types: A Deep Dive
Welcome to today's blog post where we'll be exploring one of the core components of Python programming: data types. Python is a high-level, dynamic, and interpreted programming language. It’s loved by many developers for its simple syntax and readability. As with any other programming language, Python also has a set of data types which form the fundamental building blocks when you're starting to learn how to code.
In Python, we have several data types, including:
- Numeric Types: int, float, complex
- Sequence Types: list, tuple, range
- Text Sequence Type: str
- Mapping Type: dict
- Set Types: set, frozenset
- Boolean Type: bool
- Binary Types: bytes, bytearray, memoryview
Let's dive deeper into these data types.
Numeric Types
Integer:
Integers are whole numbers. In Python, you don't need to declare the data type explicitly.
age = 30
print(type(age)) # Outputs: <class 'int'> Float:
Floats are numbers that contain a decimal point.
height = 180.5
print(type(height)) # Outputs: <class 'float'> Complex:
Complex numbers are written with a "j" as the imaginary part.
complex_num = 3+4j
print(type(complex_num)) # Outputs: <class 'complex'> Sequence Types
List:
Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data.
fruits = ['apple', 'banana', 'cherry']
print(type(fruits)) # Outputs: <class 'list'> Tuple:
Tuples are used to store multiple items in a single variable. A tuple is a collection which is ordered and unchangeable.
coordinates = (4.21, 9.29)
print(type(coordinates)) # Outputs: <class 'tuple'> Range:
The range type represents an immutable sequence of numbers and is commonly used for looping a specific number of times in for loops.
numbers = range(1,6)
for num in numbers:
print(num) # Outputs: 1 2 3 4 5 Text Sequence Type
String:
Strings in python are contiguous series of characters delineated by single or double-quotes.
greeting = "Hello, World!"
print(type(greeting)) # Outputs: <class 'str'> Mapping Type
Dictionary:
A dictionary is a collection which is unordered, changeable and indexed.
student = {'name': 'John', 'age': 22} print(type(student)) # Outputs: <class 'dict'> Set Types
Set:
A set is a collection which is unordered and unindexed.
fruits = {'apple', 'banana', 'cherry'}
print(type(fruits)) # Outputs: <class 'set'> Frozenset:
Frozenset is a new class that has the characteristics of a set, but unlike set, frozensets are immutable.
fruits = frozenset(['apple', 'banana', 'cherry'])
print(type(fruits)) # Outputs: <class 'frozenset'> Boolean Type
Boolean:
Booleans represent one of two values: True or False . In programming, you often need to know if an expression is True or False .
is_adult = True
print(type(is_adult)) # Outputs: <class 'bool'> Binary Types
Bytes:
Bytes objects are immutable sequences of single bytes.
x = bytes(5)
print(type(x)) # Outputs: <class 'bytes'> Bytearray:
Byte arrays are sequences of bytes that are mutable.
x = bytearray(5)
print(type(x)) # Outputs: <class 'bytearray'> Memoryview:
The memoryview function allows direct read and write access to an object’s byte-oriented data without needing to copy it first.
x = memoryview(bytes(5))
print(type(x)) # Outputs: <class 'memoryview'> Dynamic Typing
Python is a dynamically typed language. This implies that you don't need to declare the type of a variable when you create it. The Python interpreter infers the type based on the value you assign. Consider the following example:
x = 4 In the above code snippet, Python understands that x is an integer.
Type Conversion
Python also supports type conversion, often known as 'type casting'. This feature allows you to convert a variable of one data type to another. This can be particularly useful when dealing with user inputs or retrieving data from a file. Here's an example:
pi = 3.14
pi_as_integer = int(pi) In the example above, pi_as_integer would be 3 , as the int() function truncates the decimal part when converting a float to an integer.
Mutable and Immutable Types
Python data types are either mutable (can be changed after creation) or immutable (cannot be changed after creation). Understanding the difference is vital as it can impact how your variables behave.
For instance, lists are mutable. You can modify their content without creating a new list:
fruits = ['apple', 'banana']
fruits.append('cherry') # The list is now ['apple', 'banana', 'cherry'] On the other hand, tuples are immutable. If you attempt to alter a tuple after its creation, Python will throw an error: