Main Data Types in Python
Python provides several fundamental data types that are used to store and manipulate data. Understanding these types is essential for effective programming.
1. Integer (int)
Represents whole numbers, both positive and negative, without decimals.
2. Floating Point (float)
Represents real numbers with decimal points.
3. String (str)
Represents sequences of characters, enclosed in single or double quotes.
4. Boolean (bool)
Represents logical values: True or False.
5. List (list)
An ordered, mutable collection of items, enclosed in square brackets.
6. Tuple (tuple)
An ordered, immutable collection of items, enclosed in parentheses.
7. Dictionary (dict)
An unordered collection of key-value pairs, enclosed in curly braces.
Worked Example
Suppose we have the following variables:
``python``
a = 7
b = 3.5
c = "Python"
d = [1, 2, 3]
e = (4, 5, 6)
f = {"name": "Alice", "age": 30}
g = True
Let's check their types step by step:
- $a = 7$ is an integer: $\text{type}(a) = \text{\int}$
- $b = 3.5$ is a float: $\text{type}(b) = \text{float}$
- $c = \text{"Python"}$ is a string: $\text{type}(c) = \text{str}$
- $d = [1, 2, 3]$ is a list: $\text{type}(d) = \text{list}$
- $e = (4, 5, 6)$ is a tuple: $\text{type}(e) = \text{tuple}$
- $f = {\text{"name"}: \text{"Alice"}, \text{"age"}: 30}$ is a dictionary: $\text{type}(f) = \text{dict}$
- $g = \text{True}$ is a boolean: $\text{type}(g) = \text{bool}$
Takeaways
- Python's main data types include integers, floats, strings, booleans, lists, tuples, and dictionaries.
- Each type serves a specific purpose and has unique properties (e.g., mutability, ordering).
- Understanding data types is crucial for writing correct and efficient Python code.