python function problems for practice pdf 8 problems with solution
python function problems for practice pdf
problem 1
Define a function that accepts 2 values and return its sum, subtraction and multiplication.
Hint
Input:
Enter value of a = 7
Enter value of b = 5
Expected output
Sum is = 12
Sub is = 2
Multiplication is = 35
solution
def result(a, b): sum = a+b sub = a-b mul = a*b print(f"Sum is {sum}, Sub is {sub}, & Multiply is {mul}") a = int(input("Enter value of a: ")) # 7 b = int(input("Enter value of b: ")) # 5 result(a,b)
problem 2
Define a function that accepts roll number and returns whether the student is present or absent.
Hint
Use a list to store sample roll. no.
Get input from a user and check if the number
exist in the list or not, if exists return present
else return absent
solution
def detail(roll): x = [23,43,22,56] if roll in x: print(f"Roll number {roll} is present") else: print(f"Roll number {roll} is absent") roll = int(input("Enter roll no. ")) # 24 detail(roll) # output # Roll number 24 is absent
problem 3
Define a function in python that accepts 3 values and returns the maximum of three numbers.
Hint
Input:
Value of a = 30
Value of b = 22
value of c = 18
Expected output
30 is the maximum among all
solution
def max(a, b, c): if a > b and a > c: print(f"{a} is maximum among all") elif b > a and b > c: print(f"{b} is maximum among all") else: print(f"{c} is maximum among all") max(30,22,18)
problem 4
Define a function that accepts a number and returns whether the number is even or odd .
Hint
Input:
Enter a number = 4
Expected output
4 is an even number
Hint
Input:
Enter a number = 5
Expected output
4 is an odd number
solution
def func(x): if x % 2 == 0: print(f"{x} is Even number") else: print(f"{x} is Odd number") x = int(input("Enter a number ")) func(4)
problem 5
Define a function which counts vowels and consonant in a word.
Hint
Input:
Enter a word = pythonlobby
Expected output
Count of vowel is = 2
Count of consonant is = 9
solution
def count(val): vov = 0 con = 0 for i in range(len(val)): if val[i] in ['a','e','i','o','u']: vov = vov+1 else: con = con + 1 print("Count of vowels is ",vov) print("Count of consonant is ",con) x = input("Enter Value: ") # pythonlobby count(x)
problem 6
Define a function that returns Factorial of a number.
Hint
Input:
Enter a number = 6
Expected output
Factorial is 720
solution
def factorial(num): fact = 1 while(num!=0): fact *= num num = num - 1 print("Factorial is",fact) num = int(input("Enter number ")) factorial(num)
problem 7
Define a function that accepts lowercase words and returns uppercase words.
Hint
Input:
Enter a word = pythonlobby
Expected output
Result is = PYTHONLOBBY
solution
def response(text): z = text.upper() print(z) text = input("Enter String: ") # pythonlobby response(text)
problem 8
Define a function that accepts radius and returns the area of a circle.
Hint
Input:
Enter radius 4
Expected output
Area of a circle is 50.24
solution
def area(radius): area = 3.14*radius*radius return area radius = int(input("Enter Radius: ")) # 4 print(area(radius))
- Get link
- X
- Other Apps
Comments
Post a Comment