#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

This is how we can create any function according to out need. 


Q:- Create a funtions in which you take the name as input and in reture you have to print the last character of the name.
Code:-
def last_name(a):
return a[len(a)-1] #reture a[-1]
num1=input("Enter your name:- ")
print(last_name(num1))
Output:- 
               Enter your name:- Gurpreet
               t
Q:- Create a function in which take a number as an input and print it is even or odd.
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)
OR
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))
OR
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))


Output:-
Enter the number:- 18
18 is even

Q3: Define a function for check number is even or odd. If number is even print true and for odd print odd.
def is_even(a):
if a%2==0:
return True
return False

num1=int(input("Enter the number:- "))
print(is_even(num1))

 OR
def is_even(a):
return a%2==0

num1=int(input("Enter the number:- "))
print(is_even(num1))
Output:-
Enter the number:- 26  #press Enter
True

Q4:- Create a function in which you have to take 2 numbers as input and print the greater number.
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)}")
Output:-
Enter 2 numbers separated by comma:- 36,45  #press Enter
The greater number :45

Q5:- Function for find greatest in 3 numbers
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

Popular posts from this blog

#4 Python :- Variables

#3 Python :- Comments, Printing First Statement and Escape sequences:-