PCEP study materials
PCEP study resources

PCEP flashcards

PCEP · Build recall with a focused review session.

32 cards. Read the term and the definition here. The deck at the top of this page is optional.

Return type of input()
input() always returns a string, even when the user types digits. Convert with int() or float() before arithmetic, otherwise + concatenates text and comparisons behave as string comparisons.
Floor division operator
The // operator divides and rounds the result down to the nearest integer, returning a float if either operand is a float. For example, 7 // 2 is 3 and -7 // 2 is -4.
None value
None is a singleton of type NoneType representing the absence of a value. Functions without a return statement produce None, and the correct test is 'x is None', not equality.
print() sep and end
The sep keyword sets the string inserted between printed arguments and defaults to one space. The end keyword sets what follows the last argument and defaults to a newline.
Integer literal prefixes
The prefix 0b marks a binary literal, 0o an octal literal and 0x a hexadecimal literal. Underscores may separate digits for readability, as in 1_000_000.
Python identifier rules
An identifier may contain letters, digits and underscores but cannot begin with a digit. Identifiers are case-sensitive, and keywords such as class, for or None cannot be used as names.
Exponentiation associativity
The ** operator is right-associative, so 2 ** 3 ** 2 is evaluated as 2 ** (3 ** 2) and gives 512. It also binds tighter than unary minus, so -2 ** 2 equals -4.
Loop else clause
The optional else block of a for or while loop runs after the loop finishes normally, but is skipped entirely if the loop exits early through a break statement.
range() stop value
range(start, stop, step) generates integers from start up to but never including stop. A negative step counts downward, and a step of zero raises ValueError.
Falsy values in Python
Zero of any numeric type, empty strings, empty lists, tuples, dictionaries and sets, None and False all evaluate as false in a conditional. Every other object is truthy.
break versus continue
break terminates the innermost enclosing loop immediately and skips its else block. continue skips the remaining statements of the current iteration and moves to the next one.
Chained comparisons
An expression such as 0 <= x < 10 is evaluated as 0 <= x and x < 10, with the middle operand evaluated only once. Chains may combine any of the comparison operators.
Conditional expression syntax
The form A if condition else B evaluates condition first, then yields A when it is true and B when it is false. It is an expression, so it must produce a value.
Single-element tuple syntax
A one-item tuple requires a trailing comma, written as (1,) or 1,. Without that comma, (1) is simply an integer wrapped in parentheses and not a tuple at all.
dict get() method
d.get(key, default) returns the value stored for key, or default when the key is absent; default is None if omitted. Unlike d[key], it never raises KeyError.
Empty set creation
The literal {} creates an empty dictionary, not an empty set. Use set() to build an empty set; braces containing elements, such as {1, 2}, do create a set.
Slice expression
sequence[start:stop:step] returns a new sequence from start up to but excluding stop. Negative indices count from the end, and omitted bounds default to the sequence ends.
append versus extend
append(x) adds the single object x as one new element, while extend(iterable) adds each item of that iterable individually, increasing the list length by the number of items.
String immutability
Strings cannot be modified in place: methods such as upper(), replace() and strip() return new strings. Assigning to an index, as in s[0] = 'x', raises TypeError.
Set operators
The operator | gives union, & intersection, - difference and ^ symmetric difference, and all require sets on both sides. The named methods like union() accept any iterable instead.
Mutable default argument
Default parameter values are evaluated once, when the def statement runs. A mutable default such as a list is therefore shared across calls, so appended items accumulate unexpectedly.
LEGB name resolution
Python looks a name up in local, then enclosing function, then global, then built-in scope. Assignment inside a function creates a local name unless global or nonlocal is declared.
finally block purpose
A finally block always executes, whether the try block completed normally, raised an exception, or executed a return statement. It is used for cleanup such as closing files.
try else block
The else clause of a try statement runs only when the try block raised no exception, and it runs before finally. Exceptions raised inside else are not caught by that try's handlers.
lambda expression
lambda parameters: expression creates an anonymous function whose body is a single expression whose value is returned. It cannot contain statements such as assignments or return.
IndexError versus KeyError
IndexError is raised when a sequence index falls outside the valid range, while KeyError is raised when a dictionary is indexed with a key that is not present in it.
File mode w
Opening a file with mode 'w' truncates it to zero length if it already exists and creates it otherwise. Writing begins at the start, so previous contents are lost.
with statement for files
The with statement relies on a context manager to close the file automatically, even when an exception is raised inside the block, replacing a manual try/finally close.
readline() behaviour
readline() reads one line from the file and returns it including the trailing newline character, or returns an empty string once the end of the file has been reached.
Append mode a
Mode 'a' opens the file for writing at the end, creating it if it is missing and never truncating existing content. Mode 'a+' additionally permits reading from the file.
Module caching on import
An import statement executes a module's top-level code only the first time. Later imports reuse the cached module object that Python stores in sys.modules.
random.randint bounds
random.randint(a, b) returns a random integer n satisfying a <= n <= b, so both endpoints are included. It is equivalent to randrange(a, b + 1).