Tutorial Lecture for EE562 Artificial Intelligence for Engineers

Slides:



Advertisements
Similar presentations
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Advertisements

Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
1 Programming Languages and Paradigms Lisp Programming.
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
CSE (c) S. Tanimoto, 2007 Python Introduction 1 Introduction to Python for Artificial Intelligence Outline: Why Python? Interactive programming,
Structured programming
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
CSC 9010: Natural Language Processing
Recitation 1 Programming for Engineers in Python.
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
January 24, 2006And Now For Something Completely Different 1 “And Now For Something Completely Different” A Quick Tutorial of the Python Programming Language.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Python Tutorial Lecture for EE562 Artificial Intelligence for Engineers 1.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Python Functions.
Python Conditionals chapter 5
An Introduction. What is Python? Interpreted language Created by Guido Van Rossum – early 90s Named after Monty Python
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Scope, Aliasing, Tuples & Mutability Intro2CS – week 4a 1.
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
1 Objective Caml (Ocaml) Aaron Bloomfield CS 415 Fall 2005.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Strings CSE 1310 – Introduction to Computers and Programming Alexandra Stefan University of Texas at Arlington 1.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
CS314 – Section 5 Recitation 9
NWEN241 – Systems Programming
Functional Programming
CSc 120 Introduction to Computer Programing II Adapted from slides by
ML: a quasi-functional language with strong typing
Introduction to Python
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
CSC 458– Predictive Analytics I, Fall 2017, Intro. To Python
Computer Programming Fundamentals
Introduction to Python
Representation, Syntax, Paradigms, Types
CS 326 Programming Languages, Concepts and Implementation
A lightening tour in 45 minutes
CSE 3302 Programming Languages
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language. It was created by Guido van Rossum during.
Functions CIS 40 – Introduction to Programming in Python
Introduction to Python
Data Analysis using Python-I
CHAPTER FOUR Functions.
Ruth Anderson University of Washington CSE 160 Spring 2018
Introduction to Python
Guide to Programming with Python
Representation, Syntax, Paradigms, Types
CSC 458– Predictive Analytics I, Fall 2018, Intro. To Python
Margaret Derrington KCL Easter 2014
Python Tutorial for C Programmer Boontee Kruatrachue Kritawan Siriboon
Representation, Syntax, Paradigms, Types
Python I Some material adapted from Upenn cmpe391 slides and other sources.
Homework Any Questions?.
G. Pullaiah College of Engineering and Technology
(more) Python.
CISC101 Reminders All assignments are now posted.
Representation, Syntax, Paradigms, Types
CISC101 Reminders Assignment 2 due today.
Python Review
Python Basics. Topics Features How does Python work Basic Features I/O in Python Operators Control Statements Function/Scope of variables OOP Concepts.
Functions John R. Woodward.
Presentation transcript:

Tutorial Lecture for EE562 Artificial Intelligence for Engineers Python Tutorial Lecture for EE562 Artificial Intelligence for Engineers

Why Python for AI? For many years, we used Lisp, because it handled lists and trees really well, had garbage collection, and didn’t require type declarations. Lisp and its variants finally went out of vogue, and for a while, we allowed any old language, usually Java or C++. This did not work well. The programs were big and more difficult to write. A few years ago, the AI faculty started converting to Python. It has the object-oriented capabilities of Java and C++ with the simplicity for working with list and tree structures that Lisp had with a pretty nice, easy-to-use syntax. I learned it with very little work.

Getting Started Download and install Python 2.7 from www.python.org on your computer. They have both 2.7 and 3.2. We will use 2.7, as it is fine for AI search programs. Read “Python as a Second Language,” a tutorial that Prof. Tanimoto wrote for CSE 415 students: https://courses.cs.washington.edu/courses/cs e415/12au/materials/Tanimoto-PSL.pdf

Python Data Types int 105 float 3.14159 str “Selection:”, ‘a string’ bool True, False list [‘apple’, ‘banana’, ‘orange’] tuple (3.2, 4.5, 6.3) dict {‘one’: 1, ‘two’: 2} function lambda x:2*x builtin_function_ math.sqrt or_method

Interacting with Python Python 2.7.5 (default, Nov 12 2013, 16:18:42) [GCC 4.8.2 20131017 (Red Hat 4.8.2-1)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> 5 + 7 12 >>> x = 5 + 7 >>> x >>> print('x = '+str(x)) x = 12 >>> x = 'apple' >>> x + x 'appleapple' >>> print('x is an '+x) x is an apple

Defining Functions >>> def sqr(x): ... return x*x 25 >>> sqr(75) 5625 >>> sqr(3.14) 9.8596 >>> sqr('notanumber') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in sqr TypeError: can't multiply sequence by non-int of type 'str' You have to indent the lines of the function Nice trace for execution errors.

Defining a Recursive Function >>> def factorial(n): ... if n < 1: ... return 0 ... if n == 1: ... return 1 ... return n * factorial(n-1) ... >>> >>> factorial(3) 6 >>> factorial(10) 3628800 >>> factorial(-1) Bad Version: >>>def fact(n): if n==1: return 1 else: return n * fact(n-1) File "<stdin>", line 5, in fact ... RuntimeError: maximum recursion depth exceeded

Scopes of Bindings: In general, declare global variables to save worry, required if you change them. Global y not needed here and we have two different z’s. >>> x = 5 >>> y = 6 >>> z = 7 >>> def fee(x): ... z = x + y ... return z ... >>> r = fee(2) >>> r 8 Global y used here to change y inside the function. >>> def foo(x): ... global y ... z = x + y ... y = y + 1 ... return z ... >>> q = foo(2) >>> q 8 >>> y 7

Lists We use lists heavily in AI. Lisp lists had two parts: car (the head or first element of the list) cdr (the tail or remainder of the list) Python is MUCH more versatile. Lists are like arrays in that you can refer to any element and yet you can also work with the head and tail and much more.

Lists How do you insert at the beginning? >>> mylist = ['a', 'b', 'c'] >>> mylist[0] 'a‘ >>> mylist[1] 'b' >>> mylist[1:] ['b', 'c'] >>> mylist[2:] ['c'] >>> mylist[-1] 'c‘ >>> mylist.insert(3,'d') >>> mylist ['a', 'b', 'c', 'd'] car (or head) cdr (or tail) append How do you insert at the beginning?

Slices of Lists >>> mylist ['a', 'b', 'c', 'd'] >>> len(mylist) 4 >>> mylist[0:len(mylist)] >>> mylist[0:len(mylist):2] ['a', 'c'] >>> mylist[::-1] ['d', 'c', 'b', 'a'] >>> mylist[1:] ? go through mylist by ones go through mylist by twos go through mylist in reverse

Iterating through Lists >>> for e in mylist: ... print('element is '+e) ... element is a element is b element is c element is d >>> count = 0 >>> while count < len(mylist): ... print(mylist[count]) ... count += 1 ... a b c d

Strings Strings work a lot like lists! >>> mystring = 'abcd' 'dcba'

Dictionaries Dictionaries give us look-up table capabilities. >>> translate = {} >>> translate['I'] = 'Ich' >>> translate['go'] = 'gehe' >>> translate['to'] = 'zu' >>> translate['doctor'] = 'Artz' >>> translate['the'] = 'der' >>> print(translate['I']) Ich How can we print the translation of I go to the doctor? Is it correct German?

Functional Programming Functions can be values that are assigned to variables or put in lists. They can be arguments to or returned by functions. They can be created dynamically at run time and applied to arguments. They don’t have to have names. This is like the lambda capability of Lisp

Example of Function Creation >>> def make_adder(y): ... return lambda x: x + y ... >>> f4 = make_adder(4) >>> f4(5) 9 >>> f7 = make_adder(7) >>> f7(5) 12 This is actually pretty tame. One can construct strings and make them into functions, too. What does this mean?

Object-Oriented Programming Unlike Lisp, Python is an object-oriented language, so you can program much as you did in Java. class Coord: "2D Point Coordinates" def __init__(self, x=0, y=0): self.x = x self.y = y # def describe(self): return '('+str(self.x)+','+str(self.y)+')' def euclid(self,p2): return ((self.x-p2.x)**2+(self.y-p2.y)**2)**0.5

Using the Coord Object >>> p1 = Coord(3,5) >>> p1.describe() '(3,5)' >>> p2.describe() '(2,7)' >>> p1.euclid(p2) 2.23606797749979 >>> p2.euclid(p1)

Writing Methods Write a method to add together two points and return class Coord: "2D Point Coordinates" def __init__(self, x=0, y=0): self.x = x self.y = y Write a method to add together two points and return a new point p3 = the sum of them def add(self, p2):

Main Program with Command Line Arguments import sys def add(a, b): return a+b # if __name__=='__main__': first = int(sys.argv[1]) second = int(sys.argv[2]) sum = add(first, second) print(str(sum)) python try.py 4 5 9 stored in try.py