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

Chapter 6

The document contains programming exercises focused on functions in Python, including tasks such as swapping variables, printing student details, calculating factorials, and implementing bubble sort. It also includes functions for checking Armstrong and prime numbers, finding terms in geometric sequences, and determining perfect numbers. Each exercise is accompanied by sample code demonstrating the implementation of the respective function.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chapter 6

The document contains programming exercises focused on functions in Python, including tasks such as swapping variables, printing student details, calculating factorials, and implementing bubble sort. It also includes functions for checking Armstrong and prime numbers, finding terms in geometric sequences, and determining perfect numbers. Each exercise is accompanied by sample code demonstrating the implementation of the respective function.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Chapter 6 – Introduction to Functions

Programming Exercise
1 Write a function for swapping two variables.
def swap_variables(a, b):
temp = a
a=b
b = temp
return a, b
2 Write a Python program to print a roll number and name of a student using
functions.
def print_student_details(roll_no, name):
print("Roll No:", roll_no)
print("Name:", name)

roll_no = input("Enter the roll number: ")


name = input("Enter the student name: ")

print_student_details(roll_no, name)
3 Write an iterative version function of the factorial of a number.
def factorial(n):
fact = 1
for i in range(1, n+1):
fact *= i
return fact
4 Write a Python function to compute average for variable length arguments of
numbers.
def calculate_average(*args):
if len(args) == 0:
return None
total = sum(args)
avg = total / len(args)
return avg
5 Implement a Python function for bubble sort a set of numbers {10, 2, 7, 24, 32}.
def bubble_sort(numbers):
n = len(numbers)
for i in range(n):
for j in range(0, n-i-1):
if numbers[j] > numbers[j+1]:
numbers[j], numbers[j+1] = numbers[j+1], numbers[j]
return numbers

numbers = [10, 2, 7, 24, 32]


sorted_numbers = bubble_sort(numbers)
print(sorted_numbers)

Output: [2, 7, 10, 24, 32]


6 Write a Lambda function for finding the multiplication of two numbers.
multiplication = lambda x, y: x * y

COPYRIGHT © 2023 PEARSON INDIA EDUCATION SERVICES PVT. LTD


7 Write a function for finding whether the given number is an Armstrong number or
not.
def is_armstrong_number(num):
num_digits = len(str(num))
sum_of_digits = sum(int(digit) ** num_digits for digit in str(num))
return sum_of_digits == num
8 Write a function for checking whether the number is a prime number or not.
def is_prime_number(num):
if num < 2:
return False
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
return False

return True
9 Write a function that finds the Nth element of a geometric sequence.
def tn_gs(a, r, n):
nth_term = a * r ** (n-1)
return nth_term
10 Write a program to check whether the number is a perfect number or not.
def is_perfect_number(num):
divisor_sum = 0
for i in range(1, num):
if num % i == 0:
divisor_sum += i
if divisor_sum == num:
return True
else:
return False

COPYRIGHT © 2023 PEARSON INDIA EDUCATION SERVICES PVT. LTD

You might also like