Learn python from whole basic:
1.Functions in Python:-
#functions
def function1():
print("hello you are in function",a+b)
print(function1()) #if u don't use print in this then none will not come
function1()
function1()
function1() #the number of time u use this it will print the same
def sum2(a ,b):
return(a+b)
def sum1(a,b,c):
return(a+b+c)
print("Sum of two number is:",sum2(10,20))
print("Sum of three number is:",sum1(1,2,3))
def function2(a, b):
average = (a+b)/2
print(average)
return average #if we dont want none in result then type this return
v = function2(5, 7) #in this it will show average of upper
print(v)
#modularity
def add(a,b):
return(a+b)
def avg(a,b):
return(add(a,b)/2)
a=int(input("Enter number 1:"))
b=int(input("Enter number 2:"))
print("addition=",add(a,b))
print("average=",avg(a,b))
#functions argument
#1.Required argument
def display(str):
print(str)
display() #it will show error bcoz nothing is inside here
#2. Keyword argument
def display(str):
print(str)
display(str="this is the str than is gonna come in result")
#3.default argument
def sum1(a,b,c=1,d=2):
return(a+b+c+d)
print("sum=",sum1(1,2,3,4))
print("sum",sum1(1,2,3))
#4.Variable-length argument
def printvalue(a,*b):
print(a)
for v in b:
print(v)
printvalue(10)
printvalue(1,2,3,4)
printvalue("faizan","alam")
printvalue(10,20,30)
#anonymous function
sum = lambda a1, a2, a3:a1+ a2+ a3
print(sum(4,5,6))
#global and local variables
total = 0
def sum(arg1, arg2):
total=arg1+arg2;
print("inside the function local total:",total)
sum(10,20)
print("outside the function global total:",total)
#.to print triple quotes statement
print(function1,__doc__)
def function2(a, b):
""" """this is a doc statement in triple qutes""""""
print(function2.__doc__) #it will print the statement in triple quates
Comments
Post a Comment