Python Flashcards

1
Q

Why do long integers take longer to process in Python than in most other languages?

A

Because they are stored internally as an array of digits.

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

What is the difference between double quoted strings and single quoted strings in python?

A

There is no difference.

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

Are escape characters valid in Python?

A

Yes.

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

How are invalid escape characters treated in Python?

A

They treated like regular text.

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

How can escape characters be turned off in Python?

A

By prefacing the string with an r (to specify a raw string).

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

Do multi line strings require triple single-quotes or triple double-quotes?

A

Either.

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

When does the interpreter automatically add new line characters to multi line strings?

A

Always, unless the line ends in a backslash.

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

Does prefacing a multi line string in python with ‘r’ prevent the interpreter from adding new line characters?

A

No.

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

Does python provide bounds checking for when indexing strings?

A

Yes.

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

What is the output of the following?
x = “carlisle”
print(x[2:3])

A

r
(since slices use inclusive/exclusive format)

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

What is the output of the following?
x = “car”
print(x[::-1])

A

rac

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

What is the output of the following?
x = “bugman”
print(x[2:44])

A

gman
(valid, despite apparent out of bounds)

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

What is the output of the following?
x = “bugman”
x[2] = “s”
print(x)

A

This issues an error, since strings are immutable in Python.

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

Is the following valid, where x is a list of length 4?
print(x[2:99])

A

Yes.

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

How are lists in Python implemented at the memory level?

A

As dynamically resized arrays of pointers.

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

Why aren’t lists in Python implemented as linked lists at the memory level?

A

Because operations on linked lists (especially indexing) is much too slow.

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

What is the asymptotic complexity of accessing / updating a list element by index?

A

O(1)

18
Q

What is the asymptotic complexity of appending to a list?

A

O(1) if space is available, O(n) otherwise.

19
Q

What is the asymptotic complexity of inserting into a list?

A

O(n)

20
Q

What is the asymptotic complexity of building a list by inserting every element into the first position?

A

O(n^2)

21
Q

How can you attempt to access a key’s value in a dictionary, without raising a KeyError exception should that key not exist?

A

By using dict.get(key), over dict[key].

22
Q

What is the (amortized) cost of a lookup in a dictionary?

A

O(1), as there is a constant number of steps in executing the hash function.

23
Q

How are tuples of 1 element created?

A

With a trailing comma.

24
Q

What are the 2 primary differences between sequences and sets in python?

A

Unlike sequences, sets are unordered and do not allow duplicates.

25
Q

How do you initialize an empty set in python?

A

Using the set() function, since {} creates an empty dictionary.

26
Q

How do sets prevent duplicates?

A

By being internally implemented as a hash table.

27
Q

Does adding an element that already exists to a set cause an error?

A

No, the duplicate value is simply not inserted.

28
Q

Why must set elements be immutable in python?

A

Because they must be hashable, as the underlying data structure is a hash table.

29
Q

4 spaces can always be used in place of tab characters for delimiting coding blocks.

A

It is generally recommended to use 4 spaces over a tab, but never use both techniques in the same source file.

30
Q

Which data types can follow the “in” keyword?

A

Sequences.

31
Q

A value of NULL is assigned to all uninitialized variables.

A

False. Variables meant to be defined later must be explicitly set equal to ‘None,’ which is Python’s equivalent to Null.

32
Q

Functions return void by default.

A

False, they return None by default.

33
Q

When are module imports handled in Python, and when are include directives handled in C?

A

In C, extended code is converted into object code at compile time, whereas the Python import process occurs at run time.

34
Q

For what files does the Python interpreter create .pyc files?

A

Only for imported modules, and not for the main script.

35
Q

A module may be imported more than once.

A

A module may be subject to multiple import statements, but the interpreter will not physically import a module more than once.

36
Q

Which environment allows developers to add their own folders to the module search path?

A

PYTHONPATH
(similar to Java’s CLASSPATH)

37
Q

How do Python global variables and C global variables differ?

A

Python global variables aren’t global to all modules, unless imported.

38
Q

When can you not use the ‘global’ keyword to access a global variable from within a function?

A

If a local variable of the same name has already been declared.

39
Q

If mod_name is the name of a module, when does the following code fail?
dir(mod_name)

A

When mod_name hasn’t been imported.

40
Q

When is the __name__ global variable not accurately set to the name of the current module?

A

When the current module represents the starting module for the application, in which case __name__ is set to “__main__”.

41
Q

How can a package be defined such that only 1 of its modules it imported when “from package import *” is called.

A

__all__ = [“module_name”] must be included in the packages __init__.py file.