# Examples Of Function In Python

In this article, we are going to provide you with simple examples of using a function in Python programming language.

A function is a concept in programming where you can use it to add your code inside it in order to use the block of code later in the next lines of the program or in another file as a module.

In these examples, we have combined Python function with other programming concepts such as if statement and while loop.

These concepts are the basic fundamentals of any programming language. They may seem different from one language to another in their syntax but they are all the same when we apply their logic in the programs.

Doing even a small example every now and then will be so helpful because it will train your programming logic and give you the confidence to tackle advanced examples and projects.

Before we begin, try to learn these Python concepts if you want to understand our examples:

## Example 1

Calculate two numbers using addition, subtraction, division, and multiplication.

### Code

```#____ProgrammerCodes.com____
print(num1, "+", num2, "=" ,num1+num2)
def sub(num1,num2):
print(num1, "-", num2, "=" ,num1-num2)
def div(num1,num2):
print(num1, "/", num2, "=" ,num1/num2)
def multi(num1,num2):
print(num1, "*", num2, "=" ,num1*num2)
while True:
num1 = int(input("First Number:"))
num2 = int(input("Second Number:"))
print("""
2: Subtraction
3: Division
4: Multiplication
""")
cal = int(input("Choose a number from the list above:"))
if (cal == 1):
elif (cal == 2):
print("Subtraction")
sub(num1,num2)
elif (cal == 3):
print("Division")
div(num1,num2)
elif (cal == 4):
print("Multiplication")
multi(num1,num2)
print("______________________________________________________________")
#____ProgrammerCodes.com____
```

### Code’s explanation

```#____ProgrammerCodes.com____
print(num1, "+", num2, "=" ,num1+num2)
def sub(num1,num2):
print(num1, "-", num2, "=" ,num1-num2)
def div(num1,num2):
print(num1, "/", num2, "=" ,num1/num2)
def multi(num1,num2):
print(num1, "*", num2, "=" ,num1*num2)
#____ProgrammerCodes.com____
```

In this part of the code, we defined four functions that will be responsible for the mathematical calculation process.

```#____ProgrammerCodes.com____
num1 = int(input("First Number:"))
num2 = int(input("Second Number:"))
#____ProgrammerCodes.com____
```

We asked the user to write two numbers.

```#____ProgrammerCodes.com____
print("""
2: Subtraction
3: Division
4: Multiplication
""")
cal = int(input("Choose a number from the list above:"))
#____ProgrammerCodes.com____
```

In these lines of the code, we asked the user to define what type of mathematical calculation.

```#____ProgrammerCodes.com____
if (cal == 1):
elif (cal == 2):
print("Subtraction")
sub(num1,num2)
elif (cal == 3):
print("Division")
div(num1,num2)
elif (cal == 4):
print("Multiplication")
multi(num1,num2)
#____ProgrammerCodes.com____
```

The program will check what type of mathematical calculations the user wants then it will call the function in order to do that calculation process.

While True will allow the user to start from the beginning to do other calculations.

### Result

``````First Number:5
Second Number:2

2: Subtraction
3: Division
4: Multiplication

Choose a number from the list above:4
Multiplication
5 * 2 = 10``````

## Example 2

### Code

```#____ProgrammerCodes.com____
credits = int(input("Add Credits To Access The Members Area:"))
day = 0
while credits > 0:
day = day + 1
print("Day" , day)
print("===============================")
print("Welcome To The Members Area!")
print("===============================")
credits = credits - 1
print("__________________________________________")
print("You Don't Have Enough Credits To Access Members Area!")
#____ProgrammerCodes.com____
```

### Code’s explanation

```#____ProgrammerCodes.com____
credits = int(input("Add Credits To Access The Members Area:"))
day = 0
#____ProgrammerCodes.com____
```

In this line of code, we asked the user to add credits to be able to use the software. As an example when the user deposits one dollar the program will count as one credit so it will be one day of full access to the software features.

```#____ProgrammerCodes.com____
while credits > 0:
day = day + 1
print("Day" , day)
print("===============================")
print("Welcome To The Members Area!")
print("===============================")
credits = credits - 1
print("__________________________________________")
#____ProgrammerCodes.com____
```

The program will keep the user as a member of the software as long as the user has enough credits. The program will decrease user credits every day until the user has not enough credit.

```#____ProgrammerCodes.com____
print("You Don't Have Enough Credits To Access Members Area!")
#____ProgrammerCodes.com____
```

When the loop is finished because there are no credits available the program will show that message.

### Result

``````Add Credits To Access The Members Area:3
Day 1
===============================
Welcome To The Members Area!
===============================
__________________________________________
Day 2
===============================
Welcome To The Members Area!
===============================
__________________________________________
Day 3
===============================
Welcome To The Members Area!
===============================
__________________________________________
You Don't Have Enough Credits To Access Members Area!``````