Introduction

This is a detailed tutorial designed for coders who need to learn the Python programming language from scratch. In this course, I will try to highlight many of Python's capabilities and features. Python is an easy-to-learn programming language.
Python
Your First Program
  1. >>> print "Hello World"
  2. Hello World
  3. >>>
On UNIX:
  1. #!/usr/local/bin/python
  2. print "Hello World"
Expressions
Expressions are the same as with other languages as in the following:
Variables
Variables are not tied to a memory location like in C. They are dynamically typed.
  1. a = 4 << 3
  2. b = a * 4.5
if-else
  1. # find the maximum (z) of a and b
  2. if a < b:
  3. z = b
  4. else:
  5. z = a
elif statement
PS: There is no “switch” statement.
  1. if a == ’+’:
  2. op = PLUS
  3. elif a == ’-’:
  4. op = MINUS
  5. elif a == ’*’:
  6. op = MULTIPLY
  7. else:
  8. op = UNKNOWN
The while statement
  1. while a < b:
  2. # Do something
  3. a = a * 2
The for statement
  1. for i in [3, 4, 10, 25]:
  2. print i
Tuples:
    Tuples are like lists, but the size is fixed at the time of creation.
    1. f = (2,3,4,5) # A tuple of integers
    Functions
    1. #Return a + b
    2. def Sum(a, b):
    3. s = a + b
    4. return s# Now use it
    5. a = Sum(2, 5)# a = 7
    Files
    1. f = open("foo","w") # Open a file for writing
    2. g = open("bar","r") # Open a file for reading
    "r" Open for reading
    "w" Open for writing (truncating to zero length)
    "a" Open for append
    "r+" Open for read/write (updates)
    "w+" Open for read/write (with truncation to zero length)
    Examples of string processing functions
    1. string.atof(s) # Convert to float
    2. string.atoi(s) # Convert to integer
    3. string.atol(s) # Convert to long
    4. string.count(s,pattern) # Count occurrences of pattern in s.