LehreWiki

Basic Python

This lesson includes the basic Python language. No modules have to be imported

Built-in keywords

Python built-in keywords

help> keywords

and                 else                import              raise
assert              except              in                  return
break               exec                is                  try
class               finally             lambda              while
continue            for                 not                 yield
def                 from                or
del                 global              pass
elif                if                  print

Built-in types

Python built-in numeric types

There are four distinct numeric types:

In addition, Booleans are a subtype of plain integers

Boolean

Boolean (logical) types

Operations

x + y           sum of x and y
x - y           difference of x and y   
x * y           product of x and y      
x / y           quotient of x and y
x // y          (floored) quotient of x and y
x % y           remainder of x / y
-x              x negated       
+x              x unchanged     
abs(x)          absolute value or magnitude of x        
int(x)          x converted to integer
long(x)         x converted to long integer 
float(x)        x converted to floating point   
complex(re,im)  a complex number with real part re, 
                imaginary part im. im defaults to zero
c.conjugate()   conjugate of the complex number c
x ** y          x to the power y

Boolean Operations: and, or, not

x or y          if x is false, then y, else x 
x and y         if x is false, then x, else y 
not x           if x is false, then True, else False    

Comparisons

<               strictly less than      
<=              less than or equal      
>               strictly greater than   
>=              greater than or equal   
==              equal   
!=              not equal       

is              object identity         

Sequence types

Operations

x in s  True if an item of s is equal to x, else False  
x not in s      False if an item of s is equal to x, 
        else True       
s + t   the concatenation of s and t    
s[i]    i'th item of s
s[i:j]  slice of s from i to j  
s[i:j:k] slice of s from i to j with step k
len(s)  length of s     
min(s)  smallest item of s      
max(s)  largest item of s       

Variables, assignment

The assignment statement = creates new variables and gives them values

a=2
l=9**1000L
S='Hello'
f=1e5

Lists and tuples

In [5]: a=[1,2,3]
In [6]: b=(1,2,3)

In [7]: a[0]
Out[7]: 1

In [8]: b[0]
Out[8]: 1

In [9]: a[0]=4

In [10]: a
Out[10]: [4, 2, 3]

In [11]: b[0]=4
---------------------------------------------------------------------------
exceptions.TypeError 

Indexing and slicing

In [1]: a=range(10)

In [2]: a
Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [3]: a[0:4]
Out[3]: [0, 1, 2, 3]

In [4]: a[4:]
Out[4]: [4, 5, 6, 7, 8, 9]

In [5]: a[:4]
Out[5]: [0, 1, 2, 3]

In [6]: a[:-1]
Out[6]: [0, 1, 2, 3, 4, 5, 6, 7, 8]

In [7]: s='Hello world'
In [8]: s[:5]
Out[8]: 'Hello'

In [9]: a=tuple(range(10))
In [10]: a[0:4]
Out[10]: (0, 1, 2, 3)

List built-in functions (methods)

In [72]: L=['C','A','B']
In [73]: L.sort()

In [74]: L
Out[74]: ['A', 'B', 'C']

In [75]: L.pop()
Out[75]: 'C'

In [76]: L
Out[76]: ['A', 'B']

In [77]: L.insert(1,'C')

In [78]: L
Out[78]: ['A', 'C', 'B']

Nested data structures

In [84]: a=range(10)
In [85]: b=range(0,10,2)
In [86]: b
Out[86]: [0, 2, 4, 6, 8]

In [87]: c=[a,b]

In [88]: c
Out[88]: [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 2, 4, 6, 8]]

In [91]: c[0]
Out[91]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [92]: c[1]
Out[92]: [0, 2, 4, 6, 8]

In [93]: c[0][0:4]
Out[93]: [0, 1, 2, 3]

In [94]: c[1][0:4]
Out[94]: [0, 2, 4, 6]

Dictionary

In [95]: D={}
In [96]: D['a']=range(10)
In [97]: D['b']=range(0,10,2)

In [98]: D
Out[98]: {'a': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'b': [0, 2, 4, 6, 8]}

In [99]: D['a']
Out[99]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [100]: D.keys()
Out[100]: ['a', 'b']

Loops

In [106]: a=['Hund','Katze','Maus']
In [108]: for i in a:
   .....:     print i
   .....:
Hund
Katze
Maus

In [111]: for i,j in enumerate(a):
   .....:     print i,j
   .....:
0 Hund
1 Katze
2 Maus

Control statements

In [116]: if a==1:
   .....:     print 'a=1'
   .....: else:
   .....:     print 'a != 1'
   .....:
a=1

Modules

There are many ways to import modules. The resulting namespaces are different

LehreWiki: Python/Lesson1b (last edited 2008-10-30 13:10:15 by anonymous)