Contents
Python Data Types
Python Data Types
Python data types categorize data items, defining the kind of value they hold and determining applicable operations. Since Python treats everything as an object, its data types are classes, with variables as instances (objects) of these classes. Here are Python’s standard or built-in data types:
- Numeric
- Sequence
- Boolean
- Set
- Dictionary
- Binary Types (memoryview, bytearray, bytes)
What Are Python Data Types?
Python provides a function, type()
, to determine the data type of any value. Below is an example that assigns various data types to the variable x
and prints its type after each assignment.
x = "Hello World"
x = 50
x = 60.5
x = 3j
x = ["apple", "banana", "cherry"]
x = ("apple", "banana", "cherry")
x = range(5)
x = {"name": "John", "age": 30}
x = {"apple", "banana", "cherry"}
x = frozenset({"apple", "banana", "cherry"})
x = True
x = b"Hello"
x = bytearray(5)
x = memoryview(bytes(5))
x = None
1. Numeric Data Types in Python
Numeric types represent values with numerical data: integers, floating-point numbers, and complex numbers.
- Integers: Represented by the
int
class. Holds positive or negative whole numbers, with no limit on size. - Float: Represented by the
float
class. Real numbers with decimal points or scientific notation (e.g.,3.5
or4.2e3
). - Complex: Represented by the
complex
class, comprising a real and an imaginary part (e.g.,2 + 3j
).
Example
a = 10
print("Type of a:", type(a))
b = 12.34
print("Type of b:", type(b))
c = 2 + 3j
print("Type of c:", type(c))
Output:
Type of a: <class 'int'>
Type of b: <class 'float'>
Type of c: <class 'complex'>
2. Sequence Data Types
Sequences are collections of values stored in an ordered way. Python has several sequence data types:
- Strings
- Lists
- Tuples
- String Data Type: Strings in Python represent text data, using Unicode characters. A string can be created using single, double, or triple quotes. Example:
text1 = 'Welcome to Python'
text2 = "It's a powerful language"
text3 = '''Python supports
multiline strings'''
print(text1)
print(text2)
print(text3)
Output:
Welcome to Python
It's a powerful language
Python supports
multiline strings
Accessing String Elements: Strings can be indexed to access individual characters, with negative indexing for accessing elements from the end.
str_example = "Python"
print("First character:", str_example[0])
print("Last character:", str_example[-1])
Output:
First character: P
Last character: n
- List Data Type : Lists are collections of items that can be of any data type, and they are mutable. Example:
fruits = ["apple", "banana", "cherry"]
print("First fruit:", fruits[0])
print("Last fruit:", fruits[-1])
Output:
First fruit: apple
Last fruit: cherry
- Tuples in Python: A tuple is an immutable sequence data type in Python that can store a collection of items. Once a tuple is created, its elements cannot be changed, added, or removed. Tuples are defined using parentheses
()
and can hold elements of different data types. Example:
mixed_tuple = (1, "hello", 3.14)
print(mixed_tuple) # Output: (1, 'hello', 3.14)
Output:
(1, 'hello', 3.14)
3. Boolean Data Type in Python
Boolean represents one of two values: True
or False
.
Example:
print(type(True))
print(type(False))
Output:
<class 'bool'>
<class 'bool'>
4. Set Data Type in Python
Sets are unordered collections of unique elements. They are mutable but cannot contain duplicate items.
Example:
my_set = set(["apple", "banana", "apple"])
print(my_set) # Output: {'apple', 'banana'}
Output:
Elements in the set: {'banana', 'apple', 'cherry'}
Is 'apple' in set? True
5. Dictionary Data Type in Python
Dictionaries are unordered, mutable collections of key-value pairs. Each key is unique and maps to a value.
Example:
person = {
"name": "Alice",
"age": 25,
"city": "New York"
}
print(person["name"]) # Output: Alice
Output:
Student Name: John
Student Age: 20
6. Binary Types in Python
Python includes three binary types: bytes
, bytearray
, and memoryview
. These are used for low-level data manipulation and working with binary data.
- Bytes: An immutable sequence of bytes.
- Bytearray: A mutable sequence of bytes.
- Memoryview: A memory view object that allows Python code to access the internal data of an object that supports the buffer protocol.
b = b"Hello"
print("Bytes:", b)
ba = bytearray(5)
print("Bytearray:", ba)
mv = memoryview(bytes(5))
print("Memoryview:", mv)
Output:
Bytes: b'Hello'
Bytearray: bytearray(b'\x00\x00\x00\x00\x00')
Memoryview: <memory at 0x7f1e982f1f40>
Practice examples
Q1. Code to implement basic list operations
# Define the list
fruits = ["mango", "kiwi", "papaya"]
print(fruits)
# Append a new fruit
fruits.append("pineapple")
print(fruits)
# Remove an item
fruits.remove("kiwi")
print(fruits)
Output:
['mango', 'kiwi', 'papaya']
['mango', 'kiwi', 'papaya', 'pineapple']
['mango', 'papaya', 'pineapple']
Q2. Code to implement basic tuple operation
# Define the tuple
coordinates = (7, 9)
print(coordinates)
# Access elements in the tuple
print("X-coordinate:", coordinates[0])
print("Y-coordinate:", coordinates[1])
Output:
(7, 9)
X-coordinate: 7
Y-coordinate: 9