Python Data Types
Python Data Types
Python data types specify the kind of values a variable can hold and how they can be used.Python data types define how values are stored, processed, and interacted with in a program.Python data types categorize values to determine the operations that can be performed on them.
Here’s a detailed explanation of Python’s main data types with examples:
1.Numeric Data Types
Numeric data types in Python hold numbers and support mathematical computations./p>
1.1 Integer (int)
Represents whole numbers (positive, negative, or zero).
Example:
x = 10 # Integer y = -5 # Negative integer z = 0 # Zero print(type(x)) # Output: <class 'int'>
1.2. Floating-Point (float)
Represents real numbers with a decimal point.
Example:
a = 3.14 # Float b = -0.75 # Negative float print(type(a)) # Output: <class 'float'>
1.3. Complex Numbers (complex)
Stores complex numbers consisting of a real and an imaginary component.
Example:
c = 2 + 3j # Complex number print(type(c)) # Output: <class 'complex'> print(c.real, c.imag) # Output: 2.0 3.0
2.Sequence Data Types
These are ordered collections of data.
2.1. String (str)
Represents sequences of characters enclosed in quotes.
Example:
c = 2 + 3j # Complex number print(type(c)) # Output: <class 'complex'> print(c.real, c.imag) # Output: 2.0 3.0
2.2. List
A dynamic sequence that supports various data types and allows modifications.
Example:
my_list = [1, 2, 3, "Python", 3.14]
print(type(my_list)) # Output: <class 'list'>
my_list.append("New Element")
print(my_list) # Output: [1, 2, 3, 'Python', 3.14, 'New Element']
2.3. Tuple
An ordered, immutable collection of items.
Example:
my_tuple = (1, 2, 3, "Immutable", 3.14) print(type(my_tuple)) # Output: <class 'tuple'> print(my_tuple[1]) # Output: 2
3.Mapping Data Type
Used to store data in key-value pairs.
3.1. Dictionary (dict)
A flexible data structure that maps unique keys to corresponding values.
Example:
my_dict = {"name": "Python", "year": 1991, "version": 3.10}
print(type(my_dict)) # Output: <class 'dict'>
print(my_dict["name"]) # Output: Python
4.Set Data Types
These data types hold distinct elements without any specific order.
4.1. Set
A mutable collection of unique items.
Example:
my_set = {1, 2, 3, 3, 4}
print(type(my_set)) # Output: <class 'set'>
print(my_set) # Output: {1, 2, 3, 4}
4.2. Frozen Set
An immutable version of a set.
Example:
frozen_set = frozenset([1, 2, 3, 4]) print(type(frozen_set)) # Output: <class 'frozenset'>
5.Boolean Type (bool)
Represents one of two values: True or False.
Example:
is_active = True is_logged_in = False print(type(is_active)) # Output: <class 'bool'> print(10 > 5) # Output: True
6.Binary Types
Used to handle binary data.
6.1. Bytes
Immutable sequences of bytes.
Example:
byte_data = b"Hello" print(type(byte_data)) # Output: <class 'bytes'>
6.2. Bytearray
A mutable sequence of bytes.
Example:
mutable_bytes = bytearray(b"Hello") mutable_bytes[0] = 72 # Changing the first byte print(mutable_bytes) # Output: bytearray(b'Hello')
6.3. Memoryview
Allows direct access to memory contents without duplication.
Example:
byte_data = b"Memory" mv = memoryview(byte_data) print(mv[0]) # Output: 77 (ASCII for 'M')
7.None Type
Represents the absence of a value.
Example:
value = None print(type(value)) # Output: <class 'NoneType'>
Python's data types enable dynamic programming, making it adaptable for various domains like web development, machine learning, and automation.
Prefer Learning by Watching?
Watch these YouTube tutorials to understand Python Tutorial visually:
What You'll Learn:
- 📌 Basic Data Types: String, Integer, Float and Boolean
- 📌 Data Types in Python | Python for Beginners