Python is a dynamically typed language that supports various data types. Understanding data types is crucial for efficient programming and data manipulation. In this guide, we will explore Python's fundamental data types with practical examples.
Python provides three types of numeric data types:
int
)Integers are whole numbers, positive or negative, without decimals.
num = 10
print(type(num)) # Output: <class 'int'>
float
)Floats represent decimal numbers.
num = 10.5
print(type(num)) # Output: <class 'float'>
complex
)Complex numbers have a real and an imaginary part.
num = 2 + 3j
print(type(num)) # Output: <class 'complex'>
Sequence types store multiple values in an ordered manner.
str
)Strings are sequences of characters enclosed in quotes.
text = "Hello, Python!"
print(type(text)) # Output: <class 'str'>
list
)Lists are ordered collections that are mutable.
fruits = ["apple", "banana", "cherry"]
print(type(fruits)) # Output: <class 'list'>
tuple
)Tuples are immutable ordered collections.
colors = ("red", "green", "blue")
print(type(colors)) # Output: <class 'tuple'>
set
)Sets are unordered collections of unique elements.
unique_numbers = {1, 2, 3, 4, 4}
print(type(unique_numbers)) # Output: <class 'set'>
dict
)Dictionaries store key-value pairs.
person = {"name": "Alice", "age": 25, "city": "New York"}
print(type(person)) # Output: <class 'dict'>
bool
) Boolean values represent True
or False
.
is_python_fun = True
print(type(is_python_fun)) # Output: <class 'bool'>
NoneType
None
represents the absence of a value.
empty_value = None
print(type(empty_value)) # Output: <class 'NoneType'>
Python offers a rich set of data types to handle different kinds of data efficiently. Mastering these types helps in writing optimized and bug-free code. By understanding their properties and use cases, you can improve your Python programming skills significantly.