SlideShare a Scribd company logo
Loops in python
programming
➔ For
➔ while
Outline
1. For loop
2. Range function
3. Break, continue, and Pass
statement
4. Booleans
5. While loop
6. Nested loop
For Loop
The for loop in python iterates over the items of any sequence
be it a list or a string. Moreover, using for loop in the program
will minimize the line of code.
Points to keep in mind while using for loop in python:
➔ It should always start with for keyword and should
end with a colon(:).
➔ The statements inside the loop should be
indented(four spacebars).
Syntax:
For iterator_variable in sequence:
statement(s)
Example
Code:
for i in range(6):
print(“Hello World!”)
Output:
print(“Hello World!”)
print(“Hello World!”)
print(“Hello World!”)
print(“Hello World!”)
print(“Hello World!”)
This is actually a temporary
variable which store the
number of iteration
Number of times you want to
execute the statement
Range function
To iterate over a sequence of numbers, the built-in function
range() comes in handy. It generates arithmetic
progressions. For example, if we want to print a whole number
from 0 to 10, it can be done using a range(11) function in a for
loop.
In a range() function we can also define an optional start and
the endpoint to the sequence. The start is always included
while the endpoint is always excluded.
Example:
for i in range(1, 6):
print("Hello World!")
Output: Prints Hello World! five times
Starting point
Ending point
Range function
The range() function also takes in another optional
parameter i.e. step size. Passing step size in a range()
function is important when you want to print the value
uniformly with a certain interval.
Step size is always passed as a third parameter where the
first parameter is the starting point and the second
parameter is the ending point. And if the step_size is not
provided it will take 1 as a default step_size.
Example:
for i in range(0,20, 2):
print(i)
Output: prints all the even numbers till 20
Step size
cont….
Sum of numbers
-Write a program to find the sum of all the numbers less than
a number entered by the user. Display the sum.
Expected Output:
1
Break, Continue, & Pass Statement
(Loop control statement)
➔ A break statement is used to immediately terminate a
loop.
➔ A continue statement is used to skip out future
commands inside a loop and return to the top of the
loop.
➔ A Pass is a null operation — when it is executed,
nothing happens. It is useful as a placeholder when a
statement is required syntactically when no code
needs to be executed.
➔ These statements can be used with for or while loops.
Break, Continue, & Pass Statement (Loop control statement)
Example:
for i in range(10):
if i == 3:
break
print(i)
Output:
0
1
2
Break
for i in range(5):
if i == 3:
continue
print(i)
Output:
0
1
2
4
Continue
for i in range(10):
Pass
Output:
Nothing will be printed
Pass
Magic Number
Write a program that makes the user guess a particular
number between 1 and 100. Save the number to be
guessed in a variable called magic_number.
If the user guesses a number higher than the secret
number, you should say Too high!. Similarly, you should
say Too low! if they guess a number lower than the secret
number. Once they guess the number, say Correct!
Expected Output:
2
Boolean
A boolean is a value that can be either True or False.
Example:
brought_food = True
brought_drink = False
- Boolean variables don't have quotation marks.
- The values must start with capital letters.
While Loop
➔ while loop repeatedly carries out a target statement while the condition is
true. The loop iterates as long as the defined condition is True.
➔ When the condition becomes false, program control passes to the line
immediately following the loop.
Example:
while True:
print("I am a programmer")
break
while False:
print("Not printed because while the condition is already False")
break
Output:
I am a programmer
While Loop
Example:
x = 1
while x > 0:
print ("Hello!")
x = x - 1
print ("I like Python.")
Output:
Hello!
I like Python.
Guess the number
Write a program where the user enters a number.
If the number matches the number, acknowledge
the user and stop the program. If the number
does not match, keep asking for the next number.
Expected Output:
3
Nested Loop
Loop within a loop is called a nested loop.
➔ For loop within for loop is called a nested for loop.
➔ While loop within while loop is called as nested while
loop.
for i in range(4):
for j in range(3):
print ("Hello!")
Example: Nested for loop
i = 1
j = 5
while i < 4:
while j < 8:
print(i,",", j)
j = j + 1 j
i = i + 1
Example: Nested while loop
Nested Loop(Example)
for i in range(4):
x = 5
while x > 1:
print (x)
x = x - 1
Output:
5
4
3
2
5
4
3
2
5
4
3
2
5
4
3
2
Rolling a dice
Write a program that will print out all combinations that can
be made when 2 dice are rolled. And should continue until all
values up to 6, and 6 are printed. (Hint: You should have a
space after each comma!)
Expected Output:
4
Multiplication Table:
Write a program to print a multiplication
table to 12 of a given number.
Expected Output:
1
Printing pattern:
Write a program to print the following two
patterns implementing the nested loop
concept.
2
While_for_loop presententationin first year students

More Related Content

Similar to While_for_loop presententationin first year students (20)

PPTX
Mastering Python lesson 3a
Ruth Marvin
 
PPTX
Loops in Python.pptx
Guru Nanak Dev University, Amritsar
 
PPTX
TN 12 computer Science - ppt CHAPTER-6.pptx
knmschool
 
PPTX
1. control structures in the python.pptx
DURAIMURUGANM2
 
PPTX
loops _
SwatiHans10
 
PDF
python program
tomlee12821
 
PPTX
ForLoops.pptx
RabiyaZhexembayeva
 
PPTX
Python programming –part 3
Megha V
 
PPTX
Python if_else_loop_Control_Flow_Statement
AbhishekGupta692777
 
PPTX
Loops in Python
Arockia Abins
 
PPTX
Iterations FOR LOOP AND WHILE LOOP .pptx
VijayaLakshmi948042
 
PDF
Slide 6_Control Structures.pdf
NuthalapatiSasidhar
 
PPT
Py4inf 05-iterations (1)
karan saini
 
PPT
Py4inf 05-iterations (1)
karan saini
 
PPT
Py4inf 05-iterations
karan saini
 
PPT
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
avishekpradhan24
 
PPTX
Mastering Python lesson3b_for_loops
Ruth Marvin
 
PDF
Python Flow Control
Mohammed Sikander
 
PPTX
Python Flow Control
Kamal Acharya
 
PPTX
Module_2_1_Building Python Programs_Final.pptx
nikhithavarghese77
 
Mastering Python lesson 3a
Ruth Marvin
 
TN 12 computer Science - ppt CHAPTER-6.pptx
knmschool
 
1. control structures in the python.pptx
DURAIMURUGANM2
 
loops _
SwatiHans10
 
python program
tomlee12821
 
ForLoops.pptx
RabiyaZhexembayeva
 
Python programming –part 3
Megha V
 
Python if_else_loop_Control_Flow_Statement
AbhishekGupta692777
 
Loops in Python
Arockia Abins
 
Iterations FOR LOOP AND WHILE LOOP .pptx
VijayaLakshmi948042
 
Slide 6_Control Structures.pdf
NuthalapatiSasidhar
 
Py4inf 05-iterations (1)
karan saini
 
Py4inf 05-iterations (1)
karan saini
 
Py4inf 05-iterations
karan saini
 
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
avishekpradhan24
 
Mastering Python lesson3b_for_loops
Ruth Marvin
 
Python Flow Control
Mohammed Sikander
 
Python Flow Control
Kamal Acharya
 
Module_2_1_Building Python Programs_Final.pptx
nikhithavarghese77
 

Recently uploaded (20)

PDF
01-introduction to the ProcessDesign.pdf
StiveBrack
 
PDF
Plant Control_EST_85520-01_en_AllChanges_20220127.pdf
DarshanaChathuranga4
 
PDF
Designing for Tomorrow – Architecture’s Role in the Sustainability Movement
BIM Services
 
PDF
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
PDF
Tesia Dobrydnia - An Avid Hiker And Backpacker
Tesia Dobrydnia
 
PDF
Artificial Neural Network-Types,Perceptron,Problems
Sharmila Chidaravalli
 
PDF
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
PDF
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
PPT
FINAL plumbing code for board exam passer
MattKristopherDiaz
 
PDF
Clustering Algorithms - Kmeans,Min ALgorithm
Sharmila Chidaravalli
 
PPTX
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
PDF
June 2025 - Top 10 Read Articles in Network Security and Its Applications
IJNSA Journal
 
PDF
CLIP_Internals_and_Architecture.pdf sdvsdv sdv
JoseLuisCahuanaRamos3
 
PDF
Module - 4 Machine Learning -22ISE62.pdf
Dr. Shivashankar
 
PDF
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
DOCX
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
PPTX
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
PPTX
Precooling and Refrigerated storage.pptx
ThongamSunita
 
PPTX
Functions in Python Programming Language
BeulahS2
 
PPTX
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
AsadShad4
 
01-introduction to the ProcessDesign.pdf
StiveBrack
 
Plant Control_EST_85520-01_en_AllChanges_20220127.pdf
DarshanaChathuranga4
 
Designing for Tomorrow – Architecture’s Role in the Sustainability Movement
BIM Services
 
Generative AI & Scientific Research : Catalyst for Innovation, Ethics & Impact
AlqualsaDIResearchGr
 
Tesia Dobrydnia - An Avid Hiker And Backpacker
Tesia Dobrydnia
 
Artificial Neural Network-Types,Perceptron,Problems
Sharmila Chidaravalli
 
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
FINAL plumbing code for board exam passer
MattKristopherDiaz
 
Clustering Algorithms - Kmeans,Min ALgorithm
Sharmila Chidaravalli
 
FSE_LLM4SE1_A Tool for In-depth Analysis of Code Execution Reasoning of Large...
cl144
 
June 2025 - Top 10 Read Articles in Network Security and Its Applications
IJNSA Journal
 
CLIP_Internals_and_Architecture.pdf sdvsdv sdv
JoseLuisCahuanaRamos3
 
Module - 4 Machine Learning -22ISE62.pdf
Dr. Shivashankar
 
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
Engineering Geology Field Report to Malekhu .docx
justprashant567
 
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
Precooling and Refrigerated storage.pptx
ThongamSunita
 
Functions in Python Programming Language
BeulahS2
 
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
AsadShad4
 
Ad

While_for_loop presententationin first year students

  • 2. Outline 1. For loop 2. Range function 3. Break, continue, and Pass statement 4. Booleans 5. While loop 6. Nested loop
  • 3. For Loop The for loop in python iterates over the items of any sequence be it a list or a string. Moreover, using for loop in the program will minimize the line of code. Points to keep in mind while using for loop in python: ➔ It should always start with for keyword and should end with a colon(:). ➔ The statements inside the loop should be indented(four spacebars). Syntax: For iterator_variable in sequence: statement(s)
  • 4. Example Code: for i in range(6): print(“Hello World!”) Output: print(“Hello World!”) print(“Hello World!”) print(“Hello World!”) print(“Hello World!”) print(“Hello World!”) This is actually a temporary variable which store the number of iteration Number of times you want to execute the statement
  • 5. Range function To iterate over a sequence of numbers, the built-in function range() comes in handy. It generates arithmetic progressions. For example, if we want to print a whole number from 0 to 10, it can be done using a range(11) function in a for loop. In a range() function we can also define an optional start and the endpoint to the sequence. The start is always included while the endpoint is always excluded. Example: for i in range(1, 6): print("Hello World!") Output: Prints Hello World! five times Starting point Ending point
  • 6. Range function The range() function also takes in another optional parameter i.e. step size. Passing step size in a range() function is important when you want to print the value uniformly with a certain interval. Step size is always passed as a third parameter where the first parameter is the starting point and the second parameter is the ending point. And if the step_size is not provided it will take 1 as a default step_size. Example: for i in range(0,20, 2): print(i) Output: prints all the even numbers till 20 Step size cont….
  • 7. Sum of numbers -Write a program to find the sum of all the numbers less than a number entered by the user. Display the sum. Expected Output: 1
  • 8. Break, Continue, & Pass Statement (Loop control statement) ➔ A break statement is used to immediately terminate a loop. ➔ A continue statement is used to skip out future commands inside a loop and return to the top of the loop. ➔ A Pass is a null operation — when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically when no code needs to be executed. ➔ These statements can be used with for or while loops.
  • 9. Break, Continue, & Pass Statement (Loop control statement) Example: for i in range(10): if i == 3: break print(i) Output: 0 1 2 Break for i in range(5): if i == 3: continue print(i) Output: 0 1 2 4 Continue for i in range(10): Pass Output: Nothing will be printed Pass
  • 10. Magic Number Write a program that makes the user guess a particular number between 1 and 100. Save the number to be guessed in a variable called magic_number. If the user guesses a number higher than the secret number, you should say Too high!. Similarly, you should say Too low! if they guess a number lower than the secret number. Once they guess the number, say Correct! Expected Output: 2
  • 11. Boolean A boolean is a value that can be either True or False. Example: brought_food = True brought_drink = False - Boolean variables don't have quotation marks. - The values must start with capital letters.
  • 12. While Loop ➔ while loop repeatedly carries out a target statement while the condition is true. The loop iterates as long as the defined condition is True. ➔ When the condition becomes false, program control passes to the line immediately following the loop. Example: while True: print("I am a programmer") break while False: print("Not printed because while the condition is already False") break Output: I am a programmer
  • 13. While Loop Example: x = 1 while x > 0: print ("Hello!") x = x - 1 print ("I like Python.") Output: Hello! I like Python.
  • 14. Guess the number Write a program where the user enters a number. If the number matches the number, acknowledge the user and stop the program. If the number does not match, keep asking for the next number. Expected Output: 3
  • 15. Nested Loop Loop within a loop is called a nested loop. ➔ For loop within for loop is called a nested for loop. ➔ While loop within while loop is called as nested while loop. for i in range(4): for j in range(3): print ("Hello!") Example: Nested for loop i = 1 j = 5 while i < 4: while j < 8: print(i,",", j) j = j + 1 j i = i + 1 Example: Nested while loop
  • 16. Nested Loop(Example) for i in range(4): x = 5 while x > 1: print (x) x = x - 1 Output: 5 4 3 2 5 4 3 2 5 4 3 2 5 4 3 2
  • 17. Rolling a dice Write a program that will print out all combinations that can be made when 2 dice are rolled. And should continue until all values up to 6, and 6 are printed. (Hint: You should have a space after each comma!) Expected Output: 4
  • 18. Multiplication Table: Write a program to print a multiplication table to 12 of a given number. Expected Output: 1
  • 19. Printing pattern: Write a program to print the following two patterns implementing the nested loop concept. 2