[go: up one dir, main page]

0% found this document useful (0 votes)
8 views24 pages

Python-3 5

The document provides examples of using the input() statement and explains the if, if...else, if...elif...else, and nested if statements in Python. It describes how these statements evaluate conditions and execute corresponding code blocks based on the truth value of those conditions. Several examples illustrate the practical application of these concepts in programming.

Uploaded by

Vince Bubuli
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)
8 views24 pages

Python-3 5

The document provides examples of using the input() statement and explains the if, if...else, if...elif...else, and nested if statements in Python. It describes how these statements evaluate conditions and execute corresponding code blocks based on the truth value of those conditions. Several examples illustrate the practical application of these concepts in programming.

Uploaded by

Vince Bubuli
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/ 24

PYTHON

Examples of Programs Using the


input() statement
tax_rate = 6.5
item_price = float(input("Please enter the price of your item.\n"))
total_price = item_price*(1+tax_rate/100)
print("Your total cost is $",total_price,".",sep="")
PYTHON
IF...ELSE
STATEMENT
In computer programming, we use
the if statement to run a block code
only when a certain condition is met.
In the Python interactive shell, three
dots indicate the secondary prompt:
The ">>>" sign seen on the Python’s interactive
shell is called a prompt. Python’s interactive shell,
which appears after firing command “python3” (if
we are working on python version 3) on the
terminal, has two such prompts.

">>>" - Primary prompt


"..." - Secondary prompt
In Python, there are three forms
of the if...else statement.
if statement
if...else statement
if...elif...else statement
1. Python if statement

The if statement evaluates condition.

If condition is evaluated to True, the code inside the


body of if is executed.
If condition is evaluated to False, the code inside the
body of if is skipped.
Example:
number = 10

if number > 0:
print('Number is positive.’)

OUTPUT: Number is positive.

since number is greater than 0, the condition evaluates


True.
Example:
number = -5

if number > 0:
print('Number is positive.’)

OUTPUT:
the value of number is less than 0. Hence, the condition
evaluates to False. And, the body of if block is skipped.
2. Python if...else Statement

An if statement can have an optional else clause.


The syntax of if...else statement is:

if condition:
# block of code if condition is True

else:
# block of code if condition is False
The if...else statement evaluates
the given condition:
If the condition evaluates to True,
the code inside if is executed
the code inside else is skipped

If the condition evaluates to False,


the code inside else is executed
the code inside if is skipped
Example 2. Python if...else
Statement
number = 10

if number > 0:
print('Positive number')

else:
print('Negative number')
Example 2. Python if...else
Statement
Since the value of number is 10, the
test condition evaluates to True. Hence
code inside the body of if is executed.

If we change the value of variable to a


negative integer. Let's say -5.
Example 2. Python if...else
Statement
number = -5

if number > 0:
print('Positive number')

else:
print('Negative number')
Example 2. Python if...else
Statement
The test condition evaluates to
False. Hence code inside the body
of else is executed.
3. Python if...elif...else Statement

The if...else statement is used to execute a


block of code among two alternatives.

However, if we need to make a choice between


more than two alternatives, then we use the
if...elif...else statement.

The syntax of the if...elif...else statement is:


3. Python if...elif...else Statement

if condition1:
# code block 1

elif condition2:
# code block 2

else:
# code block 3
■If condition1 evaluates to true, code block 1 is
executed.
■If condition1 evaluates to false, then condition2
is evaluated.
■If condition2 is true, code block 2 is executed.
■If condition2 is false, code block 3 is executed.
Python if...elif...else Statement
Example:
number = 0

if number > 0:
print("Positive number")

elif number == 0:
print('Zero')
else:
print('Negative number')
Example:

we have created a variable named number


with the value 0. Here, we have two
condition expressions:

Here, both the conditions evaluate to False.


Hence the statement inside the body of else
is executed.
4.Python Nested if statements

We can also use an if statement inside


of an if statement. This is known as a
nested if statement.
Python NestedNotes:
if statements
•We can
# outer if statement add else and elif statements to
if condition1: the inner if statement as
# statement(s) required.
•We can also insert
# inner if statementinner if statement inside the
outer else or elif statements(if
if condition2:
they exist)
# statement(s) •
We can nest multiple layers
of if statements.
Example 4: Python Nested if
Statement
number = 5
if (number >= 0):
if number == 0:
print('Number is 0')
else:
print('Number is positive')
else:
print('Number is negative')

You might also like