0% found this document useful (0 votes)
2 views40 pages

Lab 3 2 Select Iteration Struct

The document provides an overview of selection and iteration structures in Python, including if statements, nested ifs, and logical operators. It also covers loop statements such as while and for loops, along with examples of using break, continue, and else statements. Additionally, it includes exercises and problems for practical application of the concepts discussed.

Uploaded by

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

Lab 3 2 Select Iteration Struct

The document provides an overview of selection and iteration structures in Python, including if statements, nested ifs, and logical operators. It also covers loop statements such as while and for loops, along with examples of using break, continue, and else statements. Additionally, it includes exercises and problems for practical application of the concepts discussed.

Uploaded by

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

Information

Technology II
Laboratory No 3 Part II
Selection & Iteration Structures
SELECTION STRUCTURES REVIEW
 allow programs to follow different courses of
action or execute different tasks depending
on the data values
< less than
> greater than

RELATIONAL
== equal to
OPERATORS
<= less than or equal to
>= greater than or equal to
!= not equal to
IF STATEMENTS REVIEW

 The most basic way to test a condition in a


Python program
 works along the same lines, testing to see
whether a condition is true or false, and
taking action only if the condition is true
 boolean variable type is used to store only
two possible values: true or false
IF STATEMENTS
Single - option structure:

if expression:
statement(s)
statement(s)
IF STATEMENTS
Example:
a = 1
if a==1:
print(”a equals to 1”);

#The body of an if statement


(printing “a equals to 1”) will be
run only if the condition is true
(a is equal to 1).
IF – ELSE STATEMENTS
Double - option structure:
if expression:
# code to perform if true
else:
# code to perform if false
IF – ELSE STATEMENTS
Example:
a = 1
if a == 1:
print("a equals to 1");
else:
print("a not equal to 1");
IF – ELSE and ELIF STATEMENTS
Multi – alternative decision:
We can put together several different if and
else statements to handle a variety of
conditions
NESTED IF
You can have if statements inside if statements, this is
called nested if statements.

x = 41

if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
IF – ELIF - STATEMENTS
if b==0:
print("b is 0")
elif b==1:
print("b is 1")
elif b=>2:
print("b is greater than 2")
else:
print("b value is negative")
AND LOGICAL OPERATOR
The and keyword is a logical operator, and is
used to combine conditional statements:

a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
OR LOGICAL OPERATOR
The or keyword is a logical operator, and is
used to combine conditional statements:

a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")
THE PASS STATEMENT
if statements cannot be empty, but if you for some reason
have an if statement with no content, put in the pass
statement to avoid getting an error.

a = 33
b = 200

if b > a:
pass
LOOP (ITERATION) STATEMENTS

The purpose of loop statements is to repeat


Python statements many times. There are
several kinds of loop statements:
• while

• for

• do … while – does not exist in Python


WHILE STATEMENT
“While” statement is used to repeat a block of
statements while some condition is true. The
condition must become false somewhere in
the loop, otherwise it will never terminate.

Syntax:
while booleanExpression:
statement(s)
Examples of a WHILE statement

The following code prints integer numbers that


are less than three:

i = 0
while i < 3:
print(i)
i+=1
Examples of a WHILE statement

The infinite loop using while statement:

while True:
statement
statement
The BREAK statement

With the break statement we can stop the loop


even if the while condition is true:

i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
The CONTINUE statement

With the continue statement we can stop the


current iteration, and continue with the next:

i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
The ELSE statement

With the else statement we can run a block of


code once when the condition no longer is
true:

i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
FOR STATEMENT
The for statement is Python’s multipurpose loop controller. It
is used for repeating code a known number of times.
This is less like the for keyword in other programming
languages, and works more like an foreach iterator
method as found in other object-orientated programming
languages.

Syntax:
for variable in range:
statement(s)
Example of a FOR loop in string
Strings are iterable objects, they contain a
sequence of characters:

for x in "banana":
print(x)

Output:
Examples of a FOR statement
BREAK
With the break statement we can stop the loop
before it has looped through all the items.

Example: Exit the loop when x is "banana":

fruits = ["apple", "banana", "cherry"]


for x in fruits:
print(x)
if x == "banana":
break
Examples of a FOR statement
CONTINUE
With the continue statement we can stop the
current iteration of the loop, and continue with
the next.
Example: Do not print banana

fruits = ["apple", "banana", "cherry"]


for x in fruits:
if x == "banana":
continue
print(x)
Examples of a FOR statement
RANGE function
To loop through a set of code a specified number
of times, we can use the range() function,
The range() function returns a sequence of
numbers, starting from 0 by default, and
increments by 1 (by default), and ends at a
specified number.

for x in range(6):
print(x)
Examples of a FOR statement
RANGE function
The range() function defaults to 0 as a starting
value, however it is possible to specify the
starting value by adding a parameter.
range(2, 6), which means values from 2 to 6
(but not including 6).

for x in range(2, 6):


print(x)
Examples of a FOR statement
RANGE function
The range() function defaults to increment the
sequence by 1, however it is possible to specify
the increment value by adding a third parameter:
range(2, 30, 3).
Example: Increment the sequence with 3

for x in range(2, 30, 3):


print(x)
FOR loop with an index

Normally, a for loop in python has no index:

for x in "banana":
if x == "a": print(x)

What if we wanted the indexes of all the a’s?


S = "banana":
for i in range(len(s)):
if s[i] == "a": print(s[i]," ",i)

The len function returns the length of any


iterable variable/set.
Examples of a FOR statement
ELSE
The else keyword in a for loop specifies a block of
code to be executed when the loop is finished.
Example: Print all numbers from 0 to 5, and print a
message when the loop has ended:

for x in range(6):
print(x)
else:
print("Finally finished!")
Nested FOR statement
A nested loop is a loop inside a loop.
The "inner loop" will be executed one time for each
iteration of the "outer loop".
Example: Print each adjective for every fruit.

adj = ["red", "big", "tasty"]


fruits = ["apple", "banana", "cherry"]

for x in adj:
for y in fruits:
print(x, y)
PASS statement in FOR statement
for loops cannot be empty, but if you for some
reason have a for loop with no content, put in
the pass statement to avoid getting an error.]

Example:

for x in [0, 1, 2]:


pass
SHORT REVIEW
to be written on a board

a) What is the exact output of the following code:

i = 5
while i < 15:
print(i)
i = i+3

ORAL
EXERCISE
SHORT REVIEW
to be written on a board

b) What is the exact output of the following code:

for i in range (2, 10):


print(i)

ORAL
EXERCISE
SHORT REVIEW
to be written on a board

c) What is the exact output of the following code:

i = 0
while (i <= 20):
print(i, ’, ’)
i = i*i + 1

ORAL
EXERCISE
SHORT REVIEW
to be written on a board

d) What is the exact output of the following code:

for i in range (1, 10, 2):


print(i)

ORAL
EXERCISE
QUESTIONS

2 a) Check if the following statements are correct, and if


not – correct them. The errors may be syntax or logic.

for x in [0; 1; 2]:


print(x)

ORAL
EXERCISE
QUESTIONS

2 b) Check if the following statements are correct, and if


not – correct them. The errors may be syntax or logic.

i = 0
while (i < 2)
print(i)
i=i+1

ORAL
EXERCISE
QUESTIONS

2 c) Check if the following statements are correct, and if


not – correct them. The errors may be syntax or logic.

for i in range(0,5):
print(i & ’;’)

ORAL
EXERCISE
PROBLEM 1
to be executed in Spyder environment and to
be presented to the teacher in the classroom

Write a program that calculates the factorial of


number 5. Use the for and while loop.

BOARD
EXERCISE
PROBLEM 2
to be executed in Spyder environment and to
be presented to the teacher in the classroom

Write a program that writes the integers between


200 and 300 (inclusive) between those two values
with step 5.

PROGRAMMING
EXERCISE

You might also like