Skip to content

[PR2429 Reconstruction] #2443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
316 changes: 158 additions & 158 deletions concepts/conditionals/about.md
Original file line number Diff line number Diff line change
@@ -1,158 +1,158 @@
# About
In Python, [`if`][if statement], `elif` (_a contraction of 'else and if'_) and `else` statements are used in Python to [control the flow][control flow tools] of execution and make decisions in a program.
Unlike many other programming languages, Python versions 3.9 and below do not offer a formal case-switch statement, instead using multiple `elif` statements to serve a similar purpose.
Python 3.10 introduces a variant case-switch statement called `pattern matching`, which will be covered separately in another concept.
Conditional statements use expressions that must resolve to `True` or `False` -- either by returning a `bool` directly, or by evaluating ["truthy" or "falsy"][truth value testing].
```python
x = 10
y = 5
# The comparison '>' returns the bool 'True',
# so the statement is printed.
if x > y:
print("x is greater than y")
...
>>> x is greater than y
```
When paired with `if`, an optional `else` code block will execute when the original `if` condition evaluates to `False`:
```python
x = 5
y = 10
# The comparison '>' here returns the bool False,
# so the 'else' block is executed instead of the 'if' block.
if x > y:
print("x is greater than y")
else:
print("y is greater than x")
...
>>> y is greater than x
```
`elif` allows for multiple evaluations/branches.
```python
x = 5
y = 10
z = 20
# The elif statement allows for the checking of more conditions.
if x > y:
print("x is greater than y and z")
elif y > z:
print("y is greater than x and z")
else:
print("z is great than x and y")
...
>>> z is great than x and y
```
[Boolen operations][boolean operations] and [comparisons][comparisons] can be combined with conditionals for more complex testing:
```python
>>> def classic_fizzbuzz(number):
if number % 3 == 0 and number % 5 == 0:
return 'FizzBuzz!'
elif number % 5 == 0:
return 'Buzz!'
elif number % 3 == 0:
return 'Fizz!'
else:
return str(number)
>>> classic_fizzbuzz(15)
'FizzBuzz!'
>>> classic_fizzbuzz(13)
'13'
```
Conditionals can also be nested.
```python
>>> def driving_status(driver_age, test_score):
if test_score >= 80:
if 18 > driver_age >= 16:
return "Student driver, needs supervision."
elif driver_age == 18:
return "Permitted driver, on probation."
elif driver_age > 18:
return "Fully licensed driver."
else:
return "Unlicensed!"
>>> driving_status(63, 78)
'Unlicsensed!'
>>> driving_status(16, 81)
'Student driver, needs supervision.'
>>> driving_status(23, 80)
'Fully licsensed driver.'
```
## Conditional expressions or "ternary operators"
While Python has no specific `?` ternary operator, it is possible to write single-line `conditional expressions`.
These take the form of `<value if True>` if `<conditional test>` else `<value if False>`.
Since these expressions can become hard to read, it's recommended to use this single-line form only if it shortens code and helps readability.
```python
def just_the_buzz(number):
return 'Buzz!' if number % 5 == 0 else str(number)
>>> just_the_buzz(15)
'Buzz!'
>>> just_the_buzz(10)
'10'
```
## Truthy and Falsy
In Python, any object can be tested for [truth value][truth value testing], and can therefore be used with a conditional, comparison, or boolean operation.
Objects that are evaluated in this fashion are considered "truthy" or "falsy", and used in a `boolean context`.
```python
>>> def truthy_test(thing):
if thing:
print('This is Truthy.')
else:
print("Nope. It's Falsey.")
# Empty container objects are considered Falsey.
>>> truthy_test([])
Nope. It's Falsey.
>>> truthy_test(['bear', 'pig', 'giraffe'])
This is Truthy.
# Empty strings are considered Falsey.
>>> truthy_test('')
Nope. It's Falsey.
>>> truthy_test('yes')
This is Truthy.
# 0 is also considered Falsey.
>>> truthy_test(0)
Nope. It's Falsey.
```
[if statement]: https://docs.python.org/3/reference/compound_stmts.html#the-if-statement
[control flow tools]: https://docs.python.org/3/tutorial/controlflow.html#more-control-flow-tools
[truth value testing]: https://docs.python.org/3/library/stdtypes.html#truth-value-testing
[boolean operations]: https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not
[comparisons]: https://docs.python.org/3/library/stdtypes.html#comparisons
# About

In Python, [`if`][if statement], `elif` (_a contraction of 'else and if'_) and `else` statements are used to [control the flow][control flow tools] of execution and make decisions in a program.
Unlike many other programming languages, Python versions 3.9 and below do not offer a formal case-switch statement, instead using multiple `elif` statements to serve a similar purpose.

Python 3.10 introduces a variant case-switch statement called `pattern matching`, which will be covered separately in another concept.

Conditional statements use expressions that must resolve to `True` or `False` -- either by returning a `bool` directly, or by evaluating ["truthy" or "falsy"][truth value testing].



```python
x = 10
y = 5

# The comparison '>' returns the bool 'True',
# so the statement is printed.
if x > y:
print("x is greater than y")
...
>>> x is greater than y
```

When paired with `if`, an optional `else` code block will execute when the original `if` condition evaluates to `False`:

```python
x = 5
y = 10

# The comparison '>' here returns the bool False,
# so the 'else' block is executed instead of the 'if' block.
if x > y:
print("x is greater than y")
else:
print("y is greater than x")
...
>>> y is greater than x
```

`elif` allows for multiple evaluations/branches.

```python
x = 5
y = 10
z = 20

# The elif statement allows for the checking of more conditions.
if x > y:
print("x is greater than y and z")
elif y > z:
print("y is greater than x and z")
else:
print("z is great than x and y")
...
>>> z is great than x and y
```

[Boolen operations][boolean operations] and [comparisons][comparisons] can be combined with conditionals for more complex testing:

```python

>>> def classic_fizzbuzz(number):
if number % 3 == 0 and number % 5 == 0:
return 'FizzBuzz!'
elif number % 5 == 0:
return 'Buzz!'
elif number % 3 == 0:
return 'Fizz!'
else:
return str(number)

>>> classic_fizzbuzz(15)
'FizzBuzz!'

>>> classic_fizzbuzz(13)
'13'
```

Conditionals can also be nested.

```python
>>> def driving_status(driver_age, test_score):
if test_score >= 80:
if 18 > driver_age >= 16:
return "Student driver, needs supervision."
elif driver_age == 18:
return "Permitted driver, on probation."
elif driver_age > 18:
return "Fully licensed driver."
else:
return "Unlicensed!"


>>> driving_status(63, 78)
'Unlicsensed!'

>>> driving_status(16, 81)
'Student driver, needs supervision.'

>>> driving_status(23, 80)
'Fully licsensed driver.'
```

## Conditional expressions or "ternary operators"

While Python has no specific `?` ternary operator, it is possible to write single-line `conditional expressions`.
These take the form of `<value if True>` if `<conditional test>` else `<value if False>`.
Since these expressions can become hard to read, it's recommended to use this single-line form only if it shortens code and helps readability.


```python
def just_the_buzz(number):
return 'Buzz!' if number % 5 == 0 else str(number)

>>> just_the_buzz(15)
'Buzz!'

>>> just_the_buzz(10)
'10'
```

## Truthy and Falsy

In Python, any object can be tested for [truth value][truth value testing], and can therefore be used with a conditional, comparison, or boolean operation.
Objects that are evaluated in this fashion are considered "truthy" or "falsy", and used in a `boolean context`.

```python
>>> def truthy_test(thing):
if thing:
print('This is Truthy.')
else:
print("Nope. It's Falsey.")


# Empty container objects are considered Falsey.
>>> truthy_test([])
Nope. It's Falsey.

>>> truthy_test(['bear', 'pig', 'giraffe'])
This is Truthy.

# Empty strings are considered Falsey.
>>> truthy_test('')
Nope. It's Falsey.

>>> truthy_test('yes')
This is Truthy.

# 0 is also considered Falsey.
>>> truthy_test(0)
Nope. It's Falsey.
```

[if statement]: https://docs.python.org/3/reference/compound_stmts.html#the-if-statement
[control flow tools]: https://docs.python.org/3/tutorial/controlflow.html#more-control-flow-tools
[truth value testing]: https://docs.python.org/3/library/stdtypes.html#truth-value-testing
[boolean operations]: https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not
[comparisons]: https://docs.python.org/3/library/stdtypes.html#comparisons
2 changes: 1 addition & 1 deletion concepts/conditionals/introduction.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Introduction

In Python, [`if`][if statement], `elif` (_a contraction of 'else and if'_) and `else` statements are used in Python to [control the flow][control flow tools] of execution and make decisions in a program.
In Python, [`if`][if statement], `elif` (_a contraction of 'else and if'_) and `else` statements are used to [control the flow][control flow tools] of execution and make decisions in a program.
Unlike many other programming languages, Python versions 3.9 and below do not offer a formal case-switch statement, instead using multiple `elif` statements to serve a similar purpose.

Python 3.10 introduces a variant case-switch statement called `pattern matching`, which will be covered separately in another concept.
Expand Down
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
"prerequisites": ["basics"],
"status": "wip"
},
{
"slug": "meltdown-mitigation",
"name": "Meltdown Mitigation",
"uuid": "50c88b36-e2e2-46e5-b6c4-bb7e714c375a",
"concepts": ["conditionals"],
"prerequisites": ["basics", "booleans", "comparisons"],
"status": "wip"
},
{
"slug": "tisbury-treasure-hunt",
"name": "Tisbury Treasure Hunt",
Expand Down
48 changes: 48 additions & 0 deletions exercises/concept/meltdown-mitigation/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Hints

## General

- The Python Docs on [Control Flow Tools][control flow tools] and the Real Python tutorial on [conditionals][real python conditionals] are great places to start.
- The Python Docs on [Boolean Operations][boolean operations] can be a great refresher on `bools`, as can the Real Python tutorial on [booleans][python booleans].
- The Python Docs on [Comparisons][comparisons] and [comparisons examples][python comparisons examples] can be a great refresher for comparisons.

## 1. Check for criticality

- Comparison operators and boolean operations can be combined and used with conditionals.
- Conditional expressions must evaluate to `True` or `False`.
- `else` can be used for a code block that will execute when a conditional test returns `False`.

```python
>>> item = 'blue'
>>> item_2 = 'green'

>>> if len(item) >=3 and len(item_2) < 5:
print('Both pass the test!')
elif len(item) >=3 or len(item_2) < 5:
print('One passes the test!')
else:
print('None pass the test!')
...
One passes the test!
```

## 2. Determine the Power output range

- Comparison operators can be combined and used with conditionals.
- Any number of `elif` statements can be used as "branches".
- Each "branch" can have a separate `return`

## 3. Fail Safe Mechanism

- Comparison operators can be combined and used with conditionals.
- Any number of `elif` statements can be used as "branches".
- Each "branch" can have a separate `return`


[python comparisons examples]: https://www.tutorialspoint.com/python/comparison_operators_example.htm
[boolean operations]: https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not
[comparisons]: https://docs.python.org/3/library/stdtypes.html#comparisons
[python booleans]: https://realpython.com/python-boolean/
[real python conditionals]: https://realpython.com/python-conditional-statements/
[control flow tools]: https://docs.python.org/3/tutorial/controlflow.html

Loading