0% found this document useful (0 votes)
19 views4 pages

4. Conditional Statements & Decision Making

Uploaded by

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

4. Conditional Statements & Decision Making

Uploaded by

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

Conditional Statements/Decision Making

Decision-making is the anticipation of conditions occurring during the execution


of a program and specified actions taken according to the conditions.

Decision structures evaluate multiple expressions, which produce TRUE or FALSE


as the outcome.

You need to determine which action to take and which statements to execute if
the outcome is TRUE or FALSE otherwise.

Python programming language assumes any non-zero and non-null values as


TRUE, and any zero or null values as FALSE value.

Python programming language provides the following types of decision-making


statements.

Statement Description

if statements An if statement consists of a Boolean expression followed


by one or more statements

if...else statements An if statement can be followed by an optional else


statement, which executes when the Boolean expression
is FALSE.
IF Statement:
The IF statement is similar to that of other languages. The if statement contains
a logical expression using which the data is compared and a decision is made
based on the result of the comparison.

Syntax
if expression:
statement(s)

#!/usr/bin/python3
var1 = 100
if var1:
print ("1 - Got a true expression value")
print (var1)

IF...ELIF...ELSE Statements:
An else statement can be combined with an if statement. An else statement
contains a block of code that executes if the conditional expression in the if
statement resolves to 0 or a FALSE value. The else statement is an optional
statement and there could be at the most only one else statement following if. if
expression:
statement(s)
else:
statement(s)

#!/usr/bin/python3
amount=int(input("Enter amount: "))
if amount<1000:
discount=amount*0.05
print ("Discount",discount)
else:
discount=amount*0.10
print ("Discount",discount)
print ("Net payable:",amount-discount)
The elif statement allows you to check multiple expressions for TRUE and
execute a block of code as soon as one of the conditions evaluates to TRUE.

Similar to the else, the elif statement is optional. However, unlike else, for
which there can be at the most one statement, there can be an arbitrary
number of elif statements following an if.

Syntax
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)

amount=int(input("Enter amount: "))


if amount<1000:
discount=amount*0.05
print ("Discount",discount)
elif amount<5000:
discount=amount*0.10
print ("Discount",discount)
else:
discount=amount*0.15
print ("Discount",discount)
print ("Net payable:",amount-discount)

num=int(input("enter number"))
if num%2==0:
if num%3==0:
print ("Divisible by 3 and 2")
else:
print ("divisible by 2 not divisible by 3")
else:
if num%3==0:
print ("divisible by 3 not divisible by 2")
else:
print ("not Divisible by 2 not divisible by 3")

Single Statement Suites


If the suite of an if clause consists only of a single line, it may go on the same line
as the header statement. Here is an example of a one-line if clause-

var = 100
if ( var == 100 ) :
print ("Value of expression is 100")
print ("Good bye!")

The Ternary Operator


The ternary operator is used for inline conditional expressions. It is best used in
simple, concise operations that are easily read

n=5
"Greater than 2" if n > 2 else "Smaller than or equal to 2"
# Out: 'Greater than 2'

Ternary operations can also be nested, as here:


n=5
"Hello" if n > 10 else "Goodbye" if n < 5 else "Good day"

Truth Values
The following values are considered false, in that they evaluate to False when
applied to a boolean operator.
 None
 False
 0, or any numerical value equivalent to zero, for example 0L, 0.0, 0j
 Empty sequences: '', "", (), []
 Empty mappings: {}
 User-defined types where the __bool__ or __len__ methods return 0 or
False

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy