Download Introduction to USF's Department of Computer Science's AI Course in Python Programming - P and more Study notes Computer Science in PDF only on Docsity! Artificial Intelligence Programming Intro to Python Chris Brooks Department of Computer Science University of San Francisco 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.1/?? Some uses of Python Things that Python is good at: System utilities Python has hooks into standard OS-level services, such as files, processes, pipes, etc. GUIs Python has interfaces to Tk, Qt, and WxPython Embedded integration Python has hooks that allow it to call/be called by C and C++ code, and also COM interfaces. Rapid Interactive Development Like Lisp, Python makes it easy to quickly put together a working program. Scripting Python has utilities for CGI, Database access, HTTP, Sockets, FTP, POP/SMTP, XML parsing, etc. Department of Computer Science — University of San Francisco 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.3/?? Python Implementations /usr/bin/python on all unix machines and on OS X IDLE and PythonWin on Windows MacPython on Macintosh Also for Amiga, PalmOS, BeOS, etc. BoaConstructor is an open-source Python IDE Eclipse has a Python plugin (PyDev) Department of Computer Science — University of San Francisco – p.4/?? Python Program Structure Programs consist of modules A module corresponds to a source file. Modules contain blocks of statements Statements create and process objects. Department of Computer Science — University of San Francisco Basic Python Python has a nice set of built-in data types Numbers Strings Lists Sets Dictionaries Files Using built-in types makes your code easier to write/maintain, more portable, and more efficient. Department of Computer Science — University of San Francisco – p.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 or Java. 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.7/?? Mathematical Operators Python has all the usual mathematical operators +,=,*,/, % «, » ** or pow for exponentiation abs, rand, |, & This makes Python a very handy desk calculator. Operations are coerced to the most specific type. 3 + (4.0 / 2) will produce a float. Common error: since variables do not have declared types, be careful of rounding: 3 /2 == 1 !! Department of Computer Science — University of San Francisco Strings One of Python’s strong suits is its ability to work with strings. Strings are denoted with double quotes, as in C, or single quotes s1 + s2 - concatenation s1 * 3 - repetition 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.9/?? Strings Strings are immutable sequences - to change them, we need to make a copy. Can’t do: s1[3] = ’c’ Must do: s2 = s1[0:2] + ’c’ + s1[3:] As in Java, making lots of copies can be very inefficient. If you need to do lots of concatenation, use join instead. We’ll return to efficiency issues throughout the semester. Department of Computer Science — University of San Francisco – p.10/?? Lists Python has a flexible and powerful list structure. Lists are mutable sequences - can be changed in place. Denoted with square brackets. l1 = [1,2,3,4] Can create nested sublists. l2 = [1,2, [3,4, [5], 6], 7] l1 + l2 - concatenation. l1 * 4 - repetition l1[3:5], l1[:3], l1[5:] - slices append, extend, sort, reverse built in. Range - create a list of integers Department of Computer Science — University of San Francisco