Chapters 7&8: Files and Lists Flashcards

1
Q

“Thank you/nYou’re welcome”

A

When printed, returns:
Thank you
You’re welcome.
/n = 1 charactcer, new line. If simply entered, will show initial version.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

fhand=open(‘blah.txt’)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

x=fhand.read()

A

assigns actual text of file to x as a string, not just handle

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

x.rstrip()

A

strips white space from right of the string, but NOT left. as opposed to lstrip or just strip

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

lift only lines starting with ‘From:’ from file, getting rid of double spacing.

A
fhand=open('givenfile.txt')
for line in fhand:
   line=line.rstrip()
   if line.startswith('From:'):
      print(line)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

text=banana

text.find(‘c’)

A

returns -1, as no c in banana.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

lift only lines containing given substr, getting rid of double spacing

A
fhand=open('givenfile.txt')
for line in fhand:
   line=line.rstrip()
   if line.find('given_substr')==-1:
      continue
   print(line)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

if blah blah: continue

A

same effect as:
if blah blah:
continue

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

newtext=open(‘output.txt’, ‘w’)

A

Write mode (second parameter, w). This creates new file, or overwrites existing file with then entered name if it exists.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

writing lines

A

line1=’This is the first line\n’
newtext.write(line1)
returns 24, number of characters entered.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

finish writing

A

newtext.close()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

list vs string

A

both sequences of values: string values are characters, list values cacn be anything

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

values of a list

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

print 1st item in a list

A

print(list[0])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

reassign 3rd item in a list to x

A

list[2]=’x’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

print all of list of oxen individually

A

for ox in oxen:

print(ox)

17
Q

concocenating lists 1 and 2, to make {1, 2, 3] and [4, 5, 6] into one list

A

list1+list 2

18
Q

list1*2 gives…

A

[1, 2, 3, 1, 2, 3]

19
Q

slice operator to extract just [1, 2] from list1:

A

list1[0:2], or just list1[:2}

20
Q

append method: adds onto end of list, e.g. add 4 to list 1 to make [1,2,3,4]

A

list1.append(4)

21
Q

extend method,

A

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

22
Q

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

A

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

23
Q

sort list reverse alphabetically

A

list1. sort()

list1. reverse()

24
Q

pop method - remove an element from list, but assign it a variable

A

x=list1.pop(1)

x now equals 2 (element 1, the 2nd element), list1 is now [1,3,4]

25
Q

element not needed? del operator

A

del list1[1]. can use a slice index, eg [2:6]

26
Q

know the name of element but not the index?

A

list1.remove(2) or (‘x’), this removed the actual element 2, not the element with [2] as its index

27
Q

to do to list1:

  • count elements
  • find highest number element
  • lowest number
  • total
  • average
A
(all in print() )
len(list1)
max(list1)
min(list1)
sum(list1)
sum(list1)/len(list1)
sum is numerical list only. Others can be used "on lists of strings etc where elements are comparable" - not sure what that means, doesnt count or find one with most chars.
28
Q

allow user to input list:

A
list=list() #tells python it'll be list type
While True:
   inp= blah
   list.append(inp)
#then eg average=sum(list)/len(list)
29
Q

convert str to list of characters:

and to list of words:

A

list1=list(str)

list1=str.split()

30
Q

convert str to list of words, but something other than space seperating words (space is default, leave brackets empty)

A

list1=str.split(delimiter)

where delimiter is the object seperating them, eg - for str ‘qw-ty-fd’

31
Q

join - the inverse of split.

convert list to string with chosen delimiter e.g. ‘ ‘

A

’ ‘.join(list1) will make [1,3,4] into ‘1 3 4’. want no delimiter? just use ‘’, ie. empty string

32
Q

using split to parse lines of a file - print all the days of week from lines starting ‘From ‘ in mbox-short.txt

A
fhand=open('mbox-short.txt')
for line in fhand:
   line=line.rstrip()
   if not line.startswith('From '):
      continue
   words=line.split()
   print(words[2]) #the day of week is 3rd word on line
33
Q

equivalent vs identical

A

equivalent strings are identical, the same object. a=x, b=x, a is b will return True. But lists which are the same are NOT the same object, a is b will return False. They are equivalent, NOT identical

34
Q

aliasing

A

using one variable as a reference for another. eg, list3=list1, list1 is list3 returns True. as the variable is the value of a new variable, they are identical. Best avoided if possible though with lists - altering one of them may cause an error.

35
Q

method to avoid aliasing list1

A

list3=list1[:]

36
Q
can use and or or logic operators to combine guardian code lines. e.g. with and:
if len(words)<=2: continue
if words[0]!='From': continue
   print(words[2])
#cacn be turned into:
A
if len(words)>2 and words[0]=='From':
    print(words[2])
37
Q

elem

A

short for element of a list. eg for elem in list1: