Function in Python

Function in Python


  • What is function?
    • A function is a named sequence of statements that performs a computation(operation).
      or,
      A parameterized sequence of statements.
    • When you define a function, you specify the name and the sequence of statements.
    • Later, you can “call” the function by name.
    • The expression in parentheses is called the argument of the function.
    • The argument is a value or variable that we are passing into the function as input to the function.
  • What is arguments?
    • A value passed to a function (or method) when calling it.
  • What is parameter?
    • Inside the function, the arguments are assigned to variables called parameters.
    • Here is an example of a user-defined function that takes an argument.
  • What is return value?
    • It is common to say that a function “takes” an argument and “returns” a result.
    • The result is called the return value.
  • What are function types?
    • Built-in function example
    • User-defined function

Built-in function

  • print()
  • max()
  • min()
  • sum()
  • pow()
  • range()  
  • reversed()
  • sorted()
  • count()

Examples:


sum()

a = (1, 2, 3, 4, 5)
x = sum(a)
print(x)

pow()


x = pow(2, 3)
print(x)

range()

x = range(6)
for n in x:
  print(n)

reversed()

alph = ("a", "b", "c", "d")
ralph = reversed(alph)
for x in ralph:
  print(x)

sorted()

a = ("b", "g", "a", "d", "f", "c", "h", "e")
x = sorted(a)
print(x)

count()

thistuple = (1, 3, 7, 8, 7, 5, 4, 6, 8, 5)
x = thistuple.count(5)
print(x)

Explanation :


Program 1: Write a python program to print Hello India with help of function.


def india():
    print("Hello india")
   
india()

Output : Hello india

Program 2 : Write a python to add two number in python with help of function.

def adding():
    a = 2
    b = 3
    print(a+b)
   
adding()

Output :5

Program 3:  Write a python to add two number in python with help of function by taking input.


#Called function

def Adding():
a = int(input('Enter A value: '))
b = int(input('Enter B value: '))

result = a+b
print(result)

#Calling function
Adding()



Output :5 if input is a=2 and b=3


Program 4:  Write a python program to find the greatest of two number.

Program :

def max_of_two( x, y ):
    if x > y:
        return x
    return y

print(max_of_two(9, 6))


Output :9

Or

def max_of_two( x, y ): if x > y: return x else: return y result=max_of_two(9, 6) print( "the greatest number is ",result)

Program 5:  Write a python program to find the greatest of two number by taking input.

Program:

#Called function
def Greatest():
    a = int(input('Enter A value: '))
    b = int(input('Enter B value: '))

    if (a>=b):
        print(a," is greater")
    else:
        print(b," is greater")
        
#Calling function
Greatest()

Output : 9 is greater

Program 6:  Write a python program to find odd and even number.

Program:

#calling function
def oddeven():
    n = int(input('Enter n value:'))
    if(n%2==0):
        print("even number.")
    else:
        print("odd number.")
#called function

oddeven()

Output : Input: 9 , Odd number


Types of Functions


1. Postional arguments
2. Keyword arguments
3. Default arguments
4. Variable length arguments(*args,**kwargs)



1. Postional arguments
  • These are the arguments passed to the function in correct Postional order.
  • Postion of arguments should match excatly with the number and Postion of the arguments of the function call.

def attach(s1,s2):
    s3=s1+s2
    print("Totals string: ",s3)

attach('New','York')


2. Keyword arguments
  • It is used to identify the parameter by their names 

def grocery(item,price):
    print("Item name: ",item)
    print("Price of the item is: ",price)

grocery(item="sugar",price="Rs.100")

3. Default arguments

def grocery(item,price=100):
    print("Item name: ",item)
    print("Price of the item is: ",price)

grocery(item="sugar")

4. Variable length arguments

-----
*args
-----
  • Start the variable name with *.
  • args means argument.
Example 1:

def calculate(*args):
    sum=0
    for i in args:
        sum+=i

    print(sum)

calculate(2,2,2,2)

Example 2:

def calculate(*b):
    sum=0
    for i in b:
        sum+=i

    print(sum)

calculate(2,2,2,2)


--------
**kwargs
--------
  • Start the variable name with ** .
  • kwargs stand for keyword arguments.
def display(**kwargs):
    for x,y in kwargs.items():
        print("key={},value={}".format(x,y))

display(rno=10,name="nandani")
        

Assignment:

  1. Find out the difference between args and kwargs. (min 3)

Program 7:  Write a python program to find odd and even number. without modulus operator.

Code:
n=9;
if n&1 == 1:
    print("odd")

else:
    print("even");


Thank-you for visiting this page.


This Page Will Update Soon, So Stay Connected.



If you have any doubt/queries then please contact us

Contact Us




Comments

Popular posts from this blog

Invalid syntax , perhaps you forgot a comma? Error in Python

MAD Project (Mobile Android Application Development)

Interview/Exam Questions on List in Python.