Chapter 3 Python
Chapter 3 Python
Eyob S.
SITE, AAiT
1 Algorithm
2 Boolean Expressions
4 Conditionals
5 Iterations
Algorithm
3. If you find a number that is larger than your current largest number,
update the largest number.
4. After checking all the numbers, the current largest number is the
maximum.
Algorithm FindMax(A)
1. max_value := A[0]
2. for each element num in A do
3. if num > max_value then
4. max_value := num
5. return max_value
Write an algorithm that will solve the following problems using a flowchart.
Write an algorithm that will solve the following problems using a flowchart.
Write an algorithm that will solve the following problems using a flowchart.
Boolean Expressions
True and False are special values that belong to the type bool; they
are not strings:
type(True), type(False)
Precedence Operator
1 **
2 +x, -x, ~x
3 *, /, //, %
4 +, -
5 ==, !=, >, <, >=, <=
6 not
7 and
8 or
Table: Operator Precedence in Python
2 < 3 ⇒ ???
7 - 1 >= 2 * 3 ⇒ ???
3 == 4 + 4 ⇒ ???
2 != 3 ⇒ ???
A B A or B A and B
A not A true true true true
true false true false true false
false true false true true false
false false false false
Table: Truth Table for Logical Operators
-2**2 ⇒ ???
4 % 2 == 0 ⇒ ???
4 % 3 == 1 ⇒ ???
0 or True ⇒ ???
0 or False ⇒ ???
1 or True ⇒ ???
1 or False ⇒ ???
4 or False ⇒ ???
4 or True ⇒ ???
1 == True ⇒ ???
2 == True ⇒ ???
-1 == True ⇒ ???
0 == False ⇒ ???
-0 == False ⇒ ???
1 == False ⇒ ???
5 == False ⇒ ???
Sequential
Branching (Conditional)
Repetition (Looping)
Example:
x = 5
y = 10
z = x + y
print(z)
Example:
if x > y:
print("x is greater than y")
elif x < y:
print("x is less than y")
else:
print("x is equal to y")
Example:
for i in range(5):
print(i)
Conditionals
if condition:
# execute this block if the condition is true
if condition:
# execute this block if condition is true
else:
# execute this block if condition is false
if x > y:
print("x is greater than y")
elif x < y:
print("x is less than y")
else:
print("x is equal to y")
A for 90-100
B for 80-89
C for 70-79
D for 60-69
F for below 60
if x > 0:
if x > y:
print("x is positive & greater than y")
else:
print("x is positive & less than or equal to y")
else:
print("x is not positive")
Iterations
For Loops
While Loops
Nested Loops
For loops are used to iterate over a sequence (e.g., list, tuple, range,
string).
Syntax:
Example:
for i in range(5):
print(i)
Syntax:
while condition:
# execute this block
Example:
count = 0
while count < 5:
print(count)
count += 1
Each time the outer loop is executed, the inner loop runs to completion.
Example:
for i in range(3):
for j in range(2):
print(i, j)
*
**
***
****
*****
******
for i in range(10):
if i == 5:
break
print(i)
for i in range(10):
if i % 2 == 0:
continue
print(i)
for i in range(10):
if i % 2 == 0:
pass # Placeholder for future code
else:
print(i)
while True:
print("This is an infinite loop")
# Use break statement to exit the loop
x = 10
while(x > 0):
x += 1
Array indexing
Loop boundaries (e.g., starting at 0 instead of 1 or vice versa)
Calculating lengths (e.g., forgetting to include or exclude the last
element)
# Incorrect loop
for i in range(1, 11):
print(i)
# Correct loop
for i in range(10):
print(i)
The sentinel value is not a valid data value but is used to signal the
end of the loop.
sentinel = -1
number = 0
sum = 0
Thank You!!!