Download - Lecture # 28 Python II
![Page 1: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/1.jpg)
Lecture # 28
Python II
![Page 2: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/2.jpg)
More Python
• Strings and Arrays
• Keyboard I/O (Input/Output)
• File I/O
![Page 3: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/3.jpg)
Jython Strings and Arrays
• If we make the following assignment
s = “Hello”
• What is s?
![Page 4: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/4.jpg)
Jython Strings and Arrays
• If we make the following assignment
s = “Hello”
• What is s?
An array of characters
![Page 5: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/5.jpg)
Jython Strings and Arrays
• If we make the following assignment
s = “Hello”
• What is s?
An array of characters
•What is s[1]?
![Page 6: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/6.jpg)
Jython Strings and Arrays
• If we make the following assignment
s = “Hello”
• What is s?
An array
•What is s[1]?
e
![Page 7: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/7.jpg)
Jython String and Array length
• If we make the following assignment
s = “Hello”
• What is the length of s?
![Page 8: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/8.jpg)
Jython String and Array length
• If we make the following assignment
s = “Hello”
• What is the length of s?
• How do we get the length of s?
![Page 9: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/9.jpg)
Jython String and Array length
• If we make the following assignment
s = “Hello”
• What is the length of s?
• How do we get the length of s?
• len(s)
Try print len(s)
![Page 10: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/10.jpg)
Jython Strings and Arrays
• If we make the following assignment
list = [27, 101, 33]
• What is list?
![Page 11: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/11.jpg)
Jython Strings and Arrays
• If we make the following assignment
list = [27, 101, 33]
• What is list?
An array or list of numbers
![Page 12: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/12.jpg)
Jython Strings and Arrays
• If we make the following assignment
list = [27, 101, 33]
• What is list?
An array or list of numbers
•What is list[2]?
![Page 13: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/13.jpg)
Jython Strings and Arrays
• If we make the following assignment
list = [27, 101, 33]
• What is list?
An array or list of numbers
•What is list[2]?
33
![Page 14: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/14.jpg)
Jython Strings and Arrays
• If we make the following assignment
names = [“Mary”, “Bill”, “Jill”]
• What is names?
![Page 15: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/15.jpg)
Jython Strings and Arrays
• If we make the following assignment
names = [“Mary”, “Bill”, “Jill”]
• What is names?
An array or list of strings
![Page 16: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/16.jpg)
Jython Strings and Arrays
• If we make the following assignment
names = [“Mary”, “Bill”, “Jill”]
• What is names?
An array or list of strings
•What is names[0]?
![Page 17: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/17.jpg)
Jython Strings and Arrays
• If we make the following assignment
names = [“Mary”, “Bill”, “Jill”]
• What is names?
An array or list of strings
•What is names[0]?
Mary
![Page 18: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/18.jpg)
Jython Strings and Arrays
• If we make the following assignment
Big_string = “The quick brown fox jumped overthe lazy dogs.”
• What is Big_string?
![Page 19: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/19.jpg)
Jython Strings and Arrays
• If we make the following assignment
Big_string = “The quick brown fox jumped overthe lazy dogs.”
• What is Big_string?
An array of characters
![Page 20: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/20.jpg)
Jython Strings and Arrays
• If we make the following assignment
Big_string = “The quick brown fox jumped overthe lazy dogs.”
• What is Big_string?
An array of characters
•What is Big_string [7]?
![Page 21: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/21.jpg)
Jython Strings and Arrays
• If we make the following assignment
Big_string = “The quick brown fox jumped overthe lazy dogs.”
• What is Big_string?
An array of characters
•What is Big_string [7]?
c
![Page 22: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/22.jpg)
Jython Keyboard I/O
• To get input from the keyboard and assignit to a variable:
![Page 23: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/23.jpg)
Jython Keyboard I/O
• To get input from the keyboard and assignit to a variable:
• fav = raw_input(“What’s your favorite food?: ”)
![Page 24: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/24.jpg)
Jython Keyboard I/O
• To get input from the keyboard and assignit to a variable:
• fav = raw_input(“What’s your favorite food?: ”)
• What’s your favorite food?: Pizza input
![Page 25: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/25.jpg)
Jython Keyboard I/O
• To get input from the keyboard and assignit to a variable:
• fav = raw_input(“What’s your favorite food?: ”)
• What’s your favorite food?: Pizza input
• print fav Pizza output
![Page 26: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/26.jpg)
Jython Keyboard I/O
To produce output using mix of data types:
s = “Hello World!”r = 1.23456i = 7print “Mixed Message: %d, %s, %f” % (i,
s, r)
What happens if you don’t use %d, %s, etc.?
![Page 27: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/27.jpg)
Jython File I/O
• Create a text file using Notepad:
Enter a String of text:“The quick brown fox jumped over the lazy
dogs.”
• Save the file as: Text.txt
![Page 28: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/28.jpg)
Jython File I/O: Reading from files
• Open the file with Python and assign thefile path/name to variable f:
f = open(pickAFile())
• Save the contents of the file in s:
s = f.read()
•Display the contents of the file:
print s
![Page 29: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/29.jpg)
Opening Files
• open(filename, mode)
– filename – text
• absolute path is best: Ex: ”C:/MyFolder/MyPix/photo.jpg”
• relative path - relative to where the program was started
(absolute path is best in JES)
![Page 30: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/30.jpg)
Opening Files
• open(filename, mode)
– mode – text• r – open for reading (default)
• w – open for writing
• a – open for writing, appending to the end of the file
• b – binary mode
• t – text mode (default)
• + - open file for updating (reading and writing)
• combinations: rt, bt, rwt
![Page 31: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/31.jpg)
Jython File I/O: Writing to files
• Open the file with Python and assign thefile path/name to variable f:
f = open(“myfile.txt”,”wt”)
• Write some text to the file:f.write(“Here is some text.”)
![Page 32: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/32.jpg)
Jython File I/O: Writing to files
• Open the file with Python and assign thefile path/name to variable f:
f = open(“myfile.txt”,”wt”)
• Write some text to the file:f.write(“Here is some text.”)f.write(“Here is some more text.\n”)
![Page 33: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/33.jpg)
Jython File I/O: Writing to files
• Open the file with Python and assign thefile path/name to variable f:
f = open(“myfile.txt”,”wt”)
• Write some text to the file:f.write(“Here is some text.”)f.write(“Here is some more text.\n”)f.write(“We’re done.\n\n THE END!”)
![Page 34: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/34.jpg)
Jython File I/O: Writing to files
• Open the file with Python and assign thefile path/name to variable f:
f = open(“myfile.txt”,”wt”)
• Write some text to the file:f.write(“Here is some text.”)f.write(“Here is some more text.\n”)f.write(“We’re done.\n\n THE END!”)f.close()
![Page 35: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/35.jpg)
Jython File I/O: Writing to files
• Open the file with Python and assign thefile path/name to variable f:
f = open(“myfile.txt”,”wt”)
• Write some text to the file:f.write(“Here is some text.”)f.write(“Here is some more text.\n”)f.write(“We’re done.\n\n THE END!”)f.close()f = open(“myfile.txt”,”rt”)print f.read()
![Page 36: Lecture # 28 Python II](https://reader036.vdocuments.us/reader036/viewer/2022062500/56815a2d550346895dc76db8/html5/thumbnails/36.jpg)
Reading and Writing
• Operations on text files– file.read()– file.readline()– file.readlines()– file.write(string)
• It must be a string– use str(i) function to convert things to strings
– file.close()