0% found this document useful (0 votes)
2 views21 pages

Python W3 - Control Flows

The document introduces Python programming concepts focused on control flows, specifically if statements, else clauses, and elif statements. It explains how to make decisions in code using indentation for blocks and provides examples for practical exercises. Additionally, it covers comparison and logical operators to enhance decision-making capabilities in programming.

Uploaded by

anilxbayrak
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
2 views21 pages

Python W3 - Control Flows

The document introduces Python programming concepts focused on control flows, specifically if statements, else clauses, and elif statements. It explains how to make decisions in code using indentation for blocks and provides examples for practical exercises. Additionally, it covers comparison and logical operators to enhance decision-making capabilities in programming.

Uploaded by

anilxbayrak
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 21

Introduction to

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.

Here’s a program that would help


us make a decision about how to
get to school. Of course the
computer can't check the weather
on its own, so it will have to ask us.

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.

You can fix it by making both print


If Statements statements indented by the same
Why do we need number of spaces (usually 2).

decisions? The block is often called the body


of the control structure. Every
Ali Baba is trying to open a cave
door to find the treasure. His
brother overhears thieves saying
the password, but he can't quite
work out what it is.

Write a program to ask Ali for a


password. If the password is
correct, the cave door will be
opened. Otherwise nothing will
happen. Your program should work
If Statements Exercise like this when Ali types "Open
sesame!":
All of the examples so far have
done something when a condition
is True, but nothing when the
condition is False.

In the real world we often want to


do one thing when a condition is
True and do something different
when a condition is False.

For example, we may want to


welcome a friend, but find out
If…. Else more about someone we don't
know. We can do that using an else
Decisions with two clause:
options
If we go back to rainy day
example. We can simplify that
example using else:

Here, either the first or second


print statement will be executed
but not both. Notice the condition
If…. Else in the else clause must be followed
by a : character, just like the if
Decisions with two statement.
options As well as being neater, the else
You are trying to log in to your old
computer, and can't remember the
password.

You sit for hours making random


guesses... I'm sure you thought it
was funny back when you came up
with that password (chEEzburg3rz).

Write a program that tells you


whether your guess is correct. If it
Enter password:itchEEzburg3rz
is correct, should grant access
Access granted.
like this:
If...Else Exercise
Enter password: 123456
Access denied.
The elif statement allows you to
check multiple expressions for
TRUE and execute a block of code
as soon as one of the conditions
evaluates to TRUE.

If.. elif.. else


Decisions with
multiple options
Write a program that asks the user
for a number. Report whether that
number is positive, zero, or
negative.

If.. elif.. else


Exercise
How do we compare
things?
How do we compare things?
Operation Operator Example

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:

This prints True because 3 is less


than 10.

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:

Making Decisions with


Numbers
Try assigning different values to x
to change how the program
Show the letter grade

Return to your grade average


program and add lines to it that
inform the user what the averaged
number equates to in the letter
grade system, where averages
equal to or above 90 are an A,
If Statements Exercise 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
We can use logical operators (and,
or, and not) to combine multiple
conditions in a single branch to
simplify control flow.
and takes two expressions and
only returns True if both
expressions are true.
or takes two expressions and
returns True if one or more
expression is true.
not takes one expression and
Logical Operators returns True only if the
Logical operators take expressions
expression is false.
that return Boolean values,
variables that are assigned
Boolean values, or Boolean values
Here is an example for how we can
use logical operators in a code
block.

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

You might also like