In this article Conditional Statements in Python we give the information about Conditionals and Boolean Values in Python are used to make decisions in programming. This gives the program the ability to do different things depending on different situations.
Conditional Statements in Python:
Boolean Values (True and False)
There are only two Boolean values in Python:
- True
- False
Example:
print(10 > 5) # True
print(10 < 5) # False
Boolean Values are mainly used in decision making statements (like if, else).
Relational Operators
These operators compare two values and give Boolean output.
Operator meaning example
== is equal to 5 == 5 → True
!= is not equal to 5! = 3 → True
> 10 is bigger > 5 → True
< is smaller than 5 < 10 → True
>= is greater or equal to 7 >= 7 → True
<= is less than or equal to 4 <= 8 → True
Logical Operators
Logical operators are used to combine or examine Boolean expressions.
Operator meaning example
and both the conditions should be True (5 > 3) and (10 > 7) → True
or At least one condition must be True (5 > 10) or (7 > 3) → True
not Reverses the Boolean not (5 > 3) → False
Conditional Statements in Python:
If statement in Python is used to make decisions. This allows the program to perform different actions depending on a certain condition.
if statement
Basic syntax of if
if condition:
code # when condition is true
if 10 > 5:
print(“10 is greater than 5”) # This line will run because the condition is true
Important points:
- If Statement checks a condition.
- If the condition is true, the code below runs.
- If the condition is False, the code below does not run.
Example 1: The Simplest If Statement
age = 18
if age >= 18:
print(“You can vote!”) # This will run because the condition is true
if-else statement
Sometimes the code has to be run even if the condition is false. For this we use else.
Syntax:
if condition:
code # when condition is true
otherwise:
code # when condition is false
marks = 85
if marks >= 90:
print(“A Grade”)
elif marks >= 75:
print(“B Grade”)
else:
print(“C Grade”)
if-elif-else Statement
Sometimes more than one condition has to be checked. For this we use elif (else if).
Syntax:
if condition1:
Code # when condition1 is true
elifCondition2:
Code # When condition 2 is true
otherwise:
Code # When all conditions are false
marks = 85
if marks >= 90:
print(“A Grade”)
elif marks >= 75:
print(“B Grade”)
else:
print(“C Grade”)
Nested if
Another if statement can be written inside one if statement. This is called Nested If.
number = 10
if number > 0:
if num % 2 == 0:
print(“This is a positive and even number.”) # This will run
otherwise:
print(“This is a positive but odd number.”)
otherwise:
print(“The number is negative.”)
Programme:
number = 10
if number > 0:
if num % 2 == 0:
print(“This is a positive and even number.”) # This will run
otherwise:
print(“This is a positive but odd number.”)
otherwise:
print(“The number is negative.”)
Example: Use of Logical and Relational Operators
a = 15
b = 10
c = 20
if (a > b) and (a < c):
print(“a is greater than b and smaller than c”)
otherwise:
print(“Conditions are not valid”)
Ternary Operator (Short if-else)
x = 10
y = 20
result = “x is larger” if x > y else “y is larger”
print(result) # Output: y is big
conclusion
- In Python, Boolean Values and Operators are used for decision-making.
- if, else, and elif are used to run different code based on conditions.
- Relational and logical operators return Boolean values, which are used in conditional statements.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining