pytutorial/python_basics/if/README.md
David Rotermund 95e94c3353
Update README.md
Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
2023-12-06 23:36:11 +01:00

66 lines
1,022 B
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Flow Control if, elif, else
{:.no_toc}
<nav markdown="1" class="toc-class">
* TOC
{:toc}
</nav>
## The goal
If I would have...
Questions to [David Rotermund](mailto:davrot@uni-bremen.de)
**Logic blocks need to be indented. Preferable with 4 spaces!**
## [The if statement](https://docs.python.org/3/reference/compound_stmts.html#the-if-statement)
```python
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
```python
if_stmt ::= "if" assignment_expression ":" suite
("elif" assignment_expression ":" suite)*
["else" ":" suite]
```
## if, elif, else with lists
```python
A = 1
if A in [0, 2, 4, 6, 8]:
print("found")
else:
print("NOT found")
```
Output
```python
NOT found
```
```python
A = 2
if A in [0, 2, 4, 6, 8]:
print("found")
else:
print("NOT found")
```
Output
```python
found
```