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

PDF
2 Python Basics II meeting 2 tunghai university pdf
PDF
LOOPS TOPIC FOR CLASS XI PYTHON SYLLABUS
PPTX
This is all about control flow in python intruducing the Break and Continue.pptx
PPTX
Loops in python including control statements and various test cases
PPTX
PYTHON 3.pptx
DOCX
iterations.docx
PPTX
Python_Loops.pptxpython loopspython loopspython loopspython loopspython loops...
PPTX
python ppt.pptx
2 Python Basics II meeting 2 tunghai university pdf
LOOPS TOPIC FOR CLASS XI PYTHON SYLLABUS
This is all about control flow in python intruducing the Break and Continue.pptx
Loops in python including control statements and various test cases
PYTHON 3.pptx
iterations.docx
Python_Loops.pptxpython loopspython loopspython loopspython loopspython loops...
python ppt.pptx

Similar to While_for_loop presententationin first year students (20)

PDF
Python_Module_2.pdf
PPTX
Mastering Python lesson 3a
PPTX
PPTX
TN 12 computer Science - ppt CHAPTER-6.pptx
PPTX
1. control structures in the python.pptx
PPTX
loops _
PDF
python program
PPTX
ForLoops.pptx
PPTX
Python programming –part 3
PPTX
Python if_else_loop_Control_Flow_Statement
PPTX
Loops in Python
PPTX
Iterations FOR LOOP AND WHILE LOOP .pptx
PDF
Slide 6_Control Structures.pdf
PPT
Py4inf 05-iterations (1)
PPT
Py4inf 05-iterations (1)
PPT
Py4inf 05-iterations
PPT
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
PPTX
Mastering Python lesson3b_for_loops
PDF
Python Flow Control
PPTX
Python Flow Control
Python_Module_2.pdf
Mastering Python lesson 3a
TN 12 computer Science - ppt CHAPTER-6.pptx
1. control structures in the python.pptx
loops _
python program
ForLoops.pptx
Python programming –part 3
Python if_else_loop_Control_Flow_Statement
Loops in Python
Iterations FOR LOOP AND WHILE LOOP .pptx
Slide 6_Control Structures.pdf
Py4inf 05-iterations (1)
Py4inf 05-iterations (1)
Py4inf 05-iterations
ppt3-conditionalstatementloopsdictionaryfunctions-240731050730-455ba0fa.ppt
Mastering Python lesson3b_for_loops
Python Flow Control
Python Flow Control
Ad

Recently uploaded (20)

PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Welding lecture in detail for understanding
PPTX
additive manufacturing of ss316l using mig welding
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Well-logging-methods_new................
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Internet of Things (IOT) - A guide to understanding
DOCX
573137875-Attendance-Management-System-original
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
bas. eng. economics group 4 presentation 1.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Construction Project Organization Group 2.pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Welding lecture in detail for understanding
additive manufacturing of ss316l using mig welding
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Well-logging-methods_new................
CYBER-CRIMES AND SECURITY A guide to understanding
Internet of Things (IOT) - A guide to understanding
573137875-Attendance-Management-System-original
UNIT 4 Total Quality Management .pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
bas. eng. economics group 4 presentation 1.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Construction Project Organization Group 2.pptx
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