Introduction to USF's AI Program: Python Programming - Prof. Christopher H. Brooks, Study notes of Computer Science

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

Pre 2010

Uploaded on 07/30/2009

koofers-user-e54-1
koofers-user-e54-1 🇺🇸

9 documents

1 / 26

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Artificial Intelligence Programming
Python Intro
Chris Brooks
Department of Computer Science
University of San Francisco
Department of Computer Science University of San Francisco p.1/26
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Introduction to USF's AI Program: Python Programming - Prof. Christopher H. Brooks and more Study notes Computer Science in PDF only on Docsity!

Artificial Intelligence Programming

Python Intro^ Chris BrooksDepartment of Computer ScienceUniversity of San Francisco

Department of Computer Science — University of San Francisco – p.1/

2b-0:^ What is Python?

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/

2b-2:^ Invoking Python Programs

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/

2b-3:^ Python Implementations

/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/

2b-5:^ Basic Python

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/

2b-6:^ Numbers

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/

2b-8:^ Strings

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/

2b-9:^ Strings

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/

2b-11:^ Dictionaries

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/

2b-12:^ Tuples

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/

2b-14:^ Files

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/

2b-15:^ Basic Python statements

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/

2b-17:^ Printing

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/

2b-18:^ Conditionals

The general format for an if statement is: if^ ^ : elif:^ ^

: else: Notice the colons after the conditionals. Compound statements consist of the colon, followed by anindented block.

Department of Computer Science — University of San Francisco – p.20/