Chapters 5&6: Iteration and Strings Flashcards

1
Q

Increment/decrement

A

Updating the value of a variable bu increasing/drecreasing by 1 (or another fixed amount?) eg x=x+1

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

while

A

statement saying “if this condition is met (ie True), execute the body. If False, skip rest of body and execute subsequent lines. Method of looping.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
n=10
while n>0:
   print(n)
   n=n-1
print('Blastoff!')
#number ofiterations?
A

10, 9, 8, 7, 6, 5, 4, 3, 2, 1, Blastoff!

10 interations

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

iteration variable

A

variable which is changed each iteration and controls when the loop finishes, n above. If there isnt one, it’s an infinite loop.

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

while True

A

A way of making a controlled infinite loop, which will repeat (if user input is part of it, ie. x=input(‘type Hi\n’)) until broken eg by user entering sought-for value, ie if x==’Hi’//[tab]break.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
while True:
   line=input('> ')
   if line[0]=='#'
      continue
   if line=='done'
      break
   print(line)
print('Done!')
A

line[0] means first character of value of line; hence if it starts with #, start loop again without finishing.
if it’s ‘done’, loop breaks and final line occurs. Else, loops back.

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

Definite loop

A

A loop which goes through a set of values, and ends with the last value. Often with ‘for’ statement, rather than ‘while’ for an indefinite loop.

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

friends=[‘Joseph’, ‘Glenn’, ‘Sally’]
for friend in friends:
print(‘Happy New Year: ‘, friend)
print(‘Done!’)

A

the ‘for’ makes it cycle through 3 times, on per friend in set ‘friends’, before executing final line. Friend is iteration variable.

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

itervar

A
iteration variable, eg:
total=0
for itervar in [3, 41, 12, 9, 74, 15]:
   total=total+itervar
print('Total: ', total) #this would summate the numbers in set. (can just use sum() though)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

fruit=’banana’
letter=fruit[1]
print(letter)

A

Returns ‘a’. first letter is [0], b, etc. Must be integer

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

len()

A

length of string, number of characters in a string (not other types)

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

length=len(fruit)

last=fruit[length]

A

error - out of range! [6] would be 7th letter, only 6. hence last=fruit[length-1] to get last letter, a. OR, count back, letter=fruit[-1] returns a, [-2] b etc.

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

spell out “fruit” letter by letter with “while” statement - traversing through a string

A

index=0

while index

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

spell out “fruit” letter by letter with “for” statement - traversing through a string

A

for char in fruit:

print(char)

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

String slices

A

assigning segments of a string for something, eg. s=’Ben Nouhan’, print(s[0:5]) returns Ben NB, it inludes first (0) but excludes last (5). [:5] is start to 5, [3:] is 3 to last. [:] is whole thing.

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

Change assignmet of variable by 1 letter, eg Ben Nouhan to Ben Nouyan

A

s=’Ben Nouhan’

s=s[:7]+’y’+s[8:]

17
Q

count ‘a’s in banana

A
count=0
word='banana'
for letter in word:
   if letter=='a'
      count=count+1
print(count)
18
Q

letter in str

A

letter in a string. can be used with for, ie for letter in str:. an also be used as argument when defining funcction, ie def count(str, letter): etc.

19
Q

char in str

A

same as letter in str, but characters in general.

20
Q

in

A

A boolean operator, takes 2 strings and returns True if 1st appears as substring in 2nd, ie ‘a’ in ‘banana’ returns True.

21
Q

Alphabetical ordering

A

< and > can be used to compare string alphabeticaclly. ‘aaron’‘Jeff’. If you don’t want cacpitalised words to have priotity, need to cconvert all to lowercase.

22
Q

Object

A

eg a string. Contains data (eg the charaters of the string) and built-in methods, which are functions built into the given object, eg string-specific ones.

23
Q

dir()

A

used like type(), except rather than giving the type of objetc, it lists all the available methods avalable for that object type.

24
Q

help(object.method)

A

function for input of object type and method you want elucidating, eg help(str.capitalize) will explain that method for strings.

25
Q

method vs function syntax

A

variable.method() vs function(variable). Brackets contain parameters if applicacble. eg word.find(‘a’)

26
Q

Invocation

A

the call of a method. ie in above example, we invoked [method] on [variable], eg invoke find on word

27
Q

find method

A

word=banana
index=word.find(‘a’, 2)
print(index) #returns 3. a is the substring it looks for in word string, 2 is the letter (first n) it starts looking from. NB if non found, returns ‘-1’

28
Q

strip method

A

line=’ here we go ‘

line.strip() #returns ‘here we go’, removes spaces, tabs, enters from outside of non-white space.

29
Q

startswith method

A

line=’Ben Nouhan’

line.startswith(‘Ben’) #returns True, as it does. Case sensitive; b will return False, Be will return True.

30
Q

count method

A

s=banana

s. count(‘a’) #returns 3.
s. count(‘a’, 1, 5) #returns 2, as it counts ‘a’s between bAnaNa A and N inclusive

31
Q

help.method square brackets meaning

A

paramaters are listed by comma as usual. Square brackets indicate a parameter is optional. square brackets within eachother mean if you use the first parameter, the second is then optional.

32
Q

find unknown substr, z, between 2 known substrs, x&y in str

A

data=’str’
xpos=data.find(x)
ypos=data.find(y)
z=data[xpos+1:ypos]

33
Q

% (used on str)

A

format operator

34
Q

format sequences %d, %g, %s

A

integer, floating-point, string

35
Q

formatting example, eg with 3 format sequences

A

‘in %d years I have spotted %g %s.’%(3, 0.1, ‘camels’) #returns… well it’s obvious. number of parameters must match number of format sequences.