Lesson-9: Adding and Calling Python Modules

Adding and Calling Python Modules

Modules are structures that contain variables and functions that allow us to easily perform some functions. We can use it in our own python files by importing. In this course, we will create our own module and use this module in our own files. Make sure that the module and main program that we will create are in the same folder.

Let’s prepare our priorities module. Let this module be a module that performs 4 operations and contains 4 functions and 2 variables. These functions will be called sum, subtract, multiply and divide. our variables will be called msj and year. check out the codes below.

Account Module Codes:

msj="Welcome To The Account Module."
year=20201

def collection(s1,s2):
    toplama=s1+s2
    return toplama

def extraction(s1,s2):
    fark=s1-s2
    return fark

def multiplication(s1,s2):
    çarpım=s1*s2
    return çarpım

def division(s1,s2):
    bölüm=s1/s2
    return bölüm

We save these codes in our project with the py extension in the account name. Now it’s time to code our main program. By connecting to the account module from our main program, we will be able to use these variables and functions. Below are the codes of our main program.

Main Program Codes:

import accound
print (accound.year)
print(accound.msj)

sayı1=int(input("1. enter number: "))
sayı2=int(input("2. enter number: "))

a=accound.collection(sayı1,sayı2)
print("2 sayının toplamı=",a)

b=accound.extraction(sayı1,sayı2)
print("2 sayının farkı=",b)

c=accound.multiplication(sayı1,sayı2)
print("2 sayının çarpımı=",c)

d=accound.division(sayı1,sayı2)
print("2 sayının bölümü=",d)

An important point to note is that we import the priorities account module into our main program. with the import account command, we can now use the account module in our main program. Another point that we should pay attention to is that we do not use variables and functions with direct names. Example:

print (account.year) — > prints the year variable in the account module on the screen.
account.sum (number1,number2) — > calls the sum function in the account module.

An important issue is that if we look at the functions in the account module, it sends the transaction values to the main program using the return function. The Return function does not send a variable to the main program. Returns the value in that variable. This part is the crucial point of using the function. So in the main program:

a=accound.collection(sayı1,sayı2)
print(“2 sayının toplamı=”,a)

accound.collection(sayı1,sayı2) function call command to a variable. It’s because of the accound.collection(sayı1,sayı2) expression returns toplama and the data inside toplama variable. I synchronized this data to variable a and printed variable a on the screen.

You may also like...

Leave a Reply

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