defhello(): name =input("Enter your name : ")print("Welcome {} to the python functions.".format(name))print("function calling starts")x =hello()print("function calling finished")print(x)
Output
function calling starts
Enter your name : John
Welcome John to the python functions.
function calling finished
None
Example 2:
# function to reverse stringdefreverse(string):return string[::-1]s =reverse(input("Enter a string : "))print(s)s =reverse(input("Enter a list space seperated : ").split())print(s)
Enter a string : gyansetu
utesnayg
Enter a list space seprated : [1 2]
['2]', '[1']
Example 3:
# function to calculate powerdefpow(x,y):return x**yx =pow(3,4)#positional Argumentsprint(x)
Output
81
Example 4:
# function to calculate powerdefpow(x,y):return x**yr =pow(y=2,x=3)print(r)
Output
9
Example 5:
# function to calculate powerdefpow(x,y):return x**yk =pow(2,4)p =pow(2,3)print(k)print(p)print(pow(y=2,x=1))print(pow(3,y=5))
Output
16
8
1
243
Example 6:
defswap(x,y):return y,xx,y =int(input("Enter x : ")),int(input("Enter y : "))x,y =swap(x,y)print(x,y)
definfo(*args):print(type(args)) c =1for var in args :print("value {} : {}".format(c,var)) c = c +1info('one','two')print("\n\n")info('hello','hi','how','are','you')
<class 'tuple'>
value 1 : one
value 2 : two
<class 'tuple'>
value 1 : hello
value 2 : hi
value 3 : how
value 4 : are
value 5 : you
(**) returns dictionary
Example 9:
definfo(**kwargs):print(type(kwargs))for key,value in kwargs.items():print("{} = {}".format(key,value))info(name='sachin',)print()info(name='python',framework=['django','flask'])print()definfo(**info):return infost1 =info(name=input("Enter your name "),addr=input("Enter your address"))print(st1)
<class 'dict'>
name = sachin
<class 'dict'>
name = python
framework = ['django', 'flask']
Enter your name gyansetu
Enter your address gurgaon
{'name': 'gyansetu', 'addr': 'gurgaon'}
Example 10:
defcalc(x,y,ch): ch = ch.strip()if ch =='+':return x + yelif ch =='-':return x - yelif ch =='*':return x * yelif ch =='/'or ch =='//'or ch =='%':if y ==0:return"Error!!Can not Divide by Zero"else:if ch =='/':return x / yelif ch =='//':return x // yelse:return x % yelif ch =="**":return x ** yelse:return"Error!!!Invalid Operation"r =calc(3,3,'**')print(r)
27
Example 11:
deffun(x,y=0,*mytuple):print("Positional Argument : ",x)print("Default Argument : ",y)print("Here is Your Var length arguments : ") c =1for var in mytuple :print("{} Arg = {}".format(c,var)) c = c +1fun(1)fun(1,2)fun(1,2,3,4,5,6,7,8,9)
defcount_Digits(num):"""count_Digits(num) -> This function takes a num as formal argument and return no of digits in the num.""" count =0while num: num = num //10 count = count +1return countn_digit =count_Digits(int(input("Enter number : ")))print("Number of Digits = ",n_digit)
Enter number : 34
Number of Digits = 2
Example 14:
#recursion#Factorialimport timenum =int(input("Enter a no to calculate factorial : "))c = time.time()s =1for var inrange(1,num+1): s = s * varn = time.time()#print(s)print("Time Taken = ",n-c)
Enter a no to calculate factorial : 23
Time Taken = 0.0
Example 15:
# Armstrong Numberdefcount_Digits(num):"""count_Digits(num) -> This function takes a num as formal argument and return no of digits in the num.""" count =0while num: num = num //10 count = count +1return countdefcheck_Armstrong(num): p =count_Digits(num) copy_num = num s =0while num : r = num %10 s = s + r**p num = num //10if s == copy_num :returnTrueelse:returnFalsedefarmstrong():ifcheck_Armstrong(int(input("Enter a number : "))):print("Given number is Armstrong ")else:print("Given number is not A Armstrong Number ")ifinput("\nType something to repeat Again"):armstrong()armstrong()
Enter a number : 59
Given number is not A Armstrong Number
Type something to repeat Again
(Note: An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371. Write a program to find all Armstrong number in the range of 0 and 999)