0% found this document useful (0 votes)
6 views30 pages

Lecture11 - Python Programming 2(2)

Lecture 11 for MM2425

Uploaded by

kcehlel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views30 pages

Lecture11 - Python Programming 2(2)

Lecture 11 for MM2425

Uploaded by

kcehlel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Decision making

Your computer can select to perform


differently based on the decision result

1
Comparison Operators
• These operators compare the values on either side
of the operator and determine the relation
between them. It is also referred as relational
operators. Various comparison operators are
== (equal to)
!= (not equal to)
> (greater than)
>= (greater than or equal to)
< (smaller than)
<= (smaller than or equal to)
2
Comparison Operators
• Type and run the following program.
x = 4
y = 5
print("x > y is", x > y)
• Output
x > y is False
• Likewise, you can try other comparison
operators (x < y, x==y, x!=y, etc.)
3
if statement
• if Statement Syntax
if test expression:
statement(s)
• Here, the program evaluates the
test expression and will execute
statement(s) only if the text
expression is True.
• If the text expression is False,
the statement(s) is not executed.
4
if statement
• Type and run the following program.
# If the number is positive, we print an appropriate message
num1 = 3
if num1 > 0:
print("The number is" , num1)
print("It is a positive number.")
print("This is always printed.")
num2 = -1
if num2 > 0:
print("The number is", num2)
print(num2, "is a positive number.")
print("This is also always printed.")

• Output
The number is 3
It is a positive number.
5
This is always printed.
Indentation
• Python uses indentation to define a block of code.
• The following is a correct example because two print statements
start with the same indentation. They form a block of code
executed when the condition is true.
if -1 > 0:
print("The number is" , num1)
print("It is a positive number.")
• The following is an incorrect example because two print statements
start with different indentation. The do not form a block of code.
if -1 > 0:
print("The number is" , num1)
print("It is a positive number.")

6
if...else Statement
• Syntax of if...else
if test expression:
Body of if
else:
Body of else
• The if..else statement evaluates test
expression and will execute body of
if only when test condition is True.
• If the condition is False, body of else
is executed. Indentation is used to
separate the blocks.
7
if...else Statement
• Type and run the following program.
num = 3
if num >= 0:
print("It is", num)
print("Positive or Zero")
else:
print("It is", num)
print("Negative number")
print("This is always printed.")

• Output
It is 3
Positive or Zero 8
if...elif...else Statement
• Syntax of if...elif...else
if test expression:
Body of if
elif test expression:
Body of elif
else:
Body of else
• The elif is short for else if. It allows us to
check for multiple expressions.
• If the condition for if is False, it checks the
condition of the next elif block and so on.
• If all the conditions are False, body of else
is executed.
• Only one block among the several
if...elif...else blocks is executed according to
the condition.
9
if...elif...else Statement
• Type and run the following program.
num = 3.4

if num > 0:
print("It is", num)
print("Positive number")
elif num == 0:
print("It is", num)
print("Zero")
else:
print("It is", num)
print("Positive number")

print("This is always printed.")


• Output
It is 3.4
Positive number
This is always printed. 10
Nested if statements
• We can have a if...elif...else statement inside
another if...elif...else statement. This is called
nesting in computer programming.
• Any number of these statements can be
nested inside one another. Indentation is the
only way to figure out the level of nesting. This
can get confusing, so must be avoided if we
can.

11
Nested if statements
• Type and run the following program.
num = -2
if num >= 0:
if num == 0:
print("It is", num)
print("Zero")
else:
print("It is", num)
print("Positive number")
else:
print("It is", num)
print("Negative number")
• Output
It is -2.0
12
Negative number
A big program example:
Checking leap year or not
# Python program to check if year is a leap year or not
year = 2000
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print(year, "is a leap year")
else:
print(year, "is not a leap year")
else:
print(year, "is a leap year")
else:
print(year, "is not a leap year")

Source of information:
https://docs.microsoft.com/en-us/office/troubleshoot/excel/determine-a-leap-year
13
Logical operators
Operator Meaning Example
True if both the
and x and y
operands are true
True if either of the
or x or y
operands is true
True if operand is
not false (complements not x
the operand)

14
Logical operators
• Type and run the following program.
a=1
b=1
if a>0 and b>0:
print("a=", a, "b=", b)
print("Both are positive")
elif (a>0 and b<0) or (a<0 and b>0):
print("a=", a, "b=", b)
print("One is positive, and another is negative")
elif a<0 and b<0:
print("a=", a, "b=", b)
print("Both are negative")
else:
print("a=", a, "b=", b)
print("At least, one of them is zero")
• Output
a= 1 b= 1 15
Looping

You computer can repeat steps

16
for loop
• The for loop in Python is
used to iterate over a
sequence or other iterable
objects.
• Syntax of for Loop
for val in sequence:
Body of for

17
for loop
• Type and run the following program.
for i in range(1,10,1):
print("i=", i)
• Output
i= 1
i= 2
i= 3
i= 4
i= 5
i= 6
i= 7
i= 8
i= 9
18
for loop
• Type and run the following program.
for i in range(1,10,2):
print("i=", i)
• Output
i= 1
i= 3
i= 5
i= 7
i= 9
19
for loop
• Type and run the following program.
for i in range(20,1,-3):
print("i=", i)
• Output
i= 20
i= 17
i= 14
i= 11
i= 8
i= 5
i= 2
20
for loop
• Type and run the following program.
sum=0
for i in range(1,11,1):
sum = sum + i
print("sum =", sum)
• Output
sum = 55

21
Break statement
• The break statement terminates the loop
containing it. Control of the program flows to
the statement immediately after the body of
the loop.
• Syntax of break
break

22
A big program example:
Checking if a number is a prime number
# Program to check if a number is prime or not
num = 4
if num > 1:
prime = True
for i in range(2,num,1):
if (num % i) == 0:
prime = False
break
else:
prime = False

if prime == True:
print(num, "is a prime number")
else:
print(num, "is a not a prime number") 23
A big program example:
Finding all prime numbers in an interval
# Display all the prime numbers within an interval
lower = 1
upper = 20
print("Prime numbers between", lower, "and", upper, "are:")

for num in range(lower, upper + 1):


if num > 1:
prime = True
for i in range(2, num, 1):
if (num % i) == 0:
prime = False
break
if prime == True:
print(num)

24
Nested for loops
• Type and run the following program.
for i in range(1, 11, 1):
print("Multiplication table of", i)
for j in range(1, 11, 1):
print(i, "x", j, "=", i*j)
print()

25
while loop
• The while loop in Python is used
to iterate over a block of code as
long as the test expression
(condition) is true.
• We generally use this loop when
we don't know beforehand, the
number of times to iterate.
• Syntax of while Loop in Python
while test_expression:
Body of while
26
while loop
• Type and run the following program.
n = 10

# initialize sum and counter


sum = 0
i = 1

while i <= n:
sum = sum + i
i = i+1

print("The sum is", sum)


• Output
The sum is 55 27
Infinite loop
• What happens when you run the following
program?
i = 0
while i < 10:
print("i=", i)
• It will loop forever and never stop. It is a
logical error that cannot be detected by the
interpreter.

28
A big program example:
Finding factors of a number
num = 60
print("The factors of", num,"are:")
print (1)

factor = 2
while factor < num + 1:
if num % factor == 0:
print(factor)
num = num / factor
else:
factor = factor + 1
29
Print the variables to trace the steps
num = 60
print("The factors of", num,"are:")
print (1)

factor = 2
while factor < num + 1:
print("A loop begins, factor=", factor, "num=", num)
if num % factor == 0:
print("Doing if part")
print(factor)
num = num / factor
print("factor=", factor, "num=", num)
else:
print("Doing else part")
factor = factor + 1
print("factor=", factor, "num=", num)
print()

print("After exiting the loop, factor=", factor, "num=", num)


30

You might also like