Python Data Types - Full Reference
String (str)
Definition: Sequence of characters inside single or double quotes.
Example:
name = 'Hello World'
print(name[0]) # H
print(name[0:5]) # Hello
print(len(name)) # 11
print(name.upper()) # HELLO WORLD
print(name.replace('World', 'Python')) # Hello Python
Integer (int)
Definition: Whole numbers without decimals.
Example:
a = 10
b=3
print(a + b) # 13
print(a // b) # 3
print(a % b) # 1
print(abs(-5)) # 5
Float (float)
Definition: Numbers with decimals.
Example:
x = 5.75
y = 2.0
print(x / y) # 2.875
print(round(x)) # 6
print(round(3.14159, 2)) # 3.14
Boolean (bool)
Definition: Logical True or False values.
Example:
age = 20
print(age >= 18) # True
print(bool(0)) # False
print(bool('Hello')) # True
Page 1
Python Data Types - Full Reference
List (list)
Definition: Ordered, mutable collection.
Example:
fruits = ['apple', 'banana', 'cherry']
fruits.append('orange')
print(fruits)
fruits.remove('banana')
print(fruits)
fruits[0] = 'grape'
print(fruits)
Tuple (tuple)
Definition: Ordered, immutable collection.
Example:
colors = ('red', 'green', 'blue')
print(colors[1]) # green
print(len(colors)) # 3
Set (set)
Definition: Unordered collection with no duplicates.
Example:
nums = {1, 2, 3}
nums.add(4)
print(nums)
nums.update([3, 5, 6])
print(nums)
nums.remove(2)
print(nums)
Dictionary (dict)
Definition: Key-value pairs.
Example:
person = {'name': 'Ali', 'age': 30}
print(person['name'])
person['age'] = 31
person['city'] = 'Cairo'
print(person)
Page 2
Python Data Types - Full Reference
del person['city']
print(person)
Page 3