Python If-else statements

Python If-else statements

Python If-else statements

Decision making is the most important aspect of almost all programming languages. As the name suggests, decision making allows us to run a particular block of code for a particular decision. Here, decisions are made on the validity of special circumstances. Condition checking is the backbone of decision making.



In Python, decision making is done by the following statements.



StatementDescription
If StatementIf the statement is used to test a specific situation. If the condition is true(satisfy), a block of code (if-block) will be executed.
If - else StatementThe if-statement is the same as the if statement except for the fact that it also provides a block of code to check the false state of the condition. If the condition provided in the statement is false, another statement will be executed.
Nested if StatementNested if statement enables us to use if? The statement inside an external statement.

Perhaps the most well-known statement type is the if statement. For example:

>>> x = int(input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
...     x = 0
...     print('Negative changed to zero')
... elif x == 0:
...     print('Zero')
... elif x == 1:
...     print('Single')
... else:
...     print('More')
...
More


There can be zero or more elif parts, and the else part is optional. The keyword ‘elif’ is short for ‘else if’, and is useful to avoid excessive indentation. An ifelifelif … the sequence is a substitute for the switch or case statements found in other languages.
















Python If-else statements Python If-else statements Reviewed by Sk Jha on October 24, 2019 Rating: 5

No comments:

Powered by Blogger.