Warning: The magic method Slickr_Flickr_Plugin::__wakeup() must have public visibility in /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php on line 152
‘#Modules’ Articles at TC101 Fall 2015
Introduction to Programming Python and C++

Tag Archives: #Modules

Mastery14 – Creating Python modules

A custom module is just a python file with fiunctions in it. You can create a python file and put in the same location as the program where you are going to call it. Here is an example:
I will create a new file called mymodule.py
def banana(filename):   
    name = open(filename)
    content = name.read()
    list = content.split(” “)
    print (list)
    counter = 0
    for i in list:
        if i.lower() == “banana”:
            counter += 1
    print (counter)

def calculate_e(precision):
    counter = 1
    accumulative = 1
    variable = 1
    while True:
        accumulative += variable*(1/counter)
        variable = (1/counter)*variable
        new = accumulative + variable*(1/counter)
        if (new – accumulative) < precision:
            break
        counter += 1
    return accumulative

And now i´m going to call that file just like if it were a module. This file is called file.py and is located in the same directory as mymodule.py:
import mymodule.py
a = mymodule.banana(“text.txt”)  #You must have a file named “text” called “text”
b = mymodule.calculate_e(35)
print (a)
print(b)
2
2.71825 
As you can see in file.py i used the functions inside of mymodule.py by calling them as if they were a module(well, it is a module now)

Mastery 13 – Python modules

Here is a code i wrote that imports and use the math module:The math module is useful to do calculations, on this example i used math.sqrt() because i didnt know that x ** 2 == x^2Here is another code with another module, this time the statistics modul…

What should you work on?

Week #12 and more partial exams for you.

For this week's readings:
C++ (TC1017) should either be looking at support for your project, ImageMagick C++ libraries are a good start.
Python (TC1014) should be finishing chapter 11 (Dictionaries).