Wednesday, 12 November 2025

Python Tutorial: Conditional Statements

Python Tutorial: Conditional Statements

Python is a smart language , compare to other programing language python code is so smart that it makes a compact and meaningful understanding of code with a good coading structure. The syntax of python is easy for both beginner and expert level programmer. Python does not extra opening and closing braket ({}) , the number of line reduce .In this chapter we will discuss about conditional statement in Python . Conditional statement means if,else,elseif. First you will know when if is required. If we have a condition and if the condition satisfy the come code segment will be executed.


1. The if Statement
Run code only if a condition is True . If condition is on executed only the statement is true , here the age is over 18  then they statement will be executed.


age = 18
if age >= 18:
    print("You are an adult!")

Output
You are an adult!


2. The else Statement
In case of else , else statement is executed only if the "if" statement fails or return false.

age = 16
if age >= 18:
    print("You are an adult!")
else:
    print("You are a minor.")

Output

You are a minor.

3. The else if Statement (else if)

"elseif" only executed when the if statement does not and the next condition satisfy "elseif" .  "elseif" does not execute automatically , if statement and previous all condition fail and "elseif" condition satisfy ,  then only "elseif" statement executed. 

score = 85

if score >= 90:
    print("Grade: A")
elif score >= 80:
    print("Grade: B")
elif score >= 70:
    print("Grade: C")
elif score >= 60:
    print("Grade: D")
else:
    print("Grade: F")

Output

Grade: B


4. Nested if Statements

In python , if statement can contain another if statement , that is called nexted if , here is the example of nexted if statement.


age = 20
citizen = True
if age >= 18:
    if citizen:
        print("You can vote!")
    else:
        print("You are old enough but not a citizen.")
else:
    print("You are too young to vote.")


Output:
You can vote!

5. Comparison Operators


Operator   Meaning
==               Equal to
!=                Not equal to
>                 Greater than
<                 Less than
>=              Greater than or equal
<=              Less than or equal

6. Logical Operators

Operator    Meaning                  Example
and            Both true                 x > 5 and x < 15
or              At least one true     x < 5 or x > 15
not            Reverse truth          not(x == y)


Example of Comparison Operators

x = 10   y = 5

if x > y:

    print("x is greater")


Logical Operators

temperature = 25

is_sunny = True

if temperature > 20 and is_sunny:

    print("Perfect day for a picnic!")


Prepared By :Ayan Banerjee




No comments:

Post a Comment

Python Tutorial: Conditional Statements

Python Tutorial: Conditional Statements Python is a smart language , compare to other programing language python code is so smart that it ma...