Author Archives: Manuel Lepe

Using Python in Linux

Here is a quick tutorial for those of you who would like to know how to  create Python files and run them in Linux. This is much a like the post about creating and running Python files in OS X. 

This time we are using Gedit and the Terminal to code and execute our files. Hope you find it useful.

This is my of my

Install Fedora in a Macbook Pro

This time I made a video tutorial for installing Linux distro Fedora 21 in a Macbook Pro. I recorded the tutorial using a virtual machine so it would be easier to record. 

Personal Tip: never ever try to triple boot a Macbook Pro, especially if you have OS X Yosemite. You will end up formatting your computer like never before, losing tons of time and end up very frustrated. 

Well here is the link to the tutorial. I hope you find it useful, and check the aditional info for more details.

This is my of my

Submit Work via Blog RSS and Github

In order to hand in our Masteries and WSQ we had to use our Blog RSS, if you want to know more about RSS I have found this page, which describe it very clearly. 

Any way the main thing about RSS, or Realy Simple Syndication, is that you can suscribe to a certain blogg and get updates everytime the site changes. So you will get in your RSS reader a new notification each time the owner of the site makes an update of it. 

Once someone is suscribed to your site you can search for especifique tags on the posts like the ones we have been using like –.

The other thign that its very important is to use Github. Github is a very cool platform that allows us to have many versions of the different projects and their source codes. 

The first thing to do is to create a repository, which is like a project, in wich we create branches, which are our different source codes. Inside our branches we can do commits, commits are the ‘versions’ of our source code that we can update. And the very beauty of this commits is that you make different versions without overwritting the different versions. So if you wreck your code you can go back to the past and have that working version up again!

My teacher Ken Bauer made a very usefull video that helped me to get a graps on Github and my computer. You can view it here.

I hope it helps you. 

This is my of my

Run Python inside Eclipse

I have uploaded a video Tutorial for creating and running Python files from inside Eclipse. Click here to see the video.

Look in the information of the video for the URL and the packages involved. 

This is my of my

Four Weeks Left (Kind of)

Whith the end of the semester almost upon ourselves, we should try to make a schedule in order to try, yes to try, to avoid those 4 last days whithout sleeping comming to Tec for more than 24 hours sometimes. In order to organize myself I used a Gantt Chart. 

Gantt Charts are a very useful and very used scheduling tool. It help us track what are do we need to do, in which order, and how much have we completed from it. It is mainly a graphical representation of the units of time a task will need in order to be completed. You can learn a little more about Gantt charts Here.

Once we have selected our tool we proceed to actually use it. I used SmartSheet, from which you can get a 30 day free trial. It is very easy to use and it helps you avoid having to format before starting to type your tasks. Another way you can create a Gantt Chart is whith a Microsoft Excel Template, that comes integrated with it, if you have it. 

My last weeks of TC1014

This is the schedule I planned for myself. I defined the WSQ and Quizzes in a weekly way because I’m not sure when thery are going to be assigned. 

For the Masteries, I resolved in first doing the ones that I haven’t done, and then to complete the ones that are incomplete. 

Well this is the way I’m supposed to work (given that there are no unsuspected events), in order to complete everything that I need for my TC1014 Python Course.

This is my of my

Estimating e

For this program we want to estimate Euler’s number, giving the user the control of how many decimals are to be exact. 

The source code of the program is here in GitHub

What it does is simply ask the user for the number of decimals as an argument to the function calculate_e.

The function calculate_e solves the formula for the infinte series of the euler’s number. For this another function of factorial is defined for the part of the denominator.

Originally the for cycle would repeat the precision times; but after some runs I started seeing that you had three numbers less than the one we asked, that’s why we have that +4 in the range of the for loop.

Then the function just does the summatory of the values that it has calculated in order to estimate the value of e.

There is note in the GitHub commit, where I explain that I have found that some times it appears that it has calcualted more decimals than you had asked, and I have found that there are some values that may appear so I state there that the number typed by the user are the numbers that are sure to be exact to the Euler’s number.

This is my of my  

Nesting of Conditional Statements

When we have nested conditionals we should look for a way to simplify them, remember the Zen of Python. So whenever you find yourself inside a horrible pyramid of five nested ifs, consider taking a look if you can simplify it, in the case you don’t well at least remember that 

Sparse is better than dense

So you may think how in the hell can you avoid having multiple nested ifs? Well the answer is boolean algebra.

Most of the time you compress several nested conditionals with just OR and ANDs operators. In Python the operators are simply the same words. Let’s take a look at that:

>>> if n>0:

         if m>0:

              print n+m

This is maybe a very simple example of what that means, but here we have two conditionals nested, that what they do is that if the number is positive and greater than zero then you print the sum of the numbers. 

Well this can be easily converted to:

>>>if n>0 and m>0:

       print n+m

And the result would be the same, cause instead of doing to steps of conditionals the conditional is checking the status of the logical operation of the “and”; wich will only be True when both conditions are met. So this way our code can be read  much more easily an it’s much more elegant.

This is my of my

Creating and using Tuples in Python 3

Tuples are much like lists in Python, for the little exception that they are inmutable, this means, once created you can’t make changes to it.

In order to create a Tuple you type the elements inside parenthesis ( … ) sepparated by a coma, just like lists.

>>>tuple=( 4, 5 , 7)

Hint: When you want to make a one element Tuple you should always put a coma after the element, this way Python won’t think that you only have an extra pair of parenthesis.

>>>tuple_1=(1,)

Since Tuples are inmutable, the methods used to modify them doesn’t exist; methods like append, remove, extend, etc. are no available for Tuples.

You can slice Tuples in order to create a new one that is a section of the one it is generated from. You can make a new Tuple from the slice of one like this:

>>>new_tuple=tuple[1:]

[ 5, 7]

And you can check if a value exists inside a Tuple the same way you do in Lists. 

  • in Tuple

In this way we can check if a certain value exists inside a Tuple, if it exists it will return True, if it doesn’t it returns False

>>> 5 in tuple

True

>>> 9 in tuple

False

  • tuple.index()

This way you can know the index of a certain value inside a Tuple, remember that this returns the index of the first apperance of the value. 

>>>tuple.index(5)

1

Booleans of Tuples

As in Lists, Tuples have the same booleans. Whenever a Tuple has at least one element, whatever the element may contain, it will return True to a boolean expresion. 

Asigning multiple values

One thing about Tuples that I tought was very interesting is the capacity to asign multiple values in one line. Her’es a little example:

>>>tuple=(5,4,6)

>>>(x, y, z)  = tuple

>>> x

5

>>> y

4

>>> z

6

This is my of my

Creating and using Lists in Python

This post is to get a little more technical and get a deeper look into the usage of Lists in Python 3.

The usage of lists is very simlpe in Python3, they are much like arrays in other lenguages except for the little detail that that lists can contain any type of element inside one list. This means you can have numbers and words in the same list without a problem. 

In order to create a list we only need to type the elements of the lists inside brackets, and separated by comas:

A_list=["a",1,"greetings",5.6989]

Lists are zero based, that means that the first element of the array has the index 0, and the last one has n-1, where n is the total number of elements inside the list. 

A_list[0]

a

A_list[3]

5.6989

Also you can use negative indexes to start counting from the right side of the list, where the index is counted as [len(A_list)-n], so to acces the last element we use a [-1]

A_list[-1]

5.6989

Another useful thing about lists is that you can slice it, this means create from a part of the list a new list. This is done by using colons in the index brackets [ : ], where the first number is the index where you want to start the slice inclusive, and the other is the end exclusive. for example fi we wanted just the half of the list:

A_list[3:4]

Or when you don’t have an input it will automatically asume that are all the elements left, whenever it is after or before, of the list.

Adding elements

The next thing we can do with lists is adding elements, this can be done in several ways, an everyone has it’s unique way to add elements:

  • list.append( )

This one is used to add an element as the final element of the list. This method adds whatever you have inside the parenthesis as a new element, so you have to be carefull because if you can end up having a list inside a list.

>>>list.append( [3,4] )

[1,3,5,6,[3,4]]

As you can see the last element of the list is a list itself.

  • list.extend( )

This method is what is used to merge two lists into one, so if in the last example we used extend that would look different:

>>>list.extend( [3,4] )

[1,3,5,6,3,4]

  • list.insert( , )

The usage of the insert method is by having to arguments, the index where we want to insert the element, and the element that we want to insert. So the difference with the last two methods is that you can actually place the element in the place that you desire.

>>>list.insert(0,Y)

[Y,1,3,5,6]

Searching for elements

Another thing that you might do with lists, is to know how many times a value repeats itself inside a list. This is done with the count method. For example:

list=[3,3,4,"c","great","zen"]

>>>list.count(3)

2

But what about knowing if a certain value is on a list? Well you can just type :

>>> "c" in list

True

>>> "also" in list

False

Quite easy right? Well so far we can know if there is a value and how many times it appears in the list, but about knowing its position? Well that what the index method comes to the rescue. If we want to know the index of certain element we type:

>>>list.index(3)

0

>>>list.index("also")

Exception!

As you can see the index returns the first time a value appears in a list, and if we look for the index a value we better prepare to take care of an exception, so using the “in” operator before doing an index search woul be quite wise. 

Removing items

First of all we must know that lists in Python never have gaps, that’s right whenever we remover an element of the list the gap is filled by the next element so there are no blank spaces between them. There are two ways of removing and item:

  • del list[ ]

This way we eliminate the element of the list that is in that especified index, so if we wanted to erease the first element of a list we would type:

>>> del list[0]

[3,4,"c","great","zen"]

  • list.remove()

The other way to remove an element from a list is by looking for a given value, this is very useful when you don’t know where the element is located. For example:

>>>list.remove(3)

[4,"c","great","zen"]

There is another method to remove items from a list wich is very similar to the remove method, but this one has the extra that you can store the value you have removed. This is the pop method.

>>>list.pop("great")

great

>>>list

[3,3,4,"c","zen"]

As you can see the argument of the pop is shown.

Booleans of lists

The booleans of lists are very easy to understand, if you use a list in a boolean context you will get True whenever the list has something inside it; as long as the list isn’t empty, whetever it might contain you will get  True. And if the list is empty you get False, quite easy.

Here is a page I found very useful, it explains with many examples the different types in Python. 

This is my of my

 

The Zen of Python

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than right now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!

—Tim Peters

This series of apparently senseless phrases are what Tim Peters considered the Zen of Python. For me it was quite hard to understand at the beggining, but then I found this page which helped me a lot to understand what this Zen was about. 

They depict in weird poetic way the customs that one should follow when coding, in this case in Python but most of them apply to any programming language, so that your code is not only efficient, but also easy to read and understand by even other people beside you. 

And well the page that I have posted above gives some good examples to the phrases that are stated in the Zen of python. But if you don’t want to look to an example of every line well I will give you a summary. 

When we say that beautifull is better than ugly is a sugestion as to ident the program, no more horrible one long lines that means too much to understand them. Whenever we do, call or whatever something we should show clearly what we have done, so there are the less amount of guesses about our code there can be.

Whenever you have to choose between a bunch of easy code that just screws people’s minds you better choose some complicated but yet fewer functions or chunks of code that can help one follow the thread of your code. And of course avoid those long chains of nested loops or conditionals, they just scream: skip me!!

The other thing that we have to take care of is about making our code too complex just for especial cases, many times we can just point at them by treating errors instead of making our code very difficult to comprehend; trying to not sacrifice too much of its functionality in the process. 

We should always try to solve the things the simpliest way we can, a way to figure out if we are doing it right is trying to explain what we have done, if we can’t; then we have a problem. 

And for last I don’t know if I have got it right, but I guess it’s abaout using variables to store the values of large statements, and use appropiate names for them, not names that don’t have much sense or even you can’t remmember.

This is my of my