1. Python Data Structure
Lists
Creating a List
my_list = [1, 2, 3, 'a', 'b', 'c']
Accessing Elements
print(my_list[0]) # Output: 1
print(my_list[-1]) # Output: 'c' (last element)
De
Slicing
print(my_list[1:3]) # Output: [2, 3]
print(my_list[:2]) # Output: [1, 2] (first two elements)
Modifying Elements
my_list[0] = 10
e_
Adding Elements
my_list.append(5)
my_list.insert(1, 'x')
cod
# Add to the end
# Insert 'x' at index 1
es
Removing Elements
my_list.remove(3) # Remove element with value 3
element = my_list.pop(2) # Remove and return element at index 2
my_list.clear() # Remove all elements
Other List Methods
my_list.sort() # Sort list in ascending order
my_list.reverse() # Reverse the list
my_list.count('a') # Count occurrences of 'a'
len(my_list) # Get the number of elements
2. Tuples
Creating a Tuple
my_tuple = (1, 2, 3, 'a', 'b')
Accessing Elements
print(my_tuple[1]) # Output: 2
,Slicing
print(my_tuple[1:3]) # Output: (2, 3)
Tuple Unpacking
a, b, c = (1, 2, 3) # Assign 1 to a, 2 to b, 3 to c
Tuple Methods
my_tuple.count(1) # Count occurrences of 1
my_tuple.index('a') # Find the index of 'a'
Note: Tuples are immutable, meaning elements cannot be modified after creation.
De
3. Sets
Creating a Set
e_
cod
my_set = {1, 2, 3, 'a'}
Adding/Removing Elements
my_set.add(4) # Add an element
my_set.remove(2) # Remove an element (raises error if not found)
es
my_set.discard(5) # Remove element if present (no error if not
found)
Set Operations
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union = set1 | set2 # Union: {1, 2, 3, 4, 5}
intersection = set1 & set2 # Intersection: {3}
difference = set1 - set2 # Difference: {1, 2}
Other Set Methods
set1.isdisjoint(set2) # Check if sets have no elements in common
set1.issubset(set2) # Check if set1 is a subset of set2
len(my_set) # Get the number of elements
4. Dictionaries
Creating a Dictionary
my_dict = {"name": "John", "age": 25, "city": "New York"}
, Accessing Elements
print(my_dict["name"]) # Output: John
print(my_dict.get("age")) # Output: 25
Adding/Modifying Elements
my_dict["email"] = "" # Add new key-value pair
my_dict["age"] = 26 # Modify existing value
Removing Elements
del my_dict["city"] # Delete the key-value pair 'city'
my_dict.pop("email") # Remove and return value of 'email'
De
my_dict.clear() # Remove all elements
Iterating Over a Dictionary
e_
for key, value in my_dict.items():
print(key, value)
Dictionary Methods
cod
my_dict.keys() # Get all keys
my_dict.values() # Get all values
my_dict.items() # Get all key-value pairs
2. OBJECT ORIENTED PROGRAMMING
1. Creating a Class
class MyClass:
def __init__(self, name):
self.name = name
def greet(self):
# Constructor
# Instance attribute
# Instance method
print(f"Hello, {self.name}!")
es
# Creating an object (instance of MyClass)
obj = MyClass("Alice")
obj.greet() # Output: Hello, Alice!
__init__ Method (Constructor)
Used to initialize objects.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
2. Class Attributes vs Instance Attributes
Lists
Creating a List
my_list = [1, 2, 3, 'a', 'b', 'c']
Accessing Elements
print(my_list[0]) # Output: 1
print(my_list[-1]) # Output: 'c' (last element)
De
Slicing
print(my_list[1:3]) # Output: [2, 3]
print(my_list[:2]) # Output: [1, 2] (first two elements)
Modifying Elements
my_list[0] = 10
e_
Adding Elements
my_list.append(5)
my_list.insert(1, 'x')
cod
# Add to the end
# Insert 'x' at index 1
es
Removing Elements
my_list.remove(3) # Remove element with value 3
element = my_list.pop(2) # Remove and return element at index 2
my_list.clear() # Remove all elements
Other List Methods
my_list.sort() # Sort list in ascending order
my_list.reverse() # Reverse the list
my_list.count('a') # Count occurrences of 'a'
len(my_list) # Get the number of elements
2. Tuples
Creating a Tuple
my_tuple = (1, 2, 3, 'a', 'b')
Accessing Elements
print(my_tuple[1]) # Output: 2
,Slicing
print(my_tuple[1:3]) # Output: (2, 3)
Tuple Unpacking
a, b, c = (1, 2, 3) # Assign 1 to a, 2 to b, 3 to c
Tuple Methods
my_tuple.count(1) # Count occurrences of 1
my_tuple.index('a') # Find the index of 'a'
Note: Tuples are immutable, meaning elements cannot be modified after creation.
De
3. Sets
Creating a Set
e_
cod
my_set = {1, 2, 3, 'a'}
Adding/Removing Elements
my_set.add(4) # Add an element
my_set.remove(2) # Remove an element (raises error if not found)
es
my_set.discard(5) # Remove element if present (no error if not
found)
Set Operations
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union = set1 | set2 # Union: {1, 2, 3, 4, 5}
intersection = set1 & set2 # Intersection: {3}
difference = set1 - set2 # Difference: {1, 2}
Other Set Methods
set1.isdisjoint(set2) # Check if sets have no elements in common
set1.issubset(set2) # Check if set1 is a subset of set2
len(my_set) # Get the number of elements
4. Dictionaries
Creating a Dictionary
my_dict = {"name": "John", "age": 25, "city": "New York"}
, Accessing Elements
print(my_dict["name"]) # Output: John
print(my_dict.get("age")) # Output: 25
Adding/Modifying Elements
my_dict["email"] = "" # Add new key-value pair
my_dict["age"] = 26 # Modify existing value
Removing Elements
del my_dict["city"] # Delete the key-value pair 'city'
my_dict.pop("email") # Remove and return value of 'email'
De
my_dict.clear() # Remove all elements
Iterating Over a Dictionary
e_
for key, value in my_dict.items():
print(key, value)
Dictionary Methods
cod
my_dict.keys() # Get all keys
my_dict.values() # Get all values
my_dict.items() # Get all key-value pairs
2. OBJECT ORIENTED PROGRAMMING
1. Creating a Class
class MyClass:
def __init__(self, name):
self.name = name
def greet(self):
# Constructor
# Instance attribute
# Instance method
print(f"Hello, {self.name}!")
es
# Creating an object (instance of MyClass)
obj = MyClass("Alice")
obj.greet() # Output: Hello, Alice!
__init__ Method (Constructor)
Used to initialize objects.
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
2. Class Attributes vs Instance Attributes