Python Flashcards
What is snake case in python?
Using underscores to form a long variable name
What is the type of the result of a division operation in python?
floating point
How do you get integer value out out of a division in python?
By using the ‘//’ operator
Does // operator do a round-off?
No
How do you distinguish between a variable definition and further use in python?
You can’t, both have same syntax
f-strings are available from which python version?
3.6 onwards.
What are f-strings?
A convenient way to format different types into string
What is another way (than f-strings) to format strings without explicitly converting them?
By having placeholders in the string with “{}” and calling the format method of the string with requisite parameters
What is the constraint on the number of parameters for the format method of the string?
It can be more, but cannot be less
In what ways “format” can take parameters for a string?
format can take parameters as positional as well as keyword based.
How does “format” method take keyword based arguments?
keyword based arguments have to be given in the last after positional arguments. They cannot be mixed.
What is necessary for passing keyword based arguments to “format” method of string?
The string should contain the keyword in { } in it
Is string.format(name=name) valid? Both identifiers
Yes, the first one will be looked for in the string, while the second one is treated as a proper variable
How does f-string and format compare?
- f-string takes actual parameters, while format takes formal parameters (which is to be filled in by passing format with actual parameters)
- f-string can take expressions that evaluate to a value, while format cannot
Which function in python reads input from keyboard and returns a string?
input(“message”)
What are boolean keywords in python?
True and False
What does print take?
An expression that can be evaluated
What are python’s boolean operators?
and, not, or
What does and/or return?
- and returns first value if it is false, otherwise it returns the second value
- or returns the first value it is true, otherwise it returns the second value
What does not return?
not always returns True or False
What does list.remove() take?
The value of the element which needs to be removed from the list
Is parens mandatory for writing tuples?
No
How to minimally express a tuple of only one item?
a,
How do you add a new element to a tuple variable?
By adding “+” another tuple of single element to the variable: v = v + (e,)
How do we represent set1 - set2 in python?
set1.difference(set2)
How do we represent symmetric difference between two sets (set1 U set2) - (set1 AND set2)
set1.symmetric_difference(set2)
What type is “{}”
dict, not set
How do we represent set intersection in python?
set1.intersection(set2)
How do we represent set union in python?
set1.union(set2)
How do we represent an empty set in python?
set()
Does dictionary keep order of elements inserted?
Yes, in python 3.7
How to make a dict from values?
Have a list of 2-element tuples and feed it to dict()
A list of grades, a tuple of grades, a set of grades - which is not a correct choice
set of grades
A list of grades, a tuple of grades - which is a better option?
if grades are not going to be added, a tuple is better, otherwise a list is better
sum(), len() - can be used on which of the following data structures: set, list, tuple
all of them
How come we can pass values and expressions to if or while in python?
Python implicitly passes all those expressions through bool to get True or False
What is the else/if keyword in python?
elif
What is the difference between while loop and for loop in python?
While loop runs an undefined number of times - based on an external event, and for loop runs for a defined number of times
What is the difference between:
if a in b:
for a in b:
In the first one, “a” is a known identifier or a constant, while in the second one, “a” is a new identifier
What is the act of taking a collection into more than one variable is called?
destructuring
What does for d in dict give?
A list of all the keys
How do you iterate over values of a dict?
dict.values()
How do you destructure key, value from a dict?
dict.items()
what does while-else/for-else statement do in python?
If for doesn’t encounter a break, then else clause will be executed, otherwise else clause will not be executed
What is the difference between aList and aList[:]
The second one gives a new copy of aList
what does aList[0] and aList[-1] represent?
The first and the last elements of the list
what does aList[-x] represent?
xth element in the list starting from the last element with index as 1 towards left side
how do you mathematically repreent aList[x:y]?
[x, y)
What is the direction of slicing a list?
Always from left to right
What happens if a list is sliced with indices pointing to elements from right to left?
An empty list is returned
In aList[-x:-y], what is the constraint to get at least one element?
x > y
How do you differently explain list comprehension?
Expression followed by the for statement with an optional if statement
What is a readable way to write a list comprehension?
expression, for statement, if statement - each in one line
Comprehension is applicable for which of the following collections? list, set, dict
All of them
When you create a collection, think about _____
comprehension
How do you create a list of tuples with lists to feed to dict()?
With a zip function
Which is more simpler? a dict made out of comprehension or a dict made out of a zip?
dict made out of a zip
When does a dict made out of comprehension make sense over a dict made out of zip?
When we want to conditionally add the list elements into the zip
Does zip takes only two lists?
No, it can take more than two
What happens if the lists given to zip are of different lengths?
zip will make a list of tuples of the size of the smallest list
can the argument to a function be nameless?
Yes
How is a variable resolved i.e. searched for resolution?
First in local scope, then in global scope
What is the type of the value returned from a function which has no explicit return statement?
None
What are the two types of arguments in python to a function?
positional and keyword arguments