python-practicals (3)
python-practicals (3)
# List methods
my_list.append(6)
print("After append:", my_list)
my_list.insert(2, 10)
print("After inserting 10 at index 2:", my_list)
# List slicing
print("\nList slicing:")
print("First three elements:", my_list[:3])
print("Last three elements:", my_list[-3:])
print("Every second element:", my_list[::2])
# List comprehension
squares = [x**2 for x in range(1, 6)]
print("\nList comprehension - squares:", squares)
# List operations
list1 = [1, 2, 3]
list2 = [4, 5, 6]
concatenated = list1 + list2
print("\nConcatenated lists:", concatenated)
# Sorting
unsorted = [3, 1, 4, 1, 5, 9, 2, 6, 5]
print("\nUnsorted:", unsorted)
print("Sorted:", sorted(unsorted))
print("Reverse sorted:", sorted(unsorted, reverse=True))
demonstrate_lists()
Output:
Original list: [1, 2, 3, 4, 5]
After append: [1, 2, 3, 4, 5, 6]
After inserting 10 at index 2: [1, 2, 10, 3, 4, 5, 6]
1
List slicing:
First three elements: [1, 2, 10]
Last three elements: [4, 5, 6]
Every second element: [1, 10, 4, 6]
Unsorted: [3, 1, 4, 1, 5, 9, 2, 6, 5]
Sorted: [1, 1, 2, 3, 4, 5, 5, 6, 9]
Reverse sorted: [9, 6, 5, 5, 4, 3, 2, 1, 1]
# Set operations
print("\nSet Operations:")
print("Union:", set1.union(set2))
print("Intersection:", set1.intersection(set2))
print("Difference (set1 - set2):", set1.difference(set2))
print("Symmetric Difference:", set1.symmetric_difference(set2))
# Set methods
test_set = {1, 2, 3}
print("\nSet Methods:")
test_set.add(4)
print("After adding 4:", test_set)
test_set.remove(2)
print("After removing 2:", test_set)
# Set comprehension
squares_set = {x**2 for x in range(1, 6)}
print("\nSet comprehension - squares:", squares_set)
# Testing membership
2
print("\nMembership testing:")
print("Is 3 in test_set?", 3 in test_set)
print("Is 6 in test_set?", 6 in test_set)
demonstrate_sets()
Output:
Set 1: {1, 2, 3, 4, 5}
Set 2: {4, 5, 6, 7, 8}
Set Operations:
Union: {1, 2, 3, 4, 5, 6, 7, 8}
Intersection: {4, 5}
Difference (set1 - set2): {1, 2, 3}
Symmetric Difference: {1, 2, 3, 6, 7, 8}
Set Methods:
After adding 4: {1, 2, 3, 4}
After removing 2: {1, 3, 4}
Membership testing:
Is 3 in test_set? True
Is 6 in test_set? False
print("Student Dictionary:")
print(student)
3
print("Age:", student['age'])
print("First course:", student['courses'][0])
print("Math grade:", student['grades']['Math'])
# Dictionary methods
print("\nDictionary Methods:")
print("Keys:", list(student.keys()))
print("Values:", list(student.values()))
print("Items:", list(student.items()))
# Dictionary comprehension
squares_dict = {x: x**2 for x in range(1, 6)}
print("\nDictionary comprehension - squares:", squares_dict)
demonstrate_dictionaries()
Output:
Student Dictionary:
{'name': 'John Doe', 'age': 20, 'courses': ['Math', 'Physics', 'Computer Science'], 'grades'
Accessing Values:
Name: John Doe
Age: 20
First course: Math
Math grade: 90
Dictionary Methods:
Keys: ['name', 'age', 'courses', 'grades']
Values: ['John Doe', 20, ['Math', 'Physics', 'Computer Science'], {'Math': 90, 'Physics': 85
Items: [('name', 'John Doe'), ('age', 20), ('courses', ['Math', 'Physics', 'Computer Science
After updates: {'name': 'John Doe', 'age': 21, 'courses': ['Math', 'Physics', 'Computer Scie
4
Would you like me to continue with the next set of practicals? We can cover
functions, modules, lambda functions, and more advanced topics next.