Introduction
In programming, we work with different kinds of data, such as numbers, text, and collections of values.
Python provides different data types to store and manage these values.
A data type defines what kind of value a variable holds and what operations can be performed on it.
Let's understand the different data types available in Python with simple examples.
1. What Are Data Types?
A data type represents the type of value stored in a variable.
For example:
name = "Arun"
age = 25
height = 5.8
is_student = True
Here, Python automatically identifies the data type of each variable.
We can check the data type using the built-in type() function.
print(type(name))
print(type(age))
print(type(height))
print(type(is_student))
Output:
<class 'str'>
<class 'int'>
<class 'float'>
<class 'bool'>
2. Types of Data Types in Python
Python has several built-in data types. They can be grouped into the following categories:
- Numeric
- String
- Sequence
- Set
- Dictionary
- Boolean
- None
3. Numeric Data Types
Numeric data types are used to store numbers.
Python provides three main numeric types.
Integer (int)
Integers represent whole numbers without decimal points.
age = 25
marks = 95
print(age)
print(type(age))
Output:
25
<class 'int'>
Float (float)
Float represents numbers containing decimal points.
price = 99.50
height = 5.8
print(price)
print(type(price))
Output:
99.5
<class 'float'>
Complex (complex)
Complex numbers contain a real part and an imaginary part.
number = 10 + 5j
print(number)
print(type(number))
Output:
(10+5j)
<class 'complex'>
4. String (str)
A string is used to store text or a sequence of characters.
Strings can be created using single quotes, double quotes, or triple quotes.
name = "Python"
language = 'Programming'
print(name)
print(type(name))
Output:
Python
<class 'str'>
We can also access individual characters using indexing.
word = "Python"
print(word[0])
print(word[1])
Output:
P
y
5. Sequence Data Types
Sequence data types store multiple values in an ordered collection.
Python provides three important sequence types.
List
A list stores multiple values in a single variable.
Lists are ordered and mutable, which means we can modify their elements.
numbers = [10, 20, 30, 40]
numbers.append(50)
print(numbers)
Output:
[10, 20, 30, 40, 50]
Tuple
A tuple is similar to a list, but it is immutable.
Once created, its elements cannot be changed.
numbers = (10, 20, 30, 40)
print(numbers)
print(type(numbers))
Output:
(10, 20, 30, 40)
<class 'tuple'>
Range
Range represents a sequence of numbers, commonly used in loops.
numbers = range(1, 6)
for num in numbers:
print(num)
Output:
1
2
3
4
5
6. Set (set)
A set is an unordered collection of unique elements.
It automatically removes duplicate values.
numbers = {10, 20, 30, 20, 10}
print(numbers)
Output:
{10, 20, 30}
Sets are useful when we need to store unique values.
7. Dictionary (dict)
A dictionary stores data in key-value pairs.
Each key is used to access its corresponding value.
student = {
"name": "Arun",
"age": 25,
"department": "IT"
}
print(student["name"])
print(student["age"])
Output:
Arun
25
Dictionaries are useful for storing structured information.
8. Boolean (bool)
Boolean data types represent only two values:
- True
- False
They are commonly used in conditional statements.
age = 25
is_eligible = age >= 18
print(is_eligible)
print(type(is_eligible))
Output:
True
<class 'bool'>
9. None Type
The None data type represents the absence of a value.
It is commonly used when a variable does not have an assigned value yet.
result = None
print(result)
print(type(result))
Output:
None
<class 'NoneType'>
10. Mutable vs Immutable Data Types
One important concept in Python is mutability.
Mutable: Objects whose contents can be modified after creation.
Examples:
- List
- Dictionary
- Set
Immutable: Objects whose contents cannot be modified after creation.
Examples:
- Integer
- Float
- String
- Tuple
- Boolean
Understanding this difference helps us understand how Python handles data.
11. Summary of Python Data Types
| Category | Data Type | Example |
|---|---|---|
| Numeric | int | 100 |
| Numeric | float | 10.5 |
| Numeric | complex | 2+3j |
| Text | str | "Python" |
| Sequence | list | [1, 2, 3] |
| Sequence | tuple | (1, 2, 3) |
| Sequence | range | range(5) |
| Set | set | {1, 2, 3} |
| Mapping | dict | {"age": 25} |
| Boolean | bool | True |
| Special | NoneType | None |
Conclusion
Python provides different data types to store and organize different kinds of information.
Understanding data types is one of the fundamental steps in learning Python programming.
Top comments (0)