Python W3 - Control Flows
Python W3 - Control Flows
Python
Muhammet Resul Ertuğrul /
Abdullah Rehman
LP CS - Intro to
Programming
Control Flows - IF
Statements
In the real world we often need to
make decisions based on a
situation. For example, how we are
going to go to school today:
If Statements
The flowchart shows the two
Why do we need different options of how to get to
decisions? school, either by taking a taxi or
walking. The green diamond which
We can implement this in Python
using control structures. The first
control structure we will look at is
the if statement.
If Statements
Why do we need
decisions?
if statements (and other control
structures) control a chunk of code
called a block. In Python, a
statement is indented to indicate it
belongs to a block.
For example:
If Statements
Why do we need The indentation must be to the
same depth for every statement in
decisions? the block and must be more than
the statement controlling the
This example is broken because
the indentation of the two lines is
different.
equal to == a == 7
not equal to != a != 7
less than < a<6
less than or equal to <= a <= 7
greater than > a>7
greater than or >= a >= 6
equal to
You can use a print statement to
test these operators in conditional
expressions:
Comparison Operators
Now we can bring together
everything we've learned in this
section, and write programs that
make decisions based on numerical
input. The example below makes
two decisions based on the value
in x:
Logical Operators
Show the letter grade
Ask the user their final grade.,
where averages equal to or above
90 are an A, equal to or above 80
are a B, equal to or above 70 are a
C, equal to or above 60 are a D and
below 60 are an F.
If Statements Exercise