Python Flashcards

1
Q

Complex number in python

A

x = 1+2j

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

Left shift and arithmetic right shift operators

A

<<

and

>>

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

What does the “def” statement do?

A
  1. def statements are executed immediately, generating a function object and binding it to a name in the current module
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Hashtable syntax in python

A

x = {}

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

Differences between the bitwise and boolean operators in python?

A

boolean are “and”, “or”, and “not”.

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

Common use of tuples?

A

Return multiple values

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

What do an object’s attributes include?

A

include both variables and functions

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

How to declare class in python? Constructor? add attributes?

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

What is interesting about the keys in python dictionaries?

A

Can have multiple data types for the keys in the same dictionary

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

Can you define two variables on one line?

A

It’s discouraged, but yes: f = 2.5; i = 1

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

What data structure is this?

evens = {2, 4, 6, 8}

A

A set, not a dictionary

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

How do you get the elements that belong to both sets? (2)

A
  1. set_1 & set_2
  2. set_1.intersection(set_2)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Operator for matrix product

A

@

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

What does this do?

from library import obj, obj2

A

From a certain module, import certain objects

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

What does * and ** do in a function call?

A
  1. Expands an iteratable of the args
  2. Expands a dictionary of the args with the names matching those in the function defintion
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are the automatic conversion to boolean rules for these values?

  • numbers
  • lists
  • dicts
  • strings
  • None
A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What are differences between python and Java? (3)

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

How do you apply a test to every value of an iterator, pass only those for which the test is True

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

How do you get the elements in a set that don’t belong to another set? (2)

A

This is called the difference

  1. odd_primes = primes - evens
  2. primes.difference(evens)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What’s true of python variables

A

they’re just pointers to objects

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

General syntax of list comprehensions

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

Check if substring exists in string

A

sub in s

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

What do we know about default args in python?

A

evaluated at the time the function definition is evaluated

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
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”
26
What are the equality tests in python? (2)
1. object identity: is 2. # equal values: ==
27
Check if key is present in a dictionary
if key in ht:
28
Instantiate a set in python (empty and not empty)
s=set() primes = {2, 3, 5, 7}
29
Look a function up by its name
bar = globals()['foo'] bar() # invoke it
30
What's true about comparisons in python?
15 \< a \<= 30
31
Describe the globals() and locals() functions
Unsure
32
How do you # apply a function to every value of an iterator?
33
Integer division operator
//
34
How do you concatenate a list with itself an arbitrary number of times?
Multiplication operator l3 = 100\*['item']
35
What's a tuple in python? How do you instantiate them?
Immutable list t2 = (1, 2, 3) or t2 = 1, 2, 3
36
Undefine a function
del globals()['']
37
How to check if two objects are the same object?
a is b
38
How do you get the elements that belong to one of two sets, but not both? (2)
1. not\_both = primes ^ evens 2. primes.symmetric\_difference(evens)
39
Operator for negation
-a
40
How do you remove a dictionary entry using its key
del ht['foo']
41
What does \*args in a function defintion do?
Can access each arg by accessing that key and val in the "kwargs" dict.
42
In a loop, how to how the loop?
break:
43
What is true about python objects?
Every object in Python has an associated hash map which stores the object's attributes, called a “dict”
44
How to make a new string with a string repeated n times?
s5 = 3 \* s1 #==\> 'foofoofoo'
45
Convert to string:
str(int) or str(float)
46
Negate in python (2)
Not can be before or after subject. if not key in ht: # or if key not in ht:
47
How do you make a hash map in python? (2)
it's the same as a dictionary. ``` ht = {} # empty dictionary dct = { key1: value1, key2: value2 } ```
48
What does this do? l3 = 100\*['item']
one item referenced 100 times. If you in-place modify any element of the list, all the others show that modification!
49
How to access individual characters in a string?
Same as accessing an element in a list
50
Operator for unary plus
+a
51
What happens when an object is instantiated?
it gets a copy of the class dict
52
How to check if loop exited via a break
53
Syntax for set comprehension?
myset = { element for key in hashmap.keys() if key%2 == 0 }
54
In a loop, how to skip to next iteration?
continue:
55
Syntax for dictionary comprehension?
hashmap = { key:value for (key,value) in enumerate(iterable) }
56
How to concatenate strings?
Use + operator
57
Coerce from int to float
3 + 4.0)
58
How to validate that two objects are not the same object
a is not b
59
For loop with indexes for start and end
for i in range(BEG,END+1,STEP):
60
How to find the index of a substring in python?
print(s.index(sub))
61
Synax for lambda function
lambda arg1, arg2: arg1 + arg2
62
Write out the four statements you can use in error handling
63
What are the things that allow python to continue the same statement onto another line?
1. backslash 2. uncloses parentheses
64
Can we replace a character in a string in python?
No. Python strings are immutable. Need to create a new string. ``` s2[1] = 'u' #==\> ERROR s3 = s2[:1]+'u'+s2[2:] #==\> 'bur' ```
65
Bitwise not operator
~
66
How to iterate each character in a string
for c in "hello":
67
Convert to integer:
int(string)
68
What does \*args in a function defintion do?
Can access each arg by accessing that element in the "args" list. Can also be used with words other than "args". "args" is just a convention
69
Convert to float:
float(string)
70
How to find number of occurrences of substring in python?
s.count('c')
71
How do you get the elements that belong to either of two sets? (2)
1. even\_or\_prime = primes | evens 2. primes.union(evens)
72
When are function definitions evaluated?
when they are encountered in the source file
73
What do we know about this? from library import \*
Dangerous! Public symbols in library will replace any local symbols with the same name
74
What’s the difference between import library as alias and from library import \*
1st imports to a new namespace 2nd imports to the local namespace
75
Why use finally at all? Why not just put the code under and remove the indent?
Because using except keeps you in the same scope as the try block, so you can access things defined in the try
76
When must you use the parentheses when defining a tuple?
When you’re making a tuple with just one element
77
what type does 1+10.0 evaluate to?
Float
78
What type does 1 + ‘10’ evaluate to?
Throws error
79
What do we know about arithmetic on immutable types in python?
1. It creates a new object even for mutation operators 2. This is interesting because this happens even though all types are just references to objects
80
What happens with “and” and “or” comparisons
Short circuit evaluation. Processes the conditions sequentially and if any aren’t satisfied when it’s checked, it ends evaluation of the condition
81
How to run something only if the loop exits without a “break” statement?
use else statement after the while
82
How do you raise to an exponent in python?
\*\*