Skip to content

Lesson 3:First steps with python

May 26, 2009

Of course, the first step any programming world teaches you is printing “Hello world”. So that’s what we will do.

Before that there are two ways of writing a python program

  1. Writing the code in a file and executing it as a whole
  2. Writing statement by statement in the prompt (>>>: This prompt can be seen when you type “python” in your command line interface (CLI))

I would suggest following the former method; mainly because it’s easier and most other programming languages also follow the same procedure: writing the code and then converting to the object code (this is the code that the computer understands.). I will follow step one throughout. If you plan on going by step 2, then all you have to do is that you have to type the statements in the prompt just like in the code snippets.

If you are following the first method, then you have to type the code in any text editor (usually notepad; but since I am all the FOSS fan I suggest use notepad++. This is a version solely for editing source codes and hence is friendlier than notepad, making it a preferred choice. Linux guys use gedit if you have the GNOME desktop environment; kedit if you use K desktop environment) and save the source code (name given to the code you type; this is converted into object code: the form computer understands). Make sure you give an extension .py to all the files.

So here’s your first true code in python:

print (“Hello world!! This is my first python program”)

Go ahead try it out.

There’s another way to execute the program: that’s by double-clicking the file. However you would notice that when you double click on the file, a window simply flashes for a second and disappears (in windows only; in linux terminal I doubt this issue arises. Nevertheless you can also add the code to the end in order to increase understandability of the program). That’s how fast the program is executed!! But you don’t get to see any output. To do so, here’s a small trick. Add this code to the end of the program:

input (“Press any key to exit”)

This piece of code asks the computer to wait for the user to press a key before exiting.

That way you can see the program’s output as well.

We will be dealing with the input statement later on as well; so don’t just wipe it from your memory ok.

I think the best way to learn something is doing it yourselves and understanding it; because the more you experiment, the more you question and the more you discover.

So I am going to put the code of the first program that deals with the print statement. It’s quite simple so just go through it, try it and understand it. If you have any doubts, just leave a comment and follow it up. Also leave a comment if you make some discovery; will be glad to learn new things.

#Program illustrating the various ways of using print statements

#To include comments in your program, type your comment after a #

print (“Hello! This is my first program in Python!!”);

q = 7                                #variable are declared thus

print (q)                       #printing variables

print (7 + 18)

print (“The sum of %d and %d is %d” % (7,18,7+18))

print(“My name is %s. I am %d years old. I am %.2fm tall” % (“John”, 23, 1.235))

#This is how values of variables will be substituted in the print statement. It’s quite #straightforward I think.

#%s, %d and %.2f are conversion specifiers that convert the value to the suitable #datatype; same as the ones in C. Try it out and understand it; it’s quite simple

q = “seven”                #variables have no datatypes. so any value can be assigned

#ranging from strings to complex numbers

print (q)

q = 3 + 2j                     #assigning complex numbers

print (q)

del q                            #de-allocating a variable from memory

q = 8                            #a de-allocated variable can be re-defined.

print (q)

del q

input (“Press any key to exit”)         #this waits for user to press a key before exiting

i think there’s an indent problem. so i suggest you copy the whole thing to notepad++ or kedit or gedit and then try to understand it. might be easy. if you still have problems, drop your email id here and i shall try to mail you the file (if i still have it that is). Enjoy.

From → Python

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: