Python if else statement
The if...else statement is the decision statement. it make the decision on basis of given condition for that it uses comparison operators.Comparison Operator
These operators are used to compare values.
Logical Operator
These operators are used to compare multiple conditions.
Operator
|
Description
|
Example
|
and
|
True
if both the
operands
are true
|
x
and y
|
or
|
True
if either of the
operands
is true
|
x
or y
|
not
|
True
if operand is false
|
not
x
|
Python if statement
The if statement is used to test a condition. If the condition is true then a single statement or a block of statements is executed.
![]() |
Flow Chart of 'If' Statement |
Python if statement syntax
if
condition :
statement(s)
|
Note: The format() method
returns the formatted string. The curly brackets {} are used to define
placeholders for the arguments to be placed. In the example bellow, {0} is
placeholder for "num" variable.
|
Example 1 : Check the entered number is positive
Logic
If a number is greater than or equal to zero then it is a positive number – (if the number>=0)
If a number is less than or equal to zero then it is a Negative number – (if the number<=0)
If a number is greater than or equal to zero then it is a positive number – (if the number>=0)
If a number is less than or equal to zero then it is a Negative number – (if the number<=0)
Coding
# input() to accept user input
num = int (input ("Enter a number:"))
#Check the value in num is greater than or equal to 0
if (num>=0) :
print("The {0} is positive number" .format(num))
|
Output
Enter a number: 10
The 10 is positive
number
|
Python if else statement
The if else is a bi-directional conditional control statement. if statement is used to test a condition, if the condition is true then statements of if block will executed, otherwise, the else block will be executed.
![]() |
Flow Chart of 'If..Else' Statement |
Python if else syntax
if
condition :
statement(s)
else
:
statement(s)
|
Example 2: Check if a number is odd or even in python
Logic
When input is divided by 2 and leaves a remainder 0 then it is a even number.
When input is divided by 2 and leaves any remainder other than 0 then it is a odd number.
When input is divided by 2 and leaves a remainder 0 then it is a even number.
When input is divided by 2 and leaves any remainder other than 0 then it is a odd number.
Coding
# input() to accept user input
num = int(input("Enter a number: "))
#Check if the value of num is divisible by 2
if (num % 2) == 0:
print("{0} is Even
number".format(num))
else:
print("{0} is Odd
number".format(num))
|
Output
Enter a number: 9
9 is Odd number
|
Python elif statement
The elif statement allows us to check for multiple conditions. Here each condition is checked, and when a condition is found to be true, the statements corresponding to that condition are executed, and the control comes out of the structure without checking remaining conditions. If none of the conditions is true then the block of last else statement will executed.
Python elif syntax
if condition 1 :
statement(s)
elif condition 2
:
statement(s)
elif condition 3
:
statement(s)
else :
statement(s)
|
Example 3: Check if a number is positive, negative and zero
Logic
If a number is greater than zero then it is a positive number – (if the number>0)
If a number is less than zero then it is a Negative number – (if the number<0)
Otherwise the number is zero.
Coding
Coding
# input() to accept user input
num = int(input("Enter a number: "))
if (num > 0):
print("{0} is a positive
number".format(num))
elif (num < 0):
print("{0} is a negative
number".format(num))
else:
print("{0} is
zero".format(num))
|
Output
Enter a number: -1
-1 is a negative
number
|
Python nested if else statement
You can nest if statements inside another if statements.![]() |
Flow Chart of 'Nested If..Else' Statement |
Python nested if else syntax
if condition 1:
if
condition 2:
statement(s)
else:
statement(s)
else:
statement(s)
|
Example 4: Check a year is leap year in python
Logic
The year should be divisible by 4 and if it is divisible by 100 then also divisible by 400.
Coding
# input() to accept user input
year = int(input("Enter a year: "))
if (year % 4)==0:
if (year % 100)==0:
if(year %
400)==0:
print("{0} is a leap
year".format(year))
else:
print("{0} is not a leap
year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0}
is not a leap year".format(year))
|
Output
Enter a year: 2019
2019 is not a leap
year
Enter a year: 2020
2020 is a leap year
|
Example 5: Check a year is leap year or not using logical operator
No comments:
Post a Comment
please do not enter any spam link in the comment box