Python Data Types
Basic Data Types in Python
Python has several built-in data types that are essential for storing and manipulating data. Below is an overview of the fundamental data types you will encounter frequently while programming in Python.
1. Numeric Types
These types are used to represent numbers.
Integer (
int
)- Represents whole numbers without any decimal point. They can be positive, negative, or zero.
- Example:
age = 25 # Positive integer temperature = -5 # Negative integer
Floating Point (
float
)- Represents real numbers with decimal points.
- Example:
price = 19.99 # Positive float pi = 3.14159 # Float representing Pi
Complex (
complex
)- Represents complex numbers, which have a real and an imaginary part. The imaginary part is denoted with a
j
. - Example:
z = 3 + 4j # 3 is the real part, 4 is the imaginary part
- Represents complex numbers, which have a real and an imaginary part. The imaginary part is denoted with a
2. Sequence Types
These types represent ordered collections of items.
String (
str
)- A sequence of characters enclosed in single or double quotes. Strings are immutable, meaning their content cannot be changed after they are created.
- Example:
name = "Alice" greeting = 'Hello, World!'
List (
list
)- An ordered, mutable collection of items. Lists can contain elements of different data types and are defined using square brackets.
- Example:
fruits = ["apple", "banana", "cherry"] # List of strings mixed_list = [1, "two", 3.0, True] # List with mixed types
Tuple (
tuple
)- An ordered, immutable collection of items, defined using parentheses. Once created, their values cannot be changed.
- Example:
coordinates = (10.0, 20.0) # Tuple with two float values
3. Mapping Type
- Dictionary (
dict
)- An unordered collection of key-value pairs, defined using curly braces. Keys must be unique and immutable, while values can be of any type.
- Example:
student = {"name": "Alice", "age": 25, "grade": "A"}
4. Set Types
These types represent unordered collections of unique items.
Set (
set
)- An unordered collection of unique items, defined using curly braces. Sets are mutable, allowing for modifications after creation.
- Example:
unique_numbers = {1, 2, 3, 4, 4} # Duplicates are ignored
Frozen Set (
frozenset
)- An immutable version of a set. Once created, the contents of a frozenset cannot be changed.
- Example:
immutable_set = frozenset([1, 2, 3])
5. Boolean Type
- Boolean (
bool
)- Represents one of two values:
True
orFalse
. This type is often used in conditions and logical operations. - Example:
is_active = True is_logged_in = False
- Represents one of two values:
Summary
Python's basic data types—numeric types, sequences, mappings, sets, and booleans—allow for a wide range of data representation and manipulation. Understanding these basic data types is crucial for effective programming, as they form the foundation for working with more complex data structures and algorithms. With this knowledge, you can create and manage data effectively in your Python programs.