Flow Control: if, elif, else: Difference between revisions

From Master of Neuroscience Wiki
Created page with " == The goal == If I would have… Questions to [mailto:davrot@uni-bremen.de David Rotermund] '''Logic blocks need to be indented.​ Preferable with 4 spaces!''' == [https://docs.python.org/3/reference/compound_stmts.html#the-if-statement The if statement] == <syntaxhighlight lang="python">if i == 1:​ print("if")​ elif i == 2:​ print("elif brach A")​ elif i == 3:​ print("elif brach B")​ else:​ print("else -- default")​</syntaxhighlight..."
 
No edit summary
 
Line 1: Line 1:


== The goal ==
If I would have…
If I would have…



Latest revision as of 16:05, 17 October 2025

If I would have…

Questions to David Rotermund

Logic blocks need to be indented.​ Preferable with 4 spaces!

The if statement

if i == 1:
    print("if")
elif i == 2:
    print("elif brach A")
elif i == 3:
    print("elif brach B")
else:
    print("else -- default")

The full statement

if_stmt ::=  "if" assignment_expression ":" suite
             ("elif" assignment_expression ":" suite)*
             ["else" ":" suite]

if, elif, else with lists

A = 1
if A in [0, 2, 4, 6, 8]:
    print("found")
else:
    print("NOT found")

Output

NOT found
A = 2
if A in [0, 2, 4, 6, 8]:
    print("found")
else:
    print("NOT found")

Output

found