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
Jose Carlos Peñuelas Armenta’s Articles at TC101 Fall 2015
Introduction to Programming Python and C++

WSQ16 – Cars

Hi, this is my last WSQ, i´m not doing because i´m lazy. This WSQ was hard for me because i had no idea how to read a file properly by lines. Frida´s blog helped me a lot to figure this out, check it on this link: http://tc1014.blogspot.mx/2015/11/cars.html

The link to my code on Github is this: https://github.com/Jocapear/TC1014/blob/master/WSQ16.py

WSQ15 – Images

I won´t do this WSQ because i would have to uninstall python and install it in 32 bits to be able to download the library that allows me to manipulate images.
The link to the library site is this:
http://docs.python-guide.org/en/latest/scenarios/imaging/

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)

Mastery30 – Reading and writing files

Reading files:To read a file you should know this functions: open(“filename”,”r”) and filename.read()The first one is used to open a file, read it and pass that information to a variable ( if you want) or to simply work with it directly.The second one …

Mastery 5 – Linux sufficient for exams

To execute a file in Linux all you have to do is go to the terminal and write “python3” and then the location of the file. Be sure to write the name of the file and “.py” after it. To make the location shorter save the file in the main directory. An ex…

Mastery 22 – Types of repetition

There are 3 types of repetition:
-For loop
-While loop
-Recursion

The FOR loop is used when you know the number of repetitions. It can be used with ranges, list tuples or dictionaries(iterable objects).  It takes a value inside the object, work with it, and then do the same thing with the next value until the iterable object is finished.

The WHILE loop is used when you dont how the number of repetitons. It works with a condition and it will repeat until the condition is false.

Recursion is when you call a function inside the same function. It can be used in special cases where you need to take a value from the function itself, this job can be done with FOR or WHILE loops but it´s shorter to write a recursion.

WSQ14 – Estimating e

This WSQ is easy if you understand what you have to do.Here is the program working witha  precision of 35If you want to see my code i will post the Github link here:https://github.com/Jocapear/TC1014/blob/master/WSQ14.py

Mastery 8 – Python conventions

Here i will show you what are the python conventions.
First of all, in python the indentation does matter. Indentantion is the way that python recognize if a code block has ended. For example:
if (a < b):
counter + =1
print (counter)
This is wrong because the IF has nothing inside it, instead it will always add 1+ to the counter because is at the same level as the IF. This is not C++ or JS where you can tell the program when your if ends with {}. The right way to do it is this:
if ( a < b):
     counter += 1
print(counter)
Also here are some tips to give a standar python style to your code:
-Just do one statement per line
Please don´t do this:
 
def ejercicio3(diccionario): return ((“Homework promedio:”, sum(diccionario[“homework”])/len(diccionario[“homework” ])), (“Quizzes promedio:”, sum(diccionario[“quizzes”])/len(diccionario[“quizzes”])), (“Tests promedio:”, sum(diccionario[“tests”])/len(diccionario[“tests”])))
Yes, this is a python program that actually works. As you can see it is very hard to read and you will make others to have a bad time trying to understand what you want to do. Don´t be a smart a** and put order to your code so that everyone can understand it. 
-Keep spaces between variables if necessary, just to make it more readable
-Build your functions on the top part of the code
I think that this is clear, declare your functions first and call them below. Try to declare all the functions you can on the top part and write the main program after that, this will make your code easier to read because if someone wants to follow the executuion order of your code he/she doesn´t have to look for a specific function on all the code; otherwise, he/she will know where the functions are and you will save him/her time.
Keep your code simple
Here is an example of the last point:
a = input(“Write a number”))
a = int(a)
for i in range(0,50)
     if i/a % 0:
          counter = counter + 1
b = counter*2
c = b+18
print (c)
This code is really simple but it can be done in less words and space, the proper way to do it is:
a =  int (input(“Write a number” ))
for i in range( 0 , 50 )
     if (i/a) % 0:
          counter += 1
print(counter*2+18)

 

Masteries 24 and 27 – Creation and use of tuples and diccionaries

 A tuple is like a list and is used in the same way, the only difference is that a tuple cannot be modified or changed.A diccionarie is a colleccion of anything that can be labeled with any other thing. It is used as a list too but to use a slot i…

Quiz11

The first excercise was the same as a WSQ, i just had to change it a little bit. I got in trouble when i tried to make excercise 2 because i didn´t know how to work with files.
I researched a little bit on the book and then i played a little bit with what i found, to make the excercise i just had to combine my new knowledge with my list and string knowledge and that was all. At the end the excercise was very simple, i just had to know how to open a file.
Hint (thinks that you need to know to open a file):
open( “filename.txt”)     This will return a file value so put it on a variable
file = open( “filename.txt”)
file.read()        This read what is inside that file, returns whatever is written on it so i recommend you to put in on a variable too

If you want to see my code i will post it on Github, here is the link:
https://github.com/Jocapear/TC1014/blob/master/Quiz11.py

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).