Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/marioaje/Python/llms.txt

Use this file to discover all available pages before exploring further.

Every useful program needs to make decisions. Should this user be granted access? Is one value larger than another? Python’s conditional statements — if, elif, and else — let your code evaluate a condition and take different paths depending on whether that condition is True or False. Mastering conditionals is the first step toward writing programs that respond intelligently to data.

Basic if / else

The simplest form of a conditional checks a single condition. If it is True, the indented block underneath runs; otherwise the else block runs. The example below stores two numbers and compares them:
if.py
# Si condicional, nos permite entregar una respuesta personalizada
variable = 1 > 2
# print(variable)
a = 2000
b = 22

if a < b:
    mensaje = f"""El valor de {b} es mayor que {a} """
    print(mensaje)   # True
else:
    mensaje = f"""El valor de {a} es mayor que {b} """
    print(mensaje)   # False
Because 2000 is not less than 22, Python skips the if block and executes the else block, printing:
El valor de 2000 es mayor que 22

F-strings

Notice the f"""...""" syntax used for mensaje. An f-string (formatted string literal) lets you embed any Python expression directly inside curly braces {} within a string. Python evaluates the expression at runtime and inserts the result into the text — no manual concatenation needed.
F-strings are the most readable way to build strings that include variable values. Prefix any string with f (or F) and place your expressions inside {}:
name = "Mario"
age  = 30
print(f"My name is {name} and I am {age} years old.")
# My name is Mario and I am 30 years old.
You can put any valid Python expression — arithmetic, method calls, comparisons — inside the braces.

if / elif / else Chain

When you have more than two possible outcomes, chain as many elif (“else if”) clauses as you need between the opening if and the final else. Python evaluates each condition from top to bottom and runs the first block whose condition is True; all others are skipped.
if.py
a = 2000
b = 22

if a == b:
    mensaje = f"""El valor de {b} es igual que {a} """
    print(mensaje)   # True
elif a < b:
    mensaje = f"""El valor de {b} es mayor que {a} """
    print(mensaje)   # True
else:
    mensaje = f"""El valor de {a} es mayor que {b} """
    print(mensaje)   # False
Here there are three branches:
ConditionMeaningRuns?
a == b2000 == 22FalseNo
a < b2000 < 22FalseNo
elseEverything else✅ Yes
So the output is:
El valor de 2000 es mayor que 22
Indentation is mandatory in Python. Unlike many other languages that use curly braces {} to group code, Python uses consistent indentation (typically 4 spaces) to define blocks. Every statement inside an if, elif, or else branch must be indented to the same level. Mixing tabs and spaces — or inconsistent spacing — will raise an IndentationError at runtime.

Login Validation Example

A practical use-case for if/else is credential checking. The snippet below from the course demonstrates a simple login guard:
if.py
login    = "mariosss"
password = "mario"

if login == password:
    print("Bienvenido al sistema...")
else:
    print("Usuario o clave incorrecta")
Because "mariosss" and "mario" are not equal, Python prints:
Usuario o clave incorrecta
In a real application you would compare against stored (and hashed) credentials, but the conditional logic is exactly the same — a single == check that routes execution to the appropriate branch.

Build docs developers (and LLMs) love