SlideShare a Scribd company logo
Python Control Structures
Python If-else statements
• Decision making is the most important aspect of almost all the
programming languages. As the name implies, decision making
allows us to run a particular block of code for a particular decision.
Here, the decisions are made on the validity of the particular
conditions. Condition checking is the backbone of decision making.
• Indentation
Python relies on indentation (whitespace at the beginning of a line)
to define scope in the code. Other programming languages often
use curly-brackets for this purpose.
Statement Description
If Statement The if statement is used to test a
specific condition. If the condition
is true, a block of code (if-block)
will be executed.
If - else Statement The if-else statement is similar to
if statement except the fact that,
it also provides the block of the
code for the false case of the
condition to be checked. If the
condition provided in the if
statement is false, then the else
statement will be executed.
Nested if Statement Nested if statements enable us to
use if ? else statement inside an
outer if statement.
The if statement
The if statement is used to test a particular condition and if the condition is true, it
executes a block of code known as if-block. The condition of if statement can be any valid logical
expression which can be either evaluated to true or false.
Syntax
if expression:
statement
Example 1
num = int(input("enter the number?"))
if num%2 == 0:
print("Number is even")
Output:
enter the number?
10 Number is even
Program to print the largest of the three numbers
a = int(input("Enter a? "));
b = int(input("Enter b? "));
c = int(input("Enter c? "));
if a>b and a>c:
print("a is largest");
if b>a and b>c:
print("b is largest");
if c>a and c>b:
print("c is largest");
Output:
Enter a? 100 Enter b? 120 Enter c? 130 c is largest
Using Else
Program to check whether a person is eligible to
vote or not.
age = int (input("Enter your age? "))
if age>=18:
print("You are eligible to vote !!");
else:
print("Sorry! you have to wait !!");
Output:
• Enter your age? 90 You are eligible to vote !!
The 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. However, using elif is optional.
The syntax of the elif statement is given below.
if expression 1:
# block of statements
elif expression 2:
# block of statements
elif expression 3:
# block of statements
else:
# block of statements
Example
marks = int(input("Enter the marks? "))
if marks > 85 and marks <= 100:
print("Congrats ! you scored grade A ...")
elif marks > 60 and marks <= 85:
print("You scored grade B + ...")
elif marks > 40 and marks <= 60:
print("You scored grade B ...")
elif (marks > 30 and marks <= 40):
print("You scored grade C ...")
else:
print("Sorry you are fail ?")
Python Loops
• The flow of the programs written in any programming language is sequential by
default. Sometimes we may need to alter the flow of the program. The execution of a
specific code may need to be repeated several numbers of times.
Why we use loops in python?
• The looping simplifies the complex problems into the easy ones. It enables us to alter
the flow of the program so that instead of writing the same code again and again, we
can repeat the same code for a finite number of times.
For example, if we need to print the first 10 natural numbers then, instead of using
the print statement 10 times, we can print inside a loop which runs up to 10 iterations.
Advantages of loops
There are the following advantages of loops in Python.
It provides code re-usability.
Using loops, we do not need to write the same code again and again.
Using loops, we can traverse over the elements of data structures (array or linked lists).
Loops
For Loop
The for loop in Python is used to iterate the statements or a part of the program several times. It
is frequently used to traverse the data structures like list, tuple, or dictionary.
The syntax of for loop in python is given below.
for iterating_var in sequence:
statement(s)
Iterating string using for loop
str = "Python"
for i in str:
print(i)
Output
P
Y
T
H
O
N
Program to print the sum of the given list.
list = [10,30,23,43,65,12]
sum = 0
for i in list:
sum = sum+i
print("The sum is:",sum)
Output:
The sum is: 183
For loop Using range() function
The range() function
The range() function is used to generate the sequence of the
numbers. If we pass the range(10), it will generate the numbers from 0 to
9. The syntax of the range() function is given below.
Syntax:
range(start,stop,step size)
 The start represents the beginning of the iteration.
 The stop represents that the loop will iterate till stop-1. The range(1,5) will
generate numbers 1 to 4 iterations. It is optional.
 The step size is used to skip the specific numbers from the iteration. It is
optional to use. By default, the step size is 1. It is optional.
Program to print numbers in sequence.
for i in range(10):
print(i,end = ' ')
Output:
0 1 2 3 4 5 6 7 8 9
What does end do:
The print() function inserts a new line at the end, by
default. In Python 2, it can be suppressed by putting ','
at the end. In Python 3, "end =' '" appends space
instead of newline.
Program to print table of given number.
n = int(input("Enter the number "))
for i in range(1,11):
c = n*i
print(n,"*",i,"=",c)
Output:
Enter the number 10
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100

More Related Content

What's hot (20)

PPTX
Operators and Control Statements in Python
RajeswariA8
 
ODP
Introduction to Python - Training for Kids
Aimee Maree
 
PDF
Python-02| Input, Output & Import
Mohd Sajjad
 
PDF
Lesson 03 python statement, indentation and comments
Nilimesh Halder
 
PPTX
Introduction to Python Part-1
Devashish Kumar
 
PPTX
Python Basics
Adheetha O. V
 
PDF
Python unit 2 as per Anna university syllabus
DhivyaSubramaniyam
 
PPTX
Introduction to Python Programming
VijaySharma802
 
PPTX
Python programming language
Ebrahim Shakhatreh
 
PDF
Basic Concepts in Python
Sumit Satam
 
PDF
Python programming msc(cs)
KALAISELVI P
 
PPTX
While loop
RabiyaZhexembayeva
 
PPTX
Intro to Python Programming Language
Dipankar Achinta
 
ODP
Moving to Python 3
Nick Efford
 
PPTX
Python programming
Ashwin Kumar Ramasamy
 
PPTX
Python for loop
Aishwarya Deshmukh
 
PPTX
Learning Python - Week 2
Mindy McAdams
 
PPTX
Python ppt
Anush verma
 
PPTX
Python advance
Deepak Chandella
 
PDF
仕事で使うF#
bleis tift
 
Operators and Control Statements in Python
RajeswariA8
 
Introduction to Python - Training for Kids
Aimee Maree
 
Python-02| Input, Output & Import
Mohd Sajjad
 
Lesson 03 python statement, indentation and comments
Nilimesh Halder
 
Introduction to Python Part-1
Devashish Kumar
 
Python Basics
Adheetha O. V
 
Python unit 2 as per Anna university syllabus
DhivyaSubramaniyam
 
Introduction to Python Programming
VijaySharma802
 
Python programming language
Ebrahim Shakhatreh
 
Basic Concepts in Python
Sumit Satam
 
Python programming msc(cs)
KALAISELVI P
 
While loop
RabiyaZhexembayeva
 
Intro to Python Programming Language
Dipankar Achinta
 
Moving to Python 3
Nick Efford
 
Python programming
Ashwin Kumar Ramasamy
 
Python for loop
Aishwarya Deshmukh
 
Learning Python - Week 2
Mindy McAdams
 
Python ppt
Anush verma
 
Python advance
Deepak Chandella
 
仕事で使うF#
bleis tift
 

Similar to Control structures pyhton (20)

PDF
Python Decision Making And Loops.pdf
NehaSpillai1
 
PDF
Unit 1- Part-2-Control and Loop Statements.pdf
Harsha Patil
 
PPTX
industry coding practice unit-2 ppt.pptx
LakshmiMarineni
 
PPTX
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
PPTX
lecture 2.pptx
Anonymous9etQKwW
 
PPTX
Loops in Python.pptx
Guru Nanak Dev University, Amritsar
 
PPTX
python BY ME-2021python anylssis(1).pptx
rekhaaarohan
 
PPS
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
PPTX
Chapter 2-Python and control flow statement.pptx
atharvdeshpande20
 
PPT
introduction to python in english presentation file
RujanTimsina1
 
PPT
Help with Pyhon Programming Homework
Helpmeinhomework
 
PPTX
Presgfdjfwwwwwwwwwwqwqeeqentation11.pptx
aarohanpics
 
PPTX
Introduction& Overview-to-C++_programming.pptx
divyadhanwani67
 
PPTX
Introduction to C++ programming language
divyadhanwani67
 
PPT
C Sharp Jn (3)
jahanullah
 
PPT
Loops
Kamran
 
PDF
1_1_python-course-notes-sections-1-7.pdf
Javier Crisostomo
 
PPTX
Decision Making & Loops
Akhil Kaushik
 
DOCX
Complete c programming presentation
nadim akber
 
PDF
gdscpython.pdf
workvishalkumarmahat
 
Python Decision Making And Loops.pdf
NehaSpillai1
 
Unit 1- Part-2-Control and Loop Statements.pdf
Harsha Patil
 
industry coding practice unit-2 ppt.pptx
LakshmiMarineni
 
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
 
lecture 2.pptx
Anonymous9etQKwW
 
python BY ME-2021python anylssis(1).pptx
rekhaaarohan
 
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
Chapter 2-Python and control flow statement.pptx
atharvdeshpande20
 
introduction to python in english presentation file
RujanTimsina1
 
Help with Pyhon Programming Homework
Helpmeinhomework
 
Presgfdjfwwwwwwwwwwqwqeeqentation11.pptx
aarohanpics
 
Introduction& Overview-to-C++_programming.pptx
divyadhanwani67
 
Introduction to C++ programming language
divyadhanwani67
 
C Sharp Jn (3)
jahanullah
 
Loops
Kamran
 
1_1_python-course-notes-sections-1-7.pdf
Javier Crisostomo
 
Decision Making & Loops
Akhil Kaushik
 
Complete c programming presentation
nadim akber
 
gdscpython.pdf
workvishalkumarmahat
 
Ad

Recently uploaded (20)

PPTX
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
PDF
Enhancing Parental Roles in Protecting Children from Online Sexual Exploitati...
ICT Frame Magazine Pvt. Ltd.
 
PDF
BRKACI-1001 - Your First 7 Days of ACI.pdf
fcesargonca
 
PPTX
PHIPA-Compliant Web Hosting in Toronto: What Healthcare Providers Must Know
steve198109
 
PDF
Boardroom AI: The Next 10 Moves | Cerebraix Talent Tech
ssuser73bdb11
 
PDF
The Internet - By the numbers, presented at npNOG 11
APNIC
 
PDF
BRKACI-1003 ACI Brownfield Migration - Real World Experiences and Best Practi...
fcesargonca
 
PPTX
西班牙巴利阿里群岛大学电子版毕业证{UIBLetterUIB文凭证书}文凭复刻
Taqyea
 
PPTX
Lec15_Mutability Immutability-converted.pptx
khanjahanzaib1
 
DOCX
Custom vs. Off-the-Shelf Banking Software
KristenCarter35
 
PDF
BRKSP-2551 - Introduction to Segment Routing.pdf
fcesargonca
 
PDF
Digital burnout toolkit for youth workers and teachers
asociatiastart123
 
PPTX
Metaphysics_Presentation_With_Visuals.pptx
erikjohnsales1
 
PPTX
04 Output 1 Instruments & Tools (3).pptx
GEDYIONGebre
 
PPTX
Networking_Essentials_version_3.0_-_Module_3.pptx
ryan622010
 
PPTX
Networking_Essentials_version_3.0_-_Module_5.pptx
ryan622010
 
PDF
BRKAPP-1102 - Proactive Network and Application Monitoring.pdf
fcesargonca
 
PDF
Top 10 Testing Procedures to Ensure Your Magento to Shopify Migration Success...
CartCoders
 
PDF
FutureCon Seattle 2025 Presentation Slides - You Had One Job
Suzanne Aldrich
 
PPTX
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
Enhancing Parental Roles in Protecting Children from Online Sexual Exploitati...
ICT Frame Magazine Pvt. Ltd.
 
BRKACI-1001 - Your First 7 Days of ACI.pdf
fcesargonca
 
PHIPA-Compliant Web Hosting in Toronto: What Healthcare Providers Must Know
steve198109
 
Boardroom AI: The Next 10 Moves | Cerebraix Talent Tech
ssuser73bdb11
 
The Internet - By the numbers, presented at npNOG 11
APNIC
 
BRKACI-1003 ACI Brownfield Migration - Real World Experiences and Best Practi...
fcesargonca
 
西班牙巴利阿里群岛大学电子版毕业证{UIBLetterUIB文凭证书}文凭复刻
Taqyea
 
Lec15_Mutability Immutability-converted.pptx
khanjahanzaib1
 
Custom vs. Off-the-Shelf Banking Software
KristenCarter35
 
BRKSP-2551 - Introduction to Segment Routing.pdf
fcesargonca
 
Digital burnout toolkit for youth workers and teachers
asociatiastart123
 
Metaphysics_Presentation_With_Visuals.pptx
erikjohnsales1
 
04 Output 1 Instruments & Tools (3).pptx
GEDYIONGebre
 
Networking_Essentials_version_3.0_-_Module_3.pptx
ryan622010
 
Networking_Essentials_version_3.0_-_Module_5.pptx
ryan622010
 
BRKAPP-1102 - Proactive Network and Application Monitoring.pdf
fcesargonca
 
Top 10 Testing Procedures to Ensure Your Magento to Shopify Migration Success...
CartCoders
 
FutureCon Seattle 2025 Presentation Slides - You Had One Job
Suzanne Aldrich
 
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
Ad

Control structures pyhton

  • 2. Python If-else statements • Decision making is the most important aspect of almost all the programming languages. As the name implies, decision making allows us to run a particular block of code for a particular decision. Here, the decisions are made on the validity of the particular conditions. Condition checking is the backbone of decision making. • Indentation Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose.
  • 3. Statement Description If Statement The if statement is used to test a specific condition. If the condition is true, a block of code (if-block) will be executed. If - else Statement The if-else statement is similar to if statement except the fact that, it also provides the block of the code for the false case of the condition to be checked. If the condition provided in the if statement is false, then the else statement will be executed. Nested if Statement Nested if statements enable us to use if ? else statement inside an outer if statement.
  • 4. The if statement The if statement is used to test a particular condition and if the condition is true, it executes a block of code known as if-block. The condition of if statement can be any valid logical expression which can be either evaluated to true or false. Syntax if expression: statement Example 1 num = int(input("enter the number?")) if num%2 == 0: print("Number is even") Output: enter the number? 10 Number is even
  • 5. Program to print the largest of the three numbers a = int(input("Enter a? ")); b = int(input("Enter b? ")); c = int(input("Enter c? ")); if a>b and a>c: print("a is largest"); if b>a and b>c: print("b is largest"); if c>a and c>b: print("c is largest"); Output: Enter a? 100 Enter b? 120 Enter c? 130 c is largest
  • 6. Using Else Program to check whether a person is eligible to vote or not. age = int (input("Enter your age? ")) if age>=18: print("You are eligible to vote !!"); else: print("Sorry! you have to wait !!"); Output: • Enter your age? 90 You are eligible to vote !!
  • 7. The 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. However, using elif is optional. The syntax of the elif statement is given below. if expression 1: # block of statements elif expression 2: # block of statements elif expression 3: # block of statements else: # block of statements
  • 8. Example marks = int(input("Enter the marks? ")) if marks > 85 and marks <= 100: print("Congrats ! you scored grade A ...") elif marks > 60 and marks <= 85: print("You scored grade B + ...") elif marks > 40 and marks <= 60: print("You scored grade B ...") elif (marks > 30 and marks <= 40): print("You scored grade C ...") else: print("Sorry you are fail ?")
  • 9. Python Loops • The flow of the programs written in any programming language is sequential by default. Sometimes we may need to alter the flow of the program. The execution of a specific code may need to be repeated several numbers of times. Why we use loops in python? • The looping simplifies the complex problems into the easy ones. It enables us to alter the flow of the program so that instead of writing the same code again and again, we can repeat the same code for a finite number of times. For example, if we need to print the first 10 natural numbers then, instead of using the print statement 10 times, we can print inside a loop which runs up to 10 iterations. Advantages of loops There are the following advantages of loops in Python. It provides code re-usability. Using loops, we do not need to write the same code again and again. Using loops, we can traverse over the elements of data structures (array or linked lists).
  • 10. Loops For Loop The for loop in Python is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like list, tuple, or dictionary. The syntax of for loop in python is given below. for iterating_var in sequence: statement(s) Iterating string using for loop str = "Python" for i in str: print(i)
  • 11. Output P Y T H O N Program to print the sum of the given list. list = [10,30,23,43,65,12] sum = 0 for i in list: sum = sum+i print("The sum is:",sum) Output: The sum is: 183
  • 12. For loop Using range() function The range() function The range() function is used to generate the sequence of the numbers. If we pass the range(10), it will generate the numbers from 0 to 9. The syntax of the range() function is given below. Syntax: range(start,stop,step size)  The start represents the beginning of the iteration.  The stop represents that the loop will iterate till stop-1. The range(1,5) will generate numbers 1 to 4 iterations. It is optional.  The step size is used to skip the specific numbers from the iteration. It is optional to use. By default, the step size is 1. It is optional.
  • 13. Program to print numbers in sequence. for i in range(10): print(i,end = ' ') Output: 0 1 2 3 4 5 6 7 8 9 What does end do: The print() function inserts a new line at the end, by default. In Python 2, it can be suppressed by putting ',' at the end. In Python 3, "end =' '" appends space instead of newline.
  • 14. Program to print table of given number. n = int(input("Enter the number ")) for i in range(1,11): c = n*i print(n,"*",i,"=",c) Output: Enter the number 10 10 * 1 = 10 10 * 2 = 20 10 * 3 = 30 10 * 4 = 40 10 * 5 = 50 10 * 6 = 60 10 * 7 = 70 10 * 8 = 80 10 * 9 = 90 10 * 10 = 100