0% found this document useful (0 votes)
10 views7 pages

Python Lesson 2

Lecture 2 of Python Programming covers control flow, including conditions and loops, essential for making decisions and repeating tasks in programming. It explains boolean logic, comparison operators, conditional statements (if, elif, else), and the use of loops (for and while) along with common mistakes. The lecture concludes with practice tasks to reinforce the concepts learned.

Uploaded by

hamzaasaidd
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)
10 views7 pages

Python Lesson 2

Lecture 2 of Python Programming covers control flow, including conditions and loops, essential for making decisions and repeating tasks in programming. It explains boolean logic, comparison operators, conditional statements (if, elif, else), and the use of loops (for and while) along with common mistakes. The lecture concludes with practice tasks to reinforce the concepts learned.

Uploaded by

hamzaasaidd
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

Python Programming Lecture 2

Python Programming: Lecture 2


Control Flow: Conditions and Loops

What is Control Flow?


In programming, control flow means controlling the order in which the computer executes
instructions.

Key Idea

Normally, Python runs code line by line. But control flow allows the program to make
decisions and repeat actions.

Why Control Flow is Important


Control flow allows us to:

• Make decisions (if-else)

• Repeat tasks (loops)

• Build intelligent programs

• Handle real-world problems

Boolean Logic and Comparisons


Before learning if statements, we must understand boolean expressions.

Comparison Operators
Comparison operators compare two values and return either True or False.

Operator Meaning
== equal to
!= not equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to

1
Python Programming Lecture 2

Examples of Comparisons
x = 10
y = 5

print ( x > y ) # True


print ( x == y ) # False
print ( x != y ) # True

Important Note

Do not confuse:

• = assignment operator

• == comparison operator

Logical Operators
Logical operators combine multiple boolean conditions.

Types of Logical Operators


Operator Meaning
and True if both are True
or True if at least one is True
not reverses the result

Examples
age = 20
has_id = True

print ( age > 18 and has_id ) # True


print ( age > 25 or has_id ) # True
print ( not has_id ) # False

Conditional Statements (if, elif, else)


Conditional statements allow the program to choose what to do based on conditions.

Basic if Statement
age = 20

if age >= 18:


print ( " You are an adult " )

2
Python Programming Lecture 2

Important Rule: Indentation


Python uses indentation to define blocks of code.

Warning: Indentation Matters!

In Python, indentation is not optional. If indentation is incorrect, Python will give an


error.

if-else Statement
age = 15

if age >= 18:


print ( " You can vote " )
else :
print ( " You cannot vote " )

if-elif-else Statement
marks = 85

if marks >= 90:


print ( " Grade : A+")
elif marks >= 80:
print ( " Grade : A")
elif marks >= 70:
print ( " Grade : B")
else :
print ( " Grade : C or below " )

Multiple Conditions Example


age = 20
citizen = True

if age >= 18 and citizen :


print ( " You are eligible to vote " )
else :
print ( " You are not eligible to vote " )

Loops in Python
Loops allow repeating the same code multiple times.

Why Loops are Useful


Instead of writing:

3
Python Programming Lecture 2

print ( " Hello " )


print ( " Hello " )
print ( " Hello " )

We can use a loop:


for i in range (3) :
print ( " Hello " )

The for Loop


A for loop is used when we know how many times we want to repeat something.

Using range()
for i in range (5) :
print ( i )

Output:

• 0

• 1

• 2

• 3

• 4

range(start, stop)
for i in range (2 , 6) :
print ( i )

Output: 2, 3, 4, 5

range(start, stop, step)


for i in range (0 , 10 , 2) :
print ( i )

Output: 0, 2, 4, 6, 8

Looping Through a String


word = " Python "

for letter in word :


print ( letter )

4
Python Programming Lecture 2

The while Loop


A while loop repeats as long as a condition is True.

Basic Example
count = 1

while count <= 5:


print ( count )
count += 1

Infinite Loops
If you forget to update the variable, the loop will never stop.
# WARNING : Infinite Loop Example
x = 1

while x <= 5:
print ( x )

Important Warning

Infinite loops are a common beginner mistake. Always ensure the loop condition will
eventually become False.

Loop Control Keywords


break
Stops the loop completely.
for i in range (10) :
if i == 5:
break
print ( i )

continue
Skips the current iteration and moves to the next.
for i in range (6) :
if i == 3:
continue
print ( i )

5
Python Programming Lecture 2

Nested Loops
Nested loops mean loops inside loops.

Example: Multiplication Table


for i in range (1 , 4) :
for j in range (1 , 4) :
print (i , " * " , j , " = " , i * j )

Practice Tasks (Homework)


Task 1: Even or Odd
Write a program that takes a number from the user and prints whether it is even or odd.

Task 2: Positive
Ask the user for a number and print:

• Positive

• Negative

• Zero

Task 3: Sum of Numbers


Write a program that prints the sum of numbers from 1 to 100 using a loop.

Task 4: Multiplication Table

Ask the user for a number and print its multiplication table from 1 to 10.

Task 5: Guess the Password


Store a password in a variable. Ask the user to enter the password repeatedly until they
enter the correct one.

Challenge Task: Prime Number Checker

Write a program that checks whether a given number is prime or not.

Common Beginner Mistakes


• Forgetting the colon (:) after if, for, while

• Incorrect indentation

6
Python Programming Lecture 2

• Infinite loops in while

• Using = instead of ==

Lecture Summary
• Boolean comparisons return True or False

• if-elif-else is used for decision making

• for loops repeat a fixed number of times

• while loops repeat until a condition becomes False

• break stops the loop, continue skips iteration

Next Lecture: Lists, Tuples, and Dictionaries

You might also like