CENG240-2021 Week5 Conditional and Repetitive Execution

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

METU Computer Engineering

CEng 240 – Spring 2021


Week 5
Sinan Kalkan

Conditional and Repetitive Execution

Disclaimer: Figures without reference are from either from “Introduction to programming concepts
with case studies in Python” or “Programming with Python for Engineers”, which are both co-authored
by me.
Variables, Values and Aliasing in

0!
24
NG
on
CE
Python
METU Computer Engineering

ly

Every data
us

¢
vio

(whether constant • If the type of the


Pre

or not) has an data is mutable,


identifier (an there is a
integer) in Python:
problem!!!

This is called
Aliasing.
2021 S. Kalkan - CEng 240 2
METU Computer Engineering

2021
Pre
vio
us
ly
on
CE
NG
24
0!

S. Kalkan - CEng 240


3
METU Computer Engineering

2021
Pre
vio
us
ly
on
CE
NG
24
0!

S. Kalkan - CEng 240


Actions for I/O

4
0!
Actions for I/O
24
NG
CE
on
METU Computer Engineering

ly

>>> print("I am %f tall, %d years old and have %s eyes" % (1.7569, 20, "blue"))
us

I am 1.756900 tall, 20 years old and have blue eyes


vio
Pre

>>> print("I am %.2f tall, %d years old and have %s eyes" % (1.7569, 20, "blue"))
I am 1.76 tall, 20 years old and have blue eyes

¢ %f à Data identifier
¢ We have the following identifiers in Python:

2021 S. Kalkan - CEng 240 5


Actions that are ignored:

0!
24
NG
on
CE Comments
METU Computer Engineering

ly
us
vio
Pre

2021 S. Kalkan - CEng 240 6


Actions that are ignored:

0!
24
NG
on
CE pass statement
METU Computer Engineering

ly
us
vio
Pre

2021 S. Kalkan - CEng 240 7


METU Computer Engineering

2021
Pre
vio
us
ly
on
CE
NG
24
0!

S. Kalkan - CEng 240


Actions in packages

8
METU Computer Engineering

2021
Pre
vio
us
ly
on
CE
NG
24
0!

S. Kalkan - CEng 240


Actions in packages

9
Writing your actions:

0!
24
NG
(1) Interact with the interpreter
on
CE
METU Computer Engineering

ly
us
vio
Pre

2021 S. Kalkan - CEng 240 10


Writing your actions:

0!
24
NG
(2) Putting your actions into a script file
on
CE
METU Computer Engineering

ly
us
vio
Pre

2021 S. Kalkan - CEng 240 11


Writing your actions:

0!
24
NG
(2) Putting your actions into a script file
on
CE
METU Computer Engineering

ly
us
vio
Pre

2021 S. Kalkan - CEng 240 12


Writing your actions:

0!
24
NG
on
CE
(3) Your actions in a module
METU Computer Engineering

ly
us
vio
Pre

To reload:
>>> from importlib import reload
>>> reload(test)

2021 S. Kalkan - CEng 240 13


This Week
METU Computer Engineering

¢ Conditional Execution
§ if statement
§ Conditional expression

¢ Repetitive Execution
§ while/for statement
§ break/continue statements
§ Set/list comprehension

2021 S. Kalkan - CEng 240 14


Administrative Notes
METU Computer Engineering

¢ No quiz this week!


¢ Lab 2 and Lab 1 makeup
¢ Midterm: 1 June, Tuesday, 17:40

2021 S. Kalkan - CEng 240 15


METU Computer Engineering

CONDITIONAL EXECUTION

2021 S. Kalkan - CEng 240 16


Conditional Statements in Python
METU Computer Engineering

¢ the syntax is important!


¢ indentation is extremely important!
¢ “else”-part can be omitted!

2021 S. Kalkan - CEng 240 17


Multiple If Statements
in Python
METU Computer Engineering

2021 S. Kalkan - CEng 240 18


Example
METU Computer Engineering

2021 S. Kalkan - CEng 240 19


Multiple Nested If Statements in
Python
METU Computer Engineering

2021 S. Kalkan - CEng 240 20


Example
METU Computer Engineering

¢ Finding the maximum of two numbers


¢ Finding the maximum of three numbers

2021 S. Kalkan - CEng 240 21


Conditional Expression in Python
METU Computer Engineering

<exp-1> if <cond-exp> else <exp-2>

Note that this is an expression not a


statement!!

2021 S. Kalkan - CEng 240 22


METU Computer Engineering

REPETITIVE EXECUTION

2021 S. Kalkan - CEng 240 23


while statement
METU Computer Engineering

2021 S. Kalkan - CEng 240 24


for statement
METU Computer Engineering

2021 S. Kalkan - CEng 240 25


Example
METU Computer Engineering

¢ Count the occurrences of characters in a


string, e.g.
"aaaaaaxxxxmyyyaaaassssssssstttuivvvv”

2021 S. Kalkan - CEng 240 26


Break statements
METU Computer Engineering

2021 S. Kalkan - CEng 240 27


“break” example
METU Computer Engineering

2021 S. Kalkan - CEng 240 28


Continue statements
METU Computer Engineering

¢ <var> will
point to
the next
item in the
list.

2021 S. Kalkan - CEng 240 29


Set and list comprehension
METU Computer Engineering

¢ [x**3 for x in range(8)]


¢ {x**3 for x in range(8)}

2021 S. Kalkan - CEng 240 30


Exercises from the book
METU Computer Engineering

2021 S. Kalkan - CEng 240 31


Final Words:
Important Concepts
METU Computer Engineering

¢ Variables; Aliasing problem; Naming Variables


¢ Actions for interacting with the environment
¢ Comments, pass statements
¢ Actions in packages (libraries)
¢ Writing your actions (interpreter vs.
script/modules)

2021 S. Kalkan - CEng 240 32


METU Computer Engineering

THAT’S ALL FOLKS!


STAY HEALTHY

2021 S. Kalkan - CEng 240 33

You might also like