0% found this document useful (0 votes)
3 views

Conditional Statements 2

The document explains conditional statements in Python, including IF, IF-ELSE, and nested conditionals, which allow programs to execute different code paths based on conditions. It covers the structure of these statements, the use of Boolean expressions, and shortcuts for conditions. Additionally, it provides examples of practical applications such as determining leap years and calculating body mass index (BMI).

Uploaded by

Deepak Bhagtani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Conditional Statements 2

The document explains conditional statements in Python, including IF, IF-ELSE, and nested conditionals, which allow programs to execute different code paths based on conditions. It covers the structure of these statements, the use of Boolean expressions, and shortcuts for conditions. Additionally, it provides examples of practical applications such as determining leap years and calculating body mass index (BMI).

Uploaded by

Deepak Bhagtani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Conditional Statements

Conditional Execution
• To write useful programs we need the ability to
check conditions and change the behaviour of the
program accordingly.
• Different conditional statements in python are:
– IF
– IF ---Else (Alternative Execution)
– IF--- ELIF---- ELSE (Chained Conditionals)
– Nested Conditionals
If Condition
• If x > 0:
print "x is positive“
• The Boolean expression after the if statement is called
the condition. If it is true, then the indented statement
gets executed. If not, nothing happens.
• Structure of If
– HEADER:
FIRST STATEMENT
...
LAST STATEMENT
If Condition

• There is no limit on the number of statements that


can appear in the body of an if statement, but there
has to be at least one.
• Occasionally, it is useful to have a body with no
statements (usually as a place keeper for code you
haven't written yet). In that case, you can use the
pass statement, which does nothing.
Alternative Execution
• A second form of the if statement is alternative
execution, in which there are two possibilities and
the condition determines which one gets executed.
• Eg:
if x%2 == 0:
print x, "is even"
else:
print x, "is odd“
• The alternatives are called branches.
Continue….
• Since the condition must be true or false,
exactly one of the alternatives will be
executed. The alternatives are called
branches, because they are branches in the
flow of execution.
Chained Conditionals
• Sometimes there are more than two possibilities
and we need more than two branches.
if x < y:
print x, "is less than", y
elif x > y:
print x, "is greater than", y
else:
print x, "and", y, "are equal“
NOTE: There is no limit of the number of elif
statements, but the last branch has to be an else
statement
Nested conditionals
• One conditional can also be nested within
another.
if x == y:
print x, "and", y, "are equal"
else:
if x < y:
print x, "is less than", y
else:
print x, "is greater than", y
if 0 < x and x < 10:
print "x is a positive single digit.“

• Python provides an alternative syntax that is


similar to mathematical notation:
if 0 < x < 10:
print "x is a positive single digit."
Shortcuts for Conditions
• Numeric value 0 is treated as False
• Empty sequence "", [] is treated as False
• Everything else is True
if m%n:
(m,n) = (n,m%n)
else:
gcd = n
Avoid Nested If
• For example, We can rewrite the following
code using a single conditional:
if 0 < x:
if x < 10:
print "x is a positive single
digit.“
Better way:
if 0 < x and x < 10:
print "x is a positive single digit."
A leap year is a calendar year containing an additional day added
to keep the calendar year synchronized with the astronomical or
seasonal year. In the Gregorian calendar, each leap year has 366
days instead of 365, by extending February to 29 days rather
than the common 28. These extra days occur in years which are
multiples of four (with the exception of centennial years not
divisible by 400). Write a Python program, which asks for a year
and calculates, if this year is a leap year or not.
We will use the modulo operator in the following solution. 'n % m' returns the
remainder, if you divide (integer division) n by m. '5 % 3' returns 2 for
example.
Body mass index (BMI) is a value derived from the mass (weight) and height of a
person. The BMI is defined as the body mass divided by the square of the body height,
and is universally expressed in units of kg/m2, resulting from mass in kilograms and
height in metres. BMI=ml2 Write a program, which asks for the length and the weight
of a person and returns an evaluation string according to the following table:

You might also like