ControlStructure is a block of statements that analyzes variables and chooses a direction in which to go based on a given parameters.
Three Fundamental Control Structures:
Sequence Control Structure
Decision Control Structure
Loop Control Structure
Sequence Control Structure is a line-by-line execution of statements sequentially in the same order in which they appear in the script.
Decision Control Structure is a control structure that depends on whether a condition is true orfalse. This structure may skip the execution of an entire block of statements or even execute one block of statements instead of another.
Loop Control Structure is a structure that allows the execution of a block of
statements multiple times until a specified condition is met.
Simple conditions compare values of two expressions.
There are six relational operators in Python, shown in the following table:
< less than
<= less than or equal to
== equal to
>= greater than or equal to
> greater than
! = not equal to
The if statement in Python is used to implement a decision. Which is used in simple decision.
A simple decision has a form of: if statement
if <condition>:
<statement>
If the condition is mutually exclusive, we can simply use a two-way decision.
A two-waydecision in Python has the form of:
if <condition>:
<statements>
else:
<statements>
A multi-way decision structure in programming is a structure wherein a decision is nested inside another decision.
there is another way to write multi-way decisions that preserves the
semantics of the nested structures but makes it more simple.
multi way has a form of called an if-elif-else statement.
if <condition>:
<statements>
elif <condition>:
<statements>
elif <condition>:
<statements>
. . .
else:
<default statements>
type of loop control structure:
Definite Loops
Indefinite Loops
3 types of decision control structure:
simple decision
two-way decision
multi-way decision
Definite Loops
-It is a type of loop where the number of iterations/repetitions is knownbefore.
the start of the execution of the body of the loop.
Definite Loops
-It is implemented by using count-controlled loops.
Indefinite Loops
-It is a type of loop where the number of iterations/repetitions is not known before the start of the execution of the body of the loop.
Indefinite Loop
-It is implemented by using condition-controlled loops.
In Python, a definite loop has a general form of:
for <var> in <sequence>:
<body>
definite
The <body> is any sequence of Python statements.
definite
The <var> is called the loop index. A loop index is a variable that is used to control a loop.
The <sequence> consists of a list of values.
Example: [1, 3, 5, 7, 9, 11], [1, 2, 3, 4, 5, 6],
The range function, in general has this form: range(<expr>)
It will produce a sequence of numbers that starts with 0 and goes up to, but does not include, the value of <expr>.
range (start,n)
This function produces a sequence
that starts with the value start and
continues up to, but does not
include n.
range (start, n, step)
This function is like the
two-parameter version, except that
it uses step as the increment
between numbers.
An accumulator pattern is a common programming pattern in which a final answer is built a piece at a time in a loop.
The general accumulator pattern is as follows:
Initialize the accumulator variable
Loop until final result is reached
update the value of accumulator variable
In Python, an indefinite loop is implemented using a while statement.
The while statement has this form:
while <condition>:
<body>
indefinite
The <condition> here is a Boolean expression, that is a true or false statement.
indefinite
The <body> is a sequence of one or more statements.
The body of the loop executes repeatedly as long as the condition remains true.
When the condition is false, the loop terminates.
The general pattern for an interactive
loop is as follows:
set moredata to ”yes”
while moredata is ”yes”
get the next data item
process the item
ask user if there is moredata
String
-It is a sequence of characters.
String
-It is formed by enclosing some characters in quotation marks. Python also allows strings to be delimited by single quotes (apostrophes).
string is immutable.
lists and tuples are more general than strings. They can store multipleitems or even sequence of different data types in a single variable.