• Linux Distribution
  • python3
  • A Shell Interface

My Setup:

  • Debian GNU/Linux and Arch Linux
  • python3
  • BASH

During multiple examples , I have shown an import keyword that imports a module into the code. Modules are separate files that have their own functions in them. There may come a time where you may want or need to clean your code and separate functionality into different files. For this, you can import and use the functions. Creating a module is simple. In the next example, I will create a file called, and add function definitions to it:

def add(x, y):
    return x + y

def sub(x, y):
    return x - y

def mult(x, y):
    return x * y

def divide(x, y):
    if y == 0 :
        return "N/A"
        return x/y

Next I will create another file called

import operate

firstInt = int(input("Insert the first number: "))
secondInt = int(input("Insert the second number: "))

#use the module name, the dot operator, and then the function name
print("Adding the two : " , operate.add(firstInt,secondInt))

Finally , I will call it within the interpreter:

clim@debian:~/Desktop/tests/pytests/modules$ python3
Insert the first number: 15
Insert the second number: 35
Adding the two :  50

You can also use an alias for your module by doing the following:

import operate as op

#to use:

You can also directly import and use without needing the module name by importing the function or variable specifically.

from operate import add

#use as normal

Python provides several modules that we will be going over in the future. You can also find a list at:

Python Project : Temperature converter

In my other tutorial series, i create a temperature converter to show the logical steps to create a program. To start off, let us define some parameters. The program will take in either a -c or -f to determine the temperature you would want to convert it to. For example, if you want to convert a Fahrenheit temperature to Celsius, you would use the -c flag. For example :

[clim@archnet:~/Desktop/cspcsite/PythonTutorials/examples/3]$ python -c TEMP_IN_FAHRENHEIT

In order to accomplish this, we will include the sys module. This will allow us to parse arguments that are passed in from the command line (the -c and temperature). There are multiple ways this program can be accomplished. For example, I could just write it all in one block and execute the code, but I want the code to look a bit more organized, so I am going to create 2 functions that will handle the conversions for each temperature type, and then just print the variable that it returns depending on the type flag.

#import the sys module
import sys

#helper functions
def printUsage():
	print("Usage:\npython3 [FLAG] [TEMP]")
	print("\t\t-c : convert to Celsius")
	print("\t\t-f : convert to Fahrenheit")

def toCelsius(fahrenheit):
	celsius = (fahrenheit - 32) * 5/9
	return celsius

def toFahrenheit(celsius):
	fahrenheit = (celsius * 9/5) + 32
	return fahrenheit

#main function

#sys.argv[0] is the name of the python script
#we will use sys.argv[1] and sys.argv[2]

if len(sys.argv) < 2:

	if conversion == '-c':
		print(" {} Fahrenheit is {} Celsius".format(temp,toCelsius(temp)))
	elif conversion == '-f':
		print(" {} Celsius is {} Fahrenheit".format(temp,toFahrenheit(temp)))
[clim@archnet:~/Desktop/cspcsite/PythonTutorials/examples/6]$ python3
python3 [FLAG] [TEMP]
		-c : convert to Celsius
		-f : convert to Fahrenheit
[clim@archnet:~/Desktop/cspcsite/PythonTutorials/examples/6]$ python3 -c 100
 100.0 Fahrenheit is 37.77777777777778 Celsius
[clim@archnet:~/Desktop/cspcsite/PythonTutorials/examples/6]$ python3 -f 37.77777777777
 37.77777777777 Celsius is 99.99999999998599 Fahrenheit

Leave a Reply