SlideShare a Scribd company logo
1 of 18
Programming in Python
Week-4 Content
Conditional Statements in Python
● Till now all the statements we have used, have been sequential
● Control structure - directs the order of execution of code
● In real world, we usually evaluate information & then decide the course of
action
● The code block will be executed only if condition is satisfied.
● if - used in python for conditional execution of statement
● Indentation is important while defining if-blocks
● if-blocks can be nested
The else & elif clauses
● Sometimes you might want to execute a block of code if the if-condition is
evaluated to False.
● If if-condition is True, the block under if will be executed, else the block under
else will be executed.
● If there are multiple conditions, elif can be used
● If one of the condition is true, others will be ignored
Nested if blocks
If <expr>: - Indentation level 0
<stmt> - Indentation level 1
<stmt>
if <expr>: - Inner if - at indentation level 1
<stmt> - Indentation level 2
<stmt>
else: - Else paired with inner if - level 1
<stmt>
<stmt>
else: - Else paired with the first if - level 0
<stmt>
<stmt>
Loops in Python
● Python has 2 loop commands:
○ for loop
○ while loop
● All loops have same basic functionality, they only differ in syntax.
● The loops will run till a condition is met.
while loop
● Execute a set of sentences until the condition is true.
● Indented statements required
● Condition should evaluate to True or False
● Loop can be broken in between
break, continue, pass
● break - The break is a keyword in python which is used to bring the program
control out of the loop.
● The break statement breaks the loops one by one.
● continue - The continue statement in Python returns the control to the
beginning of the loop.
● The continue statement rejects all the remaining statements in the current
iteration of the loop and moves the control back to the top of the loop.
● pass - The pass statement in Python is used when a statement is required
syntactically but you do not want any command or code to execute.
● The pass statement is a null operation; nothing happens when it executes.
for-loop
● used for sequential traversal
● falls under the category of definite iteration
● The loop will be iterated only for a specified number of times.
● We can also execute statements, once for each element in a sequence data
type - list, tuple, string, etc.
● Can use break statement to break the for-loop in between
● Can use pass, continue statements to skip a piece of code.
for loops using range, strings, list
● range(start, end) function - will run the for loop from start number to end
number
● Using list, string and tuple as a for loop condition will run the loop once for
each element of these data types.
● You can iterate nested lists using nested for loops
Nested loops
● Python language allows to use one loop inside another.
● Any kind of loops can be combined together to form nested loops.
● Even if-elif-else structures can be nested inside loops and vice versa.
Nested For Loop
Level 0 indentation
Level 1 indentation (as it is a part of the outer for loop)
Level 2 indentation
These lines are a part of the inner loop
The statements on level 2 indentation will be executed the number of times
specified in the inner loop.
The whole inner loop will be executed the number of times specified in the outer
loop.
Nested while loop
Level 0 indentation
Level 1 indentation - inside the outer while loop
Level 2 indentation - statements part of the inner
while loop
The statements inside the inner while loop will be executed till the condition
specified in the inner while loop is successful.
The inner while loop will execute as long as the condition specified in the outer
while loop is successful.
Printing patterns using for-loops
Homework Questions

More Related Content

What's hot

(3) c sharp introduction_basics_part_ii
(3) c sharp introduction_basics_part_ii(3) c sharp introduction_basics_part_ii
(3) c sharp introduction_basics_part_iiNico Ludwig
 
CS844 U1 Individual Project
CS844 U1 Individual ProjectCS844 U1 Individual Project
CS844 U1 Individual ProjectThienSi Le
 
Python decision making_loops part7
Python decision making_loops part7Python decision making_loops part7
Python decision making_loops part7Vishal Dutt
 
Unit testing.pptx [repaired]
Unit testing.pptx [repaired]Unit testing.pptx [repaired]
Unit testing.pptx [repaired]Mohammad Asmar
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision ControlJayfee Ramos
 
Java loops for, while and do...while
Java loops   for, while and do...whileJava loops   for, while and do...while
Java loops for, while and do...whileJayfee Ramos
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsRanel Padon
 
The Awesome Python Class Part-2
The Awesome Python Class Part-2The Awesome Python Class Part-2
The Awesome Python Class Part-2Binay Kumar Ray
 
Python session 4 subprocess- by Gopal.A (Python developer)
Python session 4 subprocess- by Gopal.A (Python developer)Python session 4 subprocess- by Gopal.A (Python developer)
Python session 4 subprocess- by Gopal.A (Python developer)Navaneethan Naveen
 
Error and exception in python
Error and exception in pythonError and exception in python
Error and exception in pythonjunnubabu
 
Looping and switch cases
Looping and switch casesLooping and switch cases
Looping and switch casesMeoRamos
 

What's hot (19)

JavaScript iteration
JavaScript iterationJavaScript iteration
JavaScript iteration
 
(3) c sharp introduction_basics_part_ii
(3) c sharp introduction_basics_part_ii(3) c sharp introduction_basics_part_ii
(3) c sharp introduction_basics_part_ii
 
CS844 U1 Individual Project
CS844 U1 Individual ProjectCS844 U1 Individual Project
CS844 U1 Individual Project
 
Python decision making_loops part7
Python decision making_loops part7Python decision making_loops part7
Python decision making_loops part7
 
Unit testing.pptx [repaired]
Unit testing.pptx [repaired]Unit testing.pptx [repaired]
Unit testing.pptx [repaired]
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision Control
 
Java loops for, while and do...while
Java loops   for, while and do...whileJava loops   for, while and do...while
Java loops for, while and do...while
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
 
Introduction to golang
Introduction to golangIntroduction to golang
Introduction to golang
 
Php day2010
Php day2010Php day2010
Php day2010
 
Basic constructs i
Basic constructs  iBasic constructs  i
Basic constructs i
 
The Awesome Python Class Part-2
The Awesome Python Class Part-2The Awesome Python Class Part-2
The Awesome Python Class Part-2
 
study of java
study of java study of java
study of java
 
Python session 4 subprocess- by Gopal.A (Python developer)
Python session 4 subprocess- by Gopal.A (Python developer)Python session 4 subprocess- by Gopal.A (Python developer)
Python session 4 subprocess- by Gopal.A (Python developer)
 
41c
41c41c
41c
 
Operating systems
Operating systemsOperating systems
Operating systems
 
Operating systems
Operating systemsOperating systems
Operating systems
 
Error and exception in python
Error and exception in pythonError and exception in python
Error and exception in python
 
Looping and switch cases
Looping and switch casesLooping and switch cases
Looping and switch cases
 

Similar to Programming in python - Week 4

03 conditions loops
03   conditions loops03   conditions loops
03 conditions loopsManzoor ALam
 
Python Revision Tour 1 Class XII CS
Python Revision Tour 1 Class XII CSPython Revision Tour 1 Class XII CS
Python Revision Tour 1 Class XII CSclass12sci
 
Chapter05-Control Structures.pptx
Chapter05-Control Structures.pptxChapter05-Control Structures.pptx
Chapter05-Control Structures.pptxAdrianVANTOPINA
 
Control_Statements_in_Python.pptx
Control_Statements_in_Python.pptxControl_Statements_in_Python.pptx
Control_Statements_in_Python.pptxKoteswari Kasireddy
 
RaspberryPi & Python Workshop Day - 02.pptx
RaspberryPi & Python Workshop Day - 02.pptxRaspberryPi & Python Workshop Day - 02.pptx
RaspberryPi & Python Workshop Day - 02.pptxShivanshSeth6
 
Break, Continue and Pass in Python.pdf
Break, Continue and Pass in Python.pdfBreak, Continue and Pass in Python.pdf
Break, Continue and Pass in Python.pdfNehaSpillai1
 
Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdfNehaSpillai1
 
PYTHON FULL TUTORIAL WITH PROGRAMMS
PYTHON FULL TUTORIAL WITH PROGRAMMSPYTHON FULL TUTORIAL WITH PROGRAMMS
PYTHON FULL TUTORIAL WITH PROGRAMMSAniruddha Paul
 
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...jaychoudhary37
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in collegessuser7a7cd61
 
class interview demo
class interview demo class interview demo
class interview demo HELP4STUDENTS
 
Demo for Class.pptx
 Demo for Class.pptx Demo for Class.pptx
Demo for Class.pptxHELP4STUDENTS
 

Similar to Programming in python - Week 4 (20)

03 conditions loops
03   conditions loops03   conditions loops
03 conditions loops
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
python.pptx
python.pptxpython.pptx
python.pptx
 
Python Revision Tour 1 Class XII CS
Python Revision Tour 1 Class XII CSPython Revision Tour 1 Class XII CS
Python Revision Tour 1 Class XII CS
 
Chapter05-Control Structures.pptx
Chapter05-Control Structures.pptxChapter05-Control Structures.pptx
Chapter05-Control Structures.pptx
 
Loops in R
Loops in RLoops in R
Loops in R
 
Control_Statements_in_Python.pptx
Control_Statements_in_Python.pptxControl_Statements_in_Python.pptx
Control_Statements_in_Python.pptx
 
RaspberryPi & Python Workshop Day - 02.pptx
RaspberryPi & Python Workshop Day - 02.pptxRaspberryPi & Python Workshop Day - 02.pptx
RaspberryPi & Python Workshop Day - 02.pptx
 
Break, Continue and Pass in Python.pdf
Break, Continue and Pass in Python.pdfBreak, Continue and Pass in Python.pdf
Break, Continue and Pass in Python.pdf
 
Loop structures
Loop structuresLoop structures
Loop structures
 
Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdf
 
Nested Loops in C.pptx
Nested Loops in C.pptxNested Loops in C.pptx
Nested Loops in C.pptx
 
PYTHON FULL TUTORIAL WITH PROGRAMMS
PYTHON FULL TUTORIAL WITH PROGRAMMSPYTHON FULL TUTORIAL WITH PROGRAMMS
PYTHON FULL TUTORIAL WITH PROGRAMMS
 
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
FAL(2022-23)_FRESHERS_CSE1012_ETH_AP2022234000166_Reference_Material_I_15-Nov...
 
While-For-loop in python used in college
While-For-loop in python used in collegeWhile-For-loop in python used in college
While-For-loop in python used in college
 
C language 2
C language 2C language 2
C language 2
 
class interview demo
class interview demo class interview demo
class interview demo
 
python
pythonpython
python
 
Demo for Class.pptx
 Demo for Class.pptx Demo for Class.pptx
Demo for Class.pptx
 
C language (Part 2)
C language (Part 2)C language (Part 2)
C language (Part 2)
 

More from Priya Nayak

Programming in python w6
Programming in python w6Programming in python w6
Programming in python w6Priya Nayak
 
Programming in python - Week 5
Programming in python - Week 5Programming in python - Week 5
Programming in python - Week 5Priya Nayak
 
Programming in python - Week 3
Programming in python - Week 3Programming in python - Week 3
Programming in python - Week 3Priya Nayak
 
Programming in python - Week 2
Programming in python - Week 2Programming in python - Week 2
Programming in python - Week 2Priya Nayak
 
Programming in python
Programming in python Programming in python
Programming in python Priya Nayak
 

More from Priya Nayak (6)

Programming in python w6
Programming in python w6Programming in python w6
Programming in python w6
 
Programming in python - Week 5
Programming in python - Week 5Programming in python - Week 5
Programming in python - Week 5
 
Programming in python - Week 3
Programming in python - Week 3Programming in python - Week 3
Programming in python - Week 3
 
Programming in python - Week 2
Programming in python - Week 2Programming in python - Week 2
Programming in python - Week 2
 
Programming in python
Programming in python Programming in python
Programming in python
 
Git and GitHub
Git and GitHubGit and GitHub
Git and GitHub
 

Recently uploaded

Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 

Recently uploaded (20)

Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 

Programming in python - Week 4

  • 2. Conditional Statements in Python ● Till now all the statements we have used, have been sequential ● Control structure - directs the order of execution of code ● In real world, we usually evaluate information & then decide the course of action ● The code block will be executed only if condition is satisfied. ● if - used in python for conditional execution of statement ● Indentation is important while defining if-blocks ● if-blocks can be nested
  • 3.
  • 4. The else & elif clauses ● Sometimes you might want to execute a block of code if the if-condition is evaluated to False. ● If if-condition is True, the block under if will be executed, else the block under else will be executed. ● If there are multiple conditions, elif can be used ● If one of the condition is true, others will be ignored
  • 5.
  • 6. Nested if blocks If <expr>: - Indentation level 0 <stmt> - Indentation level 1 <stmt> if <expr>: - Inner if - at indentation level 1 <stmt> - Indentation level 2 <stmt> else: - Else paired with inner if - level 1 <stmt> <stmt> else: - Else paired with the first if - level 0 <stmt> <stmt>
  • 7. Loops in Python ● Python has 2 loop commands: ○ for loop ○ while loop ● All loops have same basic functionality, they only differ in syntax. ● The loops will run till a condition is met.
  • 8. while loop ● Execute a set of sentences until the condition is true. ● Indented statements required ● Condition should evaluate to True or False ● Loop can be broken in between
  • 9.
  • 10. break, continue, pass ● break - The break is a keyword in python which is used to bring the program control out of the loop. ● The break statement breaks the loops one by one. ● continue - The continue statement in Python returns the control to the beginning of the loop. ● The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop. ● pass - The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute. ● The pass statement is a null operation; nothing happens when it executes.
  • 11. for-loop ● used for sequential traversal ● falls under the category of definite iteration ● The loop will be iterated only for a specified number of times. ● We can also execute statements, once for each element in a sequence data type - list, tuple, string, etc. ● Can use break statement to break the for-loop in between ● Can use pass, continue statements to skip a piece of code.
  • 12.
  • 13. for loops using range, strings, list ● range(start, end) function - will run the for loop from start number to end number ● Using list, string and tuple as a for loop condition will run the loop once for each element of these data types. ● You can iterate nested lists using nested for loops
  • 14. Nested loops ● Python language allows to use one loop inside another. ● Any kind of loops can be combined together to form nested loops. ● Even if-elif-else structures can be nested inside loops and vice versa.
  • 15. Nested For Loop Level 0 indentation Level 1 indentation (as it is a part of the outer for loop) Level 2 indentation These lines are a part of the inner loop The statements on level 2 indentation will be executed the number of times specified in the inner loop. The whole inner loop will be executed the number of times specified in the outer loop.
  • 16. Nested while loop Level 0 indentation Level 1 indentation - inside the outer while loop Level 2 indentation - statements part of the inner while loop The statements inside the inner while loop will be executed till the condition specified in the inner while loop is successful. The inner while loop will execute as long as the condition specified in the outer while loop is successful.