1/87
Looks like no tags are added yet.
You (can/cannot) include a single quote in a double quoted string or a double quote in a single quoted string
can
numbers = [0,1,2]
print(numbers[0:2])
[0,1]
>>> fruit = "apple"
>>> fruit.index('p')
1
>>> 'm'.isupper()
false
>>> 'mA'.islower()
False
>>> aStr = 'my cat is catatonic'
>>> aStr.replace('cat','dog')
?
'my dog is dogatonic'
s = "a b\nc"
s.split()
['a','b','c']
s = "a b\nc"
s.split(' ')
['a', 'b\nc']
>>> s = '(hello!)'
>>> s.strip('()!')
>>> s.strip()
>>> s.strip(!)
>>> s.strip('!')
'hello'
'(hello!)'
Syntax Error
'(hello!)'
>>> import string
>>> string.punctuation
'!"#$%&\'()*+,-./:;
strip is a (list/string) method
string
why can't we just use the replace() method to get rid of punctuation
1) you would need to call the replace method a bunch times
2) replace will replace every occurrence of the parameter with its replacement parameter while strip removes all occurences of every character in the paramater only at the ends of the string
print(1,2,3,sep=': ')
1: 2: 3
>>> numbers = []
>>> for i in range(10):
if(i==5):
continue
numbers.append(i)
>>> numbers
[0, 1, 2, 3, 4, 6, 7, 8, 9]
>>> numbers = []
>>> while len(numbers) < 3:
numbers.append(0)
>>> numbers
[0, 0, 0]
>>> while True:
numbers.append(0)
if len(numbers) > 2:
break
>>> numbers
[0, 0, 0]
>>> girl = "sam"
>>> boy = "jake"
>>> girl, boy = "sam2","jake2"
>>> girl + boy
'sam2jake2'
>>> girl = "sam"
>>> boy = "jake"
>>> [girl,boy]=["sam2","jake2"]
>>> girl+boy
'sam2jake2'
>>> previous = 1
>>> current = 1
>>> previous,current = current,previous+current
>>> previous
1) ?
>>> current
2) ?
1
2
>>> p=2
>>> c=3
>>> [p,c]=[c,p+c]
>>> p
1)
>>> c
2)
3
5
The continue statement:
is used inside the body of a 1) _______ or 2)_______ loop
interrupts the current 3)_______ of the loop
transfers execution to the _______ of the loop
1) for
2) while
3) iteration
4) top
A while loop lets you specify any controlling _______ that you want
condition
The break statement:
is used inside the body of a 1)_______ or 2)_______ loop
_______ execution of the loop
transfers execution to the statement that _______ the loop
1) for
2) while
3) termination
4) follows
For both continue and break, if the current loop is nested inside another loop, only the _______ loop is affected
inntermost
def before(table):
for row in table:
for num in row:
if num == 0:
break
print(num, end=' ')
print()
>>> table = [ [2, 3, 0, 6],
[0, 3, 4, 5],
[4, 5, 6, 0] ]
>>> before(table)
2 3
4 5 6
def ignore(table):
for row in table:
for num in row:
if num == 0:
continue
print(num, end=' ')
print()
>>> table = [ [2, 3, 0, 6],
[0, 3, 4, 5],
[4, 5, 6, 0] ]
>>> ignore(table)
2 3 6
3 4 5
4 5 6
open a file named test (for reading) and set it to a variable x
x = open('test.txt','r')
the open() function returns an object of type _______. A method that is supported by this type is the _______ function
file
close()
default open file mode
reading
text
what is the difference between:
infile = open('example.txt', 'rt')
infile = open('example.txt', 'r')
infile = open('example.txt', 't')
infile = open('example.txt')
nothing
open a text document for reading and set it to a variable x
x = open('Test.txt','rt')
open a file named test (for writing) and set it to a variable x
x = open('Test.txt','w')
what happens if you open a file for writing and it does not exist
it gets created
what happens if you open a file for reading and it does not exist
FileNotFoundError
what does the write method print in interactive mode
The number of characters written
Anything you write to a text file must be a _______
The write method does not automatically append an _______
The write method returns the number of _______ written
string
endline
characters
file = open("Humpty.txt",'r')
for line in file:
print(line,end="")
what happens if you do not add the (end="")
it will print two newlines for each line of the file
there exists a newline at the end of each line that you read
read(n)
return value?
Read n characters starting from cursor; if fewer than n characters remain, read until the end of file
returns n characters
read()
return value?
Read starting from cursor up to the end of the file
returns the entire line
readline()
return value?
Read starting from cursor up to and including the endline character
returns one line (including the endline character)
readlines()
return value?
Read starting from cursor up to the end of the file and return a list of lines
returns the entire file as a list of strings (one line = one string)
write()
Write string s to outfile starting from cursor
returns the number of characters written
close()
closes the file
When the file is opened, a cursor is associated with the opened file. The initial position of the cursor is:
at the _______ of the file, if file mode is r
at the _______ of the file, if file mode is a or w
beginning
end
The 3 lines end with the new line character.\n \n There is a blank line above this line.\n
>>> infile = open('example.txt')
>>> infile.read(1)
1)
>>> infile.read(5)
2)
>>> infile.readline()
3)
>>> infile.read()
4)
>>> infile.close()
1) 'T'
2) 'he 3 '
3) 'lines end with the new line character.\n'
4) '\nThere is a blank line above this line.\n'
When writing to files non string value like 5 must be _________ first.
converted
Writing (if file exists, content is _______)
wiped
create a dictionary where each key is a number and each value is the spelling of that word
d = {0:"zero",1:"one",2:"two"}
Create Empty Dictionary
d = {}
Syntax of dictionary
{key:value}
A key can be any 1) _______ type. A key cannot be a 2) _______ type.
1) immutable
2) mutable
Each key in a dictionary must be _______
unique
d = {"cat":"animal","cat":"pet"}
>>> d["cat"]
1)
>>> d
2)
1) 'pet'
2) {'cat': 'pet'}
A value can be _______
any type
add a key:value pair, using index and assignment operators
d["number"]=0
>>> d["number"]=0
>>> d["number"]
1)
>>> d["number"]=1
>>> d
2)
>>> number = d.pop('number')
>>> number
3)
>>> d
4)
1) 0
2) {'number': 1}
3) 1
4) {}
pop function of dictionary
removes the key:value pair associated with the parameter and returns the value
>>> d = {0:0,1:1}
>>> len(d)
2
>>> d = {0:1,"none":True}
>>> 0 in d
1)
>>> True in d
2)
1) True
2) False
>>> d = {"Anthony":18,"Michael":20}
>>> d2 = {"Grooms":22}
>>> d.update(d2)
>>> d
1)
2) does d2 change?
1) {'Anthony': 18, 'Michael': 20, 'Grooms': 22}
2) no
what type does the dictionary update function accept
dictionary
>>> d = {0:0}
>>> d.update({0:1})
>>> d
?
{0: 1}
two ways to go through keys of a dictionary
1) for key in d.keys():
2) for key in d:
how to go through values of a dictionary
for value in d.values():
Retrieving one element from among 10,000,000 in a dictionary is ________
immediate
is there any way to get an empty string w/ indexing
no
how to go from negative index to positive
-index plus length
with string "abby"
negative index (-1) maps to positive index 3 because -1 + 4 = 3
"abby".capitalize()
Abby
"apple".endswith('E')
False
difference between find and index
index will throw indexerror if index is out of bounds
find will return -1
>>> "apple".find("le")
3
>>> "apple".find("y")
-1
>>> import string
>>> string.punctuation
?
'!"#$%&\'()*+,-./:;
lst = ["a","b","c"]
lst.split("b")
AttributeError
min(s)
for string a list
lowest character according to ascii
for list it will give you smallest string
min(["a",0,"0"])
TypeError: '
>>> min(["a","A"])
'A'
it is faster for python to preform addition verse multiplication
true
python automatically closes files on your behave
true
don't forget to _______ the files
close
how to close a file
file.close()
a txt file contains the numbers 0 through 4 vertically on separate lines
f = open("numbers.txt",'r')
>>> f.readline()
1)
>>> f.readline()
2)
1) '0\n'
2) '1\n'
Difference between write and append mode?
Both utilize the _______ method.
write clears the file while append adds to it
write
cursor ________ as you read
moves
remember that newlines appear as _______ in a string
\n
examples of white space
tab
space
newline
values for dictionaries could be _______
repeated
dictionary (is/is not) a sequence
is not