Docsity

Introduction to Python - Lecture Slides | CS 662, Study notes of Computer Science

Toggle sidebar
bg1
Artificial Intelligence Programming
Intro to Python
Chris Brooks
Department of Computer Science
University of San Francisco
Departmentof Computer Science —University of San Francisco – p.1/??
2-2: 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
Departmentof Computer Science —University of San Francisco – p.2/??
2-3: 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.
Departmentof Computer Science —University of San Francisco – p.3/??
2-4: Invoking Python Programs
•There are four ways to run a Python program:
◦Interactively
◦From the command line
◦As a script
◦From another program
Departmentof Computer Science —University of San Francisco – p.4/??
2-5: 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
Departmentof Computer Science —University of San Francisco – p.5/??
2-6: Python Program Structure
•Programs consist of modules
◦A module corresponds to a source file.
•Modules contain blocks of statements
•Statements create and process objects.
Departmentof Computer Science —University of San Francisco – p.6/??
pf3
pf4
pf5

Partial preview of the text

Download Introduction to Python - Lecture Slides | CS 662 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 ?? Department of Computer Science — University of San Francisco – p.1/ What is Python? 2-2: 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/ Some uses of Python 2-3: 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 – p.3/ Invoking Python Programs 2-4: 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/ Python Implementations 2-5: /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 ◦ Python Program Structure 2-6: Programs consist of modules • A module corresponds to a source file. ◦ Modules contain blocks of statements • Statements create and process objects. •

Basic Python 2-7: Python has a nice set of built-in data types • Numbers ◦ Strings ◦ Lists ◦ 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.7/ Numbers 2-8: Numbers work like we’d expect. • There are integers, equivalent to longs in C. (1, -31311, 4000) • , which are of unlimited size (31111L, long integers There are • 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/ Mathematical Operators 2-9: 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. ◦ ?? Department of Computer Science — University of San Francisco – p.9/ Strings 2-10: One of Python’s strong suits is its ability to work with strings. • Strings are denoted with double quotes, as in C. • 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.10/ Strings 2-11: 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:] ◦ Lists 2-12: 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 •

Printing 2-19: 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/ Conditionals 2-20: 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 an • indented block. ?? Department of Computer Science — University of San Francisco – p.20/ Conditionals 2-21: Logical tests return 1 for true and 0 for false. • “True” and “False” are shorthand • and, or, not are available for compound tests • ?? Department of Computer Science — University of San Francisco – p.21/ Syntax 2-22: Tabs are used to delimit blocks. • Statements can cross multiple lines if: • They’re within delimiters ◦ You use a backslash ◦ ?? Department of Computer Science — University of San Francisco – p.22/ Iteration 2-23: Python has the familiar while loop. • One wrinkle: it has an optional else clause to execute if the test • is false. Additional loop control • break ◦ Exit the innermost loop without doing an else • continue ◦ Return to the beginning of the loop • pass ◦ Do nothing • For 2-24: For is a generic iterator in Python. • Lets you loop over any indexable data structure. • for item in [1,2,3,4,5] ◦ for char in “This is a string” ◦ for key in dictionary.keys() ◦ This provides a uniform (polymorphic) interface to a set of • different data structures.

Functions 2-25: def is used to define a function. • return returns a value • Functions maintain local scope • Names are resolved locally, then globally, then built-in • Functions 2-26: Multiple values can be returned in a tuple. • We can also provide default arguments. • Functions can be called with keywords. • myfunc(spam=1, eggs=2) ◦ *args can be used to catch arbitrary argument lists and store • them in a tuple. **args can be used to catch arbitrary argument lists and store • them in a dictionary.