5 - Python Statements Flashcards

1
Q

Here’s the general format for a for loop in Python:

A

for item in object:
statements to do stuff

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

The ‘else’ statement looks like:

A

for num in list1:
if num % 2 == 0:
print(num)
else:
print(‘Odd number’)

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

Iterating through a sequence that contains tuples, this is ‘tuple unpacking’, and it looks like:

A

Now with unpacking!

list2 = [(2,4),(6,8),(10,12)]

for tup in list2:
print(tup)

(2, 4)
(6, 8)
(10, 12)

for (t1,t2) in list2:
print(t1)

2
6
10

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

Iterating through Dictionaries, returning just the keys looks like:

A

d = {‘k1’:1,’k2’:2,’k3’:3}

k1
k2
k3

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

Iterating through Dictionaries, returning just the keys and values looks like:

A

Dictionary unpacking

for k,v in d.items():
print(k)
print(v)

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

Sorted list of dictionary values:

A

sorted(d.values())

[1, 2, 3]

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

A ‘while’ loop with an ‘else’ statement looks like”:

A

x = 0

while x < 10:
print(‘x is currently: ‘,x)
print(‘ x is still less than 10, adding 1 to x’)
x+=1

else:
print(‘All Done!’)

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

The ‘break’ keyword does what?

A

break: Breaks out of the current closest enclosing loop.

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

The continue keyword does what?

A

continue: Goes to the top of the closest enclosing loop

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

What does ‘pass’ do?

A

pass: Does nothing at all.

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

What function allows you to generate integers?

A

Notice how 11 is not included, up to but not including 11, just like slice notation!

range()

list(range(0,11))

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

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

How do you save a Range() to a list?

A

Notice how 11 is not included, up to but not including 11, just like slice notation!

list(range(0,11))

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

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

What does ‘enumerate’ do:

A

Notice the tuple unpacking!

Keeping track of how many loops you’ve gone through is so common, that enumerate was created so you don’t need to worry about creating and updating this index_count or loop_count variable.

for i,letter in enumerate(‘abcde’):
print(“At index {} the letter is {}”.format(i,letter))

At index 0 the letter is a
At index 1 the letter is b
At index 2 the letter is c
At index 3 the letter is d
At index 4 the letter is e

also

for i in enumerate(‘abcde’):
print(i)

(0, ‘a’)
(1, ‘b’)
(2, ‘c’)
(3, ‘d’)
(4, ‘e’)

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

What does zip() do?

A

You can use the zip() function to quickly create a list of tuples by “zipping” up together two lists.

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

A zip() function looks like:

A

mylist1 = [1,2,3,4,5]
mylist2 = [‘a’,’b’,’c’,’d’,’e’]

zip(mylist1,mylist2)

list(zip(mylist1,mylist2))

[(1, ‘a’), (2, ‘b’), (3, ‘c’), (4, ‘d’), (5, ‘e’)]

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

what does the ‘in’ operator return?

A

boolean
(Is this correct?)

17
Q

An ‘in’ operator looks like:

A

‘x’ in [‘x’,’y’,’z’]
True

‘x’ in [1,2,3]
False

18
Q

Can the ‘in’ operator be combined with the ‘not’ operator?

A

Yes

19
Q

What does a combination of the ‘in’ and ‘not’ operators look like?

A

‘x’ not in [‘x’,’y’,’z’]
False

‘x’ not in [1,2,3]
True

20
Q

What does the usage of the min() and max() look like:

A

mylist = [10,20,30,40,100]

min(mylist)
10

max(mylist)
100

21
Q

How do you shuffle a list?

A

This shuffles the list “in-place” meaning it won’t return

from random import shuffle

# anything, instead it will effect the list passed

shuffle(mylist)

22
Q

How do you create a random integer within a set range of numbers?

A

Return random integer in range [a, b], including both end points.

from random import randint

randint(0,100)

23
Q

What does ‘List Comprehensions’ do?

A

In addition to sequence operations and list methods, Python includes a more advanced operation called a list comprehension.

List comprehensions allow us to build out lists using a different notation. You can think of it as essentially a one line for loop built inside of brackets.

24
Q

A simple List Comprehension looks like:

A

Grab every letter in string

lst = [x for x in ‘word’]

lst

[‘w’, ‘o’, ‘r’, ‘d’]

25
Q

How to use List Comprehension to square numbers in a range of numbers?

A

Square numbers in range and turn into list

lst = [x**2 for x in range(0,11)]

lst
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

26
Q

Using an ‘if’ statement with List Comprehension looks like?

A

Check for even numbers in a range

lst = [x for x in range(11) if x % 2 == 0]

lst
[0, 2, 4, 6, 8, 10]

27
Q

More complicated arithmetic with List Comprehension looks like?

A

Convert Celsius to Fahrenheit

celsius = [0,10,20.1,34.5]

fahrenheit = [((9/5)*temp + 32) for temp in celsius ]

fahrenheit
[32.0, 50.0, 68.18, 94.1]

28
Q

A nested List Comprehension looks like?

A

lst = [ x2 for x in [x2 for x in range(11)]]

lst
[0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000]