0% found this document useful (0 votes)
79 views

CENG240-2021 - Week6 - Examples With Conditional and Repetitive Execution

This document is a lecture slide presentation on conditional and repetitive execution in Python. It discusses multiple if statements, conditional expressions, while loops, for loops, break and continue statements, and set and list comprehensions. It provides examples of each concept. It also lists some practice problems involving searching lists, number conversions between decimal and binary, binary operations, averaging nested lists, and bubble sorting.

Uploaded by

zepozepo06
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

CENG240-2021 - Week6 - Examples With Conditional and Repetitive Execution

This document is a lecture slide presentation on conditional and repetitive execution in Python. It discusses multiple if statements, conditional expressions, while loops, for loops, break and continue statements, and set and list comprehensions. It provides examples of each concept. It also lists some practice problems involving searching lists, number conversions between decimal and binary, binary operations, averaging nested lists, and bubble sorting.

Uploaded by

zepozepo06
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

METU Computer Engineering

CEng 240 – Spring 2021


Week 6
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.
Multiple If Statements

0!
24
NG
in Python
on
CE
METU Computer Engineering

ly
us
vio
Pre

2021 S. Kalkan - CEng 240 2


0!
Conditional Expression in Python
24
NG
CE
on
METU Computer Engineering

ly
us
vio
Pre

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

Note that this is an expression not a


statement!!

2021 S. Kalkan - CEng 240 3


METU Computer Engineering

2021
Pre
vio
us
ly
on
CE
NG
24
0!

S. Kalkan - CEng 240


while statement

4
METU Computer Engineering

2021
Pre
vio
us
ly
on
CE
NG
24
0!

S. Kalkan - CEng 240


for statement

5
METU Computer Engineering

2021
Pre
vio
us
ly
on
CE
NG
24
0!

S. Kalkan - CEng 240


Break statements

6
0!
Continue statements
24
NG
CE
on
METU Computer Engineering

ly
us
vio
Pre

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

2021 S. Kalkan - CEng 240 7


0!
Set and list comprehension
24
NG
CE
on
METU Computer Engineering

ly
us

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


vio

¢
Pre

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

2021 S. Kalkan - CEng 240 8


This Week
METU Computer Engineering

¢ More examples with conditional and iterative


execution

2021 S. Kalkan - CEng 240 9


Administrative Notes
METU Computer Engineering

¢ No quiz this week!


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

2021 S. Kalkan - CEng 240 10


Problems
METU Computer Engineering

¢ Sequential search: Find whether a number is in a list of


numbers or not.
¢ Example input/output:
§ 5 is in [100, 4, 48, 5] => True
§ 5 is in [38, 45, 20, 3] => False

2021 S. Kalkan - CEng 240 11


Problems
METU Computer Engineering

¢ Decimal number to binary conversion


¢ Example input/output:
§ 5=> “101”.
§ 9 => “1001”

2021 S. Kalkan - CEng 240 12


Problems
METU Computer Engineering

¢ Calculate AND operation on two binary strings.


¢ Example input/output:
§ “1011” AND “1001” => “1001”.
§ “1010” AND “0101” => “0000”.

2021 S. Kalkan - CEng 240 13


Problems
METU Computer Engineering

¢ Binary string to decimal conversion


¢ Example input/output:
§ “101” => 5.
§ “1001” => 9.

2021 S. Kalkan - CEng 240 14


Problems
METU Computer Engineering

¢ In a list, if you encounter another list of numbers, replace that


nested list of numbers with the average of the numbers.
¢ Example input/output:
§ [[1, 3], [4, 5, 6], 7, [10, 20]] => [2, 5, 7, 15].
§ [20, 30, [4, 8]] => [20, 30, 6]

2021 S. Kalkan - CEng 240 15


Problems
METU Computer Engineering

¢ Bubble sort: Sort numbers in increasing order


¢ Example input/output:
§ [5, 1, 3, 6] => [1, 3, 5, 6]
§ [10, -4, 8, 20] => [-4, 8, 10, 20]

2021 S. Kalkan - CEng 240 16


Final Words:
Important Concepts
METU Computer Engineering

¢ The same as last week.

2021 S. Kalkan - CEng 240 17


METU Computer Engineering

THAT’S ALL FOLKS!


STAY HEALTHY

2021 S. Kalkan - CEng 240 18

You might also like