Intro

Some Resources

rickspencer3's chapters

What is Python?

Python Interpreter

Language Concepts

Indentation levels

   1 for i in xrange(5,20):
   2     print i

2. Strong Dynamic Typing

Dynamic Typing

>>> x = 1 
>>> x 
1 
>>> type(x) 
<type 'int'> 
>>> x = "string" 
>>> x 
'string' 
>>> type(x) 
<type 'str'> 

Strong Typing

>>> x = 1 
>>> print x + " is a string" 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
TypeError: unsupported operand type(s) for +: 'int' and 'str' 

Types

Numeric types

>>> x = 5 
>>> y = 5L 
>>> x * y 
25L 
>>> x = 5 
>>> y = 5.0 
>>> x * y 
25.0 
>>> x = 5L 
>>> y = 5.0 
>>> x * y 
25.0 
>>> x = 25 
>>> y = 5.01 
>>> x / y 
4.9900199600798407

Strings

None is an object of NoneType

Lists and Tuples

create a tuple

>>> wintermonths = ("December","January","February") 
>>> wintermonths 
('December', 'January', 'February') 

create a list

>>> grades = ["A","A-","B","C"] 
>>> grades 
['A', 'A-', 'B', 'C'] 

Accessing

Indexed with [] as you are used to

>>> wintermonths 
('December', 'January', 'February') 
>>> wintermonths[0] 
'December' 

A handy trick for starting from the back

>>> grades 
['A', 'A-', 'B', 'C'] 
>>> grades[-1] 
'C' 
>>> grades[-2] 
'B' 

in

Test if a list or tuple contains a value

>>> grades = ["A","A-","B","C"] 
>>> grades 
['A', 'A-', 'B', 'C'] 
>>> "A" in grades 
True 
>>> "F" in grades 
False 

Dictionaries

>>> grades 
['A', 'A-', 'B', 'C'] 
>>> i = grades.index("A") 
>>> i 
0 
>>> i = grades.index("B") 
>>> i 
2 

Test with "in" first, or you are liable to get an error

>>> grades 
['A', 'A-', 'B', 'C'] 
>>> i = grades.index("F") 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
ValueError: list.index(x): x not in list 

Comparisons and branching

a = "xxx"

if a is "boo":
    print "a is boo"
elif a is not "foo":
    print "a is not foo"
else:
    print "a is foo"

a = 1

if a == 1:
    print "a is 1"

Loops

>>> for g in grades: 
...  print g 
... 
A 
A- 
B 
C 

>>> x = 0 
>>> while x < 10: 
...  x += 1 
...  print x 
... 
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 

>>> grades = [] 
>>> for i in xrange(10): 
...  digits.append(i) 
... 
>>> digits 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 

Error Handling

>>> grades = ['A', 'A-', 'B', 'C'] 
>>> 
>>> grades = ['A', 'A-', 'B', 'C'] 
>>> try: 
...  grades.index("F") 
... except ValueError: 
...  print "no Fs" 
... 
no Fs 

comments and doc comments

Modules, Files, Classes

file overview

import pygtk
pygtk.require('2.0')
import gtk

class program2:
    def __init__(self, startval):
       self.x = startval

    def run(self):
        for i in range(1,10):
            self.x += 1
            self.output()

    def output(self):
        if self.x > 9:
            print "x is greater than 9"
        else:
            print "x is not greater than 9"

if __name__ == "__main__":
    p = program2(0)
    p.run()
    p = program2(1)

    p.run()

Code section

class overview

   1 class program3(program2):
   2     def __init__(self):
   3         program2.__init__(self, 5)

z = 1
class program:
    x = 2 #class variable
    def __init__(self):
        print self.x #not necessarily 2, could have been modified by another instance
        print z
        self.y = 3 #instance variable

if __name__ == "__main__":
    p = program()
    print p.y

OO syntax

UbuntuOpportunisticDeveloperWeek/IntroToPythonForProgrammers (last edited 2010-02-25 17:54:59 by static-68-151-229-77)