PYTHON LOOPS
By Zeeshan Hussain (Software Engineer)
Portfolio: [Link] | WhatsApp: +923181482800 | Lecturer: Zeeshan Hussain
WHAT IS LOOP ?
• A loop is a control flow statement that allows you to execute a block
of code repeatedly.
• Python provides two main types of loops: the for loop and the while
loop.
Portfolio: [Link] | WhatsApp: +923181482800 | Lecturer: Zeeshan Hussain
FOR LOOP
• A for loop is used to iterate over sequences such as lists, strings,
tuples, etc.
• Syntax:
• Example:
Portfolio: [Link] | WhatsApp: +923181482800 | Lecturer: Zeeshan Hussain
WHILE LOOP
• The while loop repeats a block of code as long as a specified condition
is True.
• Syntax:
• Example:
Portfolio: [Link] | WhatsApp: +923181482800 | Lecturer: Zeeshan Hussain
LOOP CONTROL STATEMENTS
• Python provides loop control statements to change the execution
flow of loops:
• Break, continue:
Portfolio: [Link] | WhatsApp: +923181482800 | Lecturer: Zeeshan Hussain
BREAK STATEMENT
• Terminates the loop prematurely.
• For Example:
Portfolio: [Link] | WhatsApp: +923181482800 | Lecturer: Zeeshan Hussain
CONTINUE STATEMENT
• Skips the rest of the code inside the loop for the current iteration and
proceeds to the next iteration.
• For Example:
Portfolio: [Link] | WhatsApp: +923181482800 | Lecturer: Zeeshan Hussain
TASK # Print Even Numbers
• Write a Python program that prints all even numbers between 1 and
20 inclusive.
• Here's how the program should work:
• Start a for loop that iterates over numbers from 1 to 20 inclusive.
• Inside the loop, check if the current number is even.
• If the number is even, print it.
• Continue iterating until the loop finishes.
Portfolio: [Link] | WhatsApp: +923181482800 | Lecturer: Zeeshan Hussain
TASK # Generate a Multiplication Table
• Write a Python program that generates a multiplication table for a
given number entered by the user. The multiplication table should
include multiples of the given number from 1 to 10.
• Here's how the program should work:
• Prompt the user to enter a number for which they want to generate a
multiplication table.
• Use a for loop to iterate over numbers from 1 to 10.
• Calculate the product of the entered number and the current number in the
loop.
• Print the multiplication table, displaying each multiplication operation along with
its result.
Portfolio: [Link] | WhatsApp: +923181482800 | Lecturer: Zeeshan Hussain
IF ANY QUESTION, YOU CAN
ASK!
Portfolio: [Link] | WhatsApp: +923181482800 | Lecturer: Zeeshan Hussain
THANK YOU!
Portfolio: [Link] | WhatsApp: +923181482800 | Lecturer: Zeeshan Hussain