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.
Your First Program
- >>> print "Hello World"
- Hello World
- >>>
On UNIX:
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.
if-else
-
-
- if a < b:
- z = b
-
- else:
- z = a
elif statement
PS: There is no “switch” statement.
- if a == ’+’:
- op = PLUS
-
- elif a == ’-’:
- op = MINUS
-
- elif a == ’*’:
- op = MULTIPLY
-
- else:
- op = UNKNOWN
The while statement
The for statement
- for i in [3, 4, 10, 25]:
- print i
Tuples:
Tuples are like lists, but the size is fixed at the time of creation.
Functions
-
- def Sum(a, b):
- s = a + b
- return s
-
- a = Sum(2, 5)
Files
- f = open("foo","w")
-
- g = open("bar","r")
"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
- string.atof(s)
- string.atoi(s)
- string.atol(s)
- string.count(s,pattern)