Example 1:create a function to Add two numbers

      
          def add_numbers(a, b):
    return a + b

result = add_numbers(5, 3)
print("Sum:", result)

#Example with Variable length
def sum_numbers(*args):
    return sum(args)
print("Total:", sum_numbers(1, 2, 3))
print("Total:", sum_numbers(1, 2, 3, 4, 5))
          

Example 2:create a function to find LCM

      
          import math

def find_lcm(x, y):
    gcd = math.gcd(x, y)
    return abs(x * y) // gcd

# Example usage:
x = 48
y = 180
print("LCM of", x, "and", y, "is:", find_lcm(x, y))
          

Example 3:Create a function to find GCD|HCF

      
          import math

def find_gcd(x, y):
    return math.gcd(x, y)

# Example usage:
x = 48
y = 180
print("GCD of", x, "and", y, "is:", find_gcd(x, y))
          

Example 4:The Lambda function

A lambda function in Python is a small anonymous function expressed as a single statement. It can have any number of arguments, but can only have one expression. It is often used for creating small, one-off functions that are not necessarily bound to a name (i.e., anonymous), and can be used in places where you need a function for a short period of time.

Characteristics

Characteristics Conciseness: Lambda functions are single-line functions, which makes them syntactically concise. Limited Functionality: They can only perform limited functions - essentially anything that can be expressed in a single expression. They don't have multiple expressions or commands. No statements: Lambda functions can't include statements or annotations. They are meant for simple logic. Anonymity: Lambda functions are anonymous, which means they are not declared in the standard manner by using the def keyword. You can use them without assigning them to a variable.

      
          multiply = lambda x, y: x * y

print("Product:", multiply(4, 5))
          

Example 5:create a function to find Factorial of a number

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

print("Factorial of 5:", factorial(5))
          

Example 6:How to find the GCD of a list

      
          import math
def gcdCalc(x,y):
    result = math.gcd(x,y)
    return result
def gcdgiver(numbers):
    initial = numbers[0]
    for i in numbers[1:]:
        initial = gcdCalc(initial,i)
    return initial
numbers = [15,25,35,45]
print("Gcd = ",gcdgiver(numbers))          

Example 7:

can you solve this question:Six bells commence tolling together and toll at intervals of 2, 4, 6, 8, 10 and 12 seconds respectively. In 30 minutes, how many times do they toll together?

      
          import math
from functools import reduce

def lcm(a, b):
    return abs(a * b) // math.gcd(a, b)

def lcm_multiple(numbers):
    return reduce(lcm, numbers)

# Finding the least common multiple of the tolling intervals: 2, 4, 6, 8, 10, and 12
bells_intervals = [2, 4, 6, 8, 10, 12]
lcm_bells = lcm_multiple(bells_intervals)
print(lcm_bells)

# Total time in seconds for 30 minutes
total_time_seconds = 30 * 60

# Number of times the bells toll together including the initial time at zero
times_toll_together = total_time_seconds // lcm_bells + 1
print(times_toll_together)

#output:The time that it takes for all of the bells to toll together =  16
          

Example 8:

An electronic device makes a beep after every 60 sec. Another device makes a beep after every 62 sec. They beeped together at 10 a.m. The next time, when they would beep together at the earliest is? can you solve this question

      
          import math

def lcm_cal(x,y):
    result = math.gcd(x,y)
    result2 = abs(x*y)//result
    return result2
total_time = lcm_cal(60,62)
answer = (total_time)/60
initial_time = 10
print("The Next time both will beep together will be = ",(initial_time),":", int(answer)," am")
#output:The Next time both will beep together will be =  10 : 31  am          

Example 9:A function that calls itself

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

print("Factorial of 5:", factorial(5))
          

Example 10:Sorting using Lambda function

      
          data = [(1, 2), (3, 1), (5, 10), (4, 4)]
data_sorted = sorted(data, key=lambda x: x[1])
print(data_sorted)  # Output: [(3, 1), (1, 2), (4, 4), (5, 10)]