SlideShare una empresa de Scribd logo
1 de 19
M Vishnuvardhan
Control Structures
Control Structures are used to modify the flow of the program.
There are two types of control structures
» Branching statements
» Looping statements
M Vishnuvardhan
Branching statements – if
Syntax:
if condition:
statement1
statement2
:
statementn
Next statement
eg:
if a>b:
print(“a is big”)
NOTE all the statements which need to part of the if should be placed in same
indentation
M Vishnuvardhan
if else
The if-else statement provides an else block combined with the if statement
which is executed in the false case of the condition.
Syntax:
if condition:
#block of statements
else:
#another block of statements (else-block)
Eg:
num = int(input("enter the number?"))
if num%2 == 0:
print("Number is even...")
else:
print("Number is odd...")
M Vishnuvardhan
elif statement
The elif statement enables us to check multiple conditions and execute the
specific block of statements depending upon the true condition among them. We
can have any number of elif statements in our program depending upon our need.
Syntax:
if expression 1:
# block of statements
elif expression 2:
# block of statements
elif expression 3:
# block of statements
else:
# block of statements
M Vishnuvardhan
Nested if
Nested if statements mean an if statement inside another if statement.
Syntax:
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# inner if Block is end here
# outer if Block is end here
M Vishnuvardhan
Shorthand if & if else
If you have only one statement to execute, you can put it on the same line as the
if statement.
Syntax: if condition: #true block
Eg: if a > b: print("a is greater than b")
If you have only one statement to execute, one for if, and one for else, you can
put it all on the same line
Syntax: #true block if condition else #false block
Eg: print("A is big ") if a > b else print("B is big")
Note: This technique is known as Ternary Operators, or Conditional
Expressions.
M Vishnuvardhan
Looping - while
With the while loop we can execute a set of statements as long as a condition is
true.
Syntax: while condition:
#body of the loop
Eg: i = 1
while i < 6:
print(i)
i += 1
Note: remember to increment i, or else the loop will continue
forever.
M Vishnuvardhan
While loop with else
With the else statement we can run a block of code once when the condition no
longer is true
Syntax: while condition:
#body of the loop
else:
#false block
Eg: i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
M Vishnuvardhan
For loop
For loop can used in two ways.
» Using on a sequence
» Using range()
A for loop is used for iterating over a sequence
(i.e, either a list, a tuple, a dictionary, a set,
or a string).
Syntax: for x in sequence:
body of the loop
M Vishnuvardhan
For loop - sequence
Looping through a List
Eg: fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
Looping through a String
name=“ssbn”
for x in name:
print(x)
M Vishnuvardhan
For loop – range()
The range() function returns a sequence of numbers, starting from 0 by default,
and increments by 1 (by default), and ends at a specified number.
range (start, stop, step)
start is the starting value and is inclusive optional (default-0)
Stop is the end value and is exclusive and is mandatory
Step is incrementation from start to end optional (default -1)
Syntax: for var in range(start, stop, step):
#body of the loop
Eg: for x in range(0,6,1):
print(x)
M Vishnuvardhan
For loop – range()
» Step can be optional default is 1
for x in range(0, 6):
print(x) #prints 0,1,2,3,4,5
» Start can be optional default is 0
for x in range(6):
print(x) #prints 0,1,2,3,4,5
» Step can be other than 1
for x in range(0,6,3):
print(x) #prints 0,3
M Vishnuvardhan
For loop with else
else keyword in a for loop specifies a block of code to be executed when the loop
is finished:
Eg: for x in range(6):
print(x)
else:
print("Finally finished!")
M Vishnuvardhan
Nested loops
A nested loop is a loop inside a loop. The "inner loop" will be executed one time
for each iteration of the "outer loop":
Eg:
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)
M Vishnuvardhan
Jump statements - break
The break keyword is used to break out a for loop, or a while loop.
Syntax : break
Eg: i = 1
while i < 9:
print(i)
if i == 3:
break
i += 1
Eg:
i = 1
for i in range(6):
print(i)
if i == 3:
break
i += 1
M Vishnuvardhan
Jump statements - continue
The continue keyword is used to end the current iteration in a for
loop (or a while loop), and continues to the next iteration.
Syntax: continue
Eg: for i in range(1,11):
if i%2!=0:
continue
print(i)
M Vishnuvardhan
Jump statements - pass
The pass statement is used as a placeholder for future code. When
the pass statement is executed, nothing happens, but avoids getting
an error when empty code is not allowed. Empty code is not allowed
in loops, function definitions, class definitions, or in if
statements
Syntax: pass
Eg: if(a<b):
pass
else:
print("b<a")
M Vishnuvardhan

Más contenido relacionado

La actualidad más candente

Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructurerajshreemuthiah
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its functionFrankie Jones
 
Python Programming Strings
Python Programming StringsPython Programming Strings
Python Programming StringsSreedhar Chowdam
 
Command line arguments
Command line argumentsCommand line arguments
Command line argumentsAshok Raj
 

La actualidad más candente (6)

Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its function
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
Delegates in C#
Delegates in C#Delegates in C#
Delegates in C#
 
Python Programming Strings
Python Programming StringsPython Programming Strings
Python Programming Strings
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
 

Similar a Python Control Structures.pptx

Notes2
Notes2Notes2
Notes2hccit
 
control statements in python.pptx
control statements in python.pptxcontrol statements in python.pptx
control statements in python.pptxAnshu Varma
 
Python unit 2 M.sc cs
Python unit 2 M.sc csPython unit 2 M.sc cs
Python unit 2 M.sc csKALAISELVI P
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languagetanmaymodi4
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languageTanmay Modi
 
Chapter 13.1.5
Chapter 13.1.5Chapter 13.1.5
Chapter 13.1.5patcha535
 
CONTROL STRUCTURE IN VB
CONTROL STRUCTURE IN VBCONTROL STRUCTURE IN VB
CONTROL STRUCTURE IN VBclassall
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 
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
 
basic of desicion control statement in python
basic of  desicion control statement in pythonbasic of  desicion control statement in python
basic of desicion control statement in pythonnitamhaske
 
GE3151 PSPP UNIT III QUESTION BANK.docx.pdf
GE3151 PSPP UNIT III QUESTION BANK.docx.pdfGE3151 PSPP UNIT III QUESTION BANK.docx.pdf
GE3151 PSPP UNIT III QUESTION BANK.docx.pdfAsst.prof M.Gokilavani
 
ch2 Python flow control.pdf
ch2 Python flow control.pdfch2 Python flow control.pdf
ch2 Python flow control.pdfRanjanaThakuria1
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3Megha V
 
loopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfloopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfDheeravathBinduMadha
 

Similar a Python Control Structures.pptx (20)

Notes2
Notes2Notes2
Notes2
 
control statements in python.pptx
control statements in python.pptxcontrol statements in python.pptx
control statements in python.pptx
 
Python unit 2 M.sc cs
Python unit 2 M.sc csPython unit 2 M.sc cs
Python unit 2 M.sc cs
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Python_Module_2.pdf
Python_Module_2.pdfPython_Module_2.pdf
Python_Module_2.pdf
 
Chapter 13.1.5
Chapter 13.1.5Chapter 13.1.5
Chapter 13.1.5
 
CONTROL STRUCTURE IN VB
CONTROL STRUCTURE IN VBCONTROL STRUCTURE IN VB
CONTROL STRUCTURE IN VB
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
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
 
basic of desicion control statement in python
basic of  desicion control statement in pythonbasic of  desicion control statement in python
basic of desicion control statement in python
 
While loop
While loopWhile loop
While loop
 
Loops in Python.pptx
Loops in Python.pptxLoops in Python.pptx
Loops in Python.pptx
 
GE3151 PSPP UNIT III QUESTION BANK.docx.pdf
GE3151 PSPP UNIT III QUESTION BANK.docx.pdfGE3151 PSPP UNIT III QUESTION BANK.docx.pdf
GE3151 PSPP UNIT III QUESTION BANK.docx.pdf
 
ch2 Python flow control.pdf
ch2 Python flow control.pdfch2 Python flow control.pdf
ch2 Python flow control.pdf
 
Python programming –part 3
Python programming –part 3Python programming –part 3
Python programming –part 3
 
Lecture 9- Control Structures 1
Lecture 9- Control Structures 1Lecture 9- Control Structures 1
Lecture 9- Control Structures 1
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 
Iteration
IterationIteration
Iteration
 
loopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdfloopingstatementinpython-210628184047 (1).pdf
loopingstatementinpython-210628184047 (1).pdf
 

Más de M Vishnuvardhan Reddy (20)

Python Sets_Dictionary.pptx
Python Sets_Dictionary.pptxPython Sets_Dictionary.pptx
Python Sets_Dictionary.pptx
 
Lists_tuples.pptx
Lists_tuples.pptxLists_tuples.pptx
Lists_tuples.pptx
 
Python Strings.pptx
Python Strings.pptxPython Strings.pptx
Python Strings.pptx
 
Python Basics.pptx
Python Basics.pptxPython Basics.pptx
Python Basics.pptx
 
Python Operators.pptx
Python Operators.pptxPython Operators.pptx
Python Operators.pptx
 
Python Datatypes.pptx
Python Datatypes.pptxPython Datatypes.pptx
Python Datatypes.pptx
 
DataScience.pptx
DataScience.pptxDataScience.pptx
DataScience.pptx
 
Html forms
Html formsHtml forms
Html forms
 
Cascading Style Sheets
Cascading Style SheetsCascading Style Sheets
Cascading Style Sheets
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Scanner class
Scanner classScanner class
Scanner class
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Java intro
Java introJava intro
Java intro
 
Java applets
Java appletsJava applets
Java applets
 
Exception handling
Exception handling Exception handling
Exception handling
 
Control structures
Control structuresControl structures
Control structures
 
Constructors
ConstructorsConstructors
Constructors
 
Classes&amp;objects
Classes&amp;objectsClasses&amp;objects
Classes&amp;objects
 
Shell sort
Shell sortShell sort
Shell sort
 

Último

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 

Último (20)

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 

Python Control Structures.pptx

  • 1.
  • 2. M Vishnuvardhan Control Structures Control Structures are used to modify the flow of the program. There are two types of control structures » Branching statements » Looping statements
  • 3. M Vishnuvardhan Branching statements – if Syntax: if condition: statement1 statement2 : statementn Next statement eg: if a>b: print(“a is big”) NOTE all the statements which need to part of the if should be placed in same indentation
  • 4. M Vishnuvardhan if else The if-else statement provides an else block combined with the if statement which is executed in the false case of the condition. Syntax: if condition: #block of statements else: #another block of statements (else-block) Eg: num = int(input("enter the number?")) if num%2 == 0: print("Number is even...") else: print("Number is odd...")
  • 5. M Vishnuvardhan elif statement The elif statement enables us to check multiple conditions and execute the specific block of statements depending upon the true condition among them. We can have any number of elif statements in our program depending upon our need. Syntax: if expression 1: # block of statements elif expression 2: # block of statements elif expression 3: # block of statements else: # block of statements
  • 6. M Vishnuvardhan Nested if Nested if statements mean an if statement inside another if statement. Syntax: if (condition1): # Executes when condition1 is true if (condition2): # Executes when condition2 is true # inner if Block is end here # outer if Block is end here
  • 7. M Vishnuvardhan Shorthand if & if else If you have only one statement to execute, you can put it on the same line as the if statement. Syntax: if condition: #true block Eg: if a > b: print("a is greater than b") If you have only one statement to execute, one for if, and one for else, you can put it all on the same line Syntax: #true block if condition else #false block Eg: print("A is big ") if a > b else print("B is big") Note: This technique is known as Ternary Operators, or Conditional Expressions.
  • 8. M Vishnuvardhan Looping - while With the while loop we can execute a set of statements as long as a condition is true. Syntax: while condition: #body of the loop Eg: i = 1 while i < 6: print(i) i += 1 Note: remember to increment i, or else the loop will continue forever.
  • 9. M Vishnuvardhan While loop with else With the else statement we can run a block of code once when the condition no longer is true Syntax: while condition: #body of the loop else: #false block Eg: i = 1 while i < 6: print(i) i += 1 else: print("i is no longer less than 6")
  • 10. M Vishnuvardhan For loop For loop can used in two ways. » Using on a sequence » Using range() A for loop is used for iterating over a sequence (i.e, either a list, a tuple, a dictionary, a set, or a string). Syntax: for x in sequence: body of the loop
  • 11. M Vishnuvardhan For loop - sequence Looping through a List Eg: fruits = ["apple", "banana", "cherry"] for x in fruits: print(x) Looping through a String name=“ssbn” for x in name: print(x)
  • 12. M Vishnuvardhan For loop – range() The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number. range (start, stop, step) start is the starting value and is inclusive optional (default-0) Stop is the end value and is exclusive and is mandatory Step is incrementation from start to end optional (default -1) Syntax: for var in range(start, stop, step): #body of the loop Eg: for x in range(0,6,1): print(x)
  • 13. M Vishnuvardhan For loop – range() » Step can be optional default is 1 for x in range(0, 6): print(x) #prints 0,1,2,3,4,5 » Start can be optional default is 0 for x in range(6): print(x) #prints 0,1,2,3,4,5 » Step can be other than 1 for x in range(0,6,3): print(x) #prints 0,3
  • 14. M Vishnuvardhan For loop with else else keyword in a for loop specifies a block of code to be executed when the loop is finished: Eg: for x in range(6): print(x) else: print("Finally finished!")
  • 15. M Vishnuvardhan Nested loops A nested loop is a loop inside a loop. The "inner loop" will be executed one time for each iteration of the "outer loop": Eg: adj = ["red", "big", "tasty"] fruits = ["apple", "banana", "cherry"] for x in adj: for y in fruits: print(x, y)
  • 16. M Vishnuvardhan Jump statements - break The break keyword is used to break out a for loop, or a while loop. Syntax : break Eg: i = 1 while i < 9: print(i) if i == 3: break i += 1 Eg: i = 1 for i in range(6): print(i) if i == 3: break i += 1
  • 17. M Vishnuvardhan Jump statements - continue The continue keyword is used to end the current iteration in a for loop (or a while loop), and continues to the next iteration. Syntax: continue Eg: for i in range(1,11): if i%2!=0: continue print(i)
  • 18. M Vishnuvardhan Jump statements - pass The pass statement is used as a placeholder for future code. When the pass statement is executed, nothing happens, but avoids getting an error when empty code is not allowed. Empty code is not allowed in loops, function definitions, class definitions, or in if statements Syntax: pass Eg: if(a<b): pass else: print("b<a")