Data Types in Python
0 865
Introduction
In Python, data types classify the nature of data a variable can hold. Since Python treats everything as an object, understanding its data types is crucial for effective programming. This guide explores the various built-in data types in Python.
Numeric Data Types
Numeric types represent numbers and are immutable. Python supports three numeric types:
- int: Whole numbers, e.g.,
10,-5 - float: Decimal numbers, e.g.,
3.14,-0.001 - complex: Numbers with real and imaginary parts, e.g.,
2 + 3j
a = 10
b = 3.14
c = 2 + 3j
print(type(a)) # <class 'int'>
print(type(b)) # <class 'float'>
print(type(c)) # <class 'complex'>
Sequence Data Types
Sequences are ordered collections of items. Python includes several sequence types:
- str: Immutable text sequences
- list: Mutable sequences of items
- tuple: Immutable sequences of items
text = "Hello"
numbers = [1, 2, 3]
coordinates = (10.0, 20.0)
print(type(text)) # <class 'str'>
print(type(numbers)) # <class 'list'>
print(type(coordinates))# <class 'tuple'>
Mapping Data Type
Mappings store key-value pairs. Python's primary mapping type is:
- dict: Mutable collection of key-value pairs
person = {"name": "Alice", "age": 30}
print(type(person)) # <class 'dict'>
Set Data Types
Sets are unordered collections of unique items. Python provides:
- set: Mutable set
- frozenset: Immutable set
unique_numbers = {1, 2, 3}
immutable_set = frozenset([4, 5, 6])
print(type(unique_numbers)) # <class 'set'>
print(type(immutable_set)) # <class 'frozenset'>
Boolean Data Type
Booleans represent truth values:
- bool:
TrueorFalse
is_valid = True
print(type(is_valid)) # <class 'bool'>
Binary Data Types
Binary types handle binary data:
- bytes: Immutable sequence of bytes
- bytearray: Mutable sequence of bytes
- memoryview: Memory view object of another binary object
data = bytes([65, 66, 67])
mutable_data = bytearray([68, 69, 70])
view = memoryview(data)
print(type(data)) # <class 'bytes'>
print(type(mutable_data))# <class 'bytearray'>
print(type(view)) # <class 'memoryview'>
Conclusion
Understanding Python's data types is fundamental for writing efficient and error-free code. Each type serves a specific purpose and choosing the right one is key to effective programming.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:



Comments
Waiting for your comments