Python Basics

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

Python… Conditional

Statements
Outline

 Nested Conditionals
 Multi-way Decision Statements
 else-if statement vs if-ifStatement.
 Conditional Expressions
 Errors in Conditional Statements
Flow chart vs. Python code

 input ()

 print()

 Expressions / statements

 if else

3
Conditional processing
 Python has several types of selection statements:
 One-way if statements,
 Two-way ifelse statements,
 Nested if statements,
 Multi-way if-elif-else statements and
 Conditional expressions.

4
A one-way if statement
if Statements executes the statements if
the condition is true.

 A one-way if statement executes an action if and only if the


condition is true.
 The syntax for a one-way if statement is:
if boolean-expression:
statement(s)
# Note that the statement(s) must be indented .
The statement(s) must be indented at least one space to the
right of the if keyword and each statement must be indented
using the same number of spaces.

5
If-Statement Example
Two-Way if-else Statements
 A one-way if statement takes an action if the specified condition is True.
 If the condition is False, nothing is done.
 But what if you want to take one or more alternative actions when the condition is
False?
 You can use a two-way if-else statement.
 The actions that a two-way if-else statement specifies differ based on
whether the condition is True or False.
 Here is the syntax for a two-way if-else statement:
if boolean-expression:
statement(s)-for-the-true-case
else: A two-way if-else statement
decides which statements to
statement(s)-for-the-false-case execute based on whether the
condition is true or false.

7
If-else Statement
Nested if and Multi-Way if-elif-else
Statements

 One if statement can be placed inside another if statement to


form a nested if statement.
 The statement in an if or if-else statement can be any legal
Python statement, including another if or if-else statement.
 The inner if statement is said to be nested inside the outer if
statement.
 The inner if statement can contain another if statement; in
fact, there is no limit to the depth of the nesting.

9
Nested if and Multi-Way if-elif-else
Statements

 For example, the following is a nested if statement:


if i > k:
if j > k:
print("i and j are greater than k")
else:
print("i is less than or equal to k")

 The if j > k statement is nested inside the if i > k statement.

10
Nested if and Multi-Way if-elif-else
Statements

Nested if-elif-else Multi-way if-elif-else


statement statement

11
Multiway-if-elif-else: Example
Multiway-if-elif-else: Example
Nested if and Multi-Way if-elif-else
Example
Conditional Expressions
 Conditional expressions are in a completely different style.
The syntax is:
expression1 if boolean-expression else expression2
Example: y = 1 if x > 0 else -1
▪ Same as
if x > 0:
y=1 A conditional
else: expression evaluates an
y = -1
expression based on a
condition.

15
Common Errors
Common Errors

Which is
correct and
equivalent?

Which
is
better?
Practice Example

 You are to write a program to compute personal


income tax. Your program should prompt the user
to enter the filing status and taxable income and
then compute the tax. Enter 0 for single filers, 1 for
married filing jointly, 2 for married filing separately,
and 3 for head of household.
18
Summary

 Nested Conditionals
 Multi-way Decision Statements
 else-if statement vs if-ifStatement.
 Conditional Expressions
 Errors in Conditional Statements

You might also like