#----------------------------- 1 - creating a function ----------------------------
def myFunction():
print("Learn Pyhton from us..")
# Calling a function
myFunction()
#----------------------------- 2 - passing arguments in function --------------------
# passing arguments. (Arguments - the values passed inside the paramaeters)
sum =0
def total(n1,n3):
sum= n1+ n3
print("Total : ",sum)
num =int(input("Enter the 1st number :"))
num2 = int(input("Enter the 2nd number :"))
total(num,num2)
total([8,97,23],[12,89,77]) # two list are considered as two arguments
total(5,6,8) # Results in error, because can only pass 2 arguments
#----------------------------- 3 - passing *n tuple arguments in function ------------
def total(*nums): # * - is used when number of parameters is not known
sum= nums[2]+nums[3]
print("Total : ",sum)
total(5,4,3,2,1,6,7,8)
#----------------------------- 4 - passing **n dictionary arguments in function ------
def send(**value): # syntax : def function_name(**name):
print("The value is " + value["value"])
send(key = "webgapp", value = "Python")
send(key = "webgapp", value = "HTML")
#----------------------------- 5 - passing keyword arguments in function ----------
def course(course3, course2, course1): # need to specify only the keys
print("The easiest course is " , course1)
print("The shortest course is ",course3)
print("Web development course is ", course2)
course(course1 = "Python", course2 = "Django", course3 = "HTML")
#----------------------------- 6 - passing list of arguments in function ----------
def course(name):
for i in name:
print(i)
value = ["Python", "Django", "HTML","CSS"]
course(value)
#----------------------------- 7 - return a value in function ----------------------
def many(val):
return 5 * val # retuns a value
print(many(3)) # returns 5 * 3
print(many("hi")) # returns hi for 5 times
print(many(9.9)) # returns 5 * 9.9
#----------------------------- 8 - recursion in function -----------------------------
def web(n):
if(n > 0):
ans = n + web(n -1)
print(ans)
else:
ans = 0
return ans
print("\n\nRecursion Example anss")
tim = int(input("Enter the number : "))
web(tim)
#----------------------------- 9 - finding square number in function -----------------
def square( list ):
squares = [ ]
for j in list:
squares.append( j**2 )
return squares
# calling the defined function
items = [6,7, 2, 8,21]
number = square( items )
print( "Squares of the list are: ", number )
#----------------------------- 10 - pass by values in function -------------------
def myFun(value):
print("Value received id :", id(value))
# User's key code
key = 12
print("Value passed id : ", id(key))
myFun(key)