Python Data Types Explained With Practical Examples
Python Data Types Explained With Practical Examples When you write a Python program, you work with different kinds of values every day. You might store a person's name, an age, a price, a list of products, or information returned by an API. Python needs to know what kind of value it is dealing with, and that's where data types come in. If you're learning Python, data types are one of those topics…
Python offers a variety of data types to represent different kinds of values in a program. Understanding these types is crucial as you progress in your Python journey and start working with functions, APIs, databases, and data analysis tools like Pandas. Here are some of the most commonly used Python data types explained with practical examples.
Numbers:
Python uses `int` for whole numbers and `float` for numbers with decimal values.
```python
age = 45
price = 1250.50
print(type(age))
print(type(price))
```
Output:
```
<class 'int'>
<class 'float'>
```
While integers are precise, floats may not always represent decimal numbers exactly due to internal representation.
Strings:
A string is a sequence of characters. They support indexing and slicing.
```python
name = "Kailas"
print(name)
print(type(name))
```
Output:
```
Kailas
<class 'str'>
```
You can access individual characters and substrings.
```python
print(language[0])
print(language[0:3])
```
Output:
```
P
Pyt
```
Strings are immutable, meaning you cannot change individual characters after creation.
Lists:
A list is useful when you need to keep multiple values together. Lists are mutable.
```python
languages = ["Python", "SQL", "Java"]
print(languages)
```
Output:
```
['Python', 'SQL', 'Java']
```
You can modify elements and add new ones.
```python
languages[1] = "C++"
languages.append("Go")
```
Tuples:
Tuples also store multiple values but are immutable.
```python
coordinates = (18.52, 73.85)
print(coordinates)
```
Output:
```
(18.52, 73.85)
```
Once a tuple is created, elements cannot be changed.
Dictionaries:
A dictionary stores information using key-value pairs.
```python
student = {"name": "Rahul", "age": 25, "city": "Pune"}
print(student["name"])
```
Output:
```
Rahul
```
Dictionaries make data easier to understand and are widely used in Python applications, APIs, configuration files, and JSON data.
Sets:
A set stores unique values.
```python
numbers = {10, 20, 30, 10, 20}
print(numbers)
```
Output:
```
{10, 20, 30}
```
Sets are useful for operations like union, intersection, or difference.
Boolean Values:
Boolean values represent either `True` or `False`.
```python
age = 25
is_adult = age >= 18
print(is_adult)
```
Output:
```
True
```
You'll encounter Boolean values in conditions, filtering, validation, and data processing.
None in Python:
`None` is a special value representing the absence of a value.
```python
result = None
print(result)
print(type(result))
```
Output:
```
None
<class 'NoneType'>
```
It's important not to confuse `None` with `0`, `False`, or an empty list, as they represent different things.
Type Conversion:
Sometimes the value you receive isn't in the type you need. Python provides conversion functions like `int()`, `float()`, `str()`, `list()`, and `tuple()`.
```python
age = input("Enter your age: ")
print(type(age))
age = int(age)
print(age + 5)
```
Output:
```
Enter your age: 45
50
```
Mutable vs Immutable Data Types:
Some objects can be changed after creation (mutable), while others cannot (immutable). Lists and dictionaries are mutable, while numbers, strings, tuples, and booleans are immutable.
```python
numbers = [10, 20, 30]
numbers.append(40)
print(numbers)
```
Output:
```
[10, 20, 30, 40]
```
Strings behave differently:
```python
name = "Python"
# name[0] = 'J' # Raises TypeError
```
Practical Example:
In a small employee application, you might use various data types together:
```python
employee_id = 101
employee_name = "Amit"
salary = 55000.50
is_active = True
skills = ["Python", "SQL", "Power BI"]
employee = {
"id": employee_id,
"name": employee_name,
"salary": salary,
"active": is_active,
"skills": skills
}
```
This example demonstrates how different Python data types work together in a real-world application. Understanding data types is fundamental to writing effective Python code, especially as you move into more complex areas like data analytics and data engineering.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.