Python if else Statement
Python uses the if
, else
, and elif
statements to make decisions in programs. These help in executing code based on conditions.
Python में if
, else
और elif
का उपयोग decision लेने के लिए किया जाता है। यह conditions के अनुसार code चलाता है।
Example 1: if statement
x = 10
if x > 5:
print("x is greater than 5")
Output:
x is greater than 5
Prints message if condition is true.
अगर शर्त सही है तो मैसेज print होता है।
Example 2: if else
num = 3
if num % 2 == 0:
print("Even")
else:
print("Odd")
Output:
Odd
Checks if number is even or odd.
यह चेक करता है कि नंबर even है या odd।
Example 3: elif
marks = 75
if marks >= 90:
print("Grade A")
elif marks >= 60:
print("Grade B")
else:
print("Grade C")
Output:
Grade B
Selects grade based on marks.
marks के अनुसार grade चुनता है।
Example 4: Nested if
a = 10
b = 5
if a > 0:
if b > 0:
print("Both numbers are positive")
Output:
Both numbers are positive
Checks nested condition.
अंदर की शर्त भी चेक करता है।
Example 5: Input with if
age = int(input("Enter your age: "))
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")
Output:
Enter your age: 18 Eligible to vote
Takes input and applies if-else.
User input लेकर if-else लागू करता है।