Introduction to Python language

In this tutorial we will learn how to program in the python language.

What is python

Python is a high level script language, it's produces a very clean and organized code because it uses identation as block delimiters, it also support object oriented paradigm. Bellow is a code sample:

Python Standard library

Python's standard library is very complete, it has modules for database handling, standard internet protocols, graphical user interface, etc...

Installation

Download Python-2.4.1.exe , after that install it, and test at the DOS by typing "python -V"

Now go to and run the "python shell", after you get the ">>>" type print "hello world!"

You can also try 1+2, 2*2, 3**2 (Exponential)

print "1+2=", 1+2 (Simple sum)

Here is a list of common arithmetic operators:

command name example output
+ Addition 4+5 9
- Subtraction 2-2 0
* Multiplication 3*3 9
/ Division 7/2 3
% Reminder 7%2 1
** Expoent 8**2 64

Comments

Comments are used to help you remember what that old routine does :) (Even so there is times that I just can't remember). Comments are ignored by the python interpreter.

# This is a comment

Variables

Python don't need variable declaration, just type a name and assign a value, that python will create a variable, python is not strongly-typed language so some care must be taken in order to avoid some commom bugs.

Creating a program

Python programs are just text files (.py) that will be passed to python. In the video bellow we show how to create a simple program with variables and comments.

Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player

Strings

Strings are a sequence of alphanumerical value, a string variable can be declared like this.

>>> var_string = "SomeString"

A variable called var_string will store the value "SomeString"

Loops statements

In python and in most languages we have these loop statements

Those statements are used when you have to repeat a group of instructions many times based in some condition.

Before the for, and while statements let's first see the boolean operators

operator function
< less
<= less or equal
> greater
>= greater or equal
!= not equal
<> not equal
== equal

while

The while statement will execute a block of instructions while some condition is true. For example

while condition:

instructions1

instructions2

instructionsn...

Observe that the block of instructions of the while statement has a identation.

for

The for statement in Python differs a bit from what you may be used to in C or Pascal. Rather than always iterating over an arithmetic progression of numbers Python’s for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence. For example:

Condition statement

Conditions create a area of code that will be executed only if a condition is met. In python we have these condition statements

if syntax

The if statement means that something will be executed if some condition is met:

if condition:

doSomething;

elseif otherCondition:

doSomething;

else:

doDefaultSomething;

Example:

Functions

In python and in most programming languages, functions are block of codes that can accept arguments and return a value.

def functionname(arg1, arg2, ...):

statement1;

statement2;

statementn...;

return someValue;

x = functionname(1,2,...);

One can also define a function with no arguments and no return value.