Home of RF and Wireless Vendors and Resources

One Stop For Your RF and Wireless Need

Python Programming Tutorial-Page1

Refer following pages to learn complete Python language tutorial.
Python tutorial page1
Python tutorial page2
Python tutorial page3
Python tutorial page4
Python tutorial page5
Python tutorial page6

What is Python?

An Interpreted high-level programming language similar to Perl, Ruby, Tcl, and other so-called scripting languages created by Guido Van Rossum around 1990 and named in the honor of Monty Python.

Common applications and non-uses of Python

•  Text processing/data processing
•  Application scripting
•  Systems administration/programming
•  Internet programming
•  Graphical user interfaces
•  Testing

Python is not suitable for device drivers and low-level systems,computer graphics, visualization, and games and for numerical algorithms/scientific computing.

But Python is still used in these application domains, but only as a high-level control language. Important computations are actually carried out in C, C++, Fortran, etc. For example, you would not implement matrix multiplication in Python.

Running Python

•  Python programs run inside an interpreter
•  The interpreter is a simple console-based application that normally starts from a command shell (e.g. the Unix shell)
shell % python
Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license"
•  Expert programmers usually have no problem using the interpreter in this way , but it is not so user-friendly for beginners.
•  Python includes a simple integrated development called IDLE (which is another Monty Python reference)

The Python Interpreter

•  When you start Python, you get an "interactive" mode where you can experiment
•  If you start typing statements, they will run immediately
•  No edit/compile/run/debug cycle
•  In fact, there is no "compiler"

Interactive Mode

The interpreter runs a "read-eval" loop
>>> print "hello world"
hello world
>>> 37*42
>>> for i in range(5):
... print i

Getting Help

help(name) command
Type help() with no name for interactive help
Documentation is available at http://docs.python.org

Creating python program

•  Programs are put in .py files
•  Source files are simple text files
•  Create with your favorite editor (e.g. emacs)
•  Can also edit programs with IDLE or other Python IDE

Python Statement

•  A Python program is a sequence of statements
•  Each statement is terminated by a newline
•  Statements are executed one after the other until you reach the end of the file.
•  When there are no more statements, the program stops


Comments are denoted by #
# This is a comment
height = 442 # Meters

Extend to the end of the line
There are no block comments in Python (e.g., /* ... */)


A variable is just a name for some value
Variable names follow same rules as C
You do not declare types (int, float, etc.)
height = 442 # An integer
height = 442.0 # Floating point
height = "Really tall" # A string
Differs from C++/Java where variables have a fixed type that must be declared.


python keyword

Variables can not have one of these names
These are mostly C-like and have the same meaning in most cases

Case Sensitivity

•  Python is case sensitive
•  These are all different variables:
name = "Jake"
•  Language statements are always lower-case
print "Hello World" # OK
PRINT "Hello World" # ERROR
while x < 0: # OK
WHILE x < 0: # ERROR


The while statement executes a loop
while thickness <= height:
thickness = thickness * 2
numfolds = numfolds + 1
print numfolds, thickness


Indentation used to denote blocks of code Indentation must be consistent Colon (:) always indicates start of new block while thickness <= height: There is a preferred indentation style Always use spaces Use 4 spaces per level Avoid tabs Always use a Python-aware editor


•  If-else
if a < b:
print "Computer says no"
print "Computer says yes"

•  If-elif-else
if a == '+':
op = PLUS
elif a == '-':
op = MINUS
elif a == '*':
op = TIMES


Relational operators
< > <= >= == !=

Boolean expressions (and, or, not)
if b >= a and b <= c:
print "b is between a and c"

if not (b < a or b > c):
print "b is still between a and c"
Non-zero numbers, non-empty objects also evaluates as True
x = 42
if x:
# x ix non zero

The print statement

print x
print x,y,z
print "Your name is", name
print x, #Omits newline

Produces a single line of text

Share this page

Translate this page