Chapters 7&8: Files and Lists Flashcards
(37 cards)
“Thank you/nYou’re welcome”
When printed, returns:
Thank you
You’re welcome.
/n = 1 charactcer, new line. If simply entered, will show initial version.
fhand=open(‘blah.txt’)
Opens the given text file if it exists in the python directory (in quick access). Value fhand is assigned with is only the file handle. But some subsequent functions, but not print, will be done on entire text file.
x=fhand.read()
assigns actual text of file to x as a string, not just handle
x.rstrip()
strips white space from right of the string, but NOT left. as opposed to lstrip or just strip
lift only lines starting with ‘From:’ from file, getting rid of double spacing.
fhand=open('givenfile.txt') for line in fhand: line=line.rstrip() if line.startswith('From:'): print(line)
text=banana
text.find(‘c’)
returns -1, as no c in banana.
lift only lines containing given substr, getting rid of double spacing
fhand=open('givenfile.txt') for line in fhand: line=line.rstrip() if line.find('given_substr')==-1: continue print(line)
if blah blah: continue
same effect as:
if blah blah:
continue
newtext=open(‘output.txt’, ‘w’)
Write mode (second parameter, w). This creates new file, or overwrites existing file with then entered name if it exists.
writing lines
line1=’This is the first line\n’
newtext.write(line1)
returns 24, number of characters entered.
finish writing
newtext.close()
list vs string
both sequences of values: string values are characters, list values cacn be anything
values of a list
called elements or items. comma and space between them, enclosed in square brackets, strings have speech marks numbers and variables dont (as usual). Lists within lists work as you’d think - lists, within square brackets, just become another element.
print 1st item in a list
print(list[0])
reassign 3rd item in a list to x
list[2]=’x’
print all of list of oxen individually
for ox in oxen:
print(ox)
concocenating lists 1 and 2, to make {1, 2, 3] and [4, 5, 6] into one list
list1+list 2
list1*2 gives…
[1, 2, 3, 1, 2, 3]
slice operator to extract just [1, 2] from list1:
list1[0:2], or just list1[:2}
append method: adds onto end of list, e.g. add 4 to list 1 to make [1,2,3,4]
list1.append(4)
extend method,
same, but can take a list as argument and append each element onto the list to be appended. using append method appends the whole list as a single new element
Warning: list methods are not like functions or string methods - they permanently alter, x=x.blah isnt needed, and may delete content. they’re ‘void’.
Whereas with string methods this is fine, and will set the new value to a new variable, leaving previous variable unaltered. eg line=line.rstrip; for lists it would just be eg list.split(), no list= before.
while list1=list1[:2] won’t work, return list1[:2] will if eg defining a function
sort list reverse alphabetically
list1. sort()
list1. reverse()
pop method - remove an element from list, but assign it a variable
x=list1.pop(1)
x now equals 2 (element 1, the 2nd element), list1 is now [1,3,4]