360 likes | 489 Views
Lecture # 28 Python II. More Python. Strings and Arrays Keyboard I/O (Input/Output) File I/O. Jython Strings and Arrays. If we make the following assignment s = “ Hello ” What is s ?. Jython Strings and Arrays. If we make the following assignment s = “ Hello ” What is s ?
E N D
Lecture # 28 Python II
More Python • Strings and Arrays • Keyboard I/O (Input/Output) • File I/O
Jython Strings and Arrays • If we make the following assignment s = “Hello” • What is s?
Jython Strings and Arrays • If we make the following assignment s = “Hello” • What is s? An array of characters
Jython Strings and Arrays • If we make the following assignment s = “Hello” • What is s? An array of characters • What is s[1]?
Jython Strings and Arrays • If we make the following assignment s = “Hello” • What is s? An array • What is s[1]? e
Jython String and Array length • If we make the following assignment s = “Hello” • What is the length of s?
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?
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)
Jython Strings and Arrays • If we make the following assignment list = [27, 101, 33] • What is list?
Jython Strings and Arrays • If we make the following assignment list = [27, 101, 33] • What is list? An array or list of numbers
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]?
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
Jython Strings and Arrays • If we make the following assignment names = [“Mary”, “Bill”, “Jill”] • What is names?
Jython Strings and Arrays • If we make the following assignment names = [“Mary”, “Bill”, “Jill”] • What is names? An array or list of strings
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]?
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
Jython Strings and Arrays • If we make the following assignment Big_string = “The quick brown fox jumped over the lazy dogs.” • What is Big_string?
Jython Strings and Arrays • If we make the following assignment Big_string = “The quick brown fox jumped over the lazy dogs.” • What is Big_string? An array of characters
Jython Strings and Arrays • If we make the following assignment Big_string = “The quick brown fox jumped over the lazy dogs.” • What is Big_string? An array of characters • What is Big_string [7]?
Jython Strings and Arrays • If we make the following assignment Big_string = “The quick brown fox jumped over the lazy dogs.” • What is Big_string? An array of characters • What is Big_string [7]? c
Jython Keyboard I/O • To get input from the keyboard and assign it to a variable:
Jython Keyboard I/O • To get input from the keyboard and assign it to a variable: • fav = raw_input(“What’s your favorite food?: ”)
Jython Keyboard I/O • To get input from the keyboard and assign it to a variable: • fav = raw_input(“What’s your favorite food?: ”) • What’s your favorite food?: Pizza input
Jython Keyboard I/O • To get input from the keyboard and assign it to a variable: • fav = raw_input(“What’s your favorite food?: ”) • What’s your favorite food?: Pizza input • print fav Pizza output
Jython Keyboard I/O To produce output using mix of data types: s = “Hello World!” r = 1.23456 i = 7 print “Mixed Message: %d, %s, %f” % (i, s, r) What happens if you don’t use %d, %s, etc.?
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
Jython File I/O: Reading from files • Open the file with Python and assign the file 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
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)
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
Jython File I/O: Writing to files • Open the file with Python and assign the file path/name to variable f: f = open(“myfile.txt”,”wt”) • Write some text to the file: f.write(“Here is some text.”)
Jython File I/O: Writing to files • Open the file with Python and assign the file 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”)
Jython File I/O: Writing to files • Open the file with Python and assign the file 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!”)
Jython File I/O: Writing to files • Open the file with Python and assign the file 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()
Jython File I/O: Writing to files • Open the file with Python and assign the file 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()
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()