Ternary

2 snippets across 2 stacks — JavaScript, Python

JSJavaScript

Ternary Operator

JS · Control Flow
syntax
condition ? valueIfTrue : valueIfFalse
example
const age = 20;
const category = age >= 18 ? "adult" : "minor";
console.log(category); // "adult"

// Nested (use sparingly)
const tier = age >= 65 ? "senior" : age >= 18 ? "adult" : "minor";

Note Great for simple inline conditions. Avoid deeply nested ternaries -- they quickly become unreadable. Use if/else for complex logic.

PYPython

if / elif / else

PY · Control Flow
syntax
if condition:
    ...
elif condition:
    ...
else:
    ...
example
score = 85
if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
else:
    grade = "F"
print(grade)
output
B

Note Python uses indentation instead of braces. There is no switch statement in older Python; use match/case (3.10+) for pattern matching.