HOME
ABOUT

Control Flow: Conditionals

Control Flow: Conditionals

if Statement

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

if-else Statement

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

if-elif-else Statement

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

Nested if Statements

num = 15
if num > 0:
    if num % 2 == 0:
        print("Positive even number")
    else:
        print("Positive odd number")
else:
    print("Number is not positive")

Ternary Operator

age = 20
status = "Adult" if age >= 18 else "Minor"
print(status)

Related Articles

  • Introduction to Python
  • Setting Up Python Environment
  • Python Syntax Basics
  • Variables and Data Types
  • Operators
  • Input and Output
  • Control Flow: Conditionals
  • Control Flow: Loops
  • Functions
  • Lists and Tuples
  • Dictionaries and Sets
  • String Manipulation
  • How to execute SQL queries (Select, Update, Insert, Delete) effectively using Python?