Computer Science, asked by nandanirani04, 2 days ago

write a program in python to show the use of if elif. conditional statement​

Answers

Answered by kalsisimarjotkaur
5

By default, statements in the script are executed sequentially from the first to the last. If the processing logic requires so, the sequential flow can be altered in two ways:

Python uses the if keyword to implement decision control. Python's syntax for executing a block conditionally is as belowif [boolean expression]:

statement1

statement2

...

statementNAny Boolean expression evaluating to True or False appears after the if keyword. Use the : symbol and press Enter after the expression to start a block with an increased indent. One or more statements written with the same level of indent will be executed if the Boolean expression evaluates to True.

To end the block, decrease the indentation. Subsequent statements after the block will be executed out of the if condition. The following example demonstrates the if condition.

Example: if Condition

price = 50

if price < 100:

print("price is less than 100")

Output

price is less than 100

In the above example, the expression price < 100 evaluates to True, so it will execute the block. The if block starts from the new line after : and all the statements under the if condition starts with an increased indentation, either space or tab. Above, the if block contains only one statement. The following example has multiple statements in the if condition.

Example: Multiple Statements in the if Block

price = 50

quantity = 5

if price*quantity < 500:

print("price*quantity is less than 500")

print("price = ", price)

print("quantity = ", quantity)

Output

price*quantity is less than 500

price = 50

quantity = 5

Above, the if condition contains multiple statements with the same indentation. If all the statements are not in the same indentation, either space or a tab then it will raise an IdentationError.

Example: Invalid Indentation in the Block

price = 50

quantity = 5

if price*quantity < 500:

print("price is less than 500")

print("price = ", price)

print("quantity = ", quantity)

Output

print("quantity = ", quantity)

^

IdentationError: unexpected indent

The statements with the same indentation level as if condition will not consider in the if block. They will consider out of the if condition.

Example: Out of Block Statements

price = 50

quantity = 5

if price*quantity < 100:

print("price is less than 500")

print("price = ", price)

print("quantity = ", quantity)

print("No if block executed.")

Output

No if block executed.

The following example demonstrates multiple if conditions.

Example: Multiple if Conditions

price = 100

if price > 100:

print("price is greater than 100")

if price == 100:

print("price is 100")

if price < 100:

print("price is less than 100")

Output

price is 100

Notice that each if block contains a statement in a different indentation, and that's valid because they are different from each other.

Answered by AkshDave
0

Answer:

Mark me as brainlist

Explanation:

write a program in python to show the use of if elif. conditional statement

Similar questions