In this tutorial, you will learn about Python Data Type.
What is Data Type in Python?
Data Types define the Type of data a variable that can hold which type of value.
Python has a lots of data types, You can see below.
1. Numeric Types
int: It represents whole numbers (positive, negative, or zero) without decimals.
Example:
x = 20
y = -10
z = 0
float: It represents numbers with decimals (floating-point numbers).
x = 20.15
y = -10.57
complex: It represents complex numbers with a real and imaginary part.
Example:
z = 5 + 10j
2. Text Type
string (str): A sequence of characters, or simply text. Strings are enclosed in quotes.
Example:
name = "John"
greeting = 'Hello Friends'
3. Sequence Types(Ordered collections)
Collections of items that follow a specific order.
list: A collection of items. You can add, remove, or change items in a list. It can hold different data types.
Example:
fruits = ["apple", "banana", "orange", 75]
Tuple: It is similar to a list, but immutable (you can’t change it after it’s created). It’s faster and safer for fixed collections.
Example:
coordinates = (10.1, 20.5, 30.5)
Range: It represents a sequence of numbers. Commonly used in loops.
Example
range(0, 10) represents numbers from 0 to 9.
4. Mapping Type
Dictionary (dict): It stores key-value pairs. You use keys (unique) to access the values.
person = {"name": "John", "age": 35}
To access the name:
person["name"] gives "John".
5. Set Types (Unordered collections)
Each item must be unique in the collection.
Set: An unordered collection of unique items (no duplicates allowed).
my_set = {1, 2, 3, "apple"}
Frozen Set: It is a immutable version of a set. it cannot be changed after created.
frozen_set = frozenset([1, 2, 3])
6. Boolean Type
Boolean (bool): It represents True or False
Example:
is_loggedin= true, is_open = false
7. Binary Types
Binary types are used to handle and manipulate binary data.
Binary Data is not a human-readable text because it’s in a byte form.
Python provides two primary binary types to represent and work with binary data:
Bytes:
1. It is an immutable sequence of bytes.
2. Each element in an bytes object is an integer and it’s rang from 0 to 255, representing an 8-bit byte.
3. It is useful for representing raw binary data, such as reading from or writing to a file in binary mode or handling binary network protocols.
4. You define bytes by prefixing a string literal with a b.
binary_data = b"hello" # 'b' indicates this is a bytes object
print(binary_data) # Output: b'hello'
print(binary_data[0]) # Output: 104 (ASCII value of 'h')
Bytearray:
1. It is a mutable sequence of bytes.
2. It is similar to bytes, but allows for modification of its elements.
3. Often used when you need to manipulate or modify binary data, such as changing bytes or appending new data.
4. You can create a bytearray from a bytes object or from an iterable of integers.
Example:
mutable_binary_data = bytearray(b"hello")
print(mutable_binary_data) # Output: bytearray(b'hello')
# Modify the first byte
mutable_binary_data[0] = 72 # ASCII value of 'H'
print(mutable_binary_data) # Output: bytearray(b'Hello')
Memoryview:
1. A memoryview object exposes the buffer interface, allowing you to work with large datasets without copying the data.
2. It is useful for slicing and modifying parts of large binary objects without creating new copies.
Example
binary_data = b"hello world"
mv = memoryview(binary_data)
print(mv[0:5]) # Output:
print(mv.tobytes()) # Output: b'hello world'
8. None Type
None: It represents the absence of a value or a null value. It is often used as a default value or a placeholder.
Example:
result = None
Python Data Types – Interview Questions
Q 1: What are data types in Python?
Ans: Data types define the type of value a variable can store.
Q 2: Name some built-in data types in Python.
Ans: int, float, str, list, tuple, and dict.
Q 3: How do you check the data type of a variable?
Ans: By using the type() function.
Q 4: Is Python dynamically typed?
Ans: Yes, Python determines data types at runtime.
Q 5: Can a variable change its data type?
Ans: Yes, a variable can store different data types at different times.
Python Data Types – Objective Questions (MCQs)
Q1. Which of the following is a valid data type in Python?
Q2. What will be the data type of the variable x in the following code?
Q3. Which of the following is an example of a list in Python?
Q4. What will be the output of the following code?
x = True
print(type(x))
Q5. Which of the following statements about Python data types is true?