狠狠撸

狠狠撸Share 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)

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

Similar to Control structures pyhton (20)

Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdf
NehaSpillai1
?
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
?
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
Anonymous9etQKwW
?
Loops in Python.pptx
Loops in Python.pptxLoops in Python.pptx
Loops in Python.pptx
Guru Nanak Dev University, Amritsar
?
python BY ME-2021python anylssis(1).pptx
python BY ME-2021python anylssis(1).pptxpython BY ME-2021python anylssis(1).pptx
python BY ME-2021python anylssis(1).pptx
rekhaaarohan
?
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
Niket Chandrawanshi
?
Chapter 2-Python and control flow statement.pptx
Chapter 2-Python and control flow statement.pptxChapter 2-Python and control flow statement.pptx
Chapter 2-Python and control flow statement.pptx
atharvdeshpande20
?
introduction to python in english presentation file
introduction to python in english presentation fileintroduction to python in english presentation file
introduction to python in english presentation file
RujanTimsina1
?
Help with Pyhon Programming Homework
Help with Pyhon Programming HomeworkHelp with Pyhon Programming Homework
Help with Pyhon Programming Homework
Helpmeinhomework
?
Presgfdjfwwwwwwwwwwqwqeeqentation11.pptx
Presgfdjfwwwwwwwwwwqwqeeqentation11.pptxPresgfdjfwwwwwwwwwwqwqeeqentation11.pptx
Presgfdjfwwwwwwwwwwqwqeeqentation11.pptx
aarohanpics
?
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
jahanullah
?
Loops
LoopsLoops
Loops
Kamran
?
1_1_python-course-notes-sections-1-7.pdf
1_1_python-course-notes-sections-1-7.pdf1_1_python-course-notes-sections-1-7.pdf
1_1_python-course-notes-sections-1-7.pdf
Javier Crisostomo
?
Decision Making & Loops
Decision Making & LoopsDecision Making & Loops
Decision Making & Loops
Akhil Kaushik
?
Complete c programming presentation
Complete c programming presentationComplete c programming presentation
Complete c programming presentation
nadim akber
?
gdscpython.pdf
gdscpython.pdfgdscpython.pdf
gdscpython.pdf
workvishalkumarmahat
?
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
ssuser7a7cd61
?
While_for_loop presententationin first year students
While_for_loop presententationin first year studentsWhile_for_loop presententationin first year students
While_for_loop presententationin first year students
SIHIGOPAL
?
A Quick Taste of C
A Quick Taste of CA Quick Taste of C
A Quick Taste of C
jeremyrand
?
Review Python
Review PythonReview Python
Review Python
ManishTiwari326
?
Python Decision Making And Loops.pdf
Python Decision Making And Loops.pdfPython Decision Making And Loops.pdf
Python Decision Making And Loops.pdf
NehaSpillai1
?
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHON
vikram mahendra
?
python BY ME-2021python anylssis(1).pptx
python BY ME-2021python anylssis(1).pptxpython BY ME-2021python anylssis(1).pptx
python BY ME-2021python anylssis(1).pptx
rekhaaarohan
?
Chapter 2-Python and control flow statement.pptx
Chapter 2-Python and control flow statement.pptxChapter 2-Python and control flow statement.pptx
Chapter 2-Python and control flow statement.pptx
atharvdeshpande20
?
introduction to python in english presentation file
introduction to python in english presentation fileintroduction to python in english presentation file
introduction to python in english presentation file
RujanTimsina1
?
Help with Pyhon Programming Homework
Help with Pyhon Programming HomeworkHelp with Pyhon Programming Homework
Help with Pyhon Programming Homework
Helpmeinhomework
?
Presgfdjfwwwwwwwwwwqwqeeqentation11.pptx
Presgfdjfwwwwwwwwwwqwqeeqentation11.pptxPresgfdjfwwwwwwwwwwqwqeeqentation11.pptx
Presgfdjfwwwwwwwwwwqwqeeqentation11.pptx
aarohanpics
?
1_1_python-course-notes-sections-1-7.pdf
1_1_python-course-notes-sections-1-7.pdf1_1_python-course-notes-sections-1-7.pdf
1_1_python-course-notes-sections-1-7.pdf
Javier Crisostomo
?
Decision Making & Loops
Decision Making & LoopsDecision Making & Loops
Decision Making & Loops
Akhil Kaushik
?
Complete c programming presentation
Complete c programming presentationComplete c programming presentation
Complete c programming presentation
nadim akber
?
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
ssuser7a7cd61
?
While_for_loop presententationin first year students
While_for_loop presententationin first year studentsWhile_for_loop presententationin first year students
While_for_loop presententationin first year students
SIHIGOPAL
?
A Quick Taste of C
A Quick Taste of CA Quick Taste of C
A Quick Taste of C
jeremyrand
?

Recently uploaded (20)

SCREENING REPORTS OF TUBERCLOSIS OF NURSING OFFICERS (2).docx
SCREENING REPORTS OF TUBERCLOSIS OF NURSING OFFICERS (2).docxSCREENING REPORTS OF TUBERCLOSIS OF NURSING OFFICERS (2).docx
SCREENING REPORTS OF TUBERCLOSIS OF NURSING OFFICERS (2).docx
naveenithkrishnan
?
IDM Crack 2025 Internet Download Manger Patch
IDM Crack 2025 Internet Download Manger PatchIDM Crack 2025 Internet Download Manger Patch
IDM Crack 2025 Internet Download Manger Patch
wistrendugftr
?
Elliptic Curve Cryptography Algorithm with Recurrent Neural Networks for Atta...
Elliptic Curve Cryptography Algorithm with Recurrent Neural Networks for Atta...Elliptic Curve Cryptography Algorithm with Recurrent Neural Networks for Atta...
Elliptic Curve Cryptography Algorithm with Recurrent Neural Networks for Atta...
IJCNCJournal
?
Introduction on how unique identifier systems are managed and coordinated - R...
Introduction on how unique identifier systems are managed and coordinated - R...Introduction on how unique identifier systems are managed and coordinated - R...
Introduction on how unique identifier systems are managed and coordinated - R...
APNIC
?
B.Sc Nursing OSCE INC.pdf for all nursing college mandatory to practice
B.Sc Nursing OSCE INC.pdf for all nursing college mandatory to practiceB.Sc Nursing OSCE INC.pdf for all nursing college mandatory to practice
B.Sc Nursing OSCE INC.pdf for all nursing college mandatory to practice
naveenithkrishnan
?
digital india initiative of indian government
digital india initiative of indian governmentdigital india initiative of indian government
digital india initiative of indian government
arujn1
?
AstuteAP: AI-Powered Supplier Invoice Automation for Seamless Accounts Payabl...
AstuteAP: AI-Powered Supplier Invoice Automation for Seamless Accounts Payabl...AstuteAP: AI-Powered Supplier Invoice Automation for Seamless Accounts Payabl...
AstuteAP: AI-Powered Supplier Invoice Automation for Seamless Accounts Payabl...
AstuteBusiness
?
Advanced Liquid Coding Techniques for Custom Shopify Development Services.pdf
Advanced Liquid Coding Techniques for Custom Shopify Development Services.pdfAdvanced Liquid Coding Techniques for Custom Shopify Development Services.pdf
Advanced Liquid Coding Techniques for Custom Shopify Development Services.pdf
CartCoders
?
CQE-7-Nursing-Quality-indicators 5.3.25 ppt.pptx
CQE-7-Nursing-Quality-indicators 5.3.25 ppt.pptxCQE-7-Nursing-Quality-indicators 5.3.25 ppt.pptx
CQE-7-Nursing-Quality-indicators 5.3.25 ppt.pptx
naveenithkrishnan
?
加拿大毕业证购买(百年理工学院成绩单)颁颁文凭学历认证
加拿大毕业证购买(百年理工学院成绩单)颁颁文凭学历认证加拿大毕业证购买(百年理工学院成绩单)颁颁文凭学历认证
加拿大毕业证购买(百年理工学院成绩单)颁颁文凭学历认证
taqyed
?
KeepItOn-2024-Internet-Shutdowns-Annual-Report.pdf
KeepItOn-2024-Internet-Shutdowns-Annual-Report.pdfKeepItOn-2024-Internet-Shutdowns-Annual-Report.pdf
KeepItOn-2024-Internet-Shutdowns-Annual-Report.pdf
sabranghindi
?
COMPUTER NETWORK With it's Types and Layer
COMPUTER NETWORK With it's Types  and LayerCOMPUTER NETWORK With it's Types  and Layer
COMPUTER NETWORK With it's Types and Layer
sureshrani1169
?
Social Media Marketing & Optimization | Prasun Dinda
Social Media Marketing & Optimization | Prasun DindaSocial Media Marketing & Optimization | Prasun Dinda
Social Media Marketing & Optimization | Prasun Dinda
Prasun Dinda
?
Advantages of Outsourcing IT Security Solutions
Advantages of Outsourcing IT Security SolutionsAdvantages of Outsourcing IT Security Solutions
Advantages of Outsourcing IT Security Solutions
Dalin Owen
?
Week-2-1.pptx Media and Information Literacy
Week-2-1.pptx Media and Information LiteracyWeek-2-1.pptx Media and Information Literacy
Week-2-1.pptx Media and Information Literacy
AngelAndres30
?
Importance of understanding buyer behaviors.pptx
Importance of understanding buyer behaviors.pptxImportance of understanding buyer behaviors.pptx
Importance of understanding buyer behaviors.pptx
ankitregmi20580419
?
Mastering FortiWeb: An Extensive Admin Guide for Secure Deployments
Mastering FortiWeb: An Extensive Admin Guide for Secure DeploymentsMastering FortiWeb: An Extensive Admin Guide for Secure Deployments
Mastering FortiWeb: An Extensive Admin Guide for Secure Deployments
Atakan ATAK
?
Antorik Q Final.pptx999999999999999999999
Antorik Q Final.pptx999999999999999999999Antorik Q Final.pptx999999999999999999999
Antorik Q Final.pptx999999999999999999999
PrayasChatterjee1
?
Intelligent-Systems-in-Manufacturing.pptx
Intelligent-Systems-in-Manufacturing.pptxIntelligent-Systems-in-Manufacturing.pptx
Intelligent-Systems-in-Manufacturing.pptx
ErickWasonga2
?
加拿大毕业证(鲍罢惭成绩单)多伦多大学毕业证如何办理
加拿大毕业证(鲍罢惭成绩单)多伦多大学毕业证如何办理加拿大毕业证(鲍罢惭成绩单)多伦多大学毕业证如何办理
加拿大毕业证(鲍罢惭成绩单)多伦多大学毕业证如何办理
taqyed
?
SCREENING REPORTS OF TUBERCLOSIS OF NURSING OFFICERS (2).docx
SCREENING REPORTS OF TUBERCLOSIS OF NURSING OFFICERS (2).docxSCREENING REPORTS OF TUBERCLOSIS OF NURSING OFFICERS (2).docx
SCREENING REPORTS OF TUBERCLOSIS OF NURSING OFFICERS (2).docx
naveenithkrishnan
?
IDM Crack 2025 Internet Download Manger Patch
IDM Crack 2025 Internet Download Manger PatchIDM Crack 2025 Internet Download Manger Patch
IDM Crack 2025 Internet Download Manger Patch
wistrendugftr
?
Elliptic Curve Cryptography Algorithm with Recurrent Neural Networks for Atta...
Elliptic Curve Cryptography Algorithm with Recurrent Neural Networks for Atta...Elliptic Curve Cryptography Algorithm with Recurrent Neural Networks for Atta...
Elliptic Curve Cryptography Algorithm with Recurrent Neural Networks for Atta...
IJCNCJournal
?
Introduction on how unique identifier systems are managed and coordinated - R...
Introduction on how unique identifier systems are managed and coordinated - R...Introduction on how unique identifier systems are managed and coordinated - R...
Introduction on how unique identifier systems are managed and coordinated - R...
APNIC
?
B.Sc Nursing OSCE INC.pdf for all nursing college mandatory to practice
B.Sc Nursing OSCE INC.pdf for all nursing college mandatory to practiceB.Sc Nursing OSCE INC.pdf for all nursing college mandatory to practice
B.Sc Nursing OSCE INC.pdf for all nursing college mandatory to practice
naveenithkrishnan
?
digital india initiative of indian government
digital india initiative of indian governmentdigital india initiative of indian government
digital india initiative of indian government
arujn1
?
AstuteAP: AI-Powered Supplier Invoice Automation for Seamless Accounts Payabl...
AstuteAP: AI-Powered Supplier Invoice Automation for Seamless Accounts Payabl...AstuteAP: AI-Powered Supplier Invoice Automation for Seamless Accounts Payabl...
AstuteAP: AI-Powered Supplier Invoice Automation for Seamless Accounts Payabl...
AstuteBusiness
?
Advanced Liquid Coding Techniques for Custom Shopify Development Services.pdf
Advanced Liquid Coding Techniques for Custom Shopify Development Services.pdfAdvanced Liquid Coding Techniques for Custom Shopify Development Services.pdf
Advanced Liquid Coding Techniques for Custom Shopify Development Services.pdf
CartCoders
?
CQE-7-Nursing-Quality-indicators 5.3.25 ppt.pptx
CQE-7-Nursing-Quality-indicators 5.3.25 ppt.pptxCQE-7-Nursing-Quality-indicators 5.3.25 ppt.pptx
CQE-7-Nursing-Quality-indicators 5.3.25 ppt.pptx
naveenithkrishnan
?
加拿大毕业证购买(百年理工学院成绩单)颁颁文凭学历认证
加拿大毕业证购买(百年理工学院成绩单)颁颁文凭学历认证加拿大毕业证购买(百年理工学院成绩单)颁颁文凭学历认证
加拿大毕业证购买(百年理工学院成绩单)颁颁文凭学历认证
taqyed
?
KeepItOn-2024-Internet-Shutdowns-Annual-Report.pdf
KeepItOn-2024-Internet-Shutdowns-Annual-Report.pdfKeepItOn-2024-Internet-Shutdowns-Annual-Report.pdf
KeepItOn-2024-Internet-Shutdowns-Annual-Report.pdf
sabranghindi
?
COMPUTER NETWORK With it's Types and Layer
COMPUTER NETWORK With it's Types  and LayerCOMPUTER NETWORK With it's Types  and Layer
COMPUTER NETWORK With it's Types and Layer
sureshrani1169
?
Social Media Marketing & Optimization | Prasun Dinda
Social Media Marketing & Optimization | Prasun DindaSocial Media Marketing & Optimization | Prasun Dinda
Social Media Marketing & Optimization | Prasun Dinda
Prasun Dinda
?
Advantages of Outsourcing IT Security Solutions
Advantages of Outsourcing IT Security SolutionsAdvantages of Outsourcing IT Security Solutions
Advantages of Outsourcing IT Security Solutions
Dalin Owen
?
Week-2-1.pptx Media and Information Literacy
Week-2-1.pptx Media and Information LiteracyWeek-2-1.pptx Media and Information Literacy
Week-2-1.pptx Media and Information Literacy
AngelAndres30
?
Importance of understanding buyer behaviors.pptx
Importance of understanding buyer behaviors.pptxImportance of understanding buyer behaviors.pptx
Importance of understanding buyer behaviors.pptx
ankitregmi20580419
?
Mastering FortiWeb: An Extensive Admin Guide for Secure Deployments
Mastering FortiWeb: An Extensive Admin Guide for Secure DeploymentsMastering FortiWeb: An Extensive Admin Guide for Secure Deployments
Mastering FortiWeb: An Extensive Admin Guide for Secure Deployments
Atakan ATAK
?
Antorik Q Final.pptx999999999999999999999
Antorik Q Final.pptx999999999999999999999Antorik Q Final.pptx999999999999999999999
Antorik Q Final.pptx999999999999999999999
PrayasChatterjee1
?
Intelligent-Systems-in-Manufacturing.pptx
Intelligent-Systems-in-Manufacturing.pptxIntelligent-Systems-in-Manufacturing.pptx
Intelligent-Systems-in-Manufacturing.pptx
ErickWasonga2
?
加拿大毕业证(鲍罢惭成绩单)多伦多大学毕业证如何办理
加拿大毕业证(鲍罢惭成绩单)多伦多大学毕业证如何办理加拿大毕业证(鲍罢惭成绩单)多伦多大学毕业证如何办理
加拿大毕业证(鲍罢惭成绩单)多伦多大学毕业证如何办理
taqyed
?

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