Python Data Types
Data Type:
A data type is a classification that tells a programming language what kind of value a variable holds and what operations can be performed on it.
Common Python Data Types:
Python has several built-in data types, which can be grouped into categories:
1. Numeric Types
int — Integer values
Example: 10, -3
float — Floating-point (decimal) numbers
Example: 3.14, -0.001
complex — Complex numbers
Example: 2 + 3j
2. Text Type
str — String (text)
Example: “hello”, ‘Python’
3. Boolean Type
bool — Boolean values
Example: True, False
4. Sequence Types
list — Ordered, mutable collection
Example: [1, 2, 3]
tuple — Ordered, immutable collection
Example: (1, 2, 3)
range — Sequence of numbers
Example: range(0, 5)
5. Set Types
set — Unordered, unique values
Example: {1, 2, 3}
frozenset — Immutable version of a set
6. Mapping Type
dict — Key-value pairs
Example: {“name”: “Alice”, “age”: 25}
7. Binary Types
bytes, bytearray, memoryview — For handling binary data
8. None Type
None Type — Represents the absence of a value
Example: None
Note: Python is a dynamically type programming language. We can asign data to a variable without specifying data type.
Example:
name = “Alice” # str
age = 30 # int
height = 5.6 # float
is_student = False # bool
scores = [90, 85, 88] # list
person = {“name”: “Alice”, “age”: 30} # dict
Check data type of a variable:
To check the data type of a variable in Python, use the built-in type() function.
Example:
x = 42
print(type(x)) # Output: <class ‘int’>
y = “Hello”
print(type(y)) # Output: <class ‘str’>
Collection Data Types:
Differences:
Performance
List data type:
A list in Python is a mutable, ordered collection of items. It can hold items of different data types, including other lists.
Get Krishna Chaitanya’s stories in your inbox
Join Medium for free to get updates from this writer.
Example:
my_list = [10, “hello”, 3.14, True]
print(my_list)
Output: [10, ‘hello’, 3.14, True]
Properties of a List:
List Methods:
1. append(x): Adds a single item x to the end of the list.
fruits = [‘apple’, ‘banana’]
fruits.append(‘cherry’)
print(fruits)
Output: [‘apple’, ‘banana’, ‘cherry’]
2. extend(iterable): Adds all elements of an iterable (like a list) to the end of the list.
fruits = [‘apple’]
fruits.extend([‘banana’, ‘cherry’])
print(fruits)
Output: [‘apple’, ‘banana’, ‘cherry’]
3. insert(i, x): Inserts item x at index i.
fruits = [‘apple’, ‘cherry’]
fruits.insert(1, ‘banana’)
print(fruits)
Output: [‘apple’, ‘banana’, ‘cherry’]
4. remove(x): Removes the first occurrence of item x.
numbers = [1, 2, 3, 2]
numbers.remove(2)
print(numbers)
Output: [1, 3, 2]
5. pop([i]): Removes and returns the item at index i. If i is not provided, removes the last item.
fruits = [‘apple’, ‘banana’, ‘cherry’]
last = fruits.pop()
print(last)
Output: cherry
print(fruits)
Output: [‘apple’, ‘banana’]
item = fruits.pop(0)
print(item)
Output: apple
6. clear(): Removes all items from the list.
fruits = [‘apple’, ‘banana’]
fruits.clear()
print(fruits)
Output: []
7. index(x): Returns the first index of item x.
colors = [‘red’, ‘blue’, ‘green’, ‘blue’]
print(colors.index(‘blue’))
Output: 1
8. count(x): Returns the number of times item x appears.
nums = [1, 2, 3, 2, 2]
print(nums.count(2))
Output: 3
9. sort(): Sorts the list in place in ascending order.
nums = [3, 1, 4, 2]
nums.sort()
print(nums)
Output: [1, 2, 3, 4]
10. reverse(): Reverses the elements in place.
letters = [‘a’, ‘b’, ‘c’]
letters.reverse()
print(letters)
Output: [‘c’, ‘b’, ‘a’]
11. copy(): Returns a shallow copy of the list.
original = [1, 2, 3]
copied = original.copy()
print(copied)
Output: [1, 2, 3]






