A Python Tour
Just A Brief Introduction


"The only way to learn a new programming language is by writing programs in it."
          - B. Kernighan and D. Ritchie

Computers are good at following instructions, but not at reading your mind."
          - D. Knuth




Some Definitions

Programming Language - a language that is designed to express calculations that can be carried out by a computer.

Program - a set of instructions written in a programming language that can be carried out by a computer to solve a problem.

Program Execution ("running" the program) - the computer carries out the program's instructions.


Machine language (or code) - the low-level language of a computer that consists of each computation's encoding as a sequence of 0s and 1s. In machine language, a program could look like this:

0110111100001010
1111011110101111
0000111101010111
...

where a sequence of 0s and 1s (called bits) correspond to operations such as Add, Store, Get Instruction, etc. Each computer has its own machine language.






How Python Works
 

piglet.cs.utexas.edu:eberlein> python
Python 2.5.4 (r254:67916, Jan 16 2009, 16:27:43)
[GCC 4.2.2] on linux2
Type "help", "copyright", "credits" or "license" for more information
>>>> print "welcome to cs 303e"
welcome to cs 303e
>>>> for i in range(3):
...              print i
...
0
1
2
>>>>






An Example: Some Source Code in Python


def main(): # definition of main function
    print "hello"
    print "world"
    print "hello", "world"

myNum = 13
print myNum*myNum

# call the main function
main()

Output:
169
hello
world
hello world

   





          Repetition and Conditional Actions


                for i in [1, 2, 3]:
                    print i

                for i in range(2):
                    print i

                i = 3
                while i < 10:
                    print i
                    i = i+1
                           i = 5
                    if i < 10:
                        print i, "is less than 10"








Python Program Structure












Lists in Python


Using Python, it is easy to work with lists of items. Here is a small example:

list = ["a", "b", "c", "d"]
print list

list[0] = "z"     # change the first list item to "z"
print list

print list[0:2]   # print the sublist of entries in positions 0 to 1


Output:
['a', 'b', 'c', 'd']
['z', 'b', 'c', 'd']
['z', 'b']