KEMBAR78
Conditional Flow in Python | PDF | Grammar | Computer Programming
0% found this document useful (0 votes)
8 views6 pages

Conditional Flow in Python

Notes

Uploaded by

gokuhello859
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Conditional Flow in Python

Notes

Uploaded by

gokuhello859
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1

Conditional Flow In Python

>> >> 1. if Statement


>> x = 10
>> if x > 5:
>> print("x is greater than 5")

>> >> 2. if-else Statement


>> age = 16
>> if age >= 18:
>> print("You can vote")
>> else:
>> print("You cannot vote")

>> >> 3. if-elif-else Statement


>> marks = 85
>> if marks >= 90:
>> print("Grade: A+")
>> elif marks >= 75:
>> print("Grade: A")
>> elif marks >= 60:
2

>> print("Grade: B")


>> else:
>> print("Grade: C")

>> >> 4. Nested if


>> num = 12
>> if num > 0:
>> if num % 2 == 0:
>> print("Positive Even Number")
>> else:
>> print("Positive Odd Number")
>> else:
>> print("Number is Negative")

>> >> 5. Short-hand if


>> x = 20
>> if x > 10: print("x is greater than 10")

>> >> 6. Short-hand if-else (Ternary Operator)


>> a, b = 10, 20
>> print("a is greater") if a > b else print("b is greater")
3

x = 10
if x>5:
print("x is greater than 10")

>> input method using if else condition


age = int(input("enter your age: "))
if age > 18:
print("you can vote")
else:
print("you are under age")

>> if else elif

Num = int(input("Enter your number: "))


if Num > 0 :
print("number is positive")
elif Num < 0:
print("number is negative")
else:
print("number is zero")

age = int(input("Enter your age: "))


if age > 18:
4

nationality = input("your nationality: ")


if nationality.lower() == "indian":
print("You can vote")
else:
print("you must be an Indian to vote ")
else:
print("you are not eligible to vote")

>> age category

age = int (input("enter your age: "))


if age < 13:
print("you are a child.")
elif age< 20:
print (" you are a Teen ager.")
elif age < 60:
print("you are an Adult")
else:
print("you are a senior citizen:")

age = int (input("enter your age: "))


if age < 13:
print("you are a child.")
elif age< 20:
5

print (" you are a Teen ager.")


elif age < 60:
print("you are an Adult")
else:
print("you are a senior citizen:")

num1 = float(input("Enter first number: "))


num2 = float(input( "Enter second number: "))
operation = input("choose operation(+,-,*,/): ")
if operation == "+":
print("result:", num1 + num2)
elif operation == "-":
print("result:", num1 - num2)
elif operation == "*":
print("result:", num1 * num2)
elif operation == "/":
if num2 != 0:
print("result:", num1/num2)
else:
print("error: division by zero!")
else:
print("Invalid operation!")

>> weather suggestion


6

weather = input("enter the weather (sunny, rainy, cold):


").lower()
if weather == "sunny":
print("Wear sunglasses")
elif weather == "rainy":
print("take an umbrella")
elif weather == "cold":
print("wear a jacket")
else:
print("I don't know that weather type.")

>> traffic light

light = input("Enter traffic light color (red, yellow green): ").


lower()
if light == "red":
print("stop")
elif light == "yellow":
print (“Get Ready")
elif light == "green":
print("go")
else:
print ("invalid light color!")

You might also like