0% found this document useful (0 votes)
2 views

python assignment4

The document outlines various Python programs that implement mathematical functions and operations using both normal and lambda functions. It includes implementations for addition, subtraction, factorial, prime checking, Fibonacci series, sine and cosine series, palindrome checking, Armstrong number verification, and a basic calculator. Each section provides code snippets along with example outputs for user interaction.

Uploaded by

Kabir Peswani
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 assignment4

The document outlines various Python programs that implement mathematical functions and operations using both normal and lambda functions. It includes implementations for addition, subtraction, factorial, prime checking, Fibonacci series, sine and cosine series, palindrome checking, Armstrong number verification, and a basic calculator. Each section provides code snippets along with example outputs for user interaction.

Uploaded by

Kabir Peswani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

4 FUNCTIONS

AIM:-Implement the following programs in python using functions and lambda


functions
THEORY:-
In Python, functions are blocks of reusable code that perform a specific task.
They allow you to group related code together, which can be executed as
needed. Functions are defined using the keyword, and they can accept
arguments (input values) and return a result (output). This modular approach
makes your code more organized, readable, and reusable.
CODE:
1.ADD

#wap in python to add 2 numbers using lambda function

#1)normal function
def add (a, b):
addition = a + b

return addition

a = int(input("Enter the first number: "))


b = int(input("Enter the second number: "))

addition = add(a, b)
print(f"The sum of {a} and {b} is: {addition}")

#2)lambda function

add_numbers = lambda x, y: x + y

num1 =int(input("Enter the first number: "))


num2 =int(input("Enter the second number: "))

result = add_numbers(num1, num2)

print(f"The sum of {num1} and {num2} is {result}")

OUTPUT
Enter the first number: 2
Enter the second number: 4
The sum of 2 and 4 is: 6
Enter the first number: 2
Enter the second number: 3
The sum of 2 and 3 is 5

2.SUBTRACT
#wap in python to subtract 2 numbers

#1) normal function


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

return subtract

a = int(input("Enter the first number: "))


b = int(input("Enter the second number: "))

subtract = subtract(a, b)

print(f"The difference of {a} and {b} is: {subtract}")

#2) lambda function

subtract_numbers = lambda x, y: x - y

num1 = int(input("Enter the first number: "))


num2 = int(input("Enter the second number: "))

result = subtract_numbers(num1, num2)


print(f"The result of {num1} - {num2} is {result}")

OUTPUT-
Enter the first number: 2
Enter the second number: 3
The difference of 2 and 3 is: -1
Enter the first number: 4
Enter the second number: 2
The result of 4 - 2 is 2

3.FACTORIAL
#wap in python to implement factorial using
#1)normal function
#2)lambda function

#1)
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)

number = int(input("Enter a number: "))


print(f"The factorial of {number} is {factorial(number)}.")
#2)
factorial_lambda = lambda n: 1 if n == 0 or n == 1 else n * factorial_lambda(n -
1)

number = int(input("Enter a number: "))


print(f"The factorial of {number} is {factorial_lambda(number)}.")

OUTPUT-
Enter a number: 3
The factorial of 3 is 6.
Enter a number: 5
The factorial of 5 is 120.

4.PRIME

#wap a program in python to implement prime number with


#1)normal function
#2)lambda function

#1)
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
number = int(input("Enter a number "))
if is_prime(number):
print(f"{number} is a prime number.")
else:
print(f"{number} is not a prime number.")
#2
is_prime_lambda = lambda n: n > 1 and all(n % i != 0
for i in range(2, int(n**0.5) + 1))

number = int(input("Enter a number: "))


if is_prime_lambda(number):
print(f"{number} is a prime number.")
else:
print(f"{number} is not a prime number.")
# 3)wap in python in which the range should be given to print prime numbers

def is_prime(num):
if num <= 1:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False
return True

start = int(input("Enter the start of the range: "))


end = int(input("Enter the end of the range: "))

print(f"Prime numbers between {start} and {end} are:")

for num in range(start, end + 1):


if is_prime(num):
print(num, end=" ")

OUTPUT-
Enter a number 3
3 is a prime number.
Enter a number: 775
775 is not a prime number.
Enter the start of the range: 23
Enter the end of the range: 32
Prime numbers between 23 and 32 are:
23 29 31

5.FIBONACCI

#wap in python to implement fibonacci series


#1)using normal function
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b

n = int(input("Enter the number"))


print("Fibonacci Series:")
fibonacci(n)

#2)fibonacci series using lambda function


fibonacci_lambda = lambda n: [0, 1] if n <= 1 else [0, 1] +
[sum(fibonacci_lambda(n)[-2:]) for n in range(2, n)]

n = int(input("Enter the number(using lambda function) "))


print("Fibonacci Series:", fibonacci_lambda(n))

OUTPUT-

Enter the number5


Fibonacci Series:
0 1 1 2 3 Enter the number(using lambda function) 7
Fibonacci Series: [0, 1, 1, 2, 3, 5, 8]
6.SINE COSINE SERIES

#wap in python to implement sine series

import math

def sine_series(x, n_terms=10):


sine_value = 0
for n in range(n_terms):
sine_value += ((-1)**n) * (x**(2*n + 1)) / math.factorial(2*n + 1)
return sine_value

x = float(input("Enter the value in radians "))


n_terms = int(input("Enter the number of terms "))

result = sine_series(x, n_terms)


print(f"The value of sin({x}) using the sine series is: {result}")
OUTPUT-
Enter the value in radians 34
Enter the number of terms 2
The value of sin(34.0) using the sine series is: -6516.66666666666
#wap in python to implement cosine series

import math

def cosine_series(x, n_terms=10):


cosine_value = 0
for n in range(n_terms):
cosine_value += ((-1)**n) * (x**(2*n)) / math.factorial(2*n)
return cosine_value

x = float(input("Enter the value of x "))


n_terms = int(input("Enter the number of terms "))

result = cosine_series(x, n_terms)


print(f"The value of cos({x}) using the cosine series is: {result}")

OUTPUT-
Enter the value of x 3
Enter the number of terms 4
The value of cos(3.0) using the cosine series is: -1.1375

7.PALINDROME

#wap in python to implement palindrome take input from user


#1)normal function
#2)lambda function

#1)
def is_palindrome(s):
s = s.replace(" ", "").lower()
return s == s[::-1]

user_input = input("Enter a string: ")

if is_palindrome(user_input):
print(f"'{user_input}'is a palindrome")
else:
print(f"'{user_input}'is not a palindrome")

#2)
is_palindrome_lambda = lambda word: word == word[::-1]

user_input = input("Enter a string: ")


if is_palindrome_lambda(user_input):
print(f"'{user_input}' is a palindrome")
else:
print(f"'{user_input}' is not a palindrome")
OUTPUT-
Enter a string: NITIN
'NITIN'is a palindrome
Enter a string: GODFATHER
'GODFATHER' is not a palindrome

8.ARMSTRONG NUMBER

#wap in python to implement armstrong number


def is_armstrong(num):
num_str = str(num)
num_digits = len(num_str)

sum_of_powers = sum(int(digit) ** num_digits for digit in num_str)

return sum_of_powers == num

num = int(input("Enter a number"))

if is_armstrong(num):
print(f"{num} is an Armstrong number!")
else:
print(f"{num} is not an Armstrong number!")

OUTPUT-
Enter a number253
253 is not an Armstrong number!

9.CALCULATOR

#wap in python to implement calculator


def add(x, y):
return x + y

def subtract(x, y):


return x - y

def multiply(x, y):


return x * y

def divide(x, y):


if y != 0:
return x / y
else:
return "Error! Division by zero."

print("Select operation:")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
choice = input("Enter choice(1/2/3/4): ")

num1 = float(input("Enter first number: "))


num2 = float(input("Enter second number: "))

if choice == '1':
print(f"{num1} + {num2} = {add(num1, num2)}")
elif choice == '2':
print(f"{num1} - {num2} = {subtract(num1, num2)}")
elif choice == '3':
print(f"{num1} * {num2} = {multiply(num1, num2)}")
elif choice == '4':
print(f"{num1} / {num2} = {divide(num1, num2)}")
else:
print("Invalid input!")

OUTPUT-
Select operation:
1. Add
2. Subtract
3. Multiply
4. Divide
Enter choice(1/2/3/4): 1
Enter first number: 3
Enter second number: 2
3.0 + 2.0 = 5.0

You might also like