You are on page 1of 26

ENG 202: Computers and Engineering

Object Oriented Programming in PYTHON


LECTURE 3 – Branching and Iteration

Maurice J. KHABBAZ, Ph.D.


1 2 3 4 5
Last Time
Syntax and Scalar Simple Expressions. Variables
Semantics. Objects. Operations. and Values.

Monday, January 27, 2020


Today

STRING OBJECT BRANCHING AND INDENTATION ITERATION AND


TYPE CONDITIONALS LOOPS

Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 3
Strings
• Letters, special characters, spaces, digits.
• Enclose in quotation marks or single quotes:
>>> hi = ”hello there”
• Concatenate strings:
>>> name = “Elio”
>>> greet = hi + name
>>> print(greet) Erroneous Output
hello thereElio
>>> geeting = hi + “ ” + name
>>> print(greeting) White Space
Hello there Elio 👍🏼
• Do some operations on a string as defined in PYTHON docs:
>>> silly = hi + “ ” + name * 3
>>> print(silly)
hello there ElioElioElio
Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 4
INPUT/OUTPUT: The print Command
• Used to output results to the PYTHON console.
• print is a PYTHON keyword (a.k.a. identifier, reserved word).
• Example:
x = 1
print(x)
x_str = str(x)
print(“My fav num is”, x, “.”, “x = ”, x)
print(“My fav num is ”, x_str, “.”, “x = ”, x)
print(“My fav num is ” + x_str + “.” + “x = ” + x_str)

Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 5
INPUT/OUTPUT: The input(“”) Command
• Used to feed the computer with user input.
• Prints whatever is within the quotes (“”).
• User types a something (e.g. a value) and hits the ENTER key.
• Binds the fed value to a variable.
text = input(“Type anything: “);
print(5*text)
• input returns a string so type casting is necessary for numbers:
num = int(input(“Type a number: “))
print(5*num)

Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 6
Comparison Operators On
int, float and string
• Let i and j be variable names.
• Comparisons below evaluate to a Boolean-typed value:
i > j
i >= j
i < j
i <= j
i == j → equality test, True if i is the same as j.
i != j → inequality test, True if i is not the same as j.

Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 7
Logic Operators On Boolean Variables
• A binary variable can take either one of two values: True or False.
• Consider a and b to be Boolean variables.
• Three essential Boolean operators: not, and, or.
• The not operator is unilateral (i.e. takes only one operand):
not a → True if a is False. Else, False.
• The and/or operators are bilateral (i.e. take two operands):
a and b → True if both a and b are True. Else, False.
a or b → True if either/both a and b are True. Else, False.

Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 8
Binary Truth Table For Two Operands

a b not a not b a and b a or b


False False True True False False
False True True False False True
True False False True False True
True True False False True True

Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 9
Comparison/Logic Operators Example
pset_time = 15
sleep_time = 8
print(sleep_time > pset_time) Output? False

drive = True
drink = False
both = drink and drive
print(both) Output? False

Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 10
Maze Game

🙂 🙂 🙂
🙂
If right clear, If right blocked, If right & front blocked, If right, front & left blocked,
Go right Go forward Go left Go back

Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 11
Flow Control, Branching:
if statement

Simple if statement False


condition
if <condition>: True
<expression 1>
Body <expression 2> expression 1
. . . expression 2
. . .

Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 12
Flow Control, Branching:
if...else statement
if...else statement
False
if <condition>: condition
<expression 1>
<expression 2> True
. . .
expression 1 expression 3
else:
expression 2 expression 4
<expression 3>
. . . . . .
<expression 4>
. . .

Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 13
Flow Control, Branching:
if...elif...else statement
if...elif...else
statement
False
if <cond. 1>: cond. 1 cond. 2
<expr. 1>
. . . True True
elif <cond. 2>:
expr. 1 expr. 2 expr. 3
<expr. 2>
. . . . . . . . .
. . .
else:
<expr. 3>
. . .
Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 14
Indenation
• How you denote code blocks in PYTHON is of utmost importance.
x = float(input(“Enter a value for x: ”))
y = float(input(“Enter a value for y: ”))
if x == y:
print(“x and y are equal”)
if y != 0
print(“therefore, x/y is”, x/y)
elif x < y:
print(“x is smaller”)
else:
print(“y is smaller”)
print(“Thanks!”)

Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 15
= V.S. ==
x = float(input(“Enter a value for x: ”))
y = float(input(“Enter a value for y: ”))
if x == y:
print(“x and y are equal”)
if y != 0
print(“therefore, x/y is”, x/y)
elif x < y:
print(“x is smaller”)
else:
print(“y is smaller”)
print(“Thanks!”)

Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 16
17
Flow Control, Loops:
while Loop

while loop statement False


condition
while <condition>: True
<expression 1>
<expression 2> expression 1
. . . expression 2
. . .

Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 18
while Loop
Example
• Filling an ice cream cone:
• You have the right for 5 scoops.
• Program may look like this:

scoops = 5
while scoops > 0:
print(“Another scoop!”)
scoops -= 1
print(“Enjoy! Come back soon!”)

Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 19
Flow Control, Loops:
for Loop v = 0

for loop statement


True
for <v> in range(<n>) v == n-1
<expr. 1>
<expr. 2> False
. . . expr. 1
expr. 2
• Each time through the loop v takes a value.
. . .
• First time: v starts at the smallest value:
• If not defined, smallest value is 0.
• Next time: v takes the previous value + 1 and …
Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 20
Iterate Through Numbers In A Known Sequence
while loop does it that way: for loop does it this way:

n = 0 for n in range(5):
while n < 5: print(n)
print(n)
n += 1

USE for LOOP WHEN NUMBER OF ITERATIONS KNOWN AHEAD

Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 21
range(start, stop, step)
• range is a pre-defined function (i.e. a reserved word).
• It takes three input parameter values (as shown above).
• Default parameter values are: start = 0, step = 1 (optional)
• Looping process continues until reaching stop-1
Example 1: Example 2:

mySum = 0 mySum = 0
for i in range (7,10): for i in range (5,11,2):
mySum += i mySum += i
print(mySum) print(mySum)
Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 22
break Statement
• When encountered throughout a looping flow:
• Causes immediate interruption of the looping process.
• Skips remaining expressions within the loop.
• Exits only innermost loop!
• Generic code:
while <condition_1>:
while <condition_2>:
<inner_expressions>
inner break
outer <other_inner_expressions>
<outer_expressions>
break
<other_outer_expressions>
<out_of_loop_expressions>

Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 23
break Statement Example
mySum = 0
for i in range(5, 11, 2):
mySum += i
if mySum == 5:
break Output?
mySum += 1 5

print(mySum)

Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 24
for Loops V.S. while Loops
• Know number of iterations • Unbounded number of iterations.
a priori.
• Can end early via break.
• Can end early via break.
• Can use a counter but must
• Use a counter. initialize it outside loop and
increment it inside loop.

• Can be re-written as • Not always re-writable as for


while loops. loop.
Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 25
Monday, January 27, 2020 NDU - ENG 202 - Maurice J. KHABBAZ, Ph.D. 26

You might also like