HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to Python: Print and RepeatVariablesData Types & NumbersStringsListsConditions (if / elif / else)Loops (for / while)FunctionsOperatorsDictionariesTuples & SetsError Handling (try / except)Classes (Object-Oriented)File OperationsComments & IndentationBooleans (True / False)Lambda & map / filterModules (import)InheritanceVariable Scope
REFERENCE
📖 Overview
🖥️ Output & Input
🔢 Numbers & Math
✍️ String
📋 List
🗂️ Dictionary & Set
🔁 Condition & Loop
if / elif / elseforrange()enumerate()zip()while
🧩 Function
📁 File & Error
🔄 Type & Conversion
Python›REFERENCE›Condition & Loop›if / elif / else

if / elif / else

Runs different code based on a condition.

Syntax

if condition:
    ...
elif condition:
    ...
else:
    ...

Description

If the if condition is true, the indented block below it runs. elif specifies other conditions, else specifies the block to run if none match. Indentation (whitespace) is required.

Example

Loading editor…

Expected output

Passed

When to use?

Input validation, grade calculation, branching the flow.

Common mistakes

  • ⚠️ A colon (:) after the condition and indentation on the next line are required.
  • ⚠️ Use == for equality, not =.

Related commands

forwhile
for →
HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code