You are on page 1of 6

Week 2

• Given a list numbers = [10, 20, 30, 40, 50], write a program to double each element in
the list. Print the updated list.
• Create a tuple containing the names of the days of the week. Write a program that
asks the user for a number between 1 and 7 and prints the day of the week
corresponding to that number.
• Given two sets set1 = {2, 4, 6, 8} and set2 = {1, 2, 3, 4}, write a program that prints
out their union and intersection.
• Given a dictionary mapping names of students to their grades, write a program that
lists names of students who have a grade above the class average.
• You're given two sets representing attendees of two events. Write a program to find
out which attendees went to both events, which attendees went only to the first event,
and which attendees went only to the second event. Use set operations.
• Create a dictionary that maps a person's name to their age. Add entries for three
people, then write a 6 program that takes a name as input and outputs the person's age
• Array Creation: Write a program to create a NumPy array with integers from 1 to 10
and print it.
• Reshaping Arrays: Create a 1D NumPy array with 9 consecutive numbers and reshape
it to form a 3x3 matrix
• Basic Arithmetic: Given two NumPy arrays, `[2, 4, 6, 8]` and `[1, 3, 5, 7]`, perform
element-wise addition, subtraction, multiplication, and division.
• Array Slicing: Given a 5x5 matrix filled with numbers from 1 to 25, extract the center
3x3 matrix using array slicing.
• Statistical Functions: Generate a NumPy array of 15 random integers between 10 and
50. Compute and print the mean, median, and standard deviation of the numbers.

19
Program 1: Given a list numbers = [10, 20, 30, 40, 50], write a program to double each
element in the list. Print the updated list.

Solution:
print("Kushal Vithal | 22103091")
numbers = [10, 20, 30, 40, 50]
print(f"Initial List:{numbers}")
doubled_numbers = [2 * num for num in numbers]
print("Doubled Numbers:", doubled_numbers)

Output:

Program 2: Create a tuple containing the names of the days of the week. Write a program
that asks the user for a number between 1 and 7 and prints the day of the week corresponding
to that number.

Solution:
print("Kushal Vithal | 22103091")
days = (
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
)
number = int(input("Enter a number between 1 and 7: "))
if 1 <= number <= 7:
print("Day of the Week:", days[number - 1])
else:
print("Invalid input. Please enter a number between 1 and 7.")

Output:

Program 3: Given two sets set1 = {2, 4, 6, 8} and set2 = {1, 2, 3, 4}, write a program that
prints out their union and intersection.

Solution:
print("Kushal Vithal | 22103091")

20
set1 = {2, 4, 6, 8}
set2 = {1, 2, 3, 4}
print("Union:", set1.union(set2))
print("Intersection:", set1.intersection(set2))

Output:

Program 4: Given a dictionary mapping names of students to their grades, write a program
that lists names of students who have a grade above the class average.

Solution:
print("Kushal Vithal | 22103091")
grades = {"Alice": 85, "Bob": 92, "Charlie": 78, "David": 95, "Eva": 88}
average = sum(grades.values()) / len(grades)
above_average_students = [name for name, grade in grades.items() if grade > average]
print("Students with Grades Above Class Average:", above_average_students)

Output:

Program 5: You're given two sets representing attendees of two events. Write a program to
find out which attendees went to both events, which attendees went only to the first event,
and which attendees went only to the second event. Use set operations.

Solution:
print("Kushal Vithal | 22103091")
event1_attendees = {"Alice", "Bob", "Charlie"}
event2_attendees = {"Bob", "David", "Eva"}
both_events = event1_attendees.intersection(event2_attendees)
only_event1 = event1_attendees - event2_attendees
only_event2 = event2_attendees - event1_attendees
print("Attendees at Both Events:", both_events)
print("Attendees Only at Event 1:", only_event1)
print("Attendees Only at Event 2:", only_event2)

Output:

21
Program 6: Create a dictionary that maps a person's name to their age. Add entries for three
people, then write a 6 program that takes a name as input and outputs the person's age.

Solution:
print("Kushal Vithal | 22103091")
people_age = {"Alice": 25, "Bob": 30, "Charlie": 22}
input_name = input("Enter a name: ")
print(f"{input_name}'s Age:", people_age.get(input_name, "Name not found"))

Output:

Program 7: Array Creation: Write a program to create a NumPy array with integers from 1
to 10 and print it.

Solution:
print("Kushal Vithal | 22103091")
import numpy as np

numpy_array = np.arange(1, 11)


print("NumPy Array:", numpy_array)

Output:

Program 8: Reshaping Arrays: Create a 1D NumPy array with 9 consecutive numbers and
reshape it to form a 3x3 matrix.

Solution:
print("Kushal Vithal | 22103091")
import numpy as np

numpy_array = np.arange(1, 10)


print("NumPy Array:", numpy_array)
reshaped_array = numpy_array.reshape(3, 3)
print("Reshaped Array:")
print(reshaped_array)

22
Output:

Program 9: Basic Arithmetic: Given two NumPy arrays, `[2, 4, 6, 8]` and `[1, 3, 5, 7]`,
perform element-wise addition, subtraction, multiplication, and division.

Solution:
print("Kushal Vithal | 22103091")
import numpy as np

array1 = np.array([2, 4, 6, 8])


array2 = np.array([1, 3, 5, 7])
print("First array:", array1)
print("Second array:", array2)
addition = array1 + array2
subtraction = array1 - array2
multiplication = array1 * array2
division = array1 / array2
print("Addition:", addition)
print("Subtraction:", subtraction)
print("Multiplication:", multiplication)
print("Division:", division)

Output:

Program 10: Array Slicing: Given a 5x5 matrix filled with numbers from 1 to 25, extract the
center 3x3 matrix using array slicing.

Solution:
print("Kushal Vithal | 22103091")
import numpy as np

matrix = np.arange(1, 26).reshape(5, 5)


center_matrix = matrix[1:4, 1:4]

23
print("Center 3x3 Matrix:")
print(center_matrix)

Output:

Program 11: Statistical Functions: Generate a NumPy array of 15 random integers between
10 and 50. Compute and print the mean, median, and standard deviation of the numbers.

Solution:
print("Kushal Vithal | 22103091")
import numpy as np

random_integers = np.random.randint(10, 50, 15)


mean = np.mean(random_integers)
median = np.median(random_integers)
std_dev = np.std(random_integers)
print("Random Integers:", random_integers)
print("Mean:", mean)
print("Median:", median)
print("Standard Deviation:", std_dev)

Output:

24

You might also like