Python Statements Flashcards

1
Q

How does a for loop act as an iterator in python?

A

It goes through items that are in a sequence or any other iterable item, performing operations on each item as a stand alone object.

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

What objects can be iterated?

A

Strings, lists, tuples, and built in iterables for dictionaries (keys or values)

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

What should the iteration obtained item be named in a for loop?

A

That is up to the coder, it better make sense though.

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

What is tuple unpacking?

A

If you are iterating through a sequence that contains tuples, the item can actually be the tuple itself. Ex: for a,b in list1: print(tup)

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

With tuples in a sequence, we can access the items inside of them through ___. Why is this important?

A

unpacking Many objects will deliver their iterables through tuples

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

list 3 methods for extracting lists from dictionaries.

A

when wrapped in the list() function: .keys() - returns a list of keys .values() - returns a list of values .items() - Returns a list of tuples

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

If you want to return a sorted list of dictionary view objects, what function should you use?

A

Ex: sorted(d.values())

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

what 3 statements can we use to add functionality to while loops?

A

break: breaks out of the current closest enclosing loop. continue: goes to the top of the closest enclosing loop. Pass: does nothing at all

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

Where are break and continue statements usually place?

A

Nested in conjunction with an if statement to perform an action based on some condition.

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

What function allows you to generate a list of integers?

A

range() with parameters for (start,stop,step) *the stop number will not be included

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

What is a generator function?

A

A special type of function that will generate information and not need to save it to memory.

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

What does the enumerate function do?

A

Turns a list into a list of tuples with the first item in the pair being the index position of the second item.

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

What is the zip function used for?

A

quickly generate a list of tuples by “zipping” up together to lists Ex: zip(list1,list2)

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

What else can the in operator be used for?

A

To quickly check if an object is in a list

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

What functions give you the min or max of a numerical list?

A

min()

max()

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

name and describe the functionality of the two random library functions mentioned in this chapter.

A

shuffle() – shuffles the items in the list

randint() –returns a random integer in the range provided by two parameters

17
Q

Explain the structure of a list comprehension.

A

new_list = [x for x in old_list] translation: return x for every item x(could be manipulated) in list old_list.

18
Q

How do you add an if statement to a list comprehension?

A

Just add the syntax of the if statement to the end of the list comprehension.

19
Q

How do you do a nest list comprehension?

A

after “in” in the higher level list comprehension, put the syntax of the nest list comprehension.