Function

Function of python

Let's understand about function of Python

 

 

 

 

some function example:

type()

print()

input()

int()


why we use function

i. reduce duplicacy in code

ii. makes our code easy to understand

iii. wrapping our code into small boxes

 

Let's understand with example:

we call function 1 time

 
    def greet():
print("Hello world")
print("Welcome to AttainU")
print("print some text")
greet()
 
 
    output:
 
    Hello world
    Welcome to AttainU
    print some text
    

 

def is a Keyboard.

greet is an identifier. Identifier can any name you give.

greet() call the function.

 

we call function 2 times & that's why its run 2 times


def greet():
print("Hello world")
print("Welcome to AttainU")
print("print some text")
greet()

greet()
 
 
    output:
 
    Hello world
    Welcome to AttainU
    print some text 
 
    Hello world
    Welcome to AttainU
    print some text


 

 

we call function many times as we want.



n = int(input("Enter value of n :"))
s = 0
for i in range(1, n+1):
s+= i ** 3
print(s)
    
    input: 5  
    output: 225




Above code we write under a function

 
    def sum_of_cubes():
n = int(input("Enter value of n :"))
s = 0
for i in range(1, n+1):
s+= i ** 3
print(s)
sum_of_cubes()
    
    input: 5
    output: 225
    




Use the function in some others files without writing same code.

 
    from func import sum_of_cubes
    sum_of_cubes()
 


Another Function example


def add(a,b):
s = a + b
return s
res = add(2,4)
print(res)

    output: 6
  


Pascal Tringal


 
 ## Pascal tringle
n = int(input("enter a value: "))
for row in range(1,n+1):
for stars in range(row):
print("*", end = " ")
print()
 
    output:
    enter a value: 5
    *
    * *
    * * *
    * * * *
    * * * * *  


Another Function example:


def test(n):
for i in range(n,1,-1):
return i
break
return 0
x = test(10)
print(x)
 
    output: 10



Another Function example:

 
 ## Star (15)
def star(n):
lin = 1
while lin<=n:
no_of_stars = 0
while no_of_stars<lin:
print("*", end=" ")
no_of_stars += 1
print()
lin += 1
star(3)



You can Multiply String with a number

 
    print("Hello world" * 3)

print("Hello world \n" * 3)
 
    output:
    Hello worldHello worldHello world

    Hello world
    Hello world
    Hello world


Print Star


def star(n):
for line in range(1, n+1):
print("*" * line)
star(5)
     
    output:
    *
    **
    ***
    ****
    *****
 



Function Example:

 
 
def ABC():
sum = 10
for i in range(10):
sum = 0
print(sum)
ABC()
 
 
   output: 0
 



 
    sum = 0
def ABC():
sum = 10
for i in range(10):
sum = 5             ## this is local variable

ABC()
print(sum)
 
    output: 0 
    
    output 0 because this is out of defination.


Global Variable


s = 5
# Global Variable
def ABC():
print(s)
# Local Variable
ABC()
print(s)
 
    Output: 
    5
    5
 



s = 5
# Global Variable
def ABC():
print(s)
# Local Variable
ABC()

 
    Output: 
    5
  
 


Another Global Variable

 
    s = 5
# Global Variable
def ABC():
s = 10
print(s)
# Local Variable
ABC()
print(s)

    Output: 
    10
    5
  










إرسال تعليق (0)
أحدث أقدم