You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
defmyfunc(*args):
returnsum(args) *0.05print(myfunc(50,60))
#args - python treats agrs as tuples#args is just name , it can be anything , just make sure it should followed with '*' symboldefmyfuncforkargs(**kwargs):
print(kwargs)
if'fruit'inkwargs:
print('My fruit of choice is {}'.format(kwargs['fruit']))
else:
print('I did not find any fruit here')
myfuncforkargs(fruit="apple",icecream="butterscotch")
#kargs - means it is sending the argubments as key word arguments called as dictonories#we can use both at same timedefmy_function(*args,**kwargs):
print('I would like {} {}'.format(args[0],kwargs['food']))
my_function(10,20,30,food="eggs",fruit="apple")
11.2 My Function
#Example#Creating a functiondefmy_function():
print("Hello from a function")
#Calling a Functionmy_function()
#How to send parametersdefgreetings(name):
print("Good Morning, "+name)
#Calling a greetings functiongreetings("Vinay")
#Returning functiondefaddition(num1,num2):
returnnum1+num2;
#Calling addition function#print("Addition =>"+addition(10,20)) #Will give errorresult=addition(10,20)
print("Addition of 10 and 20 is {}".format(result))
#Default Parameter Value#The following example shows how to use a default parameter value.#If we call the function without parameter, it uses the default value:defsubstraction(num1=0,num2=0):
print(num1-num2)
substraction(30,20)
substraction();
substraction(50,50)
11.3 neted statement And Scope
x=20defprinter():
x=10returnxprint(x)
#LEGB#1. Local(L): Defined inside function/class#2. Enclosed(E): Defined inside enclosing functions(Nested function concept)#3. Global(G): Defined at the uppermost level#4. Built-in(B): Reserved names in Python builtin modules#Globlename="Global Sudhir"defgreet():
#ENCLOSINGname="Enclosing Sudhir"defhello():
#LOCALname="LOCAL Sudhir"print("Hello "+name)
hello()
greet()