lecture # 28 python ii

36
Lecture # 28 Python II

Upload: amil

Post on 04-Feb-2016

40 views

Category:

Documents


2 download

DESCRIPTION

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 ? - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture # 28 Python II

Lecture # 28

Python II

Page 2: Lecture # 28 Python II

More Python

• Strings and Arrays

• Keyboard I/O (Input/Output)

• File I/O

Page 3: Lecture # 28 Python II

Jython Strings and Arrays

• If we make the following assignment

s = “Hello”

• What is s?

Page 4: Lecture # 28 Python II

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

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

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

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

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

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

Jython Strings and Arrays

• If we make the following assignment

list = [27, 101, 33]

• What is list?

Page 11: Lecture # 28 Python II

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

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

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

Jython Strings and Arrays

• If we make the following assignment

names = [“Mary”, “Bill”, “Jill”]

• What is names?

Page 15: Lecture # 28 Python II

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

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

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

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

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

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

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

Jython Keyboard I/O

• To get input from the keyboard and assignit to a variable:

Page 23: Lecture # 28 Python II

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

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

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

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

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

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

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

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

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

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

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

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

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

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