Functions and Class in Python

Photo by Joey F. on Unsplash

Functions and Class in Python

When you have a large code and you have to use a block of code multiple times in different area of the code functions are used. A function is a block of code which can be called in different area of the program. I personally feel that functions are one of the important part in any programming language because when you are building an app , a website or assigning some function to any hardware a block of code can be reused multiple times. eg-

def sum(a,b):
    sum=a+b
    return sum

sum(a,b)

Types of functions in python-Built in functions This function is already built in python by default print() function

len()

type()

range()

  1. User defined function Function written by the user or the programmer.

    Q WAF to print the length of a list. ( list is the parameter) using python.

list=["a","b","c","d"]

def print_len(list):
    print(len(list))

print_len(list)

Q WAF to print the elements of a list in a single line. ( list is the parameter) using python.

list=["a","b","c","d"]


def print_len(lst):
    print(len(lst))
    n=len(lst)
    i=0
    for i in range(n):
        print(lst[i],end=" ")
        i=i+1

print_len(list)

Q WAF to find the factorial of n. (n is the parameter) using python.

num=int(input("Enter the num:"))
def fact(n):
        i=1
        for i in range(1,n+1):
                n=n*i

        return n
print(fact(num))

Q WAF to convert USD to INR using python.

n=5
def converter(usd):
    inr=usd*83
    print(usd,"1 USD=",inr)      
converter(n)

Q Function to input a number and print if its an odd number or an even number. n=int(input("Enter a number")) using python.

def odd(n):
    d=n%2
    if d==0:
        print("Even")
    else:
        print("Odd")
odd(n)

Recursion When a function calls itself repeatedly.

Eg:

def show(n):
        if(n==0):
                return#Cheaks the function and ends the loop
        print(n)
        show(n-1)# calls the function
n=5
show(n)

#n value constantly reduces and returns only when n becomes 0.

Understanding recursion using factorial

def fact(n):
        if(n==0 or n==1):#cheaks the condition
                return n
        else:
                print(n)
                return n*fact(n-1)#takes n value and multiplys with the factorial of n-1. 
that becoms 5*f(4)*f(3)*f(2)
        5*    4 *  3 *  2

n=5
print(fact(n))

Q Write a recursive function to calculate the sum of first n natural numbers using python.

n=5
def sum(n):
    if(n==0):
        return n
    else:
        return n+sum(n-1)

print(sum(n))

Q. Write a recursive function to print all elements in a list using python.

n=[1,2,3,4,5]
def ppt(n,i=0):
    l=len(n)
    if i==l:
        return 0
    print(n[i])
    ppt(n,i+1)

ppt(n)

Class: a class describes the contents of the objects that belong to it: it describes an aggregate of data fields (called instance variables), and defines the operations (called methods). object: an object is an element (or instance) of a class; objects have the behaviors of their class.

Syntax:

class a:

def __init__ (self,b,c):

self.a=a

self.b=b

WAP to assign a job to a job to John using class in python.

class Person:
    def __init__(self, name, job):
        self.name = name  
        self.job = job    
    def display_info(self):
        print(f"{self.name} is assigned to {self.job}.")

# Create instances for John and Peter
john = Person("John", "Engineer")
peter = Person("Peter", "Designer")

# Display their assigned jobs
john.display_info()
peter.display_info()

Hope you learnt something in this blog. This was just an introduction to the topic. Feel free to explore. Don't forget to like and follow for more coding related blogs.