#13 Python :- Functions
Functions:-
There are two types of functions in python :-
i) Defined function:- These are the functions whose work is already pre-defined .
e.q.: type() funtion , len() funtion etc.
ii) Undefined function:- These are the functions whose work is not defined we have to write some code to execute such functions.
Let us see how we can defined a function:-
Note:- Defining a functions also means to creating a new function according to our need.
Q:- Make a function to add 2 numbers.
Code:- def add_two(a,b):
return a+b #we can also use print in place of return like:- print(a+b)
num1=int(input("Enter the first number:- "))
num2=int(input("Enter the second number:- "))
num3=add_two(num1, num2)
print(num3)
Output:-
Enter the first number:- 15
Enter the second number:- 25
40
def last_name(a):
return a[len(a)-1] #reture a[-1]
num1=input("Enter your name:- ")
print(last_name(num1))
def odd_even(a):
if a%2==0:
print(f"{a} is even")
else:
print(f"{a} is odd")
num1=int(input("Enter the number:- "))
odd_even(num1)
def odd_even(a):
if a%2==0:
return f"{a} is even"
else:
return f"{a} is odd"
num1=int(input("Enter the number:- "))
print(odd_even(num1))
def odd_even(a):
if a%2==0:
return f"{a} is even"
return f"{a} is odd"
num1=int(input("Enter the number:- "))
print(odd_even(num1))
def is_even(a):
if a%2==0:
return True
return False
num1=int(input("Enter the number:- "))
print(is_even(num1))
def is_even(a):
return a%2==0
num1=int(input("Enter the number:- "))
print(is_even(num1))
def big(a,b):
if a>b:
return a
return b
num1,num2 = input("Enter 2 numbers separated by comma:- ") .split(",")
num1=int(num1)
num2=int(num2)
print(f"The greater number :{big(num1,num2)}")
def big(a,b,c):
if a>b and a>c:
return a
elif b>a and b>c:
return b
return c
num1= int(input("Enter the 1st number:- "))
num2=int(input("Enter the second number:- "))
num3=int(input("Enter the third number:- "))
print(big(num1,num2,num3))
Output:-
Enter the 1st number:- 25
Enter the second number:- 99
Enter the third number:- 102
102
Functions inside function:-
We can call one function into another .
e.q. like above problem Q5 we can also create it by using Q4
Code:-
def big(a, b):
if a > b:
return a
return b
def greater(a, b, c):
greatest = big(a, b)
return big(greatest, c)#WE can also use:- return big(big(a, b), c)
num1 = int(input("Enter the first number:- "))
num2 = int(input("Enter the second number:- "))
num3 = int(input("Enter the third number:- "))
print(greater(num1, num2, num3))
Output:-
Enter the first number:- 10
Enter the second number:- 5
Enter the third number:- 25
25
Q6:- Create a function is_palindrome in which you have to check weather the word is palindrome or not.
def is_palindrome(a):
return a==a[::-1]
temp=1
while True:
name=input("Enter the word:- ")
if is_palindrome(name)==True:
print(f"You got {is_palindrome(name)} in your {temp} attempt")
break
print("Sorry! Try Again")
temp+=1
continue
Output:-
Enter the word:- madhu
Sorry! Try Again
Enter the word:- gurpreet
Sorry! Try Again
Enter the word:- monu
Sorry! Try Again
Enter the word:- mom
You got True in your 4 attempt
Comments
Post a Comment