Introduction
Python is a dynamically typed language. It means when you define variable, you do not have to provide any information related to type of that variable. So one of its core strengths is its diverse range of built-in data types, which allow developers to efficiently store and manipulate different kinds of data. Understanding Python data types is crucial for writing effective code. In this article, we will explore the fundamental data types in Python
Numeric Data Types
We can distinguish following numeric data types:
- Integer (int) – Integers are just a whole number without decimal points. They can have both positive and negative values
example = 10
By default, Python support large integeres as well. It means you do not need to predefine any size of that number.
- Floating-Point – It contains real number and contain decimal points
pi = 3.14159
- Complex – this kind of number contains not only real numbers, but also imaginary part. Here you can see the example
z = 3 + 4j
Boolean Data Type (bool)
The Boolean data type keeps one ofTrue
or False
values. It is typically used for logical operations.
decision = True
Booleans are a subclass of integers in Python, where True
equals 1
and False
equals 0
.
Sequence Data Types
Python provides few sequence data types, which allows you store, manipulate ordered collections
- Strings (str) – It is sequence of characters which can be define as follows:
output_double_quotes = "hello"
output_single_quotes = 'hello hello'
output_triple_qoutes = '''hello
this is the text !
'''
It uses single, double and triple qoutes. Strings support indexing, slicing, and various built-in methods for manipulation.
- Lists ( list )- Lists are ordered, mutable (modifiable) collections of items. They can contain different kind of elements.
numbers = [1,2,3, 4.13]
mixed = [1, 3.23, "text"]
Lists allow indexing, slicing, and various operations like appending and sorting.
- Tuples (tuple) – Tuples are similar to lists but are immutable (cannot be modified after creation).
coordinates = (10, 20)
colors = ('red', 'green', 'blue')
Tuples are useful when data should remain constant.
Set Data Type
A set is an unordered collection of unique items.
unique_numbers = {1, 2, 3, 4, 5}
names = {"Alice", "Bob", "Charlie"}
Sets support operations like union, intersection, and difference.
Dictionary Data Type (dict)
Dictionaries store key-value pairs, making them useful for mapping relationships between data.
person = {
"name": "John",
"age": 30,
"city": "New York"
}
Dictionaries allow fast data retrieval using keys.
None Type (NoneType)
None represents the absence of a value.
result = None
It is commonly used to indicate that a variable has no value assigned.
Conclusion
Python provides a rich set of data types to handle different kinds of data efficiently. Understanding these types is essential for writing clean and effective Python programs. By leveraging the right data type for a given problem, you can optimize memory usage and enhance performance.