Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

python-practicals (3)

The document outlines practical exercises in Python focusing on list, set, and dictionary operations, demonstrating their creation, manipulation, and various methods. Each section includes code snippets that illustrate key concepts such as list slicing, set operations, and dictionary methods, along with expected outputs. The document serves as a guide for understanding fundamental data structures in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

python-practicals (3)

The document outlines practical exercises in Python focusing on list, set, and dictionary operations, demonstrating their creation, manipulation, and various methods. Each section includes code snippets that illustrate key concepts such as list slicing, set operations, and dictionary methods, along with expected outputs. The document serves as a guide for understanding fundamental data structures in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical 8: List Program

Practical Name: List Program Objective: To demonstrate list operations,


methods, and manipulations in Python.
def demonstrate_lists():
# Creating and modifying lists
my_list = [1, 2, 3, 4, 5]
print("Original list:", my_list)

# 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]

List comprehension - squares: [1, 4, 9, 16, 25]

Concatenated lists: [1, 2, 3, 4, 5, 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]

Practical 9: Set Program


Practical Name: Set Program Objective: To demonstrate set operations and
methods in Python, including union, intersection, and difference.
def demonstrate_sets():
# Creating sets
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
print("Set 1:", set1)
print("Set 2:", set2)

# 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}

Set comprehension - squares: {1, 4, 9, 16, 25}

Membership testing:
Is 3 in test_set? True
Is 6 in test_set? False

Practical 10: Dictionary Program


Practical Name: Dictionary Program Objective: To demonstrate dictionary
creation, manipulation, and common operations in Python.
def demonstrate_dictionaries():
# Creating a dictionary
student = {
'name': 'John Doe',
'age': 20,
'courses': ['Math', 'Physics', 'Computer Science'],
'grades': {'Math': 90, 'Physics': 85, 'Computer Science': 95}
}

print("Student Dictionary:")
print(student)

# Accessing and modifying values


print("\nAccessing Values:")
print("Name:", student['name'])

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()))

# Adding and updating entries


student['email'] = 'john@example.com'
student['age'] = 21
print("\nAfter updates:", student)

# Dictionary comprehension
squares_dict = {x: x**2 for x in range(1, 6)}
print("\nDictionary comprehension - squares:", squares_dict)

# get() method with default value


print("\nUsing get() method:")
print("Phone number:", student.get('phone', 'Not available'))

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

Dictionary comprehension - squares: {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

Using get() method:


Phone number: Not available

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.

You might also like