Python Flash Cards
Python Flash Cards
Operator
Type Operator Description Syntax/Example
Page 1
Sheet1
Page 2
Sheet1
Edge
Case/Best
Explanation Practice
Ensure both
are numbers;
otherwise, it
will
concatenate
Adds a and b. strings.
Negative
numbers
should be
handled
Subtracts b from a. correctly.
Multiplying by
zero results in
Multiplies a by b. zero.
Division by
zero raises a
ZeroDivisionE
Divides a by b, result is a float. rror.
Useful for
integer
division but be
cautious with
negative
Floors the division. numbers.
Returns the remainder of a Check for zero
divided by b. to avoid errors.
Large powers
can cause
Raises a to the power of b. overflow.
Use is for
identity
Evaluates to True if a is equal comparison
to b. (same object).
Evaluates to True if a is not
equal to b.
Type
comparison
Evaluates to True if a is greater should be
than b. consistent.
Evaluates to True if a is less
than b.
Page 3
Sheet1
Short-circuits;
Evaluates to True if either a or if a is true, b is
b is true. not evaluated.
Page 4
Sheet2
Edge
Syntax/ Case/Best
Function Description Example Explanation Practice
Reads a line of Prompts the Always
input from the user for input validate the
user as a and stores it in input to avoid
input() string. input(prompt) a variable. type errors.
Converts a Takes input as Use try-except
string or a string and for error
number to an converts it to handling on
int() integer. int(value) an integer. conversion.
Converts a
string or Takes input as Same as
number to a a string and above; handle
floating-point converts it to a exceptions for
float() number. float(value) float. invalid input.
Converts a Converts a
value to a number to a
str() string. str(value) string.
Page 5
Sheet3
Edge
Syntax/ Case/Best
Operation Description Example Explanation Practice
Be mindful of
Joins two or memory usage
more strings Combines the with large
Concatenation together. str1 + str2 two strings. strings.
Repeats a
string a
specified Ensure n is a
number of Repeats str n non-negative
Repetition times. str * n times. integer.
Be cautious
Extracts with index
Extracts a characters values; out-of-
portion of a from start to range raises an
Slicing string. str[start:end] end-1. error.
Returns the
number of Returns the
characters in a length of the
Length string. len(str) string.
Page 6
Sheet3
Concatenates
Joins elements elements of a Ensure the list
of a list into a delimiter.join(l list into a is not empty to
Joining single string. ist) string. avoid errors.
Checks if a
substring Returns True if
Checking exists within a substring is
Substring string. substring in str found.
Allows
dynamic Use f-strings
Formats f"{value}" or insertion of for better
strings with "{} values into readability and
Formatting placeholders. {}".format() strings. performance.
Page 7
Sheet4
Edge
Syntax/ Case/Best
Concept Description Example Explanation Practice
Declaring a Creates a Keep variable
variable and variable x and names
Variable assigning a assigns it the descriptive for
Declaration value. x = 10 value 10. readability.
Must start with
a letter or
underscore,
can include Avoid using
Rules for letters, reserved
Variable naming numbers, and keywords (e.g.,
Naming variables. variable_name underscores. class, def).
A variable can
change its
Python uses type; here, x Ensure type
dynamic first holds an consistency
Dynamic typing for x = 5; x = integer and where
Typing variables. "Hello" then a string. necessary.
Suggesting a Use type hints
variable's type Indicates that a to improve
for clarity def add(a: int, and b should code
Type Hinting (optional). b: int) -> int: be integers. readability.
Naming
convention for Constants are Document the
constants usually named purpose of
(convention in uppercase constants in
Constants only). PI = 3.14 letters. comments.
Ensure the
Assigning number of
values to variables
multiple Assigns 1 to a, matches the
Multiple variables in 2 to b, and 3 to number of
Assignment one line. a, b, c = 1, 2, 3 c. values.
Page 8
Sheet5
Edge
Control Syntax/ Case/Best
Structure Description Example Explanation Practice
Executes the
Allows block if the Keep
branching condition conditions
Conditional logic based on evaluates to simple for
Statements conditions. if condition: True. readability.
Executes if the
previous Use elif to
elif conditions are avoid deeply
another_condit False and this nested if
ion: one is True. statements.
Executes if
none of the Ensure else is
previous only used
conditions are when
else: True. applicable.
Validate inputs
to avoid
Checks if a is unexpected
if a < b: less than b. comparisons.
Short-circuit
behavior can
Combine Evaluates to save
Logical multiple True if both a computation
Operators conditions. if a and b: and b are True. time.
Can be used to
check for
Evaluates to multiple
True if either a conditions
if a or b: or b is True. efficiently.
Evaluates to Keep logic
True if a is clear to avoid
if not a: False. confusion.
Repeatedly
executes a Use break to
block of code Iterates over exit and
until a each item in an continue to
condition is for item in iterable (like a skip to the next
Loops met. iterable: list or string). iteration.
Continues Avoid infinite
looping as long loops by
as the ensuring the
while condition is loop condition
condition: True. changes.
Page 9
Sheet5
Page 10
Sheet6
Edge
Syntax/ Case/Best
Concept Description Example Explanation Practice
Creates a
Defines a def function that Use descriptive
Function reusable block function_name can be called names for
Definition of code. (parameters): later. clarity.
Ensure
Inputs to the def Takes name as parameters are
Parameters function. greet(name): an input. documented.
Page 11