Ex 8 Functions
Ex 8 Functions
Aim:
The aim of this program is to create a function that accepts two numbers and a
mathematical operation (add, subtract, multiply, divide) as inputs, then performs
the corresponding arithmetic operation and returns the result.
Algorithm:
1. Define a function that takes three inputs: two numbers (num1, num2) and
an operation type (operation).
2. Use conditional checks to determine which operation to perform.
3. Perform the corresponding operation and return the result.
4. Handle division by zero by displaying an error message when attempting
to divide by zero.
print(calculator(10, 5, 'add'))
print(calculator(10, 5, 'sub'))
print(calculator(10, 5, 'multiply'))
print(calculator(10, 5, 'divide'))
print(calculator(10, 0, 'divide')) # Output: Error: Cannot divide by zero
print(calculator(10, 5, 'unknown')) # Output: Error: Invalid operation
Sample Outputs:
15
5
50
2.0
Error: Cannot divide by zero
Error: Invalid operation
if n == 0 or n == 1:
return 1
# Recursive case: n * factorial of (n-1)
else:
return n * factorial(n - 1)
number = 5
result = factorial(number)
# List of numbers
num_list = [1, 2, 3, 4, 5]
# Creating a set
my_set = {1, 2, 3}
# Adding an element
my_set = add_element_to_set(my_set, 4)
print("After adding 4:", my_set)
# Removing an element
my_set = remove_element_from_set(my_set, 2)
print("After removing 2:", my_set)
Output:
After adding 4: {1, 2, 3, 4}
After removing 2: {1, 3, 4}
2. Using Functions with Tuples
Tuples are immutable collections. You can't directly modify a tuple like a list,
but you can create a new one from existing tuples. Here's how you might use
functions with tuples.
Example Function for Tuples:
# Function to concatenate two tuples
def concatenate_tuples(tuple1, tuple2):
return tuple1 + tuple2
# Creating tuples
tuple1 = (1, 2, 3)
tuple2 = (4, 5)
# Concatenating tuples
result_tuple = concatenate_tuples(tuple1, tuple2)
print("Concatenated tuple:", result_tuple)
# Accessing an element
element = get_element_from_tuple(tuple1, 1)
print("Element at index 1:", element)
Output:
Concatenated tuple: (1, 2, 3, 4, 5)
Element at index 1: 2
3. Using Functions with Dictionaries
Dictionaries in Python are key-value pairs. You can use functions to add,
remove, or modify dictionary items.
Example Function for Dictionaries:
# Function to add or update a key-value pair in a dictionary
def add_or_update_dict(d, key, value):
d[key] = value
return d
# Creating a dictionary
my_dict = {'name': 'Alice', 'age': 25}
# Removing a key
my_dict = remove_key_from_dict(my_dict, 'name')
print("After removing 'name':", my_dict)
Output:
After updating age: {'name': 'Alice', 'age': 26}
After removing 'name': {'age': 26}
# Function to demonstrate "call by value" with immutable objects (integer)
def call_by_value(x):
print("Inside call_by_value function, before change:", x)
x = 100 # Changing the local copy of x, does not affect the original
print("Inside call_by_value function, after change:", x)
# Main program
Output:-
Before call_by_value: 10
Inside call_by_value function, before change: 10
Inside call_by_value function, after change: 100
After call_by_value: 10
def modify_number(x):
x = 10 # Local assignment, does not affect the original variable
# main program
a=5
modify_number(a)
print(a) # Output: 5, original value of 'a' is unchanged
def modify_list(lst):
lst.append(4) # Modifies the original list
my_list = [1, 2, 3]
modify_list(my_list)
print(my_list) # Output: [1, 2, 3, 4], the original list is modified
Output:-
5
[1,2,3,4]
Palindrome or not:-
def isPalindrome(string):
if (string == string[::-1]) :
return "The string is a palindrome."
else:
return "The string is not a palindrome."
print(isPalindrome(string))