1/46
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
|---|
No study sessions yet.
algorithm
a procedure for solving a problem in terms of the actions to execute and the order in which these actions execute.
program control
Specifying the order in which statements (actions) execute in a program
pseudocode
An informal language that helps you develop algorithms without worrying about strict Python syntax; similar to everyday English and easily converted to a program.
pseudocode normally describes
Only statements representing actions that occur (e.g., input, output, or calculations).
sequential execution
statements in a program execute in the order in which they’re written.
transfer of control
the next statement to execute is other than the next one in sequence; achieved with control statements
The three forms of control written in all programs
Sequential execution
Selection statement
Iteration statement
Flowchart
A graphical representation of an algorithm or a part of one. Uses rectangles, diamonds, rounded rectangles, and arrows (flowlines) to show how forms of control operate.
Rectangle
indicates any action
Flowlines
arrows that show the order in which the actions execute
In a flowchart for a complete algorithm,
the first symbol is a rounded rectangle containing the word “Begin.” The last symbol is a rounded rectangle containing the word “End.”
In a flowchart for only a part of an algorithm,
use small circles called connector symbols.
Three selection statements that execute code based on a condition—an expression that evaluates to either True or False
if performs an action if a condition is True or skips the action if the condition is False.
if... else statement performs an action if a condition is True or performs a different action if the condition is False .
if... elif... else statement performs one of many different actions, depending on the truth or falsity of several conditions.
Two iteration statements
while and for
suite
group of statements controlled by a clause. Must be indented after a statement
Diamond
symbol for decision. Contains a condition that can be either True or False. Two flowlines emerging from it:
One indicates the direction to follow when the condition in the symbol is True
The other indicates the direction to follow when the condition is False
if… else statement flowchart

if... elif... else statement
Can test for many cases. Only the action for the first True condition executes.
if... elif... else statement flowchart

is else required or optional?
optional. Handles values that do not satisfy any of the conditions. Without an else, if no conditions are True, the program does not execute any of the statement’s suites.
while statement
Repeats one or more actions while a condition remains True.
To prevent an infinite loop, something in the while suite must change product's value, so the condition eventually becomes False
while statement flowchart

for statement
Repeat an action or several actions for each item in a sequence of items (e.g., string, list, or range)
string
a sequence of individual characters
Function print()’s end Keyword Argument
Changes what is printed after each item (default is a newline). print(character, end=' ')
Function print()’s sep Keyword Argument
Specifies what separates printed items (default is a space). print(1,2,3, sep=', ').
iterable
An object from which the for statement can take one item at a time
list
a comma-separated collection of items enclosed in square brackets [ ]. Mutable
Mutable
contents can be changed, added to, or removed after creation
range() function
Creates an iterable object that represents a sequence of consecutive integer values starting from 0 and continuing up to, but not including, the argument (end) value.
Augmented assignments
shorthand assignment where a variable updates itself
*=
+=
-=
/=
//=
**=
%=
sentinel values
denotes the end of a data set, but it is not part of the data
sentinel-controlled iteration
number of iterations is not known in advance. The loop continues to execute until a specific "sentinel value" is encountered, which acts as a signal to terminate the loop
f-string (formatted string)
allows inserting values into a string. The letter f before the string's opening quote indicates it's an f-string. You specify where to insert values by using placeholders delimited by curly braces { }. Replacement-text expressions (what’s inside { }) may contain values, variables, or other expressions.
Modifier
In an f-string, you can optionally follow a replacement-text expression with a colon (:) and a format specifier that describes how to format the replacement text.
Formatting type :.2f
The format specifier . formats the input as a floating-point number (f) with two digits to the right of the decimal point (.2). Rounds to the hundredths position
range(start, end+1, step)
position to start. Default is 0
position to end before
increment it if step is positive. decrement (go backwards) if step is negative
Formatting type :>2
right aligned (>) in a field of width 2
Formatting type :<
left aligned
break statement
immediately terminates the loop
continue statement
skips the rest of the current iteration of the loop and proceeds to the next iteration
In a while, the condition is then tested to determine whether the loop should continue executing
In a for, the loop processes the next item in the sequence (if any)
Boolean Operator and
Ensure that two conditions are both True
Boolean Operator or
Ensure that one or both of two conditions are True
Boolean Operator not
“Reverse” the meaning of a condition
mean
the average value in a set of values.
import statistics
statistics.mean(list)
median
the middle value when all the values are arranged in sorted order.
import statistics
statistics.median(list)
mode
the most frequently occurring value.
import statistics
statistics.mode(list)