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
]]>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:
And how I want it to end:
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.
Then I will open the file in writing mode to start modifying it, so first, I have to get rid of the even lines.
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!
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.
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
And now open your file…. and it has to look like this, you did it!
As you can see, it is very easy, you just write up what you want
]]>
Here’s the code in Atom:
Here’s the code in Cygwin:
Here’s the code in Github: https://github.com/diegoalatorre/TC1014/blob/master/wsq17
]]>
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.
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
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.
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.
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 –
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
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…
This means just one thing, everything is an object
That is why python does not runs when it finds an error
Stay stick to the status quo!, Just dont
1 + ‘1’ dont guess!
This gets to an unknown to me part of python, the protocols
Whuuuut?!
It’s better to fix the rpoblem ASAP because if not, debugging and functionality of further code could be negatibely affected
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
it all has an order
like local variables, nonlocal variables, the evil global variables, and the built in functions, it has a good hierarchy
]]>
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:
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
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:
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?
]]>Here’s the working program.
Here’s the source code and my GitHub repository.
]]>
Here’s the working program. It’s really long because I gave the user options.
Here’s the image before and after
Here’s the source code and my GitHub repository.
]]>Here’s the code in Atom:
Here’s the code in Cygwin:
Here’s the code in Github:
]]>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:
As always
]]>