Python Statements Flashcards
How does a for loop act as an iterator in python?
It goes through items that are in a sequence or any other iterable item, performing operations on each item as a stand alone object.
What objects can be iterated?
Strings, lists, tuples, and built in iterables for dictionaries (keys or values)
What should the iteration obtained item be named in a for loop?
That is up to the coder, it better make sense though.
What is tuple unpacking?
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)
With tuples in a sequence, we can access the items inside of them through ___. Why is this important?
unpacking Many objects will deliver their iterables through tuples
list 3 methods for extracting lists from dictionaries.
when wrapped in the list() function: .keys() - returns a list of keys .values() - returns a list of values .items() - Returns a list of tuples
If you want to return a sorted list of dictionary view objects, what function should you use?
Ex: sorted(d.values())
what 3 statements can we use to add functionality to while loops?
break: breaks out of the current closest enclosing loop. continue: goes to the top of the closest enclosing loop. Pass: does nothing at all
Where are break and continue statements usually place?
Nested in conjunction with an if statement to perform an action based on some condition.
What function allows you to generate a list of integers?
range() with parameters for (start,stop,step) *the stop number will not be included
What is a generator function?
A special type of function that will generate information and not need to save it to memory.
What does the enumerate function do?
Turns a list into a list of tuples with the first item in the pair being the index position of the second item.
What is the zip function used for?
quickly generate a list of tuples by “zipping” up together to lists Ex: zip(list1,list2)
What else can the in operator be used for?
To quickly check if an object is in a list
What functions give you the min or max of a numerical list?
min()
max()