1 CS 330 - Artificial Intelligence - Python in one week I Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Fall 2019 Materials adopted from Kaggle
Announcement • Course website login • Finish course survey by today (especially your interested topic and programming experience) • Read materials on course website • Progress on topics for final project? Final report on Archive? • Python programming and industry experience of learning new programming language in a week 2
• 10:30 am • Monday • Sept 16 • Morken 132
https://www.plu.edu/communication/life-under-drones/ Panel: “Horizons in Emerging Tech: Drone Innovations in Engineering, Medicine, Library Science, and the Humanities” on Thursday 9.19 from 9 - 9:50 am Extra course participation credit - Providing pictures or writing summaries on Sakai!
Overview • Learning Python in a week • Python VS JAVA 5
Hello World example #javac HelloWorld.java #java HelloWorld #python Hello.py 6
Two different kind of high-level languages • Interpreters • Compilers 7
Python introduction Command mode VS script mode #python #print 1+1 Create hi.py, and write statement: print 1+1 #python hi.py 8
Python introduction • syntax, variable assignment, numbers • functions and getting help • booleans and conditionals • lists and objects • loops and list comprehensions • strings and dictionaries • files and trees 9
Python introduction • syntax, variable assignment, numbers • functions and getting help • booleans and conditionals • lists and objects • loops and list comprehensions • strings and dictionaries • files and trees 10
Syntax, variable assignment, numbers • Python was named for the British comedy troupe Monty Python (https://en.wikipedia.org/wiki/Monty_Python) Read the following, think and share output with your neighbors (2 mins) spam_amount = 0 print(spam_amount) # Ordering Spam, egg, Spam, Spam, bacon and Spam (4 more servings of Spam) spam_amount = spam_amount + 4 if spam_amount > 0: print("But I don't want ANY spam!") viking_song = "Spam " * spam_amount print(viking_song) 11
Syntax, variable assignment, numbers • spam_amount = 0 variable assignment!!! (1 min) What is different from other languages (JAVA?). Think and share with your neighbors • No variable “declare” • No variable type 12
Syntax, variable assignment, numbers • print(spam_amount) function call!!! • # Ordering Spam, egg, Spam, Spam, bacon and Spam (4 more servings of Spam) spam_amount = spam_amount + 4 Re-assignment!!! 13
Syntax, variable assignment, numbers • if spam_amount > 0: print("But I don't want ANY spam!") What is the output? What is different from JAVA? • Colon (:) • Indented (4 spaces), no curly brace 14
Syntax, variable assignment, numbers • viking_song = "Spam " * spam_amount • print(viking_song) What is the output? 15
Numbers and arithmetic in Python • spam_amount = 0 numbers and arithmetic Try it: Try it: #python #python #spam_amount = 0.0 #spam_amount = 0 #type(spam_amount) #type(spam_amount) 16
Numbers and arithmetic in Python Arithmetic 17
Numbers and arithmetic in Python • True division and floor division Think about the different from JAVA? print(5 / 2) print(5 // 2) print(6 / 2) print(6 // 2) 18
Numbers and arithmetic in Python • Order of operations PEMDAS - P arentheses, E xponents, M ultiplication/ D ivision, A ddition/ S ubtraction s1 = 5 8 - 3 + 2 s2 = 11 # now I want average score -3 + 4 * 2 aveS = s1 + s2/2 print(aveS) aveS = (s1 + s2)/2 print(aveS) 19
Numbers and arithmetic in Python • Builtin functions for working with numbers min, max, abs, int, float print(min(1, 2, 3)) print(abs(32)) print(max(1, 2, 3)) print(abs(-32)) how about more numbers? print(float(10)) print(int(3.33)) # They can even be called on strings! print(int('807') + 1) 20
Exercise • Try: CS330_ex1.py
Python introduction • syntax, variable assignment, numbers • functions and getting help • booleans and conditionals • lists and objects • loops and list comprehensions • strings and dictionaries • files and trees 22
Functions and getting help functions: calling them, defining them, and looking them up using Python's built-in documentation. Think: how we define function in JAVA? In JAVA, functions take a specific number of arguments, each having a particular type: public String print(String s) { System.out.println(s); } Call function: print(“hello”) 23
Functions and getting help Python functions are allowed much more flexibility print("The print function takes an input and prints it to the screen.") print("Each call to print starts on a new line.") print("You'll often call print with strings, but you can pass any kind of value. For example, a number:") print(2 + 2) print("If print is called with multiple arguments...", "it joins them", "(with spaces in between)", "before printing.") print('But', 'this', 'is', 'configurable', sep='!...') print() print("^^^ print can also be called with no arguments to print a blank line.") 24
Functions and getting help If you forgot what a function is doing, what to do? For example, what abs is doing? In JAVA, you could read JAVA Doc. In Python, you could use help() function: help(abs) —— not help(abs(-2)) help(print) 25
Functions and getting help How to define a function in Python? def least_difference(a, b, c): diff1 = abs(a - b) diff2 = abs(b - c) diff3 = abs(a - c) return min(diff1, diff2, diff3) 26
Functions and getting help How to call function in Python? least_difference(1, 10, 100) print( least_difference(1, 10, 100), least_difference(1, 10, 10), least_difference(5, 6, 7), # Python allows trailing commas in argument lists. How nice is that? ) 27
Functions and getting help help(least_difference) # what do you find? Docstrings: def least_difference(a, b, c): """Return the smallest difference between any two numbers among a, b and c. >>> least_difference(1, 5, -5) 4 """ diff1 = abs(a - b) diff2 = abs(b - c) diff3 = abs(a - c) return min(diff1, diff2, diff3) 28
Functions and getting help now if you type: help(least_difference), you will find: Help on function least_difference in module __main__: least_difference(a, b, c) Return the smallest difference between any two numbers among a, b and c. >>> least_difference(1, 5, -5) 4 29
Functions and getting help What would happen if we didn't include the return keyword in our function? def least_difference(a, b, c): """Return the smallest difference between any two numbers among a, b and c. """ diff1 = abs(a - b) diff2 = abs(b - c) diff3 = abs(a - c) min(diff1, diff2, diff3) None None None print( The same: least_difference(1, 10, 100), least_difference(1, 10, 10), mystery = print() least_difference(5, 6, 7), print(mystery) ) 30
Functions and getting help Default arguments for function in Python: When we called help(print), we saw that the print function has several optional arguments. For example, we can specify a value for sep to put some special string in between our printed arguments: print(1, 2, 3, sep=' < ‘) and print(1, 2, 3) 31
Functions and getting help Adding optional arguments with default values to the functions in Python: def greet(who="Colin"): print("Hello,", who) greet() greet(who="Dr. Cao") # (In this case, we don't need to specify the name of the argument, because it's unambiguous.) greet("world") 32
Functions and getting help Functions are objects too: def f(n): return n * 2 x = 12.5 In this case, x refers to an object of type float, and f refers to an object of type: print( type(x), type(f), sep='\n' ) print(x) print(f) 33
Functions and getting help we can call the function we receive as an argument: def f(n): return n * 2 def call(fn, arg): """Call fn on arg""" return fn(arg) def squared_call(fn, arg): """Call fn on the result of calling fn on arg""" return fn(fn(arg)) print( call(f, 1), squared_call(f, 1), sep='\n', # '\n' is the newline character - it starts a new line ) 34
Functions and getting help By default, max returns the largest of its arguments. But if we pass in a function using the optional key argument, it returns the argument x that maximizes key(x) (aka the 'argmax'). def mod_5(x): """Return the remainder of x after dividing by 5""" return x % 5 print( Which number is biggest? 'Which number is biggest?', 100 max(100, 51, 14), Which number is the biggest 'Which number is the biggest modulo 5?', modulo 5? max(100, 51, 14, key=mod_5), 14 sep='\n', ) 35
Recommend
More recommend