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
Q

What do mutation operators do?

A

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”

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

What are the equality tests in python? (2)

A
  1. object identity: is
  2. # equal values: ==
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Check if key is present in a dictionary

A

if key in ht:

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

Instantiate a set in python (empty and not empty)

A

s=set()

primes = {2, 3, 5, 7}

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

Look a function up by its name

A

bar = globals()[‘foo’]

bar() # invoke it

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

What’s true about comparisons in python?

A

15 < a <= 30

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

Describe the globals() and locals() functions

A

Unsure

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

How do you # apply a function to every value of an iterator?

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

Integer division operator

A

//

34
Q

How do you concatenate a list with itself an arbitrary number of times?

A

Multiplication operator

l3 = 100*[‘item’]

35
Q

What’s a tuple in python?

How do you instantiate them?

A

Immutable list

t2 = (1, 2, 3)

or

t2 = 1, 2, 3

36
Q

Undefine a function

A

del globals()[’’]

37
Q

How to check if two objects are the same object?

A

a is b

38
Q

How do you get the elements that belong to one of two sets, but not both? (2)

A
  1. not_both = primes ^ evens
  2. primes.symmetric_difference(evens)
39
Q

Operator for negation

A

-a

40
Q

How do you remove a dictionary entry using its key

A

del ht[‘foo’]

41
Q

What does *args in a function defintion do?

A

Can access each arg by accessing that key and val in the “kwargs” dict.

42
Q

In a loop, how to how the loop?

A

break:

43
Q

What is true about python objects?

A

Every object in Python has an associated hash map which stores the object’s attributes, called a “dict”

44
Q

How to make a new string with a string repeated n times?

A

s5 = 3 * s1 #==> ‘foofoofoo’

45
Q

Convert to string:

A

str(int) or str(float)

46
Q

Negate in python (2)

A

Not can be before or after subject.

if not key in ht:
# or
if key not in ht:

47
Q

How do you make a hash map in python? (2)

A

it’s the same as a dictionary.

ht = {} # empty dictionary
dct = { key1: value1, key2: value2 }
48
Q

What does this do?

l3 = 100*[‘item’]

A

one item referenced 100 times.
If you in-place modify any
element of the list, all the others
show that modification!

49
Q

How to access individual characters in a string?

A

Same as accessing an element in a list

50
Q

Operator for unary plus

A

+a

51
Q

What happens when an object is instantiated?

A

it gets a copy of the class dict

52
Q

How to check if loop exited via a break

A
53
Q

Syntax for set comprehension?

A

myset = { element for key in hashmap.keys() if key%2 == 0 }

54
Q

In a loop, how to skip to next iteration?

A

continue:

55
Q

Syntax for dictionary comprehension?

A

hashmap = { key:value for (key,value) in enumerate(iterable) }

56
Q

How to concatenate strings?

A

Use + operator

57
Q

Coerce from int to float

A

3 + 4.0)

58
Q

How to validate that two objects are not the same object

A

a is not b

59
Q

For loop with indexes for start and end

A

for i in range(BEG,END+1,STEP):

60
Q

How to find the index of a substring in python?

A

print(s.index(sub))

61
Q

Synax for lambda function

A

lambda arg1, arg2: arg1 + arg2

62
Q

Write out the four statements you can use in error handling

A
63
Q

What are the things that allow python to continue the same statement onto another line?

A
  1. backslash
  2. uncloses parentheses
64
Q

Can we replace a character in a string in python?

A

No. Python strings are immutable. Need to create a new string.

s2[1] = 'u' #==\> ERROR
s3 = s2[:1]+'u'+s2[2:] #==\> 'bur'
65
Q

Bitwise not operator

A

~

66
Q

How to iterate each character in a string

A

for c in “hello”:

67
Q

Convert to integer:

A

int(string)

68
Q

What does *args in a function defintion do?

A

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
Q

Convert to float:

A

float(string)

70
Q

How to find number of occurrences of substring in python?

A

s.count(‘c’)

71
Q

How do you get the elements that belong to either of two sets? (2)

A
  1. even_or_prime = primes | evens
  2. primes.union(evens)
72
Q

When are function definitions evaluated?

A

when they are encountered in the source file

73
Q

What do we know about this?

from library import *

A

Dangerous! Public
symbols in library
will replace any
local symbols with
the same name

74
Q

What’s the difference between import library as alias and from library import *

A

1st imports to a new namespace 2nd imports to the local namespace

75
Q

Why use finally at all? Why not just put the code under and remove the indent?

A

Because using except keeps you in the same scope as the try block, so you can access things defined in the try

76
Q

When must you use the parentheses when defining a tuple?

A

When you’re making a tuple with just one element

77
Q

what type does 1+10.0 evaluate to?

A

Float

78
Q

What type does 1 + ‘10’ evaluate to?

A

Throws error

79
Q

What do we know about arithmetic on immutable types in python?

A
  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
Q

What happens with “and” and “or” comparisons

A

Short circuit evaluation. Processes the conditions sequentially and if any aren’t satisfied when it’s checked, it ends evaluation of the condition

81
Q

How to run something only if the loop exits without a “break” statement?

A

use else statement after the while

82
Q

How do you raise to an exponent in python?

A

**