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
charliegdrummer’s 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/
Masteries 13 and 14: Modules https://kenscourses.com/tc101fall2015/2015/masteries-13-and-14-modules/ Thu, 26 Nov 2015 03:23:11 +0000 http://charliegdrummer.wordpress.com/?p=393 Continúa leyendo Masteries 13 and 14: Modules]]> Modules, what the heck are those things?

In python, modules are independent files that contain functions. These functions can be called in the file you are importing the modules. It is an easy way to keep functions outside your files, and it’s very useful when creating large code files.

To import a module into a file, guess what, you use import modulename, yeah, as simple as that.

From that file, you can use all the functions.

Here is

How to create a module

Since a module is justa file with only functions and statesments,so for your module you’d wish to have only those to make it easy.

In my module I am going to have two functions, one for fibonacci, that can be seen in this QUIZ, and one for getting the distance between two points, in this QUIZ.

So i just take the functions and store them in the file, just like this

deffibdefdist

I chose to name the module fibdist, just as there:

modulename

And that’s it, I have just created my own module named fibdist!

Import a module and use it

As I said, to import is just, well, import and the name of the module WITHOUT the extension, in my case will be import fibdist

modulename

My module is already set in my file, so now I want to create a proggram that asks the user if he wants to do a fibonacci test or to get the distance between two points,

choice

Then, I can do all my coding, if it is 1 is fibonacci, is 2 is distance and so on

fibcalleddistancecalled

 

So here is the important part, in order to use the functions see right there, in the lines 8 and 18, I am using the function from the modules

To do that, the correct format is

modulename.functionname(parameters)

use

The tricky part here is that you need to know what the name and the parameters of the function are in order to use them correctly.

 

As you can see, it is really easy, and with that you can use those functions anytime you need to, that’s why modules are awesomly powerfully great!

As always

Github code: Module

Github code: file

richard_dramatic

 

 

]]>
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/
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/
WSQ14: e number https://kenscourses.com/tc101fall2015/2015/wsq14-e-number/ Fri, 13 Nov 2015 00:16:12 +0000 http://charliegdrummer.wordpress.com/?p=194 Continúa leyendo WSQ14: e number]]> The natural number represented by the letter e; make it posible to calculate, at leats with an approximation desired; that is what the WSQ14 asks me to do.

So what I have to do is to make a program that calculates the value of e given an aproximation that the user inputs.

The value of e is define by the limit of the following formula when n is reaching the infinity

(1 + 1/n)^n

Because the formula is defined and is already tested, to do the code is pretty easy.

Just had to put the formula, keep aumenting the value of n one by one, and I will stop untill I met the estimation the user wanted.

That last part is achieved by comparing the absolute diference between the previos result and the current one with the estimation, if it is bigger, then I will have to keep doing the calculations.

Here is that part

Capture

So we start limit and oldlimit in a way that the diference will be greater than one
Then we equal the oldlimit to the limit in order to save the value, and asing to limit the value of the applicated formula in the terms of n, we add n 1, and keep doing it until we get to the estimation

So the coding was really easy, only because of Euler

As always:

Github code

]]>
https://creativecommons.org/licenses/by/4.0/
WSQ13: Those Babylonians are crazy https://kenscourses.com/tc101fall2015/2015/wsq13-those-babylonians-are-crazy/ Thu, 12 Nov 2015 22:59:57 +0000 http://charliegdrummer.wordpress.com/?p=191 Continúa leyendo WSQ13: Those Babylonians are crazy]]> The babylonian method to get the sqare root of any number, yes, the one that takes an estimation, usually dividing the number by two, and then it takes the original number and add it to the estimation, then the result is divided by two, and there you got an iteration; the babylonian method is to do this iteration till get the sqare root

On code, that can be represented as a formula:

x = (x + number/x)/2.0

number is the number from we want to get the swareroot
x starts as the number divided by two
At the end, x will be the squareroot of the desired number

To make the computer repeat the mothod untill get the desired number, I used a while loop where te condition was:

x*x number> 0.00000000001

That means that the program will run the formule while the diference between the initial number and the current result is greater that that little number. NOTE, you have to take the difference as an absolute value

It was an easy code, because the babylonian did all the math, really, those people were smarta…

As always:

Github code

]]>
https://creativecommons.org/licenses/by/4.0/
Mastery 3, 4: https://kenscourses.com/tc101fall2015/2015/mastery-3-4-2/ Thu, 29 Oct 2015 05:33:26 +0000 http://charliegdrummer.wordpress.com/?p=167 Continúa leyendo Mastery 3, 4:]]> In this post you will learn how to create an account for GitHub, Twitter, and WordPress; and how to upload your work there.

BLOG

For the blog I use wordpress; it is really easy to create a blog. As soon as you get to the wordpress page, it displays an option to create a web page, click it. Then it will ask you to choose a theme, you can skip this if you want, and pay if you want.

blog sing upblog sing up 1

Then you can choose pay or not for your own domain and member plan, and then, you can choose your username, password, and you will type your e-mail

blog sing up 3 blog sing up 4

To create a blog entry is easy; in the right- upper side, is a button with a little pencil, click it, it will redirect you to an user interface to create your blog. If you know html, you can edit your blog as it. The UI has a lot of buttons, the most important are the add multimedia, there you can add videos, images, docs… At your left, you had a field named Tags or Etiquetas, this will aid your viewers to see content they want; also for Ken Bauer. To finish your post or save it, click on save draft or guardar borrador, when you want to release it to the web, click on publish, or publicar.

blogwork 2

GITHUB

For GitHub you just need an username, an email, and a password, and that’s it jajaja 😀

PS You can choose to pay for more benefits

github sing upgithub sing up 2

To start uploading code to GitHub, you need first to create a repository; it is like a virtual folder where you upload your code. It is created when you click the green button in the right part of the home page

You will need a name, an optional description, to say if it is public or not, and a .gitignore (you can just put the type of file you will be uploading). You can optional add a readme or a licence

github workgithub work 1

Once you got your repository, you will click on the little plus in the repository you want

github work 2

There you can put all your code, I put a html code I did for a intro assignment; you need to give it the name and the ending, I.E. for python it’ll be .py

github work 4 github work 5

And that’s it 😀

Twitter

For Twitter you’ll need to type your name, e-mail, and password; then your phone number (you can skip this), and then your username and that’s it 😀

twitter sing uptwitter sing up 2 twitter sing up 3

To start posting tweets on tweeter, you just click on the little white box, and start typing! 😀

twitter work

Do not forget to add hashtags #, to categorize your tweets, and if you want another person to read it, you can add his account with an @. There I add @Ken_bauer 😀

]]>
https://creativecommons.org/licenses/by/4.0/
Mastery 1,2: IDLE Graphic User Interface Python https://kenscourses.com/tc101fall2015/2015/mastery-12-idle-graphic-user-interface-python/ Thu, 29 Oct 2015 04:40:23 +0000 http://charliegdrummer.wordpress.com/?p=149 Continúa leyendo Mastery 1,2: IDLE Graphic User Interface Python]]> Here is a post that explains how to create and tun a python file from the GUI of Python

The first thing you have to do is to run the GUI of python; when the screen shows up, you can create a new project by the command ctrl+n, or to click file new as shown on the pictures:

idle fileidle select new

A new screen will open, is a text processor, as notepad, and there you can start typing your code. When you are done, you can run it by pressing F5, or the run tab then Run Module as shown on the pictures   run runmodule

Everytime you want to run a file through GUI, it will request you to save the file; to save it just press ctrl+s or file save as in the picture:

saverunrunsave

When you save it, make sure you add     .py      at the end of the title of your file, otherwise it won’t start

PS   What you may want to avoid is to type your filename with spaces, “ñ“s, or characters that are not numbers, nor letters; a good replacer of the space is the character    _

And there you have it! it worked! 😀

wuwuwu

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