Asignment - 1
Asignment - 1
Assignment - 1
Code:
def add(a, b):
return a + b
Output:
Code:
def calculate(operation, a, b):
if operation == "add":
return a + b
elif operation == "subtract":
return a - b
elif operation == "multiply":
return a * b
elif operation == "divide":
return a / b if b != 0 else "Cannot divide by zero"
else:
return "Invalid operation"
Code:
def display_marksheet(name, marks):
total = sum(marks.values())
percentage = total / len(marks)
print(f"Name: {name}")
for subject, mark in marks.items():
print(f"{subject}: {mark}")
print(f"Total: {total}")
print(f"Percentage: {percentage:.2f}%")
Output:
4. write a python script to find the even numbers from a specified group
[11,1,20,50,5,40].
Code:
numbers = [11, 1, 20, 50, 5, 40]
even_numbers = []
print(even_numbers)
Output:
Code:
def find_even_numbers(nums):
return [num for num in nums if num % 2 == 0]
Output:
Code:
numbers = [11, 1, 20, 50, 5, 40]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)
Output:
Code:
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
def total_marks(self):
return sum(self.marks.values())
def percentage(self):
return self.total_marks() / len(self.marks)
def display(self):
print(f"Name: {self.name}")
for subject, mark in self.marks.items():
print(f"{subject}: {mark}")
print(f"Total: {self.total_marks()}")
print(f"Percentage: {self.percentage():.2f}%")
Output:
6. Write a Python program to create a function that takes one argument, and that
Code:
def multiplier(n):
return lambda x: x * n
double = multiplier(2)
triple = multiplier(3)
quadruple = multiplier(4)
quintuple = multiplier(5)
num = 15
print(f"Double the number of {num} = {double(num)}")
print(f"Triple the number of {num} = {triple(num)}")
print(f"Quadruple the number of {num} = {quadruple(num)}")
print(f"Quintuple the number of {num} = {quintuple(num)}")
Output:
7. Write a Python program to square and cube every number in a given list of
every number of the said list: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] Cube every
number of the said list: [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000].
Code:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
squared_numbers = list(map(lambda x: x ** 2, numbers))
Output:
8. Write a Python program to find all anagrams of a string in a given list of strings
Code:
bases = [2, 3, 4, 5, 6]
indices = list(range(len(bases)))
Output:
9. Write a Python program to create a list containing the power of said number in
bases raised to the corresponding number in the index using Python map.
Code:
def generate_powers(base, length):
indices = list(range(length))
return powers
base_number = 2
length = 5
result = generate_powers(base_number, length)
10. Write a Python program to square the elements of a list using the map() function.
Code:
numbers = [1, 2, 3, 4, 5]
squared_numbers = list(map(lambda x: x ** 2, numbers))
11 (a). Write a Python program to convert all the characters into uppercase and
lowercase and eliminate duplicate letters from a given sequence. Use the map()
Function.
Code:
sequence = 'AabBcCddEe'
unique_upper_sequence = list(set(upper_sequence))
unique_lower_sequence = list(set(lower_sequence))
unique_upper_sequence.sort()
unique_lower_sequence.sort()
Output:
11 (b). Write a Python program to add two given lists and find the difference between
Code:
list1 = [10, 20, 30, 40, 50]
list2 = [5, 15, 25, 35, 45]
print(f"List 1: {list1}")
print(f"List 2: {list2}")
print(f"Element-wise sum: {sum_list}")
print(f"Element-wise difference: {difference_list}")
Output:
12. Write a Python program to convert a given list of integers and a tuple of integers
in a list of strings.
Code:
int_list = [1, 2, 3, 4, 5]
int_tuple = (6, 7, 8, 9, 10)
Output:
13. Write a Python program to create a new list taking specific elements from a tuple
Code:
mixed_tuple = (10, '20', 30, '40', '50')
Output:
14. Write a Python program to compute the square of the first N Fibonacci numbers,
Code:
def fibonacci(n):
if n <= 0:
return []
elif n == 1:
return [0]
fib_sequence = [0, 1]
while len(fib_sequence) < n:
fib_sequence.append(fib_sequence[-1] + fib_sequence[-2])
return fib_sequence[:n]
def square_fibonacci_numbers(n):
fib_sequence = fibonacci(n)
squared_fibs = list(map(lambda x: x ** 2, fib_sequence))
return squared_fibs
N = 10
squared_fibs = square_fibonacci_numbers(N)
print(f"The square of the first {N} Fibonacci numbers is: {squared_fibs}")
Output: