


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of python, its uses, and its implementation, as well as its basic features such as numbers, mathematical operators, strings, lists, dictionaries, tuples, and files. It also covers python's program structure and the ways to run a python program.
Typology: Study notes
1 / 26
This page cannot be seen from the preview
Don't miss anything!



















Department of Computer Science — University of San Francisco – p.1/
Python is:^ High-level^ Object-oriented^ Free, open-source^ Dynamically typed^ Has a large collection of utility libraries^ garbage-collected^ Mixable - works nicely as a “glue” language^ Easy to learn/use
Department of Computer Science — University of San Francisco – p.2/
There are four ways to run a Python program:^ Interactively^ From the command line^ As a script^ From another program
Department of Computer Science — University of San Francisco – p.4/
/usr/bin/python on all unix machines IDLE and PythonWin on Windows MacPython on Macintosh Also for Amiga, PalmOS, BeOS, etc.
Department of Computer Science — University of San Francisco – p.5/
Python has a nice set of built-in data types^ Numbers^ Strings^ Lists^ Dictionaries^ Files Using built-in types makes your code easier towrite/maintain, more portable, and more efficient.
Department of Computer Science — University of San Francisco – p.7/
Numbers work like we’d expect. There are integers, equivalent to longs in C. (1, -31311,4000) There are^ long integers
, which are of unlimited size (31111L, 12345l) There are floats, equivalent to doubles in C.1.23, 3.1e+5 There are Octal and Hexadecimal representations as in C.(0155, 0x3af5) There are complex numbers, as in Lisp, Matlab, etc.(3.0+4j)
Department of Computer Science — University of San Francisco – p.8/
One of Python’s strong suits is its ability to work withstrings. Strings are denoted with double quotes, as in C. s1 + s2 - concatenation s1 * 3 - repitition s1[i] - indexing, s1[i:j] - slicing s1[-1] - last character “a % parrot” % “dead” - formatting for char in s1 - iteration
Department of Computer Science — University of San Francisco – p.10/
Strings are immutable sequences - to change them, weneed to make a copy.^ Can’t do: s1[3] = ’c’^ Must do: s2 = s1[0:2] + ’c’ + s1[3:]
Department of Computer Science — University of San Francisco – p.11/
A Dictionary is a Python hash table (or associative list) Unordered collections of arbitrary objects. d1 = {} - new hashtable d2 = {’spam’ : 2, ’eggs’, 3} Can index by key d2[’spam’] Can have nested Hashtables^ d3 = {’spam’ : 1, ’other’ :{’eggs’ :2, ’spam’ : 3}}^ d3[’other’][’spam’] has_key, keys(), values(), for k in keys() Typically, you’ll insert/delete with:^ d3[’spam’] = ’delicious!’^ del d3[’spam’]
Department of Computer Science — University of San Francisco – p.13/
Tuples are like immutable lists. Nice for dealing with enumerated types. Can be nested and indexed. t1 = (1,2,3), t2 = (1,2,(3,4,5)) Can index, slice, length, just like lists.^ t1[3], t1[1:2], t1[-2] Tuples are mostly useful when you want to have a list of apredetermined size/length.
Department of Computer Science — University of San Francisco – p.14/
outfile.write(S) - write the string S into the file. outfile.writelines(L) - write the list of strings L into the file. outfile.close() (this is also done by the garbage collector)
Department of Computer Science — University of San Francisco – p.16/
Python uses dynamic typing.^ No need to pre-define variables. Variables are instantiated by assigning values to them^ Referencing a variable before assignment is an error You can assign multiple variables simultaneously spam^ =^4 eggs^ =^5 spam,^ eggs^ = eggs,
spam spam,^ eggs^ = 4,
Department of Computer Science — University of San Francisco – p.17/
We’ve already seen the basic print.^ print “hello world” To use a formatting string, do:^ print “hello %s” % “bob”^ print "%s %s" % ("hello" , "world") To suppress the linefeed, include a ,
Department of Computer Science — University of San Francisco – p.19/
The general format for an if statement is: if^
:
Department of Computer Science — University of San Francisco – p.20/