Python Data Types with Examples and Output
In Python, data types define the kind of value a variable can hold. Understanding data types is crucial for writing efficient and error-free code. Python is dynamically typed, meaning you don’t need to declare the data type of a variable—Python does it automatically.
Types of Data in Python
Python provides several built-in data types. Here are the most commonly used ones:
- Numeric Types:
int
,float
,complex
- Sequence Types:
str
,list
,tuple
- Mapping Type:
dict
- Set Types:
set
,frozenset
- Boolean Type:
bool
- None Type:
NoneType
1. Numeric Data Types
Integer (int
)
Holds whole numbers.
age = 25 # Integer
print("Age:", age)
Float (float
)
Holds decimal numbers.
height = 5.9 # Float
print("Height:", height)
Complex (complex
)
Holds complex numbers in the form a + bj
.
complex_num = 3 + 4j
print("Complex Number:", complex_num)
Output:
Age: 25
Height: 5.9
Complex Number: (3+4j)
2. String Data Type (str
)
Strings are sequences of characters enclosed in single or double quotes.
message = "Hello, Python!"
print("Message:", message)
Output:
Message: Hello, Python!
3. List Data Type (list
)
Lists are ordered, mutable (changeable) collections of items.
fruits = ["apple", "banana", "cherry"]
print("Fruits List:", fruits)
Output:
Fruits List: ['apple', 'banana', 'cherry']
4. Tuple Data Type (tuple
)
Tuples are ordered but immutable (unchangeable) collections.
coordinates = (10, 20, 30)
print("Coordinates:", coordinates)
Output:
Coordinates: (10, 20, 30)
5. Dictionary Data Type (dict
)
Dictionaries store data as key-value pairs.
person = {"name": "Dharmender", "age": 30}
print("Person Dictionary:", person)
Output:
Person Dictionary: {'name': 'Dharmender', 'age': 30}
6. Set Data Type (set
)
Sets are unordered collections of unique items.
unique_numbers = {1, 2, 3, 3, 4}
print("Unique Numbers Set:", unique_numbers)
Output:
Unique Numbers Set: {1, 2, 3, 4}
7. Boolean Data Type (bool
)
Booleans represent True
or False
.
is_active = True
print("Is Active:", is_active)
Output:
Is Active: True
8. None Type (NoneType
)
Represents the absence of a value.
result = None
print("Result:", result)
Output:
Result: None
Keep Learning 🙂