CSE 415 -- (c) S. Tanimoto, 2007 Python Introduction 1 Introduction to Python for Artificial Intelligence Outline: Why Python? Interactive programming,

Slides:



Advertisements
Similar presentations
Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Advertisements

Introduction to Python Week 15. Try It Out! Download Python from Any version will do for this class – By and large they are all mutually.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
Lists Introduction to Computing Science and Programming I.
Chapter 2 Writing Simple Programs
Lecture 5 Arrays A way to organize data © MIT AITI 2003.
Functions in Python. The indentation matters… First line with less indentation is considered to be outside of the function definition. Defining Functions.
Introduction to Python: Slides Referenced in Homework 0 CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth January 2005.
Introduction to Python Guido van Rossum Director of PythonLabs at Zope Corporation
January 24, 2006And Now For Something Completely Different 1 “And Now For Something Completely Different” A Quick Tutorial of the Python Programming Language.
COMPUTER SCIENCE FEBRUARY 2011 Lists in Python. Introduction to Lists Lists (aka arrays): an ordered set of elements  A compound data type, like strings.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
CS1022 Computer Programming & Principles Lecture 1.2 A brief introduction to Python.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Python Tutorial Lecture for EE562 Artificial Intelligence for Engineers 1.
1 COMP313A Functional Programming (1). 2 Main Differences with Imperative Languages Say more about what is computed as opposed to how Pure functional.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
Introduction to Python I CSE-391: Artificial Intelligence University of Pennsylvania Matt Huenerfauth January 2005.
Computer Science 101 Introduction to Programming.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
An Introduction to Python Blake Brogdon. What is Python?  Python is an interpreted, interactive, object-oriented programming language. (from python.org)
Classes and Objects The basics. Object-oriented programming Python is an object-oriented programming language, which means that it provides features that.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 5.
Built-in Data Structures in Python An Introduction.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
Exam 1 Review Instructor – Gokcen Cilingir Cpt S 111, Sections 6-7 (Sept 19, 2011) Washington State University.
Python I Some material adapted from Upenn cmpe391 slides and other sources.
Prof. Hilfinger CS164 Lecture 51 The Pyth Language Lecture 5.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
PYTHON PROGRAMMING. WHAT IS PYTHON?  Python is a high-level language.  Interpreted  Object oriented (use of classes and objects)  Standard library.
Chapter 2 Writing Simple Programs
Functional Programming
Introduction to Python
CS 326 Programming Languages, Concepts and Implementation
Tutorial Lecture for EE562 Artificial Intelligence for Engineers
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Introduction to Functional Programming in Racket
CHAPTER FOUR Functions.
Python Primer 2: Functions and Control Flow
Bryan Burlingame 03 October 2018
Data types Numeric types Sequence types float int bool list str
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Abstraction and Repetition
More elements of Python programs
Python I Some material adapted from Upenn cmpe391 slides and other sources.
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Introduction to Functional Programming in Racket
Defining Functions with DEFUN
Abstraction and Repetition
Introduction to Strings
Functional Programming and Haskell
Functions Functions being objects means functions can be passed into other functions: sort (list, key=str.upper)
Chapter 15 Functional Programming 6/1/2019.
def-ining a function A function as an execution control structure
 A function is a named sequence of statement(s) that performs a computation. It contains  line of code(s) that are executed sequentially from top.
Python Basics. Topics Features How does Python work Basic Features I/O in Python Operators Control Statements Function/Scope of variables OOP Concepts.
Abstraction and Repetition
Functional Programming and Haskell
Presentation transcript:

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

CSE (c) S. Tanimoto, 2007 Python Introduction 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 (c) S. Tanimoto, 2007 Python Introduction 3 Getting Started Download and install Python and IDLE from 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 (c) S. Tanimoto, 2007 Python Introduction 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 (c) S. Tanimoto, 2007 Python Introduction 5 Data Types int float str list bool tuple classobj instance

CSE (c) S. Tanimoto, 2007 Python Introduction 6 Defining Functions def sqr(x): return x*x sqr(5) 25 sqr(25) 625 sqr(1.5) 2.25

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

CSE (c) S. Tanimoto, 2007 Python Introduction 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 (c) S. Tanimoto, 2007 Python Introduction 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 (c) S. Tanimoto, 2007 Python Introduction 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 (c) S. Tanimoto, 2007 Python Introduction 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 (c) S. Tanimoto, 2007 Python Introduction 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 (c) S. Tanimoto, 2007 Python Introduction 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 (c) S. Tanimoto, 2007 Python Introduction 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 (c) S. Tanimoto, 2007 Python Introduction 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 (c) S. Tanimoto, 2007 Python Introduction 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 (c) S. Tanimoto, 2007 Python Introduction 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