Curriculum
Course: Python-100-Programmes
Login
Text lesson

G-Functions-(93–100)

G-Functions-(93–100)

 

  1. Create a function to add two numbers.

Here is a simple Python function to add two numbers:

# Function to add two numbers

def add_numbers(a, b):

    return a + b

 

# Input numbers

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

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

 

# Call the function

result = add_numbers(num1, num2)

 

print(“Sum:”, result)

 

Example

Input:

Enter first number: 10

Enter second number: 25

 

Output:

Sum: 35.0

 

Explanation

  • def add_numbers(a, b): defines a function with two parameters

  • return a + b returns the sum of the numbers

  • We call the function with user inputs and print the result

 

  1. Create a function to find the square of a number.

Here is a simple Python function to find the square of a number:

# Function to find the square of a number

def square(num):

    return num * num

 

# Input number

n = float(input(“Enter a number: “))

 

# Call the function

result = square(n)

 

print(“Square of the number:”, result)

 

Example

Input:

Enter a number: 5

 

Output:

Square of the number: 25.0

 

Explanation

  • def square(num): defines a function that takes one parameter

  • return num * num calculates and returns the square

  • We call the function with user input and display the result

 

  1. Create a function that returns the cube of a number.

Here is a simple Python function to return the cube of a number:

# Function to find the cube of a number

def cube(num):

    return num ** 3

 

# Input number

n = float(input(“Enter a number: “))

 

# Call the function

result = cube(n)

 

print(“Cube of the number:”, result)

 

Example

Input:

Enter a number: 3

 

Output:

Cube of the number: 27.0

 

Explanation

  • def cube(num): defines a function with one parameter

  • return num ** 3 calculates the cube of the number

  • We call the function and print the result

 

  1. Create a function to check if a number is prime.

Here is a Python function to check if a number is prime:

# Function to check if a number is prime

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

 

# Input number

num = int(input(“Enter a number: “))

 

# Call the function

if is_prime(num):

    print(f”{num} is a prime number.”)

else:

    print(f”{num} is not a prime number.”)

 

Example

Input:

Enter a number: 17

 

Output:

17 is a prime number.

 

Explanation

  • Numbers ≤ 1 are not prime

  • Loop from 2 to √n (efficient check)

  • If any number divides n, it is not prime

  • Otherwise, it is prime

 

  1. Create a function to calculate simple interest.

Here is a Python function to calculate simple interest:

# Function to calculate simple interest

def simple_interest(principal, rate, time):

    return (principal * rate * time) / 100

 

# Input values

p = float(input(“Enter principal amount: “))

r = float(input(“Enter rate of interest: “))

t = float(input(“Enter time in years: “))

 

# Call the function

si = simple_interest(p, r, t)

 

print(“Simple Interest is:”, si)

 

Example

Input:

Enter principal amount: 1000

Enter rate of interest: 5

Enter time in years: 2

 

Output:

Simple Interest is: 100.0

 

Explanation

  • Formula: SI = (Principal × Rate × Time) / 100

  • Function simple_interest takes 3 parameters: principal, rate, time

  • Returns the calculated simple interest

 

  1. Create a function that returns the largest of three numbers.

Here is a Python function to return the largest of three numbers:

# Function to find the largest of three numbers

def largest_of_three(a, b, c):

    if a >= b and a >= c:

        return a

    elif b >= a and b >= c:

        return b

    else:

        return c

 

# Input three numbers

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

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

num3 = float(input(“Enter third number: “))

 

# Call the function

largest = largest_of_three(num1, num2, num3)

 

print(“The largest number is:”, largest)

 

Example

Input:

Enter first number: 10

Enter second number: 25

Enter third number: 15

 

Output:

The largest number is: 25.0

 

Explanation

  • Compare a, b, and c using if-elif-else

  • Return the largest number

  • Works for integers and floats

 

  1. Write a function that takes a list of numbers and returns the sum of all elements.

Here is a Python function that takes a list of numbers and returns the sum of all elements:

# Function to calculate sum of all elements in a list

def sum_of_list(numbers):

    total = 0

    for num in numbers:

        total += num

    return total

 

# Input list of numbers

n = int(input(“Enter number of elements in the list: “))

my_list = []

for i in range(n):

    my_list.append(float(input(f”Enter element {i+1}: “)))

 

# Call the function

total_sum = sum_of_list(my_list)

 

print(“Sum of all elements:”, total_sum)

 

Example

Input:

Enter number of elements in the list: 5

Enter element 1: 10

Enter element 2: 20

Enter element 3: 5

Enter element 4: 15

Enter element 5: 25

 

Output:

Sum of all elements: 75.0

 

Explanation

  • Function sum_of_list(numbers) loops through each number and adds it to total

  • Returns the sum of all elements

  • Works for integers and floats

 

  1. Write a function that takes a string and returns the number of vowels in it.

Here is a Python function that takes a string and returns the number of vowels:

# Function to count vowels in a string

def count_vowels(s):

    vowels = “aeiouAEIOU”

    count = 0

    for ch in s:

        if ch in vowels:

            count += 1

    return count

 

# Input string

text = input(“Enter a string: “)

 

# Call the function

vowel_count = count_vowels(text)

 

print(“Number of vowels:”, vowel_count)

 

Example

Input:

Hello World

 

Output:

Number of vowels: 3

 

Explanation

  • vowels = “aeiouAEIOU” contains all vowels

  • Loop through each character in the string

  • Increment count if character is a vowel

  • Return the total count

 

Scroll to Top