Python 101: Intro to Python

Most tutorials start with installation, and write a simple program called "Hello World", I will follow this tradition just to make sure we cover the basics. Then I will move into control structures and some basic OOP, because almost everything we do in Python is OOPs.

Where to get python?
http://python.org/download/ and get whichever version you want, either 2.6.4 / 3.1.1 (at the time of this writing)

2.6.4 vs 3.1.1, which one?
It gets very technical when one is asked to explain the difference. Here is the shortest and simplest version:

Go with 2.X branch because 3.X branch has a lot of changes, and many existing books, codes and resources are written based on 2.X, so you are better off learning 2.X branch.

If you are very serious about learning Python, you will be happy with 2.X and then learn the differences later.

Write Hello World

Just like any other modern programming language,

1
print "Hello World"

Notice that, unlike C++, Python does not end with semicolon ;

Hack Python Program

As an beginner, you are better off hacking programs, rather than writing the entire program yourself. Once you understand what each line means, then you can write your own program. For example, just change the values or words around.

Unlike complex language like C++, Python is very simple, yet elegant. You do not always need to declare and define types and identifiers.
When we assign a number to any variable, Python knows it is an integer by default. When it is within double quote "", Python knows it is a string.

1
2
abc = 1
cde = "What"

When you need floating point and other numerical types, then, yes, you do need to specify it. So use the option when it is mandatory.

Here is the major difference between the 3.X and 2.X. By default (2.X) if you do 3 divide 5, you get ZERO, whereas in 3.X you get an answer with demicals. If you are using 2.X, you must add one decimal point to any one of the numbers. For 3.X, you are no longer require to declare this extra decimal, because by default 3/5 will print a rounded answer.

1
2
3
4
5
6
7
8
9
10
11
IDLE 2.6.4      
>>> 15/2
7
>>> 3/5
0
>>> 3.0 / 5
0.59999999999999998
>>>
>>> 3/5.00
0.59999999999999998
>>>

Another way is to declare it a floating point, and round it up (check doc / google for answer).

Input Method
Before we jump into control structure, we must learn input method.
input("")
raw_input("")

Just remember that raw_input is safer and a better input method than input(). Once we get to list, dictionary and a few other topics, we will see variations like str(input("")), int(input("")) and etc. Basically if we add a type in-front of an input method, we are restricting the information being inputted, processed and outputted.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
a = input("Enter a number: ")  #  a = 1, you get 1
print a
b = input("Enter a word: ")  # b = fgdfg, you get error, by default input is integer
print b
c = int(input("Enter a number: "))  # c = 121, you get 121
print c
d = int(input("Enter a word: "))  # d = ere, you get error, self-explanatory
print d
e = raw_input("Enter a number: ") # e = 34, you get 34
print e
f = raw_input("Enter a word: ")  # f = fgdfg, you get fgdfg
print f
g = int(raw_input("Enter a number: "))  # g = 343, you get 343
print g
f = int(raw_input("Enter a word: "))  # f = dfdf, you get error, another self-explanatory
print f


Control Structure

There is no need to go in depth with the things I just mentioned above. Here we will briefly discuss the if-else, for and while loop control structure in Python.

One thing to keep in mind: you must be careful with your INDENTATIONS.

IF-ELSE

1
2
3
4
5
6
7
# If-Else
a = int(raw_input("Enter any number: ")  # restrict only number can be input

if a == 0:   # notice that we use colon : after each operation
        print "You won! It is zero"
else:         # notice another colon :
        print "You lost. It was zero, but you answered",a",try it next time!"

The interesting thing is the second print statement (things in bold)
print "You lost. It was zero, but you answered",a",try it next time!"
If we want to print our variable along with a string, we can just user comma to separate the result.
By default, this comma will automatically make a whitespace.

1
2
3
4
a = 10
print "We know a is equal to",a"."   # will print -->   We know a is equal to 10.
# if we do add an extra space before the end quotation
print "We know a is equal to ",a"."  # will print --> We know a is equal to  10.

Try to hack this program by adding or changing with different variables, strings and numbers.

IF-ELIF-ELSE

1
2
3
4
5
6
7
8
9
10
# if-elif-else program
a = int(raw_input("Enter a number between 0 and 10: "))
if a == 0 or a == 10:
    print "It contains a zero!"
elif a >= 1 and a < 5:
    print "It is greater than or equal to 1, and less than 5"
elif a >=5 and a < 10:
    print "It is greater than or equal to 5, and less than 10"
else:
    print "Does not follow rule!"

Python has syntaxes like or, and, not for conditional statements. You will learn them all once you get through the next two lessons.

While loop
Notice the last line print "Hello". I show this so that you see unless the loop is satisfy (stops), the last line will not execute. Moreover, you must remember that Python does not has "end" syntax. So...

BE CAREFUL WITH YOUR INDENTATION.

1
2
3
4
5
6
7
8
9
# while loop program

a = int(input("Enter a number: "))

while a != 0:
    print "Try again"
    a = int(input("Enter a number: "))

print "Hello"

Here is the result
Enter a number: 1
Try again
Enter a number: 3
Try again
Enter a number: 0
Hello

For loop
Like many languages, by default Python starts counting from ZERO. So range(100) really means from 0 to 99 (there are total of 100 integers).

If you want to print from 1 to 100 (including 100), you can do
for a in range(1, 101, 1) # range(first element, end element, counting method ]1 is forward])

1
2
3
4
# for loop

for a in range(100):
    print a

Here is the result

0
1
2
3
4
....
99

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

No Comments yet »

RSS feed for comments on this post. TrackBack URI

Leave a comment