Solved Class Activity

Consider the following list, tuple, and string: # List: [10, 20, 30, 40, 50, 60, 70, 80] # Tuple: (“Math”, “Science”, “English”, “History”, “Geography”) # String: “Python Programming” Perform the following operations: 1. Access and print the third element from each sequence (list, tuple, and string). 2. Slice and print elements from index 2 to 5 from the list and the tuple. 3. Slice and print characters from index 7 to the end of the string. 4. Use negative indexing to print the last two elements from the list and the tuple. 5. Use negative slicing to print characters from the second last to the last character of the string. Write the Python code to perform these operations and print the results.

Solution
# Given sequences
my_list = [10, 20, 30, 40, 50, 60, 70, 80]
my_tuple = ("Math", "Science", "English", "History", "Geography")
my_string = "Python Programming"

 1. Access and print the third element
print("Third element of list:", my_list[2])
print("Third element of tuple:", my_tuple[2])
print("Third character of string:", my_string[2])

 2. Slice from index 2 to 5 (end index not included)
print("List slice (2 to 5):", my_list[2:6])
print("Tuple slice (2 to 5):", my_tuple[2:6])

3. Slice characters from index 7 to end
print("String slice (7 to end):", my_string[7:])

4. Last two elements using negative indexing
print("Last two elements of list:", my_list[-2:])
print("Last two elements of tuple:", my_tuple[-2:])

5. Negative slicing (second last to last character of string)
print("Last two characters of string:", my_string[-2:])

Expected Output

Third element of list: 30
Third element of tuple: English
Third character of string: t
List slice (2 to 5): [30, 40, 50, 60]
Tuple slice (2 to 5): ('English', 'History', 'Geography')
String slice (7 to end): Programming
Last two elements of list: [70, 80]
Last two elements of tuple: ('History', 'Geography')
Last two characters of string: ng

Q 2

Imagine you are maintaining a list of your favorite books: [“To Kill a Mockingbird”, “1984”, “The Great Gatsby”, “Pride and Prejudice”]. Perform the following tasks using Python: 1. Add a new book “Moby Dick” to the list. 2. Replace ”1984″ with “Brave New World”. 3. Remove “The Great Gatsby” from the list. 4. Merge this list with another list of books: [“War and Peace”, “Hamlet”]. 5. Print the final list of books. Write the Python code to execute these tasks and print the final list of books

Solution
# Initial list of favorite books
books = ["To Kill a Mockingbird", "1984", "The Great Gatsby", "Pride and Prejudice"]

# 1. Add a new book "Moby Dick"
books.append("Moby Dick")

# 2. Replace "1984" with "Brave New World"
books[1] = "Brave New World"

# 3. Remove "The Great Gatsby"
books.remove("The Great Gatsby")

# 4. Merge with another list
more_books = ["War and Peace", "Hamlet"]
books.extend(more_books)

# 5. Print the final list of books
print("Final list of books:", books)

Expected Output

Final list of books: ['To Kill a Mockingbird', 'Brave New World', 'Pride and Prejudice', 'Moby Dick', 'War and Peace', 'Hamlet']

Q 3

Create a Python module named calculator .py that includes two functions: 1. add (a, b) – This function should return the sum of two numbers. 2. subtract (a, b) – This function should return the difference between two numbers. Then, write a script named main. py that imports your calculator module and uses these functions to perform the following: 1. Print the result of adding 15 and 8. 2. Print the result of subtracting 10 from 25. Make sure to run your main. py script and verify that the output is correct.

Solution

Step 1: Create calculator.py

# calculator.py

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

Step 2: Create main.py

# main.py

import calculator

# 1. Print the result of adding 15 and 8
print("15 + 8 =", calculator.add(15, 8))

# 2. Print the result of subtracting 10 from 25
print("25 - 10 =", calculator.subtract(25, 10))

Step 3: Run main.py

When you run:

python main.py

Expected Output:

15 + 8 = 23
25 - 10 = 15

Leave a Reply

Your email address will not be published. Required fields are marked *