Python If-Else Tutorial

What is If-Else in Python?

In Python, if and else are conditional statements used to perform different actions based on whether a condition is true or false. The if block runs when the condition is true, otherwise the else block runs.

Python में if और else conditional statements हैं जो किसी condition के true या false होने पर अलग-अलग actions perform करते हैं। जब condition true होती है तो if block चलता है, अन्यथा else block चलता है।

Why Use If-Else?

If-Else statements allow your program to make decisions and execute different code depending on dynamic conditions. This helps in controlling program flow and handling various scenarios.

If-Else statements आपके प्रोग्राम को निर्णय लेने और विभिन्न परिस्थितियों के अनुसार अलग कोड चलाने देते हैं। इससे प्रोग्राम का flow नियंत्रित होता है और कई परिस्थितियों को संभाला जा सकता है।

Syntax of If-Else in Python

if condition:
    # code runs if condition is true
else:
    # code runs if condition is false

10 Examples of If-Else Statements

Example 1: Simple If-Else
num = 10
if num > 5:
    print('Number is greater than 5')
else:
    print('Number is 5 or less')

Checks if num is greater than 5 and prints appropriate message.

num 5 से बड़ा है या नहीं जांचता है और सही संदेश दिखाता है।

Output:

Number is greater than 5
Example 2: Check Even or Odd
num = 7
if num % 2 == 0:
    print('Even number')
else:
    print('Odd number')

Checks if a number is even or odd using modulus operator.

मॉडुलस ऑपरेटर से संख्या को even या odd जांचता है।

Output:

Odd number
Example 3: Age Eligibility
age = 18
if age >= 18:
    print('Eligible to vote')
else:
    print('Not eligible to vote')

Checks if age is 18 or more to decide voting eligibility.

मतदान के लिए आयु 18 या अधिक है या नहीं जांचता है।

Output:

Eligible to vote
Example 4: Temperature Check
temp = 30
if temp > 25:
    print('It is hot')
else:
    print('It is cool')

Prints if temperature is hot or cool based on threshold.

तापमान गर्म या ठंडा होने पर संदेश दिखाता है।

Output:

It is hot
Example 5: Password Validation
password = 'abc123'
if len(password) >= 6:
    print('Password accepted')
else:
    print('Password too short')

Checks if password length is at least 6 characters.

पासवर्ड की लंबाई 6 या अधिक है या नहीं जांचता है।

Output:

Password accepted
Example 6: Grade Pass or Fail
marks = 40
if marks >= 35:
    print('Pass')
else:
    print('Fail')

Determines if marks are passing or failing based on cutoff.

मार्क्स पास या फेल तय करता है।

Output:

Pass
Example 7: User Login Check
username = 'admin'
if username == 'admin':
    print('Welcome Admin')
else:
    print('Welcome User')

Checks username and prints different welcome messages.

यूज़रनेम जांच कर अलग संदेश दिखाता है।

Output:

Welcome Admin
Example 8: Number Sign Check
num = -10
if num >= 0:
    print('Positive or Zero')
else:
    print('Negative')

Checks if number is positive, zero, or negative.

संख्या पॉजिटिव, ज़ीरो या निगेटिव है जांचता है।

Output:

Negative
Example 9: Check String Equality
str1 = 'Python'
str2 = 'python'
if str1 == str2:
    print('Strings are equal')
else:
    print('Strings are not equal')

Checks if two strings are exactly equal (case-sensitive).

दो स्ट्रिंग्स समान हैं या नहीं जांचता है।

Output:

Strings are not equal
Example 10: Multiple Conditions Using If-Else
num = 15
if num % 3 == 0 and num % 5 == 0:
    print('Divisible by both 3 and 5')
else:
    print('Not divisible by both')

Checks if number is divisible by both 3 and 5 using logical AND.

संख्या 3 और 5 दोनों से भाग जाने योग्य है या नहीं जांचता है।

Output:

Divisible by both 3 and 5