2. Loop and Flow Control Flashcards

1
Q

Control flow

A

if:
elif:
else:

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

syntax of a for loop

A

my_interable = [1,2,3]

for xxx in my_iterable:
print( xxx)

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

tuple unpacking

A

Duplicate the structure in the iterables
For (a,b) in my_tuples:
print(b)

Also mean breaking up a tuple into variable
name, hours = tuple(‘Mary’,100)

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

tuple unpacking in dictionary

A

by default dictionary loop through the key only

For key,value in d.items():
print(value)

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

while loop

A

while boolean_condition:
#do something
else:
#do something diff

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

control flow in loop (3 methods)

A

break: stop the loop
continue: stop and go back to first line
pass: do nothing

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

enumerate function

A

Zip a count with the iterable object and return a tuple

word = ‘abcde’

for item in enumerate(word):
print(item)

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

combine two list together

A

for item in zip(mylist1, mylist2):
print(item)

return tuples

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

list comprehension

A

quick way to replace for loop along with .append()

mylist = [x for x in 'word']
mylist = [x**2 for x in range(1,11) ]
mylist = [x for x in range(1,11) if x % 2 == 0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

looping through a string (or list)

A

for i in range(len(text))

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

for/while:

else:

A

else will be run at the end when the loop is done or while condition is no longer true; break will break the whole loop and break the else as well

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

1) if x:
2) if not x
3) if x is not None

A

1) if x, means if it is something (i.e. not none or not empty), do something
2) if not x, means do something if none/empty

3) similar to 1 but empty != None anymore
see if x is really not None

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