Department of Computer Science — University of San Francisco – p.3/?? Department of Computer Science — University of San Francisco – p.4/?? Department of Computer Science — University of San Fra
Basic Python Numbers Mathematical Operators
Python has a nice set of built-in data types Numbers work like we’d expect. Python has all the usual mathematical operators
Numbers There are integers, equivalent to longs in C. (1, -31311, +,=,*,/, %
Strings 4000) «, »
Lists There are long integers, which are of unlimited size ** or pow for exponentiation
Sets (31111L, 12345l)
abs, rand, |, &
Dictionaries There are floats, equivalent to doubles in C or Java.
Files 1.23, 3.1e+5 This makes Python a very handy desk calculator.
Using built-in types makes your code easier to There are Octal and Hexadecimal representations as in Operations are coerced to the most specific type.
write/maintain, more portable, and more efficient. C. (0155, 0x3af5) 3 + (4.0 / 2) will produce a float.
There are complex numbers, as in Lisp, Matlab, etc. Common error: since variables do not have declared
(3.0+4j) types, be careful of rounding: 3 /2 == 1 !!
Department of Computer Science — University of San Francisco – p.6/?? Department of Computer Science — University of San Francisco – p.7/?? Department of Computer Science — University of San Fra
Department of Computer Science — University of San Francisco – p.9/?? Department of Computer Science — University of San Francisco – p.10/?? Department of Computer Science — University of San Fran
Dictionaries Tuples Files
A Dictionary is a Python hash table (or associative list) Tuples are like immutable lists. Since it’s a scripting language, Python has a lot of
Unordered collections of arbitrary objects. Nice for dealing with enumerated types. support for file I/O
d1 = {} - new hashtable d2 = {’spam’ : 2, ’eggs’, 3} Can be nested and indexed. Operators are not too different from C.
Can index by key: d2[’spam’] t1 = (1,2,3), t2 = (1,2,(3,4,5)) Outfile = file(’fname’, ’w’) or infile = file(’fname’, ’r’)
’r’ is default and can be left out
Keys can be any immutable object. Can index, slice, length, just like lists.
t1[3], t1[1:2], t1[-2] S = infile.read() - read the entire file into the string S.
Can have nested Hashtables
d3 = {’spam’ : 1, ’other’ :{’eggs’ :2, ’spam’ : 3}} Tuples are mostly useful when you want to have a list of S = infile.read(N) - read N lines into the string S.
d3[’other’][’spam’] a predetermined size/length. S = input.readline() - read one line
has_key, keys(), values(), for k in keys() Also, constant-time access to elements. (fixed memory S = input.readlines() - read the whole file into a list of
locations) strings.
Typically, you’ll insert/delete with:
Tuples are also very useful as keys for dictionaries. Unless the file is really huge, it’s fastest to read it all
d3[’spam’] = ’delicious!’
in at once with read() or readlines()
del d3[’spam’]
Department of Computer Science — University of San Francisco – p.12/?? Department of Computer Science — University of San Francisco – p.13/?? Department of Computer Science — University of San Fran
Department of Computer Science — University of San Francisco – p.15/?? Department of Computer Science — University of San Francisco – p.16/?? Department of Computer Science — University of San Fran
Printing Conditionals Conditionals
We’ve already seen the basic print. The general format for an if statement is: Logical tests return 1 for true and 0 for false.
print “hello world” if <test1> : “True” and “False” are shorthand
<statement1>
To use a formatting string, do: <statement2> and, or, not are available for compound tests
print “hello %s” % “bob” elif: <test2> :
<statement3>
print "%s %s" % ("hello" , "world") else:
<statementn>
To suppress the linefeed, include a ,
Notice the colons after the conditionals.
Compound statements consist of the colon, followed by
an indented block.
Department of Computer Science — University of San Francisco – p.18/?? Department of Computer Science — University of San Francisco – p.19/?? Department of Computer Science — University of San Fran
Department of Computer Science — University of San Francisco – p.21/?? Department of Computer Science — University of San Francisco – p.22/?? Department of Computer Science — University of San Fran
Functions Functions Basic Python advice
def is used to define a function. Multiple values can be returned in a tuple. Let the language do the work for you
return returns a value We can also provide default arguments. e.g.: if x in [1,2,3,4] rather than an an explicit loop
Functions maintain local scope Functions can be called with keywords. this is both faster and more readable
Names are resolved locally, then globally, then built-in myfunc(spam=1, eggs=2) Use a Python-aware editor/IDE
*args can be used to catch arbitrary argument lists and Take advantage of the built-in modules whenever
store them in a tuple. possible.
**args can be used to catch arbitrary argument lists and Use the interpreter to help test your code
store them in a dictionary. All built-in modules are readable
Looking at these can give insights into how they
work, as well as how to write good Python code.
Department of Computer Science — University of San Francisco – p.24/?? Department of Computer Science — University of San Francisco – p.25/?? Department of Computer Science — University of San Fran