def hello():
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 string
def reverse(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 power
def pow(x,y):
return x**y
x = pow(3,4) #positional Arguments
print(x)
Output
81
Example 4:
# function to calculate power
def pow(x,y):
return x**y
r = pow(y=2,x=3)
print(r)
Output
9
Example 5:
# function to calculate power
def pow(x,y):
return x**y
k = 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:
def swap(x,y):
return y,x
x,y = int(input("Enter x : ")),int(input("Enter y : "))
x,y = swap(x,y)
print(x,y)
def info(*args):
print(type(args))
c = 1
for var in args :
print("value {} : {}".format(c,var))
c = c + 1
info('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:
def info(**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()
def info(**info):
return info
st1 = 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:
def calc(x,y,ch):
ch = ch.strip()
if ch == '+' :
return x + y
elif ch == '-' :
return x - y
elif ch == '*' :
return x * y
elif ch == '/' or ch == '//' or ch == '%' :
if y == 0 :
return "Error!!Can not Divide by Zero"
else :
if ch == '/' :
return x / y
elif ch == '//' :
return x // y
else :
return x % y
elif ch == "**" :
return x ** y
else :
return "Error!!!Invalid Operation"
r = calc(3,3,'**')
print(r)
27
Example 11:
def fun(x,y=0,*mytuple):
print("Positional Argument : ",x)
print("Default Argument : ",y)
print("Here is Your Var length arguments : ")
c = 1
for var in mytuple :
print("{} Arg = {}".format(c,var))
c = c + 1
fun(1)
fun(1,2)
fun(1,2,3,4,5,6,7,8,9)
def count_Digits(num):
"""count_Digits(num) -> This function takes a num as formal argument and return no of digits in the num."""
count = 0
while num:
num = num // 10
count = count + 1
return count
n_digit = count_Digits(int(input("Enter number : ")))
print("Number of Digits = ",n_digit)
Enter number : 34
Number of Digits = 2
Example 14:
#recursion
#Factorial
import time
num = int(input("Enter a no to calculate factorial : "))
c = time.time()
s = 1
for var in range(1,num+1):
s = s * var
n = time.time()
#print(s)
print("Time Taken = ",n-c)
Enter a no to calculate factorial : 23
Time Taken = 0.0
Example 15:
# Armstrong Number
def count_Digits(num):
"""count_Digits(num) -> This function takes a num as formal argument and return no of digits in the num."""
count = 0
while num:
num = num // 10
count = count + 1
return count
def check_Armstrong(num):
p = count_Digits(num)
copy_num = num
s = 0
while num :
r = num % 10
s = s + r**p
num = num // 10
if s == copy_num :
return True
else :
return False
def armstrong():
if check_Armstrong(int(input("Enter a number : "))) :
print("Given number is Armstrong ")
else :
print("Given number is not A Armstrong Number ")
if input("\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)