Examples of Function - 1
Example 1:
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:
Output
81
Example 4:
Output
9
Example 5:
Output
16
8
1
243
Example 6:
Output
Enter x : 3
Enter y : 6
6 3
Example 7:
0
1
3
55
Example 8:
<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:
<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:
27
Example 11:
Positional Argument : 1
Default Argument : 0
Here is Your Var length arguments :
Positional Argument : 1
Default Argument : 2
Here is Your Var length arguments :
Positional Argument : 1
Default Argument : 2
Here is Your Var length arguments :
1 Arg = 3
2 Arg = 4
3 Arg = 5
4 Arg = 6
5 Arg = 7
6 Arg = 8
7 Arg = 9
Example 12:
(1, 2, 3)
(4, 5, 6, 7, 8, 'hello', 'hi', 'bye')
Example 13:
Enter number : 34
Number of Digits = 2
Example 14:
Enter a no to calculate factorial : 23
Time Taken = 0.0
Example 15:
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)
Last updated
Was this helpful?