Python Flashcards
Complex number in python
x = 1+2j
Left shift and arithmetic right shift operators
<<
and
>>
What does the “def” statement do?
- def statements are executed immediately, generating a function object and binding it to a name in the current module
Hashtable syntax in python
x = {}
Differences between the bitwise and boolean operators in python?
boolean are “and”, “or”, and “not”.
Common use of tuples?
Return multiple values
What do an object’s attributes include?
include both variables and functions
How to declare class in python? Constructor? add attributes?
What is interesting about the keys in python dictionaries?
Can have multiple data types for the keys in the same dictionary
Can you define two variables on one line?
It’s discouraged, but yes: f = 2.5; i = 1
What data structure is this?
evens = {2, 4, 6, 8}
A set, not a dictionary
How do you get the elements that belong to both sets? (2)
- set_1 & set_2
- set_1.intersection(set_2)
Operator for matrix product
@
What does this do?
from library import obj, obj2
From a certain module, import certain objects
What does * and ** do in a function call?
- Expands an iteratable of the args
- Expands a dictionary of the args with the names matching those in the function defintion
What are the automatic conversion to boolean rules for these values?
- numbers
- lists
- dicts
- strings
- None
- zero is false, nonzero is true
- empty is false, non-empty is true
- empty is false, non-empty is true
- empty is false, non-empty is true
- None is false, class objects are true
What are differences between python and Java? (3)
How do you apply a test to every value of an iterator, pass only those for which the test is True
How do you get the elements in a set that don’t belong to another set? (2)
This is called the difference
- odd_primes = primes - evens
- primes.difference(evens)
What’s true of python variables
they’re just pointers to objects
General syntax of list comprehensions
Check if substring exists in string
sub in s
What do we know about default args in python?
evaluated at the time the function definition is evaluated
What do mutation operators do?
For mutable objects, hanges that actual object, instead of reassigned same variable name to a new value.
x = x + y creates a new object
– x += y modifies x “in place”
What are the equality tests in python? (2)
- object identity: is
- # equal values: ==
Check if key is present in a dictionary
if key in ht:
Instantiate a set in python (empty and not empty)
s=set()
primes = {2, 3, 5, 7}
Look a function up by its name
bar = globals()[‘foo’]
bar() # invoke it
What’s true about comparisons in python?
15 < a <= 30
Describe the globals() and locals() functions
Unsure
How do you # apply a function to every value of an iterator?