FUNCTION
A function is a group of statements made to execute them more than once in a program. A function
has a name. Functions can compute a result value and can have parameters that serve as function
inputs which may differ each time when the function is executed
Functions are used to:
- Reduce the size of the code as it increases the code reusability
- Split a complex problem into multiple modules (functions) to improve manageability
Example-1
# Global scope
X = 99 # X and func assigned in module: global
def func(Y): # Y and Z assigned in function: locals
# Local scope
Z = X + Y # X is a global
return Z
func(1) # func in module: result=100
Example-2
X = 88 # Global X
def func():
global X
X = 99 # Global X: outside def
func()
print(X) # Prints 99
Q1. Write a function Pytha that, given the lengths of the two sides of a right triangle adjacent to the right angle, computes the length of the hypotenuse of the triangle.
def Pytha(base,perpendicular): hypotnuse=((base)**2 + (perpendicular)**2)**1/2 return print(f'the hypotnuse of the triangle is {hypotnuse}') Pytha(2,2)
Q2. Implement function avg that takes as input a list that contains lists of numbers. Each number list represents the grades a particular student received for a course.
def avg(listt): for i in listt: summ = 0 lent = len(i) for x in i: summ += x b = summ / lent print(f'{b}') avg([[95, 92, 86, 87], [66, 54], [89, 72, 100], [33, 0, 0]])
Q3. Write function lastF that takes as input two strings of the form ‘FirstName’ and ‘LastName’, respectively, and returns a string of the form ‘LastName, F.’. Only the initial should be output for the first name.
def lastF(firstname,lastname): return print(f"'{lastname}',{firstname[0]}.") lastF('Hamza','Ali')
Q4. Construct an outer function “out_circle” that takes radius „r1‟ of an outer circle as argument and calculates its area. Also construct an inner function “in_circle” that calculates it‘s circumference with a smaller radius. Inner function should be enclosed within the outer.
def out_circle(rad1): print("area of outer circle is : ",round(3.14*(rad1**2),2)) while True: rad2=float(input("enter radius of in_circle : ")) if rad2>=rad1: print("'Enter a value less than that of outer radius'") else: break def in_circle(rad2): print("circumference of in_circle is : ",round(2*3.14*rad2,3)) in_circle(rad2) rad1=float(input("enter radius of outer circle : ")) out_circle(rad1)
Q5. Write a function selected_prime that prints all the prime numbers less than 1,000 that contain the digit 2 or digit 3 (or both). Organize your code to use two functions: the first one takes an integer as argument, returns True if the number is prime and False otherwise; the second function checks if an integer contains the digits 2 or 3 or both.
def is_prime(num): count = 0 for i in range(1,num+1): if num%i==0: count+=1 if count==2: return True else: return False def is_value(num): return '2' in str(num) or '3' in str(num) def selected_prime(): for i in range(1,1001): if is_prime(i) and is_value(i): print(i) selected_prime()