0% found this document useful (0 votes)
8 views5 pages

Python Control Flow: Conditionals & Loops

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)
8 views5 pages

Python Control Flow: Conditionals & Loops

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

COS103: Introduction to Programming

(Python)
Week 2: Control Flow (Conditionals and
Loops)

Objectives
ˆ Learn how to use conditional statements (if, else, and elif) to control
the flow of a program.

ˆ Understand how to implement loops (for and while) to iterate over


sequences or repeat tasks.

ˆ Practice using break, continue, and pass statements within loops.

Topics
ˆ Conditionals:

– The if statement: making decisions in a program.


– The else and elif statements: providing alternatives.
– Logical operators: and, or, not.

ˆ Loops:

– The for loop: iterating over sequences (lists, strings, ranges).


– The while loop: repeating tasks while a condition is true.
– The break, continue, and pass statements.

1
ˆ Nested Conditionals and Loops:

– Using if statements inside loops.


– Using loops inside if statements.

Lab Exercises
Lab Exercise 1: Conditional Statements
Objective: Write a Python program that checks if a number is positive,
negative, or zero.
Steps:

1. Ask the user to input a number.

2. Use an if statement to check if the number is positive, negative, or


zero.

3. Print the corresponding message for each case.

4. Example:
1 number = float ( input ( " Enter a
number : " ) )
2

3 if number > 0:
4 print ( " The number is positive . " )
5 elif number < 0:
6 print ( " The number is negative . " )
7 else :
8 print ( " The number is zero . " )

Lab Exercise 2: Using Logical Operators


Objective: Write a program to check if a number is within a certain range
(e.g., between 10 and 20).
Steps:

1. Ask the user to input a number.

2. Use the and operator to check if the number is between 10 and 20.

2
3. Print whether the number is within the range or not.

4. Example:
1 number = int ( input ( " Enter a
number : " ) )
2
3 if number >= 10 and number <= 20:
4 print ( " The number is between 10
and 20. " )
5 else :
6 print ( " The number is outside the
range . " )

Lab Exercise 3: For Loops


Objective: Write a program that prints the squares of the numbers from 1
to 5.
Steps:

1. Use a for loop to iterate over a range of numbers.

2. Print the square of each number.

3. Example:
1 for i in range (1 , 6) :
2 print ( f " The square of { i } is { i
**2} " )

Lab Exercise 4: While Loops


Objective: Write a program that repeatedly asks the user for a password
until the correct one is entered.
Steps:

1. Set a correct password (e.g., ”python”).

2. Use a while loop to keep asking for the password until the user enters
the correct one.

3. Print a success message when the correct password is entered.

3
4. Example:
1 correct_password = " python "
2 user_password = input ( " Enter your
password : " )
3
4 while user_password !=
correct_password :
5 user_password = input ( " Incorrect
password . Try again : " )
6

7 print ( " Access granted ! " )

Lab Exercise 5: Break, Continue, and Pass


Objective: Practice using the break, continue, and pass statements.
Steps:

1. Write a program that iterates over the numbers 1 to 10.

2. If the number is 5, use the break statement to stop the loop.

3. If the number is 2, use the continue statement to skip that number


and go to the next iteration.

4. If the number is 8, use the pass statement to do nothing and proceed


to the next iteration.

5. Example:
1 for i in range (1 , 11) :
2 if i == 5:
3 break
4 elif i == 2:
5 continue
6 elif i == 8:
7 pass
8 print ( i )

4
Lab Assessment
Problem 1
Write a Python program that asks the user to enter their age and checks if
they are eligible to vote (age ¿= 18).

Problem 2
Write a Python program that takes a number as input and checks if it is
divisible by both 2 and 3.

Problem 3
Create a Python program that prints the multiplication table for a given
number (1 to 10).

Problem 4
Write a Python program that asks the user for a number and prints all the
even numbers from 0 to that number using a for loop.

Additional Resources
ˆ Python Documentation: [Link]

ˆ Tutorials: [Link]

ˆ Python Control Flow (If/Else/Loops): [Link]


com/python-conditional-statements/

ˆ Online Interpreter: [Link]

You might also like