def keyword in Python

Hey Guys,
In this python tutorial, you are going to learn about keyword and especially about def keyword.
Here, we will provide you with an example program so that you can understand this concept easily.

Python def Keyword

Python keyword is a unique programming term intended to perform some action.

There are  33  keywords in Python, Each keyword is used to serve different purposes and together they build the vocabulary

Python keywords list

The list of Python keywords are mentioned  below

False               def                 if                  raise
None                del                 import              return
True                elif                in                  try
and                 else                is                  while
as                  except              lambda              with
assert              finally             nonlocal            yield
break               for                 not                 
class               from                or                  
continue            global              pass

Python is a dynamic language.

Now, we are going to see the def keyword in detail.

The def keyword is mainly used to create a function.

EXAMPLE PROGRAM




In the below program we have created multiple functions with def keyword. I hope this will be helpful to you to understand how to use def keyword in Python.

def add(x,y):

   
 """This function adds two numbers"""

    return x+y


def subtract(x,y):


    """This function subtracts two numbers"""


    return x- y

def multiply(x,y):


    """This function multiplies two numbers"""


    return x* y



def divide(x, y):


    """This function divides two numbers"""


    return x/ y



print("Select operation.")


print("1.Add")


print("2.Subtract")


print("3.Multiply")


print("4.Divide")


choice=input("Enter choice(1/2/3/4):")


num1=int(input("Enter the first number:"))

num2=int(input("Enter second number:"))


if choice=='1':


    print(num1,"+",num2,"=",add(num1,num2))


elif choice=='2':


    print(num1,"-",num2,"=",subtract(num1,num2))


elif choice=='3':


    print(num1,"*",num2,"=",multiply(num1,num2))

elif choice=='4':


    print(num1,"/",num2,"=",divide(num1,num2))


else:


    print("Invalid input")

OUTPUT:

Select operation.
1. Add
2.Subtract
3.Multiply
4.Divide
Enter choice(1/2/3/4):1
Enter the first number:20
Enter second number:30
20 + 30 = 50

You can also read:
Function argument in Python


Leave a Reply

Your email address will not be published. Required fields are marked *