1 / 17

Introduction to Python for Artificial Intelligence

Introduction to Python for Artificial Intelligence. Outline: Why Python? Interactive programming, & calculator style Data types Syntax Function definition Functional programming String processing. Why Python?. Python supports features needed in AI programming.

ianna
Download Presentation

Introduction to Python for Artificial Intelligence

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Pythonfor Artificial Intelligence • Outline: • Why Python? • Interactive programming, & calculator style • Data types • Syntax • Function definition • Functional programming • String processing CSE 415 -- (c) S. Tanimoto, 2008 Python 1

  2. Why Python? Python supports features needed in AI programming. Python is increasingly popular, and high-quality, free tools are readily available. Python’s syntax means its code tends to be more readable than code in other languages. Python has some advantages over Lisp in string processing and user-interface construction. CSE 415 -- (c) S. Tanimoto, 2008 Python 1

  3. Getting Started • Download and install Python and IDLE from www.python.org on your own computer. • Read Chapters 1-4 of the online ReadyNote “Introduction to Python for Artificial Intelligence.” • Browse some of the online Python resources, such as G. van Rossum’s tutorial. • Work on Assignment 1. CSE 415 -- (c) S. Tanimoto, 2008 Python 1

  4. Interactive Programming in IDLE’s Python Shell • The Python Shell implements a “Read-Eval-Print loop”. • Calculator-style interaction means typing in simple expressions involving +, -, *, and / with numbers. • Assignment (binding) is available • Bindings get global scope by default, when entered at the prompt • The IDLE shell features automatic copying of any line down to the prompt line. • IDLE analyzes Python code as you type, and color-codes it as best it can. CSE 415 -- (c) S. Tanimoto, 2008 Python 1

  5. Data Types • int • float • str • list • bool • tuple • classobj • instance CSE 415 -- (c) S. Tanimoto, 2008 Python 1

  6. Defining Functions def sqr(x): return x*x sqr(5) 25 sqr(25) 625 sqr(1.5) 2.25 CSE 415 -- (c) S. Tanimoto, 2008 Python 1

  7. Defining Recursive Functions def fact(n): if n==1: return 1 else: return n * fact(n-1) fact(5) 120 CSE 415 -- (c) S. Tanimoto, 2008 Python 1

  8. Functions with Optional Arguments def make_greeting(first_name='John', last_name='Doe'): '''This function takes 0, 1, or 2 arguments, and it constructs a string that represents a greeting. It uses default values for any arguments not specified. ''' return 'Hello '+first_name+' '+last_name+'!' print make_greeting() Hello John Doe! CSE 415 -- (c) S. Tanimoto, 2008 Python 1

  9. Functions with Optional Arguments print make_greeting() print make_greeting('Donna') print make_greeting('Molly', 'Smith') print make_greeting(last_name='Hancock') Hello John Doe! Hello Donna Doe! Hello Molly Smith! Hello John Hancock! CSE 415 -- (c) S. Tanimoto, 2008 Python 1

  10. Scopes of Bindings x = 5 y = 6 z = 7 def foo(x): global y z = x + y return z w = foo(1) w x y z CSE 415 -- (c) S. Tanimoto, 2008 Python 1

  11. Lists • Lists are sequences of elements separated by commas and surrounded by square brackets. • [‘a’, ‘b’, ‘c’] • [0, 1, 2] • [‘testing’, 1, 2, 3] • [ ] CSE 415 -- (c) S. Tanimoto, 2008 Python 1

  12. List Elements • List elements may be extracted with an index in square brackets. >>> L = [‘a’, ‘b’, ‘c’] >>> L[0] ‘a’ >>> L[1] ‘b’ >>> L[2] ‘c’ >>> L[-1] ‘c’ • Lists are “zero-based”; indices start at 0. Negative indices work right-to-left. CSE 415 -- (c) S. Tanimoto, 2008 Python 1

  13. Slices • Sublists are expressed using “slice” notation. >>> L2 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’] >>> L2[2:4] [‘c’, ‘d’] >>> L[1:] [‘b’, ‘c’, ‘d’, ‘e’] >>> L[:3] [‘a’, ‘b’, ‘c’] >>> L[:] Slices are copies of their original lists or sublists. CSE 415 -- (c) S. Tanimoto, 2008 Python 1

  14. Functional Programming • Functions can be values, I.e., assigned to variables, elements of lists, etc. • Functions can be arguments to other functions and returned by functions. • Functions can be synthesized at run-time. • Functions can be explicitly applied to arguments. • Functions do not have to have names. • Functions tend, but don’t have to be, “pure,” meaning without side effects and producing values that depend only upon the values of their parameters (“referentially transparent”). CSE 415 -- (c) S. Tanimoto, 2008 Python 1

  15. Anonymous Functions >>> f = lambda x: x+5 >>> f(4) 9 >>> map(f, [0, 1, 2, 3, 4]) [5, 6, 7, 8, 9] >>> map(lambda x: x*7, [0, 1, 2, 3, 4]) [0, 7, 14, 21, 28] CSE 415 -- (c) S. Tanimoto, 2008 Python 1

  16. Runtime Creation of Functions >>> def make_adder(y) return lambda(x) : x + y >>> f4 = make_adder(4) >>> f4(5) 9 >>> f7 = make_adder(7) >>> f7(11) 18 CSE 415 -- (c) S. Tanimoto, 2008 Python 1

  17. Another Example def make_quadratic_evaluator(a, b, c): return lambda(x): a*x*x + b*x + c fsqr = make_quadratic_evaluator(1,0,0) print fsqr(5) 25 CSE 415 -- (c) S. Tanimoto, 2008 Python 1

More Related