1 / 36

Lecture # 28 Python II

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 ?

amil
Download Presentation

Lecture # 28 Python II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture # 28 Python II

  2. More Python • Strings and Arrays • Keyboard I/O (Input/Output) • File I/O

  3. Jython Strings and Arrays • If we make the following assignment s = “Hello” • What is s?

  4. Jython Strings and Arrays • If we make the following assignment s = “Hello” • What is s? An array of characters

  5. Jython Strings and Arrays • If we make the following assignment s = “Hello” • What is s? An array of characters • What is s[1]?

  6. Jython Strings and Arrays • If we make the following assignment s = “Hello” • What is s? An array • What is s[1]? e

  7. Jython String and Array length • If we make the following assignment s = “Hello” • What is the length of s?

  8. 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?

  9. 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)

  10. Jython Strings and Arrays • If we make the following assignment list = [27, 101, 33] • What is list?

  11. Jython Strings and Arrays • If we make the following assignment list = [27, 101, 33] • What is list? An array or list of numbers

  12. 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]?

  13. 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

  14. Jython Strings and Arrays • If we make the following assignment names = [“Mary”, “Bill”, “Jill”] • What is names?

  15. Jython Strings and Arrays • If we make the following assignment names = [“Mary”, “Bill”, “Jill”] • What is names? An array or list of strings

  16. 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]?

  17. 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

  18. 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?

  19. 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

  20. 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]?

  21. 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

  22. Jython Keyboard I/O • To get input from the keyboard and assign it to a variable:

  23. Jython Keyboard I/O • To get input from the keyboard and assign it to a variable: • fav = raw_input(“What’s your favorite food?: ”)

  24. 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

  25. 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

  26. 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.?

  27. 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

  28. 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

  29. 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)

  30. 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

  31. 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.”)

  32. 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”)

  33. 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!”)

  34. 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()

  35. 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()

  36. 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()

More Related