La Boina Roja

Linux, the struggles are real!


2 Comments

Coursera starts a Python course on the 15th of April!

The course is called “An Introduction to Interactive Programming in Python”   and it’s free  photo thumbsup.gif  The course will cover Python 2.

Coursera is unfortunately one of those institutions that hasn’t implemented signing in by Google, Facebook etc. yet. But from what I’ve heard and read they give awesome classes. So it I think it is worth going through another sign up process again.

Since you’ve asked  photo piwink.gif no, I will not be joining that class.


Leave a comment

The abs() function visualized

The abs() function returns the absolute value of a number, it works like this:

cliccie for larger piccie

cliccie for larger piccie

If you are not the most gifted mathematician around  photo clown.gif and you need the some more explanation and/or visualization this vid will do the trick, it explains the concept rather well:

The vid is about 5 minutes long, imho you only need to watch the first two minutes to get it  photo nodding.gif


Leave a comment

Slicing in Python

sword

Ha! Another trickety trick that is used on Python newbies is slicing. The trick is in getting the correct characters(in case of a string) or the correct items(in case of a list). They often make your mind boggle  photo piangry.gif by asking you first to use len() to check the length of a list(or string).

Let’s say you have this list:

the_list = [“Master Chief”, “James”, “Miranda Keys”, “Cortana”, “343 Guilty Spark”, “Deja”]

As you know, that even though there are 6 items in the list but Python will only index 5 items. That’s because Python starts indexing from 0. Don’t be fooled by len(), however!

lenismean

As you can see len() returned 6, this is what makes slicing so tricky!

Always remember:

“Python counts from 1, but indexes from 0.”

And of course there is another thing to keep in mind( you really didn’t think it would be that easy right  photo emo.gif ), in case of slicing Python will always returns the before last index.

So if you would like to get a slice containing only the first two items, you should type this:

first_two = the_list[:2]

Why didn’t I type the 0? When your slice needs to have the first item in it there is no need to type the 0, you can do it though if you want.

Check the following screen shot where I’m attempting  photo piwink.gif to slice the first  two items (“Master Chief”, “James”).

first_two

Now, you know Python always returns the before last index. The next question will be to include the last item in your slice. Now what  photo redface.gif

You get the last item by typing the index of the first item you want and leave the space after the colon blank.(In this specific case we are talking about the 5th index, which happens to be the 6th item.) It looks like this:

ai = the_list[3:]

listai

Remember slicing does not affect the original list! originallist

(You can always do the cliccy for a larger piccie!)


1 Comment

Get even with Modulo!

DOKONUS 2c657

If you just started learning Python, one off the things people like to trick you with, is asking you to create a function with a code that should do something if it’s outcome is even. They expect you’ll mess around with stuff like “int”, type() etc.

But don’t be afraid  photo smile.gif it is very easy to create such a piece of code. Let’s say the parameter of your function is x and the code should evaluate (=check) if x is even. This is how you do it:

x % 2 == 0

See the % is called the Modulo, and when used as an arithmetic operator it returns the remainder of an integer division.

9 % 2 will return the remainder 1, 2 goes into 9 evenly 4 times.

12 % 4 there is no remainder, 4 goes into 12 evenly 3 times.

And as you know, no remainder equals to 0.

And of course if the outcome should be uneven you would use this:

x % 2 != 0


Leave a comment

Struggling through chapter 9, Reading Word Lists

So yeah, I am still struggling through “Think Python”. In chapter 9 you are supposed to download a document called “word.txt” from here. And then you are supposed to open the darn thing by typing this in the Python Shell (I use IDLE) fin = open(‘words.txt’). But of course when I typed it, I had to get a God darn error photo piangry.gif!

This was the error IOError: [Errno 2] No such file or directory: ‘words.txt’.

Since I wasn’t satisfied with what I learned from google, I decided to fiddle around myself, a known recipe for disaster photo loveit.gif  But when it hit me what the problem was, the Python Shell was looking for the file in the wrong place, I solved this issue in a minute.

How to check Python Shell’ s current working directory (the directory in which the Python Shell looks for data/programs). Go to File and then select Path Browser

selectpath

The Path Browser will open

pathbrowser

As you can see the path of the current working directory is /home/Roja, but I had the file saved in /home/Roja/thinkpython.

What did I do? I quickly found out I couldn’t move to the /thinkpython directory in the Path Browser. So I copied the file from /home/Roja/thinkpython to /home/Roja. It is not the most elegant solution I’ll admit  photo puh2.gif but it worked and I was able to continue with the book.

Remember, you can always do the cliccie for larger piccie!

(This is what the Python Shell looks like when it can’t find the file

cantfindfile

and this when it can

foundfile)


2 Comments

How to install IDLE in Fedora

This tutorial is for Python versions 2.****! For installing IDLE 3 which is needed for Python 3, check this post.

If you are going to clown around in Python, you’ ll need an interpreter. An interpreter is basically, a piece of software that translates the code you’ve written (source code) into something a computer can read(machine language).

interpret

(output = computer)

The interpreter I currently use is IDLE, it’s light, simple in use and ideal for beginners. IDLE stands for “Integrated DeveLopment Environment” and is written in and exclusively for python. This means you can’t write code for another language, like C in IDLE!

How to install IDLE:

  • open terminal
  • log in as root
  • type “yum install python-tools” (without the quotation marks) if you are using versions  of Fedora untill 21 or type “dnf install python-tools” (without the quotation marks) if you are using versions from Fedora 22

installatianIDLE
After the installation, you’ ll notice something interesting, unlike most programs IDLE can’t be found in the application menu (i.e. there is no Desktop icon to click on).  IDLE can only be opened through terminal and it, depending on your settings of course, doesn’t require you to be root to execute it.

How to open IDLE:

  • open terminal
  • type “idle”

idleterminal

The following screen will open:
pythonshell

WARNING: Do not close terminal while working in IDLE, if you do so IDLE will close too!!

Remember, you can do the cliccie for larger piccie and the first picture came from here.


2 Comments

How to install Swampy using Easy Install in Fedora

If you are going through “Think Python” you’ ll see that in chapter 4, you will be asked to install Swampy. What disappoints me about this is, there are no clear instructions on how to get the darn thing installed Photobucket Making this book, not really aimed at novices imo,  other than that it is a great book.

Before you do anything, I suggest to check which version of python you have running right now. You do this by opening the terminal and simply typing “python” without the quotation marks. It looks like this.

pythonversion

As you can see I am running python 2.7.3, the ”3″ isn’t important for this tutorial.

Now, we are going to get “Easy Install” , go to this site and scroll all the way down until you see this.

setuptoolversion

Click and download the version of setup tools which correspondents with the current version of python you have ( in my case it is setuptools-0.6c11-py2.7.egg). Make sure to download it into your downloads folder.

Open terminal  (if you are still in python, press “ctrl” and “d” at the same time to exit) and move to the downloads folder. You do this by typing “cd ~/Downloads”,  I also suggest logging in as root after you are in the downloads folder.

changedirectory

After you have logged in as root, you have to run the file you downloaded as a script, you do this by typing ” sh setuptools-0.6c11-py2.7.egg” .

easyinstall

After the installation is done, return to your home directory by typing “cd ~”. Now you are ready to install Swampy, finally! Type in terminal “easy_install Swampy”.

swampy

And you are done!

(If you want to be sure whether Swampy is installed or not, just type ” import swampy.TurtleWorld” in python. If you don’t get a message it’s installed, it will look like this:

check swampy

If it is not installed, you’ ll get some scary message Photobucket)

Remember you can always do the cliccie for a larger piccie!


1 Comment

Free online Python 2 courses!

As you know I am struggling to learn Python 2. In case you wonder why Python 2 and not Python 3? For me it was just a matter of convenience really, I couldn’t point out the differences between those 2 even if it could save my life Photobucket I started with “Learn Python the Hard Way” and it suggested to install Python 2, as you know I ru(i)n Linux on my Desktop and it had Python 2 pre- installed, so that was easy.

  1. Learn Python the Hard Way
  2. Code Academy
  3. Udacity , (CS101 Introduction to Computer Science)
  4. LearnPython.org
  5. The Python Tutorial (official tutorial by http://www.python.org)

 

“Learn Python the Hard Way” and the  require no registration Photobucket for the others you can use your Google account to log in. As you know I’m doing the first two courses right now and yes I am planning to the last 2 too! I like to listen to Sal Khan his voice and when you finish the course at Udacity, you’ll get a certificate with a picture of a robot on it Photobucket!

I would like to point out that “Learn Python the Hard Way” doesn’t seem to require much mathematical insight as far as I can see (I am at exercise 25 at the moment of writing), if you can think logical you’re fixed Photobucket The Code Academy’ s Python course is a different story though, this is a screen shot of 1 of their exercises:

cliccie for larger piccie

Photobucket

If your math is a bit rusty or you aren’t the most gifted mathematician around,  it might help to look at Khan Academy’s math curriculum.