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 gives 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:

  • To learn functions in Python, visit our article here.
  • To learn while loop in Python, visit our article here.
  • To learn decision-making (if-elif-else) in Python, visit our article here.

Example 1

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

Code

#____ProgrammerCodes.com____
def add(num1,num2):
    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("""
          1: Addition
          2: Subtraction
          3: Division
          4: Multiplication
          """)
    cal = int(input("Choose a number from the list above:"))
    if (cal == 1):
        print("Addition")
        add(num1,num2)
    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____
function in python example 1 part 1
function in python example 1 part 2

Code’s explanation

#____ProgrammerCodes.com____
def add(num1,num2):
    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("""
          1: Addition
          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):
    print("Addition")
    add(num1,num2)
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

          1: Addition
          2: Subtraction
          3: Division
          4: Multiplication
          
Choose a number from the list above:4
Multiplication
5 * 2 = 10
function in python example 1 result

Video

Example 2

The user adds credits in order to keep access to the member’s area every day.

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____
function in python example 2

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!
function in python example 2 result

Video

Related Posts

Leave a Reply

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