CS 330 - Artificial Intelligence
- Wrap up Python Programming
CS 330 - Artificial Intelligence - Wrap up Python Programming - - PowerPoint PPT Presentation
1 CS 330 - Artificial Intelligence - Wrap up Python Programming Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Fall 2019 Overview Quiz 1 next Tuesday(20 mins, closed book, closed notes) Home work
2
3
4
5
x = 'Pluto is a planet' y = "Pluto is a planet" x == y True Double quotes are convenient if your string contains a single quote character: print("Pluto's a planet!") print('My dog is named "Pluto"')
6
7
triplequoted_hello = """hello world""" print(triplequoted_hello) hello world
8
print("hello") print("world") print("hello", end='') print("pluto", end='')
9
# Indexing planet = 'Pluto' planet[0] # Slicing planet[-3:] # How long is this string? len(planet) # Yes, we can even loop over them [char+'! ' for char in planet] planet[0] = 'B' Error! Strings are immutable
10
# ALL CAPS claim = "Pluto is a planet!" claim.upper() # all lowercase claim.lower() # Searching for the first index of a substring claim.index('plan') claim.startswith(planet) claim.endswith('dwarf planet')
11
words = claim.split() words ['Pluto', 'is', 'a', 'planet!'] datestr = '1956-01-31' year, month, day = datestr.split('-')
12
'/'.join([month, day, year]) '01/31/1956' datestr = '1956-01-31' year, month, day = datestr.split('-')
13
planet + ', we miss you.' 'Pluto, we miss you.' position = 9 planet + ", you'll always be the " + position + "th planet to me.”
<ipython-input-23-c7ad5a56c57a> in <module>() 1 position = 9
TypeError: must be str, not int
14
planet + ", you'll always be the " + str(position) + "th planet to me."
"{}, you'll always be the {}th planet to me.".format(planet, position)
15
pluto_mass = 1.303 * 10**22 earth_mass = 5.9722 * 10**24 population = 52910390 # 2 decimal points 3 decimal points, format as percent separate with commas "{} weighs about {:.2} kilograms ({:.3%} of Earth's mass). It is home to {:,} Plutonians.".format( planet, pluto_mass, pluto_mass / earth_mass, population, ) "Pluto weighs about 1.3e+22 kilograms (0.218% of Earth's mass). It is home to 52,910,390 Plutonians."
16
# Referring to format() arguments by index, starting from 0 s = """Pluto's a {0}. No, it's a {1}. {0}! {1}!""".format('planet', 'dwarf planet') print(s)
19
20
file_object = open(“filename”, “mode”) #where file_object is the variable to add the file object.
21
Including a mode argument is optional because a default value of ‘r’ will be assumed if it is omitted. The ‘r’ value stands for read mode, which is just one
The modes are: ‘r’ – Read mode which is used when the file is only being read ‘w’ – Write mode which is used to edit and write new information to the file (any existing files with the same name will be erased when this mode is activated) ‘a’ – Appending mode, which is used to add new data to the end of the file; that is new information is automatically amended to the end ‘r+’ – Special read and write mode, which is used to handle both actions when working with a file So, let’s take a look at a quick example.
22
file = open(“testfile.txt”,”w”) file.write(“Hello World”) file.write(“This is our new text file”) file.write(“and this is another line.”) file.write(“Why? Because we can.”) file.close() $ cat testfile.txt
23
file = open(“testfile.text”, “r”) print file.read() file = open(“testfile.txt”, “r”) print file.read(5) file = open(“testfile.txt”, “r”) print file.readline() file = open(“testfile.txt”, “r”) print file.readline(3) file = open(“testfile.txt”, “r”) print file.readlines()
24
file = open(“testfile.txt”, “r”) for line in file: print line, with open(“testfile.txt”) as file: data = file.read() do something with data with open(“hello.text”, “r”) as f: data = f.readlines() for line in data: words = line.split() print words