SlideShare a Scribd company logo
5
Most read
8
Most read
12
Most read
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 in python used in college

More Related Content

PPSX
Modules and packages in python
PPTX
Sorting Algorithms
PPTX
Graph traversals in Data Structures
PDF
PPTX
queue & its applications
PPTX
Datastructures in python
PDF
8 python data structure-1
PPTX
Python Libraries and Modules
Modules and packages in python
Sorting Algorithms
Graph traversals in Data Structures
queue & its applications
Datastructures in python
8 python data structure-1
Python Libraries and Modules

What's hot (20)

PDF
Python-03| Data types
PPTX
Queue and its operations
PDF
Python programming : Control statements
PPTX
Binary Tree in Data Structure
PDF
Python programming : Strings
PPTX
Linked list in Data Structure and Algorithm
PPTX
POLYNOMIAL ADDITION USING LINKED LIST.pptx
PDF
Python Decision Making And Loops.pdf
PDF
pandas - Python Data Analysis
PPT
1.5 binary search tree
PPTX
áRbol 2 3
PDF
Double ended queue
PPTX
Python Functions
PPTX
My lectures circular queue
PPTX
Constructor in java
PDF
Overview of python 2019
PDF
Functions and modules in python
PPTX
Data structure power point presentation
PPTX
Stack and queue
PPTX
Trees (data structure)
Python-03| Data types
Queue and its operations
Python programming : Control statements
Binary Tree in Data Structure
Python programming : Strings
Linked list in Data Structure and Algorithm
POLYNOMIAL ADDITION USING LINKED LIST.pptx
Python Decision Making And Loops.pdf
pandas - Python Data Analysis
1.5 binary search tree
áRbol 2 3
Double ended queue
Python Functions
My lectures circular queue
Constructor in java
Overview of python 2019
Functions and modules in python
Data structure power point presentation
Stack and queue
Trees (data structure)
Ad

Similar to While-For-loop in python used in college (20)

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
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
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
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
Ad

Recently uploaded (20)

PPTX
SAP 2 completion done . PRESENTATION.pptx
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PPTX
Qualitative Qantitative and Mixed Methods.pptx
PPTX
IBA_Chapter_11_Slides_Final_Accessible.pptx
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PDF
Clinical guidelines as a resource for EBP(1).pdf
PPTX
STERILIZATION AND DISINFECTION-1.ppthhhbx
PPTX
IB Computer Science - Internal Assessment.pptx
PPTX
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
PPTX
Computer network topology notes for revision
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
PPTX
Introduction to machine learning and Linear Models
PPTX
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PDF
.pdf is not working space design for the following data for the following dat...
PDF
Introduction to Data Science and Data Analysis
PPTX
Introduction to Knowledge Engineering Part 1
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PPT
ISS -ESG Data flows What is ESG and HowHow
SAP 2 completion done . PRESENTATION.pptx
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
Qualitative Qantitative and Mixed Methods.pptx
IBA_Chapter_11_Slides_Final_Accessible.pptx
Miokarditis (Inflamasi pada Otot Jantung)
Clinical guidelines as a resource for EBP(1).pdf
STERILIZATION AND DISINFECTION-1.ppthhhbx
IB Computer Science - Internal Assessment.pptx
Market Analysis -202507- Wind-Solar+Hybrid+Street+Lights+for+the+North+Amer...
Computer network topology notes for revision
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
Introduction to machine learning and Linear Models
iec ppt-1 pptx icmr ppt on rehabilitation.pptx
Galatica Smart Energy Infrastructure Startup Pitch Deck
.pdf is not working space design for the following data for the following dat...
Introduction to Data Science and Data Analysis
Introduction to Knowledge Engineering Part 1
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
ISS -ESG Data flows What is ESG and HowHow

While-For-loop in python used in college

  • 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