Last Week Lists List methods Nested Lists Looping through lists using for loops.
-
Upload
gilbert-foster -
Category
Documents
-
view
218 -
download
0
Transcript of Last Week Lists List methods Nested Lists Looping through lists using for loops.
![Page 1: Last Week Lists List methods Nested Lists Looping through lists using for loops.](https://reader035.fdocuments.us/reader035/viewer/2022081809/5697bff11a28abf838cbb2e7/html5/thumbnails/1.jpg)
Last Week
• Lists
• List methods
• Nested Lists
• Looping through lists using for loops
![Page 2: Last Week Lists List methods Nested Lists Looping through lists using for loops.](https://reader035.fdocuments.us/reader035/viewer/2022081809/5697bff11a28abf838cbb2e7/html5/thumbnails/2.jpg)
This Week and Maybe NextWhile loops
Special characters in strings
Files – What are they?– Opening and closing files– Reading and writing to files– Comma Separated Files
![Page 3: Last Week Lists List methods Nested Lists Looping through lists using for loops.](https://reader035.fdocuments.us/reader035/viewer/2022081809/5697bff11a28abf838cbb2e7/html5/thumbnails/3.jpg)
While Loops
Sometimes we need to loop until a condition is met.
For example:
– Ask user for a password twice– If the passwords don’t match, ask again
until they match or the user quits
![Page 4: Last Week Lists List methods Nested Lists Looping through lists using for loops.](https://reader035.fdocuments.us/reader035/viewer/2022081809/5697bff11a28abf838cbb2e7/html5/thumbnails/4.jpg)
While Loop Example
English example:
Ask for password.Ask for password again.While the passwords don’t match:
Ask again.
![Page 5: Last Week Lists List methods Nested Lists Looping through lists using for loops.](https://reader035.fdocuments.us/reader035/viewer/2022081809/5697bff11a28abf838cbb2e7/html5/thumbnails/5.jpg)
While Loop Example
Python example:
password1 = input(‘Enter password: ’)Ask for password again.While the passwords don’t match:
Ask again.
![Page 6: Last Week Lists List methods Nested Lists Looping through lists using for loops.](https://reader035.fdocuments.us/reader035/viewer/2022081809/5697bff11a28abf838cbb2e7/html5/thumbnails/6.jpg)
While Loop Example
Python example:
password1 = input(‘Enter password: ’)password2 = input(‘Re-enter password: ’)While the passwords don’t match:
Ask again.
![Page 7: Last Week Lists List methods Nested Lists Looping through lists using for loops.](https://reader035.fdocuments.us/reader035/viewer/2022081809/5697bff11a28abf838cbb2e7/html5/thumbnails/7.jpg)
While Loop Example
Python example:
password1 = input(‘Enter password: ’)password2 = input(‘Re-enter password: ’)while password1 != password2:
Ask again.
![Page 8: Last Week Lists List methods Nested Lists Looping through lists using for loops.](https://reader035.fdocuments.us/reader035/viewer/2022081809/5697bff11a28abf838cbb2e7/html5/thumbnails/8.jpg)
While Loop Example
Python example:
password1 = input(‘Enter password: ’)password2 = input(‘Re-enter password: ’)while password1 != password2:
password2 = input(‘Re-enter password:’)
![Page 9: Last Week Lists List methods Nested Lists Looping through lists using for loops.](https://reader035.fdocuments.us/reader035/viewer/2022081809/5697bff11a28abf838cbb2e7/html5/thumbnails/9.jpg)
Invisible CharactersHow do we indicate a tab, newline or ‘ or “ inside a string?
\t tab \” double quote\n new line\’ single quote \\backslash
These are called escape sequences.print(“This string has a \n newline and a \t tab.”
This string has a
newline and a tab.
![Page 10: Last Week Lists List methods Nested Lists Looping through lists using for loops.](https://reader035.fdocuments.us/reader035/viewer/2022081809/5697bff11a28abf838cbb2e7/html5/thumbnails/10.jpg)
Files• Files – what are they?
– Can think of as a collection of stored information
– Computer thinks as a sequence of bits possible arranged in characters
• Files have names: – my_python.py, homework.txt, etc.
• What can we do with a file? – Read from a file and write to a file
![Page 11: Last Week Lists List methods Nested Lists Looping through lists using for loops.](https://reader035.fdocuments.us/reader035/viewer/2022081809/5697bff11a28abf838cbb2e7/html5/thumbnails/11.jpg)
Opening FilesHow do we open a file in Python?
import doctest
myfile = open(‘story.txt’, ‘r’)
•Open is the Python function
•Story.txt is the name of the file to be opened•myfile is a variable that is assigned the file object (also called stream or reader).•‘r’ is a string indicating what we will do with the file (read, write, append)
![Page 12: Last Week Lists List methods Nested Lists Looping through lists using for loops.](https://reader035.fdocuments.us/reader035/viewer/2022081809/5697bff11a28abf838cbb2e7/html5/thumbnails/12.jpg)
Using FilesAfter we are finished with a file we must close it:
myfile.close()
When we write to a file, we have two choices:Write: myfile = open(‘filename’, ‘w’)Append: myfile = open(‘filename’, ‘a’)
•write replaces the file filename•append appends to the file filename
![Page 13: Last Week Lists List methods Nested Lists Looping through lists using for loops.](https://reader035.fdocuments.us/reader035/viewer/2022081809/5697bff11a28abf838cbb2e7/html5/thumbnails/13.jpg)
Reading FilesThere are many ways to read from a file.
1.Using a for loop:myfile = open(‘filename’, ‘r’)
for line in myfile:
<do something with the line>
2.Read the whole file at once into a list of strings:
myfile = open(‘filename’, ‘r’)
list_of_lines = myfile.readlines()
![Page 14: Last Week Lists List methods Nested Lists Looping through lists using for loops.](https://reader035.fdocuments.us/reader035/viewer/2022081809/5697bff11a28abf838cbb2e7/html5/thumbnails/14.jpg)
Reading Files
3.Read the entire file into a string:myfile = open(‘filename’, ‘r’)
s = myfile.read()
4.Read a specific number of characters:myfile = open(‘filename’, ‘r’)
s = myfile.read(10) reads 10 characterss = myfile.read(10) reads then next 10 characters
![Page 15: Last Week Lists List methods Nested Lists Looping through lists using for loops.](https://reader035.fdocuments.us/reader035/viewer/2022081809/5697bff11a28abf838cbb2e7/html5/thumbnails/15.jpg)
Reading Files
5.Read one line at a time:myfile = open(‘filename’, ‘r’)
line = myfile.readline() reads a lineline = myfile.readline() reads the next line
![Page 16: Last Week Lists List methods Nested Lists Looping through lists using for loops.](https://reader035.fdocuments.us/reader035/viewer/2022081809/5697bff11a28abf838cbb2e7/html5/thumbnails/16.jpg)
End Of File (EOF)How do we know when we reach the end of the file?
This method automatically recognizes EOF.for line in myfile:
<do something with the line>
Here we have to check for the end of file:line = myfile.readline() reads the next lineand s = myfile.read(10) reads 10 characters
The EOF character is the empty string “”.
![Page 17: Last Week Lists List methods Nested Lists Looping through lists using for loops.](https://reader035.fdocuments.us/reader035/viewer/2022081809/5697bff11a28abf838cbb2e7/html5/thumbnails/17.jpg)
Writing to a fileFirst open the file for writing or appending:
myfile = open(‘story.txt’, ‘w’) start the storymyfile = open(‘story.txt’, ‘a’) continue the story
Then write to the file:myfile.write(‘Once upon a time…’)
myfile.write(‘The end.’)
Then close the file:
myfile.close()
![Page 18: Last Week Lists List methods Nested Lists Looping through lists using for loops.](https://reader035.fdocuments.us/reader035/viewer/2022081809/5697bff11a28abf838cbb2e7/html5/thumbnails/18.jpg)
Reading from a CSV file
Often we have comma-separated values data files:
First Name, Last Name, Utorid
Anna, Bretscher, bretsche
Joe, Johnson, johnsonj
Sally, Jordan, jordansa
Why do we like CSV files?–Spreadsheet applications like excel understand it–Many other programming languages also understand them–Easy to generate ourselves
![Page 19: Last Week Lists List methods Nested Lists Looping through lists using for loops.](https://reader035.fdocuments.us/reader035/viewer/2022081809/5697bff11a28abf838cbb2e7/html5/thumbnails/19.jpg)
Reading from a CSV file
How do we read comma-separated values data files:
import io
import csv
csv_file = open(‘csv_filename.csv’, ‘r’)
reader = csv.reader(csv_file)
…
for line in reader:
# read like an ordinary file
# but line is a list