CS 330 - Artificial Intelligence
- Python in one week I
Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Fall 2019 1
Materials adopted from Kaggle
CS 330 - Artificial Intelligence - Python in one week I Instructor: - - PowerPoint PPT Presentation
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
Materials adopted from Kaggle
2
https://www.plu.edu/communication/life-under-drones/ Panel: “Horizons in Emerging Tech: Drone Innovations in Engineering, Medicine, Library Science, and the Humanities”
Extra course participation credit - Providing pictures or writing summaries on Sakai!
5
6
#javac HelloWorld.java #java HelloWorld #python Hello.py
7
8
#python #print 1+1 Create hi.py, and write statement: print 1+1 #python hi.py
9
10
11
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) Read the following, think and share output with your neighbors (2 mins)
12
(1 min) What is different from other languages (JAVA?). Think and share with your neighbors
13
14
15
16
Try it: #python #spam_amount = 0 #type(spam_amount) Try it: #python #spam_amount = 0.0 #type(spam_amount)
17
Arithmetic
18
print(5 / 2) print(6 / 2) print(5 // 2) print(6 // 2)
19
8 - 3 + 2
s1 = 5 s2 = 11 # now I want average score aveS = s1 + s2/2 print(aveS) aveS = (s1 + s2)/2 print(aveS)
20
print(min(1, 2, 3)) print(max(1, 2, 3)) how about more numbers? print(abs(32)) print(abs(-32)) print(float(10)) print(int(3.33)) # They can even be called on strings! print(int('807') + 1)
22
23
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”)
24
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
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.")
25
26
def least_difference(a, b, c): diff1 = abs(a - b) diff2 = abs(b - c) diff3 = abs(a - c) return min(diff1, diff2, diff3)
27
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? )
28
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
30
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) print( least_difference(1, 10, 100), least_difference(1, 10, 10), least_difference(5, 6, 7), )
31
32
print( type(x), type(f), sep='\n' ) print(x) print(f)
33
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
35
def mod_5(x): """Return the remainder of x after dividing by 5""" return x % 5 print( 'Which number is biggest?', max(100, 51, 14), 'Which number is the biggest modulo 5?', max(100, 51, 14, key=mod_5), sep='\n', ) Which number is biggest? 100 Which number is the biggest modulo 5? 14
36
def mod_5(x): """Return the remainder of x after dividing by 5""" return x % 5 mod_5 = lambda x: x % 5 # Lambdas can take multiple comma-separated arguments abs_diff = lambda a, b: abs(a-b) print("Absolute difference of 5 and 7 is", abs_diff(5, 7)) # Or no arguments always_32 = lambda: 32 always_32()
37
# Preview of lists and strings. (We'll go in depth into both soon) # - len: return the length of a sequence (such as a string or list) # - sorted: return a sorted version of the given sequence (optional key # function works similarly to max and min) # - s.lower() : return a lowercase version of string s names = ['jacques', 'Ty', 'Mia', 'pui-wa'] print("Longest name is:", max(names, key=lambda name: len(name))) # or just key=len print("Names sorted case insensitive:", sorted(names, key=lambda name: name.lower()))
39
40
x = True print(x) print(type(x))
41
42
def can_run_for_president(age): """Can someone of the given age run for president in the US?""" # The US Constitution says you must "have attained to the Age of thirty-five Years" return age >= 35 print("Can a 19-year-old run for president?", can_run_for_president(19)) print("Can a 45-year-old run for president?", can_run_for_president(45))
43
3.0 == 3
‘3’ == 3 True False
44
def is_odd(n): return (n % 2) == 1 print("Is 100 odd?", is_odd(100)) print("Is -1 odd?", is_odd(-1))
45
def can_run_for_president(age, is_natural_born_citizen): """Can someone of the given age and citizenship status run for president in the US?""" # The US Constitution says you must be a natural born citizen *and* at least 35 years old return is_natural_born_citizen and (age >= 35) print(can_run_for_president(19, True)) print(can_run_for_president(55, False)) print(can_run_for_president(55, True))
46
True or True and False Python has precedence rules that determine the order in which operations get evaluated in expressions like above. For example, and has a higher precedence than or, which is why the first expression above is True.
47
Bug code: prepared_for_weather = have_umbrella or rain_level < 5 and have_hood or not rain_level > 0 and is_workday Good code: prepared_for_weather = have_umbrella or (rain_level < 5 and have_hood)
Alternative way: prepared_for_weather = ( have_umbrella
)
48
def inspect(x): if x == 0: print(x, "is zero") elif x > 0: print(x, "is positive") elif x < 0: print(x, "is negative") else: print(x, "is unlike anything I've ever seen...") inspect(0) inspect(-15)
49
def f(x): if x > 0: print("Only printed when x is positive; x =", x) print("Also only printed when x is positive; x =", x) print("Always printed, regardless of x's value; x =", x) f(1) f(0)
50
print(bool(1)) # all numbers are treated as true, except 0 print(bool(0)) print(bool("asf")) # all strings are treated as true, except the empty string "" print(bool("")) # Generally empty sequences (strings, lists, and other types we've yet to see like lists and tuples) # are "falsey" and the rest are "truthy"
51
if 0: print(0) elif "spam": print("spam")
52
def quiz_message(grade): if grade < 50:
else:
print('You', outcome, 'the quiz with a grade of', grade) quiz_message(80) def quiz_message(grade):
print('You', outcome, 'the quiz with a grade of', grade) quiz_message(45)