0% found this document useful (0 votes)
14 views13 pages

Asignment - 1

Uploaded by

glicmack
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views13 pages

Asignment - 1

Uploaded by

glicmack
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Name: prince vekariya

En. No. : 22ss02ca057

Subject Code: SSCA3021

Subject: Data Science

Assignment - 1

1. Write a program create a calculator using switch function.

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

def subtract(a, b):


return a - b

def multiply(a, b):


return a * b

def divide(a, b):


return a / b if b != 0 else "Cannot divide by zero"

def calculator(operation, a, b):


switcher = {
"add": add,
"subtract": subtract,
"multiply": multiply,
"divide": divide
}
func = switcher.get(operation, lambda x, y: "Invalid operation")
return func(a, b)
a, b = 10, 5
print(calculator("add", a, b))
print(calculator("subtract", a, b))
print(calculator("multiply", a, b))
print(calculator("divide", a, b))

Output:

2. Using user define function.

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"

print(calculate("add", 10, 5))


print(calculate("subtract", 10, 5))
print(calculate("multiply", 10, 5))
print(calculate("divide", 10, 5))
Output:

3. Write a python script to display student marksheet.

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}%")

marks = {"Math": 70, "Java": 70, "Python": 70}


display_marksheet("NNN", marks)

Output:
4. write a python script to find the even numbers from a specified group

[11,1,20,50,5,40].

a. use for loop only.

Code:
numbers = [11, 1, 20, 50, 5, 40]
even_numbers = []

for num in numbers:


if num % 2 == 0:
even_numbers.append(num)

print(even_numbers)

Output:

b. use user defined function.

Code:
def find_even_numbers(nums):
return [num for num in nums if num % 2 == 0]

print(find_even_numbers([11, 1, 20, 50, 5, 40]))

Output:

c. Use filter and lambda.

Code:
numbers = [11, 1, 20, 50, 5, 40]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))

print(even_numbers)

Output:

5. Create Student Marksheet.

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}%")

marks = {"Math": 85, "java": 90, "Python": 78}


student = Student("ABC", marks)
student.display()

Output:

6. Write a Python program to create a function that takes one argument, and that

argument will be multiplied with an unknown given number. Sample Output:

Double the number of 15 = 30 Triple the number of 15 = 45 Quadruple the

number of 15 = 60 Quintuple the number 15 = 75.

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

integers using Lambda. Original list of integers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Square

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

cubed_numbers = list(map(lambda x: x ** 3, numbers))

print(f"Original list of integers: {numbers}")


print(f"Square of every number: {squared_numbers}")
print(f"Cube of every number: {cubed_numbers}")

Output:

8. Write a Python program to find all anagrams of a string in a given list of strings

using Lambda. Original list of strings: ['bcda','abce','cbda','cbea','adcb']

Anagrams of 'abcd'; in the above string: ['bcda','cbda','adcb'].

Code:
bases = [2, 3, 4, 5, 6]

indices = list(range(len(bases)))

powers = list(map(lambda base, index: base ** index, bases, indices))

print(f"List of bases: {bases}")


print(f"List of indices: {indices}")
print(f"Powers of bases raised to their corresponding indices: {powers}")

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

powers = list(map(lambda x: base ** x, indices))

return powers

base_number = 2
length = 5
result = generate_powers(base_number, length)

print(f"Base number: {base_number}")


print(:f"List of powers: {result}")
Output:

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

print(f"Original list: {numbers}")


print(f"Squared numbers: {squared_numbers}")
Output:

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'

upper_sequence = list(map(lambda x: x.upper(), sequence))


lower_sequence = list(map(lambda x: x.lower(), sequence))

unique_upper_sequence = list(set(upper_sequence))
unique_lower_sequence = list(set(lower_sequence))

unique_upper_sequence.sort()
unique_lower_sequence.sort()

print(f"Original sequence: {sequence}")


print(f"Uppercase (no duplicates): {unique_upper_sequence}")
print(f"Lowercase (no duplicates): {unique_lower_sequence}")

Output:
11 (b). Write a Python program to add two given lists and find the difference between

them. Use the map() function.

Code:
list1 = [10, 20, 30, 40, 50]
list2 = [5, 15, 25, 35, 45]

sum_list = list(map(lambda x, y: x + y, list1, list2))

difference_list = list(map(lambda x, y: x - y, list1, list2))

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)

str_list = list(map(str, int_list))


str_tuple = list(map(str, int_tuple))

combined_list = str_list + str_tuple

print(f"Original list of integers: {int_list}")


print(f"Original tuple of integers: {int_tuple}")
print(f"Combined list of strings: {combined_list}")

Output:

13. Write a Python program to create a new list taking specific elements from a tuple

and convert a string value to an integer.

Code:
mixed_tuple = (10, '20', 30, '40', '50')

new_list = [int(x) for x in mixed_tuple if isinstance(x, str)]

print(f"Original tuple: {mixed_tuple}")


print(f"New list with converted integers: {new_list}")

Output:
14. Write a Python program to compute the square of the first N Fibonacci numbers,

using the map function and generate a list of the 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:

You might also like