Python Flashcards

1
Q

What does it mean that Python has dynamic types and what practical side effects are a result of that?

A

Only runtime objects have a value, not the variables themselves (that store the objects). This allows you to store different types in the same variable during execution, and means you don’t have to declare variable types when declaring them.

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

Does Python have a strong or weak typing system?

A

Strong

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

What does it mean that Python is strongly typed?

A

Variables won’t change types unless it is explicitly cast to something else. So ‘125’ doesn’t automatically become an int when you try to add 4 to it, even though it could be done, you would have to explicitly cast it to an int yourself.

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

How to represent infinity?

A

inf = float(‘inf’) or import math, math.inf

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

What does REPL stand for?

A

Read Evaluate Print Loop

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

When will the code in the else condition of a for (or while) loop run?

A

If the loop completed without breaking.

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

Does the “or” operator return anything? If so, what? Does it always evaluate every operand and why or why not?

A

Yes. The first truthy operand, or the last operand regardless of its truthiness. No, it will stop as soon as it finds a truthy operand which could be the first.

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

Does the “and” operator return anything? If so what? Does it always evaluate every operand, and why or why not?

A

Yes, the last operand (if all were truthy) or the first non-truthy operand. No, it will stop early if it finds something non-truthy, as anything non-truthy would make the result non-truthy.

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

How to get the lowercase version of a string?

A

string.lower()

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

Name two ways to get environment variables w/ Python. Why is one preferred over the other?

A

os.environ[‘variable’] (will error if variable doesn’t exit) or os.getenv(‘variable’) better cause will return None if doesn’t exist, can optionally specify a default value.

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

How do you open a file in python for reading and writing?

A

f = open(‘path’, mode=’r+t’) or r+b for binary, t is text

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

How to open a new file in Python?

A

mode=’x’, which will error if file alread exists. ‘w’ would work but would blank out the file if it does exist.

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

What can you use in Python to ensure a file is closed once operations are complete?

A

with open() as var:
stuff
Will call close if stuff fails or completes.

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

How to access arguments passed to script?

A

Import sys, sys.argv. 0 is path to script. Or argparse

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

What is the flow for exception handling in python?

A

Try, except - if try hit an error, else - if no error, finally - runs regardless of if hit an error or not

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

What is chaining a comparison in Python? When is it preferred over non chained comparisons and why?

A

Chaining is writing 1 < x <= y instead of 1 < x and x <= 5. It is almost always preferred as it will only evaluate x once in the chained version, rather than twice in the non chained. And x is a function or calculation, that can speed things up quite a bit.

17
Q

What are the prerequisites for x in y to work?

A

y just needs to be a sequence to contains elements of type x. So even string will work, just very slow (linear time)

18
Q

How do you define a set literal?

A

{item, item, item} don’t use set(), cause it only takes one argument, so you’ll have to make a tuple inside of it or something.

19
Q

What is the ternary operator?

A

if else on one line

EXPR_1 if CONDITION else EXPR_2

20
Q

Explain LEGB

A
Order in which a variable name gets resolved.
Locals - (that aren't explicitly declared global)
Enclosing - From inner to outer (of nested functions)
Globals - Defined at top level of a module or explicitly declared global from a local scope
Built-in - Reserved keywords
21
Q

How to interact with a global variable inside a local scope?

A

To read you don’t have to do anything special cause of LEGB. To modify it you need to first declare it as global then you can modify it.

22
Q

How do you interact with an outer variable (but not global) from a nested scope?

A

Use ‘nonlocal’ keyword as you would use ‘global’. i.e. declare the variable as ‘nonlocal’, then you can modify it. No need to do this if you only want to read cause LEGB.

23
Q

How do you clean up the ends of a string?

A

lstrip, rstrip, strip (head and tail). Each take one arg, a string, every character in the string will be stripped. Continues stripping until encounters a character not in the strip string. without an arg strips all whitespace.

24
Q

How far in will lstrip and rstrip go?

A

Until they hit a char not in the stripping string. Order doesn’t matter.

25
Q

How would you filter/process a string without creating a new string in memory, and then print it out?

A

Use a generator comprehension. Then print it out as ‘‘.join(generator)

26
Q

What arguments does split() take?

A

First is string to split on, whitespace by default, then maxsplit=n is how many times to split.

27
Q

What is calling multiple methods on an object at once called? Give an example.

A

Chaining

str.lower().replace(‘!’)k

28
Q

What is the return type of / in Python? Always?

A

Float, yes.

29
Q

Where should whitespce go when using : in Python?

A

If not at the end of a line (for if thing:) then it’s no space before but space after. So like my_dict = { ‘hi’: ‘bye’}

30
Q

How can you make large numeric literals easier to read? Where is this supported?

A

Since python 3.6 can use _ which has no meaning. So 100,000 can be written 100_000.

31
Q

How to get a random integer in Python?

A

random. randrange() works like range

random. randint() inclusive range a to b parameters

32
Q

How to get a random element from a sequence in Python? How would you get x random elements from that sequence?

A

random. choice(seq)

random. sample(sequence, x)

33
Q

How to put a sequence in random order?

A

random.shuffle(seq)

Doesn’t work on immutable sequences

34
Q

How do you get an attribute of an object, if the attribute name is stored as a string?

A

getattr(object, attr_name_str)

OR

object.__getattribute__(name_str)

35
Q

What are the two ways to call an instance method?

A

self.method() or Class.method(self)

36
Q

How do you reference a Class as a type (for type hinting) within the definition of its methods?

A

Just using Class name won’t work as it isn’t fully define. Can put it within string literals.