DEV Community

varatharajan
varatharajan

Posted on

Day 2 in Python

Function :
A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function.

There are two types of function in python.
Built-in library function (pre-defined function)
User-defined function

*Built-in library function: *
There are Standard functions in Python that are available to use.

for Example :
input()
print()

input() function :
The input() function allows user input.
for example :
x = input("Enter your name : ")

print() function
The print() function prints the specified message to the screen, or other standard output device.
for example
print("welcome to our class")

User-defined function:
We can create our own functions based on our requirements.

We can define a function in Python, using the def keyword. We can add any type of functionalities and properties to it as we require.

for example

A = int(input("Enter your A value : "))
B = int(input("Enter your B value : "))
C = input("Enter your operator : ")

def calculate(A,B,C):
  if C == '+':
    print("Addition--->",A+B)
  if C == '-':
    print("subtraction---->",A-B)

calculate(A,B,C)

Enter fullscreen mode Exit fullscreen mode

*Result *
Enter your A value10
Enter your B value20
Enter your operator+
Addition--->30
Enter your A value20
Enter your B value10
Enter your operator-
subtraction---->10

Top comments (0)