harkuchh

of your taste...

Python Tutorial Day 1: Basics of Python and Programming

I want to start with an apology. I am apologizing a lot lately. There’s a whole world of changes going on in my life. I wrote a post on my blog (my personal blog), and it hit the hacker news. So I got a shitload of advises, suggestions, opinions, job offers, interview calls etc. Was a very busy week. Besides I am back in harkuchh. We are about to start a development shop. Hope things will be better this time.

Let’s continue with our Python tutorial. I know many would’ve left following this tutorial series due the lack of schedule, but still I’d like to keep my promise. Hope few of you’ll like these. My vision for these tutorials is to seed a community in our city, a community of like minded geeks.

Python Tutorial Day 0: Hello Python! Nice to Meet Yea!

Hello friends,

As promised here we are starting a Python tutorial series. This will go 2 week long, including 14 tutorials. Can you learn Python only in 2 weeks? No you can’t. Teaching you everything Python has to offer is not the objective of this series. I am aiming for those who are brand new to programming, and want to introduce programming to them gently. Please read the announcement for more details about the series.

I deeply apologize for the delay in publishing this post. Many funny things are going on around me nowadays. They have started development work in harkuchh, I had my own projects I was bootstrapping and my relations with harkuchh are on shaky grounds. This series might be the last I write on this blog, but I will finish it before leaving.

Let’s not waste time and start with our day 0 of learning Python. From the title you might have already guessed, there will be 15 tutorials in total. This one is meant for setting up the Python interpreter so that you can easily write and run Python programs on your own machine. We will also discus some basic terminology related to programming and there will of course be the traditional hello world. Don’t skip on reading this one considering it a filler. If you are coming from the world of languages like PHP, C family languages or Java etc, there are some new things for you to grok.

Installing Python

You need to know about versions of Python before you install Python. Python has 2 major versions: 2.x (latest 2.7.6) and 3.x (latest 3.3.4). Those new to Python often get confused which one they should use. There are obvious benefits of Python 3.x over 2.x, but I recommend you to download and install 2.x. There are too many reasons to mention here. I am using 2.7 on my machine, so I recommend you to install a 2.x version (whichever is latest on the site) just to keep up with the programs we will write in this series.

Windows

It’s very easy for MS Windows users to install Python on their machines. Since Windows is built as an idiot-proof operating system, there are click and install packages (aka setups) available. Follow these steps.

  • Go to the official python download site
  • Download the latest 2.x package for windows from the site
  • It’s a traditional windows installer which will guide you through the installation.
  • Just run the installer and go through ‘next’ clicks to install
  • That’s all you need for now

Linux

If you are using some variant of Linux, cheer up as Python is most likely already installed on your system. Most Linux systems come preinstalled with a 2.x version of Python.

But there is one extra goodie packaged with Windows installed that is that is not installed in Linux by default. It’s called IDLE. We’ll talk about it in a minute. You can easily install it with the package manager of your distro.

Running Python

You most likely now have Python installed on your system which is ready to roll. Python has something new for those who code in more traditional languages. If you are using Windows, go to the Start Menu and search of Python IDLE. Idle is a simple GUI Python IDE which is meant for aiding new Python devs to get started quickly. If you are a Linux user and have not install Idle, you can just shoot out your terminal and enter python. A python shell will be spawned for you.

Idle shell

What you are seeing in above snapshot is the Python Shell. Python provides a shell (also called REPL) to help making programs interactively. It’s fun to use and you will use it for making almost every Python program for some time.

You can type in any valid Python statement in there, and press Enter. Python will execute that statement, prints the output and ask you for entering more. That’s why it is called REPL (Read Eval Process Loop). The shell reads every single line you enter in there, it evaluate that line, process the task you give it, and Loops for more input.

At this moment we have not learned any Python code yet, but you can try basic calcualtions like 5+5 or anything. You can also write your name in ' or ". It will simply evaluate it, print result and ask for more.

print statement: This is the first thing generally taught to new programmers. I won’t break the tradition. print statement in Python is used to print anything on the screen. Like if you want to print your name, just write print "My name". Yes, for printing a sentence, it is required to put in inside ' or "s.

Play around with repl more if you want.

Hello world program in Python

We will close today’s tutorial with the hello world program in Python. REPL allows us to execute statements interactively, but when writing actual programs, we need to save them in files for executing later. Traditionally, python programs are written in files with extension .py. We will talk about them some other day.

Let’s quickly wrap up and write our ‘hello world’ program.

I told you Idle is actually a simple IDE (Interactive Development Environment) for Python. It allows you to create Python programs, and run them from right inside the Idle. Open a new file in Idle:
* Go to File Menu * Click New File (or whatever new option is there, sorry I am using Linux right now)

Python Idle New File Hello world

It will open a new window. Note that this windows is not an interactive shell. You can write multiple statements in it and they will not automatically execute. We will write our hello world program in it.

You might have already guessed it. Our program is just one single statement.

hello_world.py
1
print "Hello world!"

Write above line in the window, and save it with any name and .py extension.

.py files can be executed in Windows by simply double clicking on them. But we will execute this one from right inside the Idle. Just press F5 key. The output of our program will be shown in the Python shell (the other window).

That’s it for today.

Note: I wrote this one in a sort of rush because I didn’t want to delay it any further. There were readers coming back to harkuchh for this tutorial series and going back disappointed. If you don’t understand anything, or find something hard to understand, or something doesn’t work, or for any kind of queries, please leave a comment.

Announcing 2 Week Python Tutorial Series

Few days back we shared our experience about how we utilize the Python programming language in our work at harkuchh blogging valley. We got very positive interest about it and readers requested tutorials about the Python programming language.

So here I am with the announcement of the python tutorial series on harkcuhh.com.

Tricks to Search Amazing Things With Google

This gonna be a long post, but I bet you won’t want to miss a single word. Krishan tried to convince me to break it into 4 different posts by giving multiple references from all his blogging ‘knowledge’ (here’s a pun if you can get it), but I din’t agree. Quality of content is more important imo.

Here I am telling best of the tricks I use to get what I want from Google. They are fun. If you have some more you think I should know, please mention in comments. Enjoy!

5 Firefox Plugins I Can’t Live Without

I use Firefox as my primary web browser. I have emotional attachments with Mozilla and their mission. I used Chrome for sometime, but I ditched it for some reasons. Firefox make me feel more like home. The philosophical bond of trust is a big thing for me.

Here’s a list of top 5 Firefox addons I use daily. Whenever I switch to a new machine, installing these is generally the first thing I do.

Why Bloggers Need to Save Themselves From Google: Lesson We Got After Losing 400K

Google is no more that same open company which had ‘don’t be evil’ as their slogan.
Google is evil. We all know it.

But Google can be particularly evil to you if you are a blogger. Being a blogger, most of your living depends on Google one way or other. Google is a major traffic source, all information you need come through Google, and Google Adsense is major source of most blogger’s income. Imagine Google having a grudge against you. I mean against you, as a person in particular. Everything you do is seen with a skeptical eye, and suddenly your websites which were rolling few days back are now ruins facing drought of visitors.

Why I Am Learning Python

Why I’m learning Python? well yes, this is really a very valid question and it has a solid answer too, I think everybody should read this, not because I’ve written this for you but because it has much much importance and advantages for you.

Five Reasons Why Every Blogger Should Learn Programming

In this post I am writing about why every blogger (and every person in general) should learn to code. Programming as itself is an asset that will make you a better and smarter person. Most important benefit you’ll get is the ability to make literate decisions. You’ll learn how to break down that large problem you are facing, and how to solve it’s one piece at a time. Besides, it gives ou great level of confidence.
Here are five reasons to convince you to learn programming.