(Grade X)Chapter 4- Programming in Python-Function exercise
Book exercise
Page number 193
Select the correct answer from the given alternatives:
a. Which of the following statements is false about user defined function in Python?
i.
Function may or may not return value.
ii. Function never returns value.
iii. Function can return multiple values.
iv. Function must be called in order to execute.
b.
Which keyword is used to define a user-defined function in Python?
i.
fun
ii. def
iii. create
iv. define
c.
What are the variables which appear in the function definition called?
i.
Arguments
ii. Local
iii. Parameters
iv. Global
d.
In which arguments the values are transferred from the arguments to the
parameters based on their position?
i.
Positional
ii. Default
iii. Order
iv. Arbitrary
e.
Which arguments are used when the number of arguments that have to be passed
into a function is not known in advance?
i.
Positional
ii. Default
iii. Keyword
iv. Arbitrary
f.
Which variable has limited scope and cannot be accessed from other part of the
program?
i. Local
ii. Limited
iii. Global
iv. Keyword
g. Variables
declared inside a function have……
i. No scope
ii. Local scope
iii. Unlimited scope
iv. Global scope
h. What does the
return statement do in a Python function?
i. Prints output to the console.
ii. Takes input from the user.
iii. Sends a value back to the calling function.
iv. Defines the function.
Page number-195
2. Short Answer
Questions
a. What is function? List the different types of function.
A function is a
block of code that performs a specific task and can be reused whenever needed.
Types of functions:
Built-in Functions
User Defined
Functions
b. Define
parameters and arguments.
Parameters are
variables listed in the function definition that receive values.
Arguments are the
actual values passed to a function when it is called.
Example:
def add(a, b): # a and b are parameters
print(a + b)
add(5, 3) # 5 and 3 are arguments
c. Explain local and global variables.
Local Variable: A
variable declared inside a function and can only be used within that function.
Global Variable: A
variable declared outside a function and can be accessed throughout the
program.
Example:
x = 10 # Global variable
def show():
y = 5
# Local variable
print(y)
show()
print(x)
d. Write the syntax
of creating user defined function.
Syntax:
def
function_name(parameters):
statements
Example:
def greet():
print("Hello")
e. What is the
purpose of the return keyword in a Python function?
The return keyword
is used to send a value back from a function to the place where the function
was called.
Example:
def add(a, b):
return a + b
result = add(2, 3)
print(result)
4. Long Answer
Questions
a. Write the
advantages of creating user defined function in a program.
Advantages of user
defined functions are:
Code Reusability:
The same code can be used multiple times.
Reduces Repetition:
Avoids writing the same code again and again.
Easy Debugging:
Errors can be found and corrected easily.
Improves
Readability: Makes programs easier to understand.
Simplifies
Maintenance: Changes can be made in one place.
Modular
Programming: Large programs can be divided into smaller manageable parts.
Saves Time and
Effort: Reduces programming workload.
b. Explain the
different types of function arguments with examples.
1. Positional
Arguments
Arguments are
passed in the same order as parameters.
def student(name,
age):
print(name, age)
student("Ram",
15)
2. Keyword
Arguments
Arguments are
passed using parameter names.
def student(name,
age):
print(name, age)
student(age=15,
name="Ram")
3. Default
Arguments
Parameters are
assigned default values.
def
greet(name="Guest"):
print("Hello", name)
greet()
greet("Ram")
4. Variable Length
Arguments
A function can
accept any number of arguments using *args.
def total(*num):
print(sum(num))
total(10, 20, 30)
c. Differentiate
between functions that return a value (non-void) and those that do not (void).
Explain with examples.
|
Void Function |
Non-Void Function |
|
Does not return
any value. |
Returns a value
using return. |
|
Performs a task
and ends. |
Performs a task
and sends a result back. |
|
Result cannot be
stored directly. |
Result can be
stored in a variable. |
Example of Void
Function
def greet():
print("Welcome")
greet()
Output:
Welcome
Example of Non-Void
Function
def add(a, b):
return a + b
result = add(5, 3)
print(result)
Output:
8
Lab sheet 1
1. Write a program to
calculate simple interest when principle, rate and time is given by the user
using a function. [Hints: I=PTR/100]
def simple_interest(p, r, t):
si = (p * r * t) / 100
return si
principle = float(input("Enter
principle: "))
rate = float(input("Enter rate:
"))
time = float(input("Enter time:
"))
result = simple_interest(principle,
rate, time)
print("Simple Interest =",
result)
2. Write a program to
calculate the area of a circle by creating user defined function.[ Hints:A-IR²]
def area_circle(r):
area = 3.14 * r * r
return area
radius = float(input("Enter radius: "))
result =
area_circle(radius)
print("Area
of Circle =", result)
3. Write a program to
calculate the total surface area of a room by using user defined function.. [Hints:
TSA=2(LB+BH+HL)]
def
total_surface_area(l, b, h):
tsa = 2 * (l*b + b*h + h*l)
return tsa
length =
float(input("Enter length: "))
breadth =
float(input("Enter breadth: "))
height =
float(input("Enter height: "))
result =
total_surface_area(length, breadth, height)
print("Total
Surface Area =", result)
4. Write a program to check
whether the given number is even or odd by using function.
def
check_even_odd(n):
if n % 2 == 0:
print("Even Number")
else:
print("Odd Number")
num =
int(input("Enter a number: "))
check_even_odd(num)
5. Write a program to display
the smallest number among any two numbers given by the user using function.
def smallest(a,
b):
if a < b:
print("Smallest number is",
a)
else:
print("Smallest number is",
b)
x =
int(input("Enter first number: "))
y =
int(input("Enter second number: "))
smallest(x, y)
6. Write a program to display
the greatest number among the three different numbers given by the user by
creating user defined function.
def greatest(a,
b, c):
if a > b and a > c:
print("Greatest number is",
a)
elif b > c:
print("Greatest number is",
b)
else:
print("Greatest number is",
c)
x =
int(input("Enter first number: "))
y =
int(input("Enter second number: "))
z =
int(input("Enter third number: "))
greatest(x, y,
z)
7. Write a program to check
whether the given number is positive, negative or neutral by using function.
def
check_number(n):
if n > 0:
print("Positive Number")
elif n < 0:
print("Negative Number")
else:
print("Neutral Number")
num =
int(input("Enter a number: "))
check_number(num)
8. Write a program to input
Selling price and Cost price from the user and calculate the profit amount or
loss Amount or display neither profit nor loss by using function.
def
profit_loss(cp, sp):
if sp > cp:
print("Profit Amount =", sp -
cp)
elif cp > sp:
print("Loss Amount =", cp -
sp)
else:
print("Neither Profit Nor
Loss")
cp =
float(input("Enter Cost Price: "))
sp =
float(input("Enter Selling Price: "))
profit_loss(cp,
sp)
9. Write a program to display the sum of all the
numbers from 1 to 20 by using function.
def
sum_numbers():
total = 0
for i in range(1, 21):
total += i
print("Sum =", total)
sum_numbers()
10. Write a program to create
user defined function factors(n) to display the factors of the given number.
def factors(n):
print("Factors are:")
for i in range(1, n + 1):
if n % i == 0:
print(i)
num =
int(input("Enter a number: "))
factors(num)
def
factors(n):
result = []
for i in range(1, n + 1):
if n % i == 0:
result.append(i)
return result
num
= int(input("Enter a number: "))
f
= factors(num)
print("Factors
are:")
for
i in f:
print(i)
11. Write a program to display
the factorial of a given number by creating user defined function.
def
factorial(n):
fact = 1
for i in range(1, n + 1):
fact = fact * i
return fact
num =
int(input("Enter a number: "))
result =
factorial(num)
print("Factorial
=", result)
12. Write a program to display
whether the given number is prime or composite by creating user defined
function.
def
check_prime(n):
count = 0
for i in range(1, n + 1):
if n % i == 0:
count += 1
if count == 2:
print("Prime Number")
else:
print("Composite Number")
num =
int(input("Enter a number: "))
check_prime(num)
13. Write a program to create a
user defined function reverse(n) to display the given number in reverse order.
def reverse(n):
rev = 0
while n > 0:
digit = n % 10
rev = rev * 10 + digit
n = n // 10
return rev
num =
int(input("Enter a number: "))
result = reverse(num)
print("Reversed
Number =", result)
14. Write a program to display
the sum of individual digits of a given number by using function.
def
digit_sum(n):
total = 0
while n > 0:
digit = n % 10
total += digit
n = n // 10
return total
num =
int(input("Enter a number: "))
result =
digit_sum(num)
print("Sum
of Digits =", result)
15. Write a program to count the number of consonants present in the given word by using function.
def consonants(w):
c = 0
u = w.upper()
for i in u:
if i != 'A' and i != 'E' and i != 'I' and i != 'O' and i != 'U':
c = c + 1
return c
w = input("Enter the word: ")
v = consonants(w)
print("Number of consonants are", v)
16. Write a program to count
the number of characters A/a present in the given word by using function.
def
count_a(word):
count = 0
for ch in word:
if ch == 'A' or ch == 'a':
count += 1
return count
text =
input("Enter a word: ")
result =
count_a(text)
print("Number
of A/a =", result)
17. Write a program to display
the shortest word among two different words by creating function named short()
def
short(word1, word2):
if len(word1) < len(word2):
print("Shortest word is",
word1)
else:
print("Shortest word is",
word2)
w1 =
input("Enter first word: ")
w2 =
input("Enter second word: ")
short(w1, w2)
18. Write a program to store
five numbers in the list. The program should then display the product of all
the numbers by creating user defined function and store them in the list.
def
product_list(lst):
product = 1
for num in lst:
product *= num
return product
numbers = []
for i in
range(5):
n = int(input("Enter number: "))
numbers.append(n)
result =
product_list(numbers)
print("Product
=", result)
19. Write a program to input
ten numbers from the user. The program should then display the greatest number
among ten numbers by using function.
def
greatest(lst):
max_num = lst[0]
for num in lst:
if num > max_num:
max_num = num
return max_num
numbers = []
for i in
range(10):
n = int(input("Enter number: "))
numbers.append(n)
result =
greatest(numbers)
print("Greatest
number =", result)
20. Write a program to
calculate the to calculate the area and volume of a cylinder Create a user
defined function area() to calculate area and volume() to calculate volume.
[Hints: A=2IRH,V=IR-H] defined function
def
area(radius, height):
area_value = 2 * 3.14 * radius * height
return area_value
def
volume(radius, height):
volume_value = 3.14 * radius * radius *
height
return volume_value
radius =
float(input("Enter radius: "))
height =
float(input("Enter height: "))
area_result =
area(radius, height)
volume_result =
volume(radius, height)
print("Area
=", area_result)
print("Volume
=", volume_result)
21. Write a program to input
two numbers from the user. Create a user to display sum, product and difference
among two numbers by returning multiple values.
def
calculate(a, b):
s = a + b
p = a * b
d = a - b
return s, p, d
x =
int(input("Enter first number: "))
y =
int(input("Enter second number: "))
sum1, product,
difference = calculate(x, y)
print("Sum
=", sum1)
print("Product
=", product)
print("Difference
=", difference)
22. Write a program to display
the greatest and smallest number among three different numbers by creating
separate user defined function.
def greatest(a,
b, c):
return max(a, b, c)
def smallest(a,
b, c):
return min(a, b, c)
x =
int(input("Enter first number: "))
y =
int(input("Enter second number: "))
z =
int(input("Enter third number: "))
print("Greatest
number =", greatest(x, y, z))
print("Smallest
number =", smallest(x, y, z))
23. Write a program to input a
word from the user. The program should then display the reverse and alternate
characters by creating function.
def reverse_word(word):
rev = ""
for ch in word:
rev = ch + rev
return rev
def alternate_char(word):
alt = ""
for i in range(len(word)):
if i % 2 == 0:
alt = alt + word[i]
return alt
text = input("Enter a word: ")
print("Reverse =", reverse_word(text))
print("Alternate characters =", alternate_char(text))
24. Write a program to
calculate the area and perimeter of a room by returning multiple values in
function. [Hints: a=lxb, v=lxbxh]
def room(l, b):
area = l * b
perimeter = 2 * (l + b)
return area, perimeter
length =
float(input("Enter length: "))
breadth =
float(input("Enter breadth: "))
a, p =
room(length, breadth)
print("Area
=", a)
print("Perimeter
=", p)