Python Guide: From Zero to Hero Part 1

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.    
  3. 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.    
  3. b = a * 4.5 
if-else
  1. # find the maximum (z) of a and b    
  2.      
  3. if a < b:    
  4.    z = b    
  5.      
  6. else:    
  7.     z = a   
elif statement
 
PS: There is no “switch” statement.
  1. if a == ’+’:  
  2.  op = PLUS  
  3.    
  4. elif a == ’-’:  
  5.  op = MINUS  
  6.    
  7. elif a == ’*’:  
  8.  op = MULTIPLY  
  9.    
  10. else:  
  11.  op = UNKNOWN 
The while statement
  1. while a < b:  
  2. # Do something  
  3.  a = a * 2 
The for statement
  1. for i in [341025]:       
  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.   
    6. a = Sum(25)# a = 7 
    Files
    1. f = open("foo","w"# Open a file for writing  
    2.    
    3. 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. 


    Recommended Free Ebook
    Similar Articles