A procedure takes inputs and produces outputs. It is an abstraction that provides a way to use the same code to operate on different data by passing in that data as its inputs.
Defining a procedure:
def <Name>(<Parameters>): <Block>
The <Parameters> are the inputs to the procedure. There is one <Name> for each input in order, separated by commas. There can be any number of parameters (including none).
To produce outputs:
return <Expression>, <Expression>, ...
There can be any number of expressions following the return (including none, in which case the output of the procedure is the special value None).
Using a procedure:
<''Procedure''>(<Input>, <Input>, ...)
The number of inputs must match the number of parameters. The value of each input is assigned to the value of each parameter name in order, and then the block is evaluated.
The if statement provides a way to control what code executes based on the result of a test expression.
if <TestExpression>: <Block>
The code in <Block> only executes if the <TestExpression> has a True value.
Alternate clauses. We can use an else clause in an if statement to provide code that will run when the <TestExpression> has a False value.
if <TestExpression>: <BlockTrue> else: <BlockFalse>
The and and or operators behave similarly to logical conjunction (and) and disjunction (or). The important property they have which is different from other operators is that the second operand expression is evaluated only when necessary.
Loops provide a way to evaluate the same block of code an arbitrary number of times.
A while loop provides a way to keep executing a block of code as long as a test expression is True.
while <TestExpression>: <Block>
If the <TestExpression> evaluates to False, the while loop is done and execution continues with the following statement. If the <TestExpression> evaluates to True, the <Block> is executed. Then, the loop repeats, returning to the <TestExpression> and continuing to evaluate the <Block> as long as the <TestExpression> is True.
str: str(<Number>) = <String>
ord: ord(<One-Character String>) = <Number>
chr: chr(<Number>) = <One-Character String>
split: <String>.split() = [<String>, <String>, ... ]
A for loop provides a way to execute a block once for each character in a string (just like looping through the elements of a list):
for <''Name''> in <''String''>: <''Block''>
The loop goes through each character of the string in turn, assigning that element to the <Name> and evaluating the <Block>.