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
‘#TC1014’ Articles at TC101 Fall 2015 https://kenscourses.com/tc101fall2015 Introduction to Programming Python and C++ Thu, 26 Nov 2015 05:27:51 +0000 en hourly 1 https://creativecommons.org/licenses/by/4.0/ ECOS https://kenscourses.com/tc101fall2015/2015/ecos-14/ Thu, 26 Nov 2015 05:27:51 +0000 http://charliegdrummer.wordpress.com/?p=497 ]]> ecos

The ECOS are an important part of the campus because it is the time where the student grade our teachers, it’s like a change of roles, now we help the campus to improve on every aspect we see fit

]]>
https://creativecommons.org/licenses/by/4.0/
Mastery 30: Files https://kenscourses.com/tc101fall2015/2015/mastery-30-files/ Thu, 26 Nov 2015 05:18:52 +0000 http://charliegdrummer.wordpress.com/?p=444 Continúa leyendo Mastery 30: Files]]> In terms of files external files to python, such as dat, text, cvs… files, the command that you use to open them is……………….. OPEN!!!!

open(“filename.extension”,”r or w”)

You put r if you are going to read the file

You put w if you want to write the file

There are others parameter you can type there, but those are the basics

So let’s start, I am going to use the 93car.dat.tx the Journal of Statistics Education gives us.

What I want to do, is to delete all those strange number to only left the brand of the car and rthe model

See the original file:

original fiel

And how I want it to end:

desired file

So let’s start

The first thing to do is to open the file in reading mode (that means r at the end of the parenthesis)  and to store aaaaalll the data inside a variable inside python, I called it text, after that close it.

open in read

Then I will open the file in writing mode to start modifying it, so first, I have to get rid of the even lines.

open in write

One way te start reading and changing the lines is with a for loop

for (any variable, I chose line) in (where you stored all the data, in my case is text)

for line in text:

That is the main loop, and since I want to get rid of the even lines, I am going to write the lines only if is even BEACUSE we count the first iteration as one, see that x?

after doing that, close it!

open your file, and you are going to see….. YES!

middle.PNG

You have just modified your first file from python!

Now, I don like all those number, so I want to errase them, I know all these numbers start on the position number 30 en every line, so starting over and doing the same process EXCEPT what is inside the for.

start over

You see, what we are going to do inside the for loop is to write each line, but cutted; you see that [0:29]? that means that it is only going to write from the first space to the 29th of each line

just cmodesadas

And now open your file…. and it has to look like this, you did it!

 

asdfas

As you can see, it is very easy, you just write up what you want

 

Github link

 

b1e0b19965f87e7b83b66070e394bb33

]]>
https://creativecommons.org/licenses/by/4.0/
#WSQ17-The Movies https://kenscourses.com/tc101fall2015/2015/wsq17-the-movies/ Thu, 26 Nov 2015 04:21:43 +0000 http://diegotc101.wordpress.com/?p=206 ]]> This WSQ is the toughest one, the most difficult part about it was, as always, the syntax that python uses for dictionaries. I used a video for Lynda and a video in youtube (from the same user I have already post) for doing this WSQ.

Here’s the code in Atom:

wsq17.1

Here’s the code in Cygwin:

wsq17.2

Here’s the code in Github: https://github.com/diegoalatorre/TC1014/blob/master/wsq17

 

]]>
https://creativecommons.org/licenses/by/4.0/
Mastery08: The Zen of Python https://kenscourses.com/tc101fall2015/2015/mastery08-the-zen-of-python/ Thu, 26 Nov 2015 01:14:28 +0000 http://charliegdrummer.wordpress.com/?p=284 Continúa leyendo Mastery08: The Zen of Python]]> The zen of python is a set of 19 principles that rule the design of the programming language python

1.- Beautiful is better than ugly

This basically means the fact that python is, unlike others, a language restricted by identations and arrangements; in python, identation and order really counts. That’s one of the main reasons of why python is the first programming language that is taught.

Is not the same to write

if (x in y):
s = all

Than to write

if (x in y):
    s = all

The correct way is the second one.

 

2.- Explicit is better than implicit

In a contrast to another programming language that is implicit, JavaScript

in JS, if you type the following line

“55” + 5

JS will IMPLICITLLY take the five as string

On the other hand, in Python that line would result in an error, you have to specify first if both are srtrings or numbers

3.- Simple is better than complex

This can be explained with the cocept of debugging, of fixing your program; If you use a lot of global variables, nesting conditional and other complex methods, you may get to you goal, but if not, seeing and fixing why you didn’t reach that goal is going to be a pain in the feelings.

4.- Complex is better than complicated

To example this concept, we can use the loops for and while, check out this code:

counter = 0
while (counter < 10):
      x = counter/3
      counter = counter + 1

You see, this code is not complex, it is easy to get what it does, but it is too complicated, there are too many steps there; this code can be simplified with a for loop

for e in range(0,11):
     x = e/3

So both codes do the same work, the diference is that one is easier to explain.

5.- Flat is better than nested

We can clear that with the conditionals. Each conditional has two paths, if the condition is met, or not. Those are the two only posibilities; now, i f you start nesting and nesting, the posibilities will grow as the powers of two, and we know it grows fast, in a four-level you will have sixteen posible paths, so things got hard. Imagine you have to debug –

 

6.- Sparse is better than dense

This is just a way of writting, you see, is really easier to read a code when it is well spaced and cute, rather than if it looks like the 380 bus at 7am. personally, I think that this principle has to be in every programming language because it makes it easier to read and manipulate the file itself

 

 

7.- Readibility counts

Just as the last one, it has more benefit to have a well ordene code rather than a 380 bus at 7am; it is easier to detect where have you made a mistake and how to correct it…

 

8.- Special cases are not special enough to break the rules

This means just one thing, everything is an object

 

9.- Altough practicality beats purity

 

10.- Errors should never pass silently

That is why python does not runs when it finds an error

 

11.- Unless explicitly silenced

 

12.- In the face of ambiguity refuse the temptation to guess

Stay stick to the status quo!,  Just dont

1 + ‘1’ dont guess!

 

13.- There should be one obvious way to do it

This gets to an unknown to me part of python, the protocols

 

14.- Altough it may not be that obvious unless you are a dutch

Whuuuut?! 🙁

15.- Now is better tha never

It’s better to fix the rpoblem ASAP because if not, debugging and functionality of further code could be negatibely affected

 

16.–Altough never is often better than now

Sometimes the problems won’t be resolved or would last a lot of time that is better to skipp them

 

17.- If the implementation it’s hard to explain, it’s a bad idea

As it’s says, if it is too complicated to explain, it is too complicated to understand, tus it may be a bad idea.

 

18.- If the implementation it’s easy to explain, it may be a good idea

Well, if it can be read fluently, and explained easylly, it can be a good idea, it MAY be, so don’t assume that if it can be read then it’s ok

 

19.- Namespaces are  one hoonking great idea … let’s do more of those

it all has an order

like local variables, nonlocal variables, the evil global variables, and the built in functions, it has a good hierarchy

 

zen_stones_by_3dbasti

]]>
https://creativecommons.org/licenses/by/4.0/
Masteries 29,9: Validate what the user inputs https://kenscourses.com/tc101fall2015/2015/masteries-299-validate-what-the-user-inputs/ Wed, 25 Nov 2015 15:43:59 +0000 http://charliegdrummer.wordpress.com/?p=240 Continúa leyendo Masteries 29,9: Validate what the user inputs]]> Here I will show show an easy way to validate what the user inputs specially what type of data is typing.

That means that you can restrict the user to type something no longer than 10 characters, or to type only an integer or a float. Here I going to show you those three cases (something no longer than ten characters, an integer, and a float).

 

So let’s start with the easiest one; to restrict the number of character the user can type, it is easy to use the length command.

len(variable)

it will return the number of characters in case of a string, the number of elements it has in case of a list.

REMEMBER, in order to do this, the variablke must be either a string or a list, otherwise it won’t work

With a loop with the condition of what the user is typing is longer than ten characters, you can r4estrict the interaction of the user and don’t let him go until he types what you want

So for the program we will ask a nickname no longer than ten characters, the loop after asking the nickname will be:

string

So it will loop untill the user types it correctly

 

Then, we will review a little harder one; to restrict the user to only type an integer; that means the input won’t have a “.” inside of it, and it can be converted to integer via python.

There exists a common way to validate that which is by exceptions in a loop. With a loop that it’s only condition is to be true, you will use the command try, to, well, try to do something, in this case, to convert the input of the user to integer via python, variable = int(variable); if it results like an error, represented in python as ValueError, then it will ask again for the number. This loop is to make sure the user didn’t input letters, just numbers.

After doing that, is necessary to verify if the input has a decimal point”.”, in that case, it will ask again for the number

So it will look like this

integer

 

The last one, the float, its a little more ease, it just have to work if you try to convert it to float; you will use the same structure, but you will try to convert it to float instead to int variable = float(variable)

It will result like this:

float

Here you saw not just how to validate, but learned the basic types of data

The strings, that are just characters

The integers, that are just numbers, not fractions, not decimals

The floats, that are numbers with decimal points

 

It is easy isn’t it?

Eazy-E.jpg

]]>
https://creativecommons.org/licenses/by/4.0/
WSQ 16 – Cars https://kenscourses.com/tc101fall2015/2015/wsq-16-cars-2/ Wed, 25 Nov 2015 14:54:12 +0000 http://davidg2897.wordpress.com/?p=363 ]]> For this WSQ I used the book to know how to read files.

Here’s the working program.

Capture

Here’s the source code and my GitHub repository.

 

]]>
https://creativecommons.org/licenses/by/4.0/
WSQ15 – Images https://kenscourses.com/tc101fall2015/2015/wsq15-images-4/ Wed, 25 Nov 2015 14:44:05 +0000 http://davidg2897.wordpress.com/?p=342 ]]> For this WSQ I used the resources Ken gave us in the TC1014 page.

Here’s the working program. It’s really long because I gave the user options.

Capture

Here’s the image before and after

image
Before
image2
After

Here’s the source code and my GitHub repository.

]]>
https://creativecommons.org/licenses/by/4.0/
Bonus Quiz https://kenscourses.com/tc101fall2015/2015/bonus-quiz-18/ Wed, 25 Nov 2015 06:41:36 +0000 http://diegotc101.wordpress.com/?p=181

]]>
https://creativecommons.org/licenses/by/4.0/
#WSQ-Cars https://kenscourses.com/tc101fall2015/2015/wsq-cars/ Wed, 25 Nov 2015 00:40:20 +0000 http://diegotc101.wordpress.com/?p=169 ]]> Doing this WSQ was more or less easy for me, the difficult part for me was to know the condition of  the If. I used help from the lynda webpage the course python 3 essentials.

Here’s the code in Atom:

wsq16.1

Here’s the code in Cygwin:

wsq16.2

Here’s the code in Github:

https://github.com/diegoalatorre/TC1014/blob/master/wsq16

]]>
https://creativecommons.org/licenses/by/4.0/
WSQ16: Opening files and reading them https://kenscourses.com/tc101fall2015/2015/wsq16-opening-files-and-reading-them/ Tue, 24 Nov 2015 18:01:33 +0000 http://charliegdrummer.wordpress.com/?p=198 Continúa leyendo WSQ16: Opening files and reading them]]> The WSQ16 is about doing a program that reads a file abaut cars and it’s specifications. The file is read such as the first 14 characters are the model o f the car, the character from 53 to 54, the miles per gallon in city, and so on.

So it asks you to give the user the average gas mileage in city (City MPG), average gas mileage on highway (Highway MPG), and the average midrange price of the vehicles in the set.

To accomplish that, I searched for a way to import text files to python and how to read it.

I learned that you can open the file, the easy way,  with the command open(“nameofthefile.extension”)…. yeah, that simple.

Then, the hard part goes on, read the file

Let’s say you code this

file = openfile(“filename.ext”)

The token file has the file in it.

The file can be read line by line with the command file.readline()
The file can be read entirely and set as a list with the command file.read()

Through websurfing I learned another way; you can read text lines with for loops; you just have to tell the computer for each line inside this variable do:

And the result of hte coding is:

for line in file

After discoveringt that, the coding was so easier.

It all abaout substrings; I already know what spaces are what thing, so I just substring with line[start:end] so its easy then, just have to assing a variable like carmodel or cmpg and print it continiously inside the loop.

The last problem was that I didn’t need the even lines, so with an if and the condition a counter is even, I skipped every even line.

Here is the result:

Capture

As always

Github Link

 

7

 

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