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

Warning: Cannot modify header information - headers already sent by (output started at /home/kenbauer/public_kenscourses/tc101fall2015/wp-content/plugins/slickr-flickr/classes/class-plugin.php:152) in /home/kenbauer/public_kenscourses/tc101fall2015/wp-includes/feed-rss2.php on line 8
Jose Carlos Peñuelas Armenta’s Articles at TC101 Fall 2015 https://kenscourses.com/tc101fall2015 Introduction to Programming Python and C++ Wed, 25 Nov 2015 13:24:33 +0000 en hourly 1 https://creativecommons.org/licenses/by/4.0/ WSQ16 – Cars https://kenscourses.com/tc101fall2015/2015/wsq16-cars-4/ Wed, 25 Nov 2015 13:24:00 +0000 http://kenscourses.com/tc101fall2015/?guid=434f4e343dfc2e9ed0297aadfb20a999 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]]>
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

]]>
https://creativecommons.org/licenses/by/4.0/
WSQ15 – Images https://kenscourses.com/tc101fall2015/2015/wsq15-images-2/ Tue, 24 Nov 2015 15:12:00 +0000 http://kenscourses.com/tc101fall2015/?guid=fc6b460d7b4ef2e0d24d484e99f40677 The link to the library site is this:
http://docs.python-guide.org/en/latest/scenarios/imaging/
]]>
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/

]]>
https://creativecommons.org/licenses/by/4.0/
Mastery14 – Creating Python modules https://kenscourses.com/tc101fall2015/2015/mastery14-creating-python-modules/ Tue, 24 Nov 2015 15:07:00 +0000 http://kenscourses.com/tc101fall2015/?guid=0a2e8871ef82adcd4c17b306cfbbdc37 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)]]>
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)

]]>
https://creativecommons.org/licenses/by/4.0/
Mastery30 – Reading and writing files https://kenscourses.com/tc101fall2015/2015/mastery30-reading-and-writing-files/ Tue, 24 Nov 2015 14:40:00 +0000 http://kenscourses.com/tc101fall2015/?guid=798ccaeca8b36e1d9e89443e9291c50f 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 will just read a file, to use this function ou should have used the open function first like this:
a = open(“filename”) 
print (a.read()) 
As you can see im not using the “r” because that means that i read it at the same time i open it, if you just want to open the file but not read it right there and use that information for later you should do something like what i did above. This has a lot of functionalities if you mix this with strings manipulation. You look for a specific word, count the lines, the total words etc. Here is an aplication for that, this documento opens and reads a file and counts how many times the word “banana” is in it (WSQ14):
def process_file(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)
process_file(“file.txt”)

To be sure that this file works correctly i must have a text file named “file” and be sure that each word written in it is separated by a space. If you noticed, reading a file is reading a file is really simple. Is just taking a value from a different file and assigning it to a varible, works like an input. What matters is how you control or process that information.

 Writing a file:
To write a file you have to use the same function as before but this time with a different parameter: open(“filename”,”w”)
This “w” thing will thell python that you want to edit/write the file, be careful because this will erase the content of that file and create a new file. To write a line use fout.write() and use as a parameter a string (or a variable with a string), when you are done use fout.close(). It is very important that you understand that the file will star writing in the last empty line. Here is an example:
 def process_file(filename):
    name = open(“filename”,”w”)

    a = “no”
    while a == “yes”:

       line = input(“Write here the new data”)
       fout.write(line)
       a = input(“want to close?”)
    fout.close() 
process_file(“file.txt”)

This function will add new strings to the “file.txt” document until the user decides to stop.

]]>
https://creativecommons.org/licenses/by/4.0/
Mastery 5 – Linux sufficient for exams https://kenscourses.com/tc101fall2015/2015/mastery-5-linux-sufficient-for-exams/ Tue, 24 Nov 2015 00:46:00 +0000 http://kenscourses.com/tc101fall2015/?guid=a0e121fb28b76e65f50fa8b256d0cb8d 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 example is this:
python3 filename.py
If you are having trouble try to star again but this time write first “lb”, this will take you to the main directory. Then you can continue writing as usual. 

]]>
https://creativecommons.org/licenses/by/4.0/
Mastery 22 – Types of repetition https://kenscourses.com/tc101fall2015/2015/mastery-22-types-of-repetition/ Mon, 23 Nov 2015 22:20:00 +0000 http://kenscourses.com/tc101fall2015/?guid=4c38d7e4786f14ce02fb6c4ba5f4025d -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.]]>
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.

]]>
https://creativecommons.org/licenses/by/4.0/
WSQ14 – Estimating e https://kenscourses.com/tc101fall2015/2015/wsq14-estimating-e-7/ Mon, 23 Nov 2015 13:09:00 +0000 http://kenscourses.com/tc101fall2015/?guid=af1388f3ff28e84838845f034954e84c This WSQ is easy if you understand what you have to do.
Here is the program working witha  precision of 35

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

]]>
https://creativecommons.org/licenses/by/4.0/
Mastery 8 – Python conventions https://kenscourses.com/tc101fall2015/2015/mastery-7-python-conventions/ Mon, 23 Nov 2015 00:00:00 +0000 http://kenscourses.com/tc101fall2015/?guid=1b4c20f20ec78922ebb7b868516f053b 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)


 

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

 

]]>
https://creativecommons.org/licenses/by/4.0/
Masteries 24 and 27 – Creation and use of tuples and diccionaries https://kenscourses.com/tc101fall2015/2015/masteries-23-and-26-creation-and-use-of-tuples-and-diccionaries/ Sun, 22 Nov 2015 14:29:00 +0000 http://kenscourses.com/tc101fall2015/?guid=80f48b8cd3ef5990dda76e0075fc591f  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 in the diccionarie you have to call the key instead of the value.
Here is some code where i show that i know how to use tuples and diccionaries:

]]>
https://creativecommons.org/licenses/by/4.0/
Quiz11 https://kenscourses.com/tc101fall2015/2015/quiz11/ Sat, 21 Nov 2015 00:33:00 +0000 http://kenscourses.com/tc101fall2015/?guid=1af6d2340044872e16b437cae2492927 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]]>
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

]]>
https://creativecommons.org/licenses/by/4.0/