Play with Python (Part 2): Preparing for the Magic

(After the previous part: Part 1: The Magic Behind Computers)

Summary: Install python, learn how to start coding, and plan to successfully finish the tutorial!

How to install Python:

I have lectured enough about the reasons for learning programming and the theories behind computers, but I know that you are not here for those! You want to learn how to make that game, right? But before you do so, you need to install python in your computer. First, you need python installer (skip if you are using ubuntu or linux). Get the python installer from this link: http://www.python.org/getit/

2-website-installer

You can see a lot of installers here. Don’t get confused! For this tutorial we will use 32-bit python 2.7 (at the time of the writeup the latest version is 2.7.6). So download the correct installer file to your computer (see the image above).

Windows:
As usual double click on the installer and let it get installed (click ‘next’ and ‘ok’ many times!).

OS X:
Double click on the package manager and install the package.

Ubuntu/Linux:
If you are using Ubuntu you’re all set, because Ubuntu comes with python pre-installed!

Basics and Sanity Check:

So when you write code, Python has an engine (interpreter) that turns your code into instructions that computers can read (those are basically collections of 0’s and 1’s and only computers can understand them). To give commands to computers python has a program (interface) called IDLE. Now to check if the installation worked properly, open IDLE from your programs.

2-select-idle

(Windows: Start Menu>All Programs > Python2.7>IDLE (Python GUI), in general you should get it in the list of programs in any operating system).

Now you should see the following window (minus the code text):

2-idle

Now the “>>>” is the terminal where you can enter any valid python command and it will show you the output. Now type:

print "Hello world! I am learning to code!"

And you should see the output text. Congratulations! You have written your first piece of code!

So what you just did is that you outputted some text on the screen and  print is the command for doing so in python. We will explain all these in a lot more details in the next chapters, but for now enjoy your new super power of being able to code!

Now try to do these math with python (We have also included the output. Of course you don’t need to type those!):

First thing you noticed is that I have added white space in between everything. You don’t really need to do this, but this makes your code look a lot cleaner. So before and after arithmetic signs we add these spaces. Finally, what let’s figure out what these signs are doing. The first one  + = addition, - = subtraction, * = multiplication, / = division. Let’s stop for a second– “but what are the next two?” you may ask. and you probably have not seen these before. Don’t worry, they are easy to understand!

The first one is “%” is modulo sign. In python a % b means you are asking for the remainder of a upon division by b. For example: 3 % 2 gave us 1 because that’s the remainder you get when you divide 3 by 2. This is a great way to check if a number is odd or even! If the remainder is 1 the number is odd, and even if it is 0!

The next one, ** means to the power. So a ** b means ab in math. And that means you are multiplying b number of a’s together. More explicitly: 2 ** 3 means 23 = 2 x 2 x 2 (i.e. multiplying 3, 2s).

Play with both the modulo and power operations on the terminal before these concepts make more sense!

Starting to Code:

Now you’re probably thinking, if you enter one line at a time and do simple addition and subtraction then probably you cannot do much. You are absolutely right! That’s why programmers write their programs in a simple text file and then make the language compiler software to run it. As we are becoming pros, we will do the same!

Select File > New Window (or press control + N). You should see a blank screen. This is just a fancy text editor (like notepad), but as you type you will see that the text contents are getting nice color coding (the color coding has a fancy name– syntax highlighting)! Now type:

Finally select: Run > Run Module or press F5 key on keyboard and it will probably ask you to save the file. Now give the file any name and end the name with .py (we named it first.py). It is the python file extension (like .pdf, .exe etc) and if you do this the computer will understand that it is python code! Now you can see the output printed in the terminal. Neat!

By the way, the gray texts after the hashtags (#) are called comments. When python runs the code, it goes from top to bottom, but ignores the comments. The goal of these comments is to make the code more clear to the human readers, but you can choose not to type them.

How to a complete this tutorial successfully:

Remember the last time you sat down to learn something? Yes, I do too! And I did not finish it! So it is important that you follow these instructions to learn python successfully:

  1. Set one hour (or maybe half an hour of time daily). This will make sure you are learning everyday.
  2. Go through one or two lesson(s) at a time. You might be tempted to go through everything in a single sitting, but that way you will forget everything really fast. So don’t rush and take your time.
  3. Type everything up yourself. I know it is really tempting to copy-paste the code, but don’t do it! Typing up will ingrain the lessons and commands to your brain as so called muscle memory. So after a while you will be able to code without looking at any tutorial or help!
  4. Remember the cool game we will be making at the end? Good!
  5. Finally don’t be disappointed if you don’t understand anything. Especially if you find anything unclear leave a comment below and I will try my best to help.

I know it was  lot of materials for a single lesson. But hey! Now you know how to write a legit piece of python code! If you did not understand anything– no worries, because we will cover everything in details. Just wanted to make sure you know how to run code and where to write code to run them!

Happy coding!

Previous Part: Part 1: The Magic Behind Computers

Next Part: Part 3: The Magic Starts with Variables

All parts: Play with Python

This entry was posted in Education, English, Python and tagged , , , .

7 Comments

  1. Rafat January 8, 2014 at 2:00 am #

    Don’t stop!keep writing.waiting for the next part.

  2. Rifat Mehreen January 8, 2014 at 10:29 am #

    it was awesome… and i did it…

  3. Avik Chakravarty January 8, 2014 at 3:26 pm #

    Hey,
    Instructions are really easy to follow. Eagerly waiting for the next post. Well done (y)

  4. Jorge Valerio January 9, 2014 at 2:06 am #

    What should i know before getting into Python? programming languages/mathematics

    • Tarik Adnan Moon January 9, 2014 at 2:28 am #

      I explained the requirements in the previous part. In general, for my tutorial you don’t need any background. I will explain everything and I will not assume any background, but patience to follow the tutorials.

  5. Shopnil May 10, 2014 at 4:43 am #

    Please publish the next part regarding python quickly. Can’t wait!! Love from country :)

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*