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

Python Conditional Statements & Loops

This document outlines a session on Python conditional statements and loop constructs, aiming to familiarize students with if, else, elif statements, for loops, and while loops. It includes instructional objectives, learning outcomes, session descriptions, examples, and activities related to the topic. Additionally, it provides self-assessment and terminal questions, along with references for further learning.

Uploaded by

Nihal Gupta
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 views14 pages

Python Conditional Statements & Loops

This document outlines a session on Python conditional statements and loop constructs, aiming to familiarize students with if, else, elif statements, for loops, and while loops. It includes instructional objectives, learning outcomes, session descriptions, examples, and activities related to the topic. Additionally, it provides self-assessment and terminal questions, along with references for further learning.

Uploaded by

Nihal Gupta
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

Department of CSE(DS)

PYTHON/BCC-302

UNIT:2
PRIYA SINGH

Session - 1

CREATED BY K. VICTOR BABU


AIM OF THE SESSION

To familiarize students with Python conditional statements and loop constructs

INSTRUCTIONAL
OBJECTIVES
This session is designed to:
1. Demonstrate the use of if, else, and elif statements in Python.
2. Describe the syntax and use of simple for loops with ranges, strings, lists, and
dictionaries.
3. Explain the use of while loops.
4. Illustrate loop manipulation using pass, continue, break, and else.

LEARNING OUTCOMES
At the end of this session, you should be able to:
1. Define and use conditional statements (if, else, elif).
2. Implement for loops for different data types.
3. Use while loops effectively.
4. Apply pass, continue, break, and else within loops.

CREATED BY K. VICTOR BABU


SESSION INTRODUCTION

• Conditional statements and loops form the foundation for decision-making and repetition
in Python programming.
• This session will cover basic control flow structures used to create logical and efficient
programs.
• Control structures are the backbone of programming.
• Conditional statements decide which block of code will run.
• Loops allow repetitive execution of code.
• Combining both gives powerful control over the program flow.

CREATED BY K. VICTOR BABU


SESSION DESCRIPTION

CONDITIONAL STATEMENTS: if, else, elif


if: Executes a block if condition is true.
elif: Checks another condition if previous if was false.
else: Runs if all previous conditions are false.

Example:
num = 10
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")

CREATED BY K. VICTOR BABU


SESSION DESCRIPTION (Cont..)

FOR LOOPS: Basics


Used to iterate over sequences like lists, strings, dictionaries.
range() generates a sequence of numbers.
Example with range:
for i in range(1, 6):
print(i)

• Prints numbers 1 to 5.

List Example:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)

CREATED BY K. VICTOR BABU


Dictionary Example:
student = {"name": "John", "age": 20}
for key in student:
print(key, ":", student[key])
WHILE LOOPS
• Executes block as long as condition is true.
Example:
count = 1
while count <= 5:
print(count)
count += 1
Prints numbers 1 to 5.
CREATED BY K. VICTOR BABU
• LOOP CONTROL: pass, continue, break, else
pass: Does nothing, placeholder.
continue: Skips current iteration.
break: Exits loop immediately.
else: Runs if loop did not end with break.
for i in range(5):
if i == 2:
continue # Skips 2
if i == 4:
break # Exits when i is 4
print(i)
else:
print("Loop completed")
CREATED BY K. VICTOR BABU
pass Example:
for i in range(3):
pass # Does nothing

EXAMPLES OF COMBINED USAGE Program Example: Check all even


numbers in list and stop if negative number is found.
numbers = [2, 4, 6, -1, 8]
for num in numbers:
if num < 0:
break
if num % 2 == 0:
print(num)
CREATED BY K. VICTOR BABU
ACTIVITIES/ CASE STUDIES/ IMPORTANT FACTS RELATED TO THE
SESSION

• Grade Checker: Use if, elif, else to assign grades.

• Number Series: For loop with range() to print even/odd numbers.

• String Search: Loop through string to count vowels.

• Dictionary Report: For loop for key–value pairs (student marks).

• Login Attempts: While loop to limit

CREATED BY K. VICTOR BABU


SUMMARY

• if, else, elif control decision making.


• for and while loops repeat actions.
• pass, continue, break, else refine loop behavior.
• Practical examples help in real coding situations.

CREATED BY K. VICTOR BABU


SELF-ASSESSMENT QUESTIONS

1. Which statement checks multiple conditions in Python?

A) if
B) else
C) elif
D) for

2. What does the continue statement do?

A) Exits the loop


B) Skips the current
iteration
C) Stops program
D) Does nothing

CREATED BY K. VICTOR BABU


TERMINAL QUESTIONS

•Describe conditional statements with syntax and example.


•List types of loops in Python with use cases.
•Analyze the effect of break and continue.
•Summarize loop manipulation techniques.

CREATED BY K. VICTOR BABU


REFERENCES FOR FURTHER LEARNING OF THE SESSION

Reference Books:
1. “Learning Python” by Mark Lutz (O’Reilly)
2. “Head First Programming” by Paul Barry
3. “Python Crash Course” by Eric Matthes

Sites and Web links:


1. Python Documentation: [Link]
2. Real Python Tutorials: [Link]
3. W3Schools Python: [Link]

CREATED BY K. VICTOR BABU


THANK YOU

Team – Python

CREATED BY K. VICTOR BABU

You might also like