SlideShare a Scribd company logo
8
Most read
12
Most read
13
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

PPT
Stacks
sweta dargad
 
PPTX
C# String
Raghuveer Guthikonda
 
PDF
Python functions
Learnbay Datascience
 
PDF
Java I/o streams
Hamid Ghorbani
 
PPTX
Binary Search Tree in Data Structure
Dharita Chokshi
 
PPTX
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
PPTX
Stack and Queue
Apurbo Datta
 
PPTX
Tuple in python
Sharath Ankrajegowda
 
Stacks
sweta dargad
 
Python functions
Learnbay Datascience
 
Java I/o streams
Hamid Ghorbani
 
Binary Search Tree in Data Structure
Dharita Chokshi
 
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
Stack and Queue
Apurbo Datta
 
Tuple in python
Sharath Ankrajegowda
 

What's hot (20)

PPTX
Python Functions
Mohammed Sikander
 
PPT
Applet and graphics programming
mcanotes
 
PPTX
LinkedList vs Arraylist- an in depth look at java.util.LinkedList
Marcus Biel
 
PPTX
Operators in java
Madishetty Prathibha
 
PPTX
Recursion
Nalin Adhikari
 
PPTX
Linked stacks and queues
Ramzi Alqrainy
 
PPTX
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
PPTX
Pointers in c++
sai tarlekar
 
PPTX
Event handling
swapnac12
 
PPTX
Polymorphism
Nochiketa Chakraborty
 
PPTX
Values and Data types in python
Jothi Thilaga P
 
PPT
Method overriding
Azaz Maverick
 
PPT
Chapter Introduction to Modular Programming.ppt
AmanuelZewdie4
 
PPTX
Data types in python
RaginiJain21
 
PDF
Control Structures in Python
Sumit Satam
 
PPT
Strings
Nilesh Dalvi
 
PPTX
SQL Functions
ammarbrohi
 
PDF
Exception handling
Pranali Chaudhari
 
PPTX
Recursion in Data Structure
khudabux1998
 
PDF
Pointers
sarith divakar
 
Python Functions
Mohammed Sikander
 
Applet and graphics programming
mcanotes
 
LinkedList vs Arraylist- an in depth look at java.util.LinkedList
Marcus Biel
 
Operators in java
Madishetty Prathibha
 
Recursion
Nalin Adhikari
 
Linked stacks and queues
Ramzi Alqrainy
 
Pointers in c++
sai tarlekar
 
Event handling
swapnac12
 
Polymorphism
Nochiketa Chakraborty
 
Values and Data types in python
Jothi Thilaga P
 
Method overriding
Azaz Maverick
 
Chapter Introduction to Modular Programming.ppt
AmanuelZewdie4
 
Data types in python
RaginiJain21
 
Control Structures in Python
Sumit Satam
 
Strings
Nilesh Dalvi
 
SQL Functions
ammarbrohi
 
Exception handling
Pranali Chaudhari
 
Recursion in Data Structure
khudabux1998
 
Pointers
sarith divakar
 
Ad

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

PDF
2 Python Basics II meeting 2 tunghai university pdf
Anggi Andriyadi
 
PDF
LOOPS TOPIC FOR CLASS XI PYTHON SYLLABUS
samirparmar24
 
PPTX
This is all about control flow in python intruducing the Break and Continue.pptx
elezearrepil1
 
PPTX
Loops in python including control statements and various test cases
AnuragSharma710741
 
PPTX
PYTHON 3.pptx
2021bcs124
 
DOCX
iterations.docx
ssuser2e84e4
 
PPTX
Python_Loops.pptxpython loopspython loopspython loopspython loopspython loops...
RutviBaraiya
 
PPTX
python ppt.pptx
ssuserd10678
 
PDF
Python_Module_2.pdf
R.K.College of engg & Tech
 
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
 
2 Python Basics II meeting 2 tunghai university pdf
Anggi Andriyadi
 
LOOPS TOPIC FOR CLASS XI PYTHON SYLLABUS
samirparmar24
 
This is all about control flow in python intruducing the Break and Continue.pptx
elezearrepil1
 
Loops in python including control statements and various test cases
AnuragSharma710741
 
PYTHON 3.pptx
2021bcs124
 
iterations.docx
ssuser2e84e4
 
Python_Loops.pptxpython loopspython loopspython loopspython loopspython loops...
RutviBaraiya
 
python ppt.pptx
ssuserd10678
 
Python_Module_2.pdf
R.K.College of engg & Tech
 
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
 
Ad

Recently uploaded (20)

PPTX
Employee Salary Presentation.l based on data science collection of data
barridevakumari2004
 
PDF
Chad Readey - An Independent Thinker
Chad Readey
 
PPTX
Analysis of Employee_Attrition_Presentation.pptx
AdawuRedeemer
 
PDF
Digital Infrastructure – Powering the Connected Age
Heera Yadav
 
PPTX
Presentation (1) (1).pptx k8hhfftuiiigff
karthikjagath2005
 
PPTX
Lecture 1 Intro in Inferential Statistics.pptx
MiraLamuton
 
PDF
345_IT infrastructure for business management.pdf
LEANHTRAN4
 
PDF
Technical Writing Module-I Complete Notes.pdf
VedprakashArya13
 
PPTX
Measurement of Afordability for Water Supply and Sanitation in Bangladesh .pptx
akmibrahimbd
 
PDF
CH2-MODEL-SETUP-v2017.1-JC-APR27-2017.pdf
jcc00023con
 
PPTX
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
abhinavmemories2026
 
PDF
Data Analyst Certificate Programs for Beginners | IABAC
Seenivasan
 
PDF
CH1-MODEL-BUILDING-v2017.1-APR27-2017.pdf
jcc00023con
 
PPTX
GR3-PPTFINAL (1).pptx 0.91 MbHIHUHUGG,HJGH
DarylArellaga1
 
PPTX
Extract Transformation Load (3) (1).pptx
revathi148366
 
PDF
oop_java (1) of ice or cse or eee ic.pdf
sabiquntoufiqlabonno
 
PPTX
artificial intelligence deeplearning-200712115616.pptx
revathi148366
 
PDF
Data_Cleaning_Infographic_Series_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
Company Presentation pada Perusahaan ADB.pdf
didikfahmi
 
PPTX
Logistic Regression ml machine learning.pptx
abdullahcocindia
 
Employee Salary Presentation.l based on data science collection of data
barridevakumari2004
 
Chad Readey - An Independent Thinker
Chad Readey
 
Analysis of Employee_Attrition_Presentation.pptx
AdawuRedeemer
 
Digital Infrastructure – Powering the Connected Age
Heera Yadav
 
Presentation (1) (1).pptx k8hhfftuiiigff
karthikjagath2005
 
Lecture 1 Intro in Inferential Statistics.pptx
MiraLamuton
 
345_IT infrastructure for business management.pdf
LEANHTRAN4
 
Technical Writing Module-I Complete Notes.pdf
VedprakashArya13
 
Measurement of Afordability for Water Supply and Sanitation in Bangladesh .pptx
akmibrahimbd
 
CH2-MODEL-SETUP-v2017.1-JC-APR27-2017.pdf
jcc00023con
 
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
abhinavmemories2026
 
Data Analyst Certificate Programs for Beginners | IABAC
Seenivasan
 
CH1-MODEL-BUILDING-v2017.1-APR27-2017.pdf
jcc00023con
 
GR3-PPTFINAL (1).pptx 0.91 MbHIHUHUGG,HJGH
DarylArellaga1
 
Extract Transformation Load (3) (1).pptx
revathi148366
 
oop_java (1) of ice or cse or eee ic.pdf
sabiquntoufiqlabonno
 
artificial intelligence deeplearning-200712115616.pptx
revathi148366
 
Data_Cleaning_Infographic_Series_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Company Presentation pada Perusahaan ADB.pdf
didikfahmi
 
Logistic Regression ml machine learning.pptx
abdullahcocindia
 

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