Python Part Flashcards
What does a condition evaluate to?
Boolean. True or False
What does a break statement do?
Immediately exits whatever loop it is in, and skips the remaining expressions in the code block.
In a for loop, there is an unbounded number of iterations that you don’t know. True or false?
False. You know the number of iterations in a for loop. In a while loop there is an unbounded number of iterations
Which of the following are mutable:
a. Strings
b. Lists
c. Tuples
b. lists
What can you use to slice a string?
string[start:stop:step]
What is a docstring?
A docstring is a string literal specified in source code that is used, like a comment, to document a specific segment of code; so, basically just an explanation of what the outputs should be and what to expect from a function, just to make your code easier to read and understand in case you wanna use it somewhere else
You can access the docstring with help(functionname)
Inside a function, you can access a variable defined outside. True or false?
True
Inside a function, you can modify a variable defined outside (without global variables). True or false?
False
What is a higher order function?
A higher-order function is a function that does at least one of the following:
takes one or more functions as arguments
returns a function as its result
Define the scope of a variable.
The scope is mapping of names to objects
I don’t know why I put this in
A return statement ends the execution of the function call and returns the result. True or false?
True
What does “lists are mutable” mean?
It means that list elements can be changed.
L.append(5). What does the dot do? (L is a list)
Not necessary append, but what does the dot between L and append mean
Lists are Python objects, everything in Python is an object. Objects have data. Objects have methods and functions. One can access this information by object_name.do_something ().
To combine lists together you can use the + operator. True or false?
True. L1 = [1,2,3] and L2 = [4,5,6]. L1+L2 = [1,2,3,4,5,6]
You can also use L1.extend(L2)
What does the method .pop on a list do?
It removes the element at the end of the list and returns the removed element.
When you use L.remove(element), what happens? What if you have the same element two times? What if the element is not in the list?
It looks for the element and removes it. If the element occurs multiple times, it only removes the first occurrence. If the element is not in the list, it gives an error.
What can you use to delete an element at a specific index in a list?
del(L[index])
Assume s is a string. What do you use to convert it to a list containing every character from s?
list(s)
What does s.split(“,”) do? s is a string
It is splitting a string on a character parameter. It splits on spaces if called without a parameter.
Think of a list. What is the difference between sort() and sorted()
L = [1,4,3,1]
sorted(L) - returns the sorted list, does not mutate L
L.sort() - mutates L = [1,2,3,4]
L.reverse() - mutates L = [4,3,2,1]
What do you use to get the absolute value of something?
abs()
Tuples are immutable. True or false?
True.