Python Datatypes
Python Data Types
In Python, data types define the type of data a variable can hold. Python provides several built-in data types.
Types of Data Types in Python
1. Numeric Types
Python supports different numeric types like integers, floating-point numbers, and complex numbers.
# Integer
x = 10
print(type(x)) # Output:
# Float
y = 10.5
print(type(y)) # Output:
# Complex
z = 3 + 5j
print(type(z)) # Output:
2. Text Type (String)
Strings in Python are sequences of characters enclosed in quotes.
# String
text = "Hello, Python!"
print(type(text)) # Output:
3. Boolean Type
Boolean represents True or False values.
# Boolean
a = True
b = False
print(type(a)) # Output:
4. Sequence Types (List, Tuple, Range)
These are used to store multiple values.
# List
my_list = [1, 2, 3, 4]
print(type(my_list)) # Output:
# Tuple
my_tuple = (1, 2, 3, 4)
print(type(my_tuple)) # Output:
# Range
my_range = range(5)
print(type(my_range)) # Output:
5. Mapping Type (Dictionary)
Dictionaries store data in key-value pairs.
# Dictionary
my_dict = {"name": "John", "age": 30}
print(type(my_dict)) # Output:
6. Set Types
Sets are unordered collections of unique items.
# Set
my_set = {1, 2, 3, 4}
print(type(my_set)) # Output:
# Frozen Set
my_frozen_set = frozenset({1, 2, 3, 4})
print(type(my_frozen_set)) # Output:
Conclusion
- Numeric Types:
int
, float
, complex
- Text Type:
str
- Boolean Type:
bool
- Sequence Types:
list
, tuple
, range
- Mapping Type:
dict
- Set Types:
set
, frozenset