Python Knowledge Flashcards

Stay fresh on Python concepts

1
Q

How to reverse a string (x) in Python?

A

x[::-1]

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

List vs. Tuple

A

Definition:
Lists are mutable sequences, while tuples are immutable sequences.

  • Lists can be changed in-place; tuples cannot.
  • Tuples can be used as dictionary keys (hashable), whereas lists cannot.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

List Comprehensions

A

Definition:
A concise syntax for creating lists from iterables with optional conditions.

  • Written as:
    [expression for item in iterable if some condition]

e.g., Increment item when <9
[item+1 for item in items if item<9]

  • Improves readability and can be more efficient than equivalent for-loops.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Generators

A

Definition:
Functions that use yield to produce a sequence of values lazily.

  • Saves memory by not storing the entire sequence in memory.
  • Can be iterated over just once (stateful).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

GIL (Global Interpreter Lock)

A

Definition:
A mutex in CPython preventing multiple native threads from executing Python bytecodes simultaneously.

  • Affects CPU-bound multi-threading (no true parallelism).
  • I/O-bound tasks can still benefit from threading.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Decorator

A

Definition:
A callable (function or class) that takes another function and extends or modifies its behavior.

  • Commonly used for logging, synchronization, caching, or authentication.
  • Syntax: @my_decorator above the function definition.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

__init__ vs. __new__

A

Definition:
__init__ initializes an instance after it’s created; __new__ actually creates the instance (rarely overridden).

__init__ is used for most initialization tasks.

__new__ is useful for customizing the creation of immutable types or singletons.

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

Python Memory Management

A

Definition:
Uses reference counting and a cyclic garbage collector to free unreachable objects.

  • The gc module handles detection and cleanup of reference cycles.
  • Reference counting is immediate for zero-count objects.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly