From 9dc6f6eb83b42a1108f5cb116f655fb90f2225dd Mon Sep 17 00:00:00 2001 From: BethanyG Date: Fri, 14 May 2021 16:29:03 -0700 Subject: [PATCH 1/7] Created about, introduction and links files. --- concepts/conditionals/about.md | 159 +++++++++++++++++++++++++- concepts/conditionals/introduction.md | 83 +++++++++++++- concepts/conditionals/links.json | 20 ++-- 3 files changed, 252 insertions(+), 10 deletions(-) diff --git a/concepts/conditionals/about.md b/concepts/conditionals/about.md index c628150d56..3f0e17b88f 100644 --- a/concepts/conditionals/about.md +++ b/concepts/conditionals/about.md @@ -1,2 +1,159 @@ -#TODO: Add about for this concept. +# About + +The [conditionals][control flow tools] [`if`][if statement], `elif` (_a contraction of 'else and if'_) and `else` are used in Python to control the flow of execution and make decisions in a program. +Unlike many other programming langauges, Python versions 3.9 and below do not offer a formal case-switch statement, 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 in another exercise. +Conditional statements pair with expressions and objects 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 `` if `` else ``. +Since these expressions can become hard to scan, 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 +[conditional statements in python]: https://realpython.com/python-conditional-statements/ +[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 diff --git a/concepts/conditionals/introduction.md b/concepts/conditionals/introduction.md index fcde74642c..edb1544b2f 100644 --- a/concepts/conditionals/introduction.md +++ b/concepts/conditionals/introduction.md @@ -1,2 +1,83 @@ -#TODO: Add introduction for this concept. +# Introduction + +The [conditionals][control flow tools] [`if`][if statement], `elif` (_a contraction of 'else and if'_) and `else` are used in Python to control the flow of execution and make decisions in a program. +Unlike many other programming langauges, Python versions 3.9 and below do not offer a formal case-switch statement, 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 in another exercise. +Conditional statements pair with expressions and objects 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' +``` + +[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 +[conditional statements in python]: https://realpython.com/python-conditional-statements/ +[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 + diff --git a/concepts/conditionals/links.json b/concepts/conditionals/links.json index eb5fb7c38a..c1778b526b 100644 --- a/concepts/conditionals/links.json +++ b/concepts/conditionals/links.json @@ -1,18 +1,22 @@ [ { - "url": "http://example.com/", - "description": "TODO: add new link (above) and write a short description here of the resource." + "url": "https://docs.python.org/3/tutorial/controlflow.html#more-control-flow-tools", + "description": "Python Docs: Control flow tools." }, { - "url": "http://example.com/", - "description": "TODO: add new link (above) and write a short description here of the resource." + "url": "https://docs.python.org/3/library/stdtypes.html#truth-value-testing", + "description": "Python Docs: Truth value testing." }, { - "url": "http://example.com/", - "description": "TODO: add new link (above) and write a short description here of the resource." + "url": "https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not", + "description": "Python Docs: Standard Types - boolean operations." }, { - "url": "http://example.com/", - "description": "TODO: add new link (above) and write a short description here of the resource." + "url": "https://docs.python.org/3/library/stdtypes.html#comparisons", + "description": "Python Docs: Comparisons." + }, + { + "url": "https://realpython.com/python-conditional-statements/", + "description": "Real Python: Conditional statements in Python." } ] From f5439990dc1127105c6e24ee94c84fc28ab695bf Mon Sep 17 00:00:00 2001 From: BethanyG Date: Fri, 14 May 2021 16:40:10 -0700 Subject: [PATCH 2/7] Added authors and blurb info for conditionals concept. --- concepts/conditionals/.meta/config.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/concepts/conditionals/.meta/config.json b/concepts/conditionals/.meta/config.json index 9b9e8da5a9..453b29d643 100644 --- a/concepts/conditionals/.meta/config.json +++ b/concepts/conditionals/.meta/config.json @@ -1,5 +1,5 @@ { - "blurb": "TODO: add blurb for this concept", - "authors": ["bethanyg", "cmccandless"], + "blurb": "The conditionals 'if', 'elif' ('else + if'), and 'else' are used to control the flow of execution and make decisions in a program. Python doesn't have a formal case-switch statement ,and uses multiple 'elif's to serve a similar purpose. Conditionals pair with expressions and objects that must resolve to 'True' or 'False'.", + "authors": ["bethanyg", "sachsom95"], "contributors": [] } From 629f8936ae470babb86ec6f68364ff47904b74d8 Mon Sep 17 00:00:00 2001 From: BethanyG Date: Fri, 14 May 2021 16:29:03 -0700 Subject: [PATCH 3/7] Created about, introduction and links files. --- concepts/conditionals/about.md | 159 +++++++++++++++++++++++++- concepts/conditionals/introduction.md | 83 +++++++++++++- concepts/conditionals/links.json | 20 ++-- 3 files changed, 252 insertions(+), 10 deletions(-) diff --git a/concepts/conditionals/about.md b/concepts/conditionals/about.md index c628150d56..3f0e17b88f 100644 --- a/concepts/conditionals/about.md +++ b/concepts/conditionals/about.md @@ -1,2 +1,159 @@ -#TODO: Add about for this concept. +# About + +The [conditionals][control flow tools] [`if`][if statement], `elif` (_a contraction of 'else and if'_) and `else` are used in Python to control the flow of execution and make decisions in a program. +Unlike many other programming langauges, Python versions 3.9 and below do not offer a formal case-switch statement, 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 in another exercise. +Conditional statements pair with expressions and objects 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 `` if `` else ``. +Since these expressions can become hard to scan, 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 +[conditional statements in python]: https://realpython.com/python-conditional-statements/ +[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 diff --git a/concepts/conditionals/introduction.md b/concepts/conditionals/introduction.md index fcde74642c..edb1544b2f 100644 --- a/concepts/conditionals/introduction.md +++ b/concepts/conditionals/introduction.md @@ -1,2 +1,83 @@ -#TODO: Add introduction for this concept. +# Introduction + +The [conditionals][control flow tools] [`if`][if statement], `elif` (_a contraction of 'else and if'_) and `else` are used in Python to control the flow of execution and make decisions in a program. +Unlike many other programming langauges, Python versions 3.9 and below do not offer a formal case-switch statement, 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 in another exercise. +Conditional statements pair with expressions and objects 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' +``` + +[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 +[conditional statements in python]: https://realpython.com/python-conditional-statements/ +[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 + diff --git a/concepts/conditionals/links.json b/concepts/conditionals/links.json index eb5fb7c38a..c1778b526b 100644 --- a/concepts/conditionals/links.json +++ b/concepts/conditionals/links.json @@ -1,18 +1,22 @@ [ { - "url": "http://example.com/", - "description": "TODO: add new link (above) and write a short description here of the resource." + "url": "https://docs.python.org/3/tutorial/controlflow.html#more-control-flow-tools", + "description": "Python Docs: Control flow tools." }, { - "url": "http://example.com/", - "description": "TODO: add new link (above) and write a short description here of the resource." + "url": "https://docs.python.org/3/library/stdtypes.html#truth-value-testing", + "description": "Python Docs: Truth value testing." }, { - "url": "http://example.com/", - "description": "TODO: add new link (above) and write a short description here of the resource." + "url": "https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not", + "description": "Python Docs: Standard Types - boolean operations." }, { - "url": "http://example.com/", - "description": "TODO: add new link (above) and write a short description here of the resource." + "url": "https://docs.python.org/3/library/stdtypes.html#comparisons", + "description": "Python Docs: Comparisons." + }, + { + "url": "https://realpython.com/python-conditional-statements/", + "description": "Real Python: Conditional statements in Python." } ] From 84ef304df763b4dd68f74f95ffa14bb46565f616 Mon Sep 17 00:00:00 2001 From: BethanyG Date: Fri, 14 May 2021 16:40:10 -0700 Subject: [PATCH 4/7] Added authors and blurb info for conditionals concept. --- concepts/conditionals/.meta/config.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/concepts/conditionals/.meta/config.json b/concepts/conditionals/.meta/config.json index 9b9e8da5a9..453b29d643 100644 --- a/concepts/conditionals/.meta/config.json +++ b/concepts/conditionals/.meta/config.json @@ -1,5 +1,5 @@ { - "blurb": "TODO: add blurb for this concept", - "authors": ["bethanyg", "cmccandless"], + "blurb": "The conditionals 'if', 'elif' ('else + if'), and 'else' are used to control the flow of execution and make decisions in a program. Python doesn't have a formal case-switch statement ,and uses multiple 'elif's to serve a similar purpose. Conditionals pair with expressions and objects that must resolve to 'True' or 'False'.", + "authors": ["bethanyg", "sachsom95"], "contributors": [] } From ef0c1b7f1266859405767f8094dc6f77a4190336 Mon Sep 17 00:00:00 2001 From: BethanyG Date: Sat, 29 May 2021 13:40:30 -0700 Subject: [PATCH 5/7] Apply suggestions from code review Applied suggestions/edits/corrections from code review. Co-authored-by: Tim Austin --- concepts/conditionals/about.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/concepts/conditionals/about.md b/concepts/conditionals/about.md index 3f0e17b88f..5de7efa90b 100644 --- a/concepts/conditionals/about.md +++ b/concepts/conditionals/about.md @@ -1,9 +1,12 @@ # About -The [conditionals][control flow tools] [`if`][if statement], `elif` (_a contraction of 'else and if'_) and `else` are used in Python to control the flow of execution and make decisions in a program. -Unlike many other programming langauges, Python versions 3.9 and below do not offer a formal case-switch statement, 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 in another exercise. -Conditional statements pair with expressions and objects that must resolve to `True` or `False` -- either by returning a `bool` directly, or by evaluating ["truthy" or "falsy"][truth value testing]. +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 langauges, 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 @@ -102,7 +105,8 @@ Conditionals can also be nested. While Python has no specific `?` ternary operator, it is possible to write single-line `conditional expressions`. These take the form of `` if `` else ``. -Since these expressions can become hard to scan, it's recommended to use this single-line form only if it shortens code and helps readability. +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): @@ -156,4 +160,3 @@ Nope. It's Falsey. [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 - From 8f009d9d7fc4ae77dbf7da78e62ec0fcf87bf50f Mon Sep 17 00:00:00 2001 From: BethanyG Date: Sat, 29 May 2021 13:56:05 -0700 Subject: [PATCH 6/7] Apply suggestions from code review Applied suggestions/edits/corrections from code review. --- concepts/conditionals/introduction.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/concepts/conditionals/introduction.md b/concepts/conditionals/introduction.md index edb1544b2f..e32dbf499f 100644 --- a/concepts/conditionals/introduction.md +++ b/concepts/conditionals/introduction.md @@ -1,9 +1,9 @@ # Introduction The [conditionals][control flow tools] [`if`][if statement], `elif` (_a contraction of 'else and if'_) and `else` are used in Python to control the flow of execution and make decisions in a program. -Unlike many other programming langauges, Python versions 3.9 and below do not offer a formal case-switch statement, 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 in another exercise. -Conditional statements pair with expressions and objects that must resolve to `True` or `False` -- either by returning a `bool` directly, or by evaluating ["truthy" or "falsy"][truth value testing]. +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 @@ -80,4 +80,3 @@ else: [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 - From 570e60bc67398395f3944b91bd63760243ad961c Mon Sep 17 00:00:00 2001 From: BethanyG Date: Sat, 29 May 2021 14:37:46 -0700 Subject: [PATCH 7/7] Attempted to remove hidden CLRF chars in code example. May have failed. --- concepts/conditionals/about.md | 314 ++++++++++++++++----------------- 1 file changed, 155 insertions(+), 159 deletions(-) diff --git a/concepts/conditionals/about.md b/concepts/conditionals/about.md index 3f0e17b88f..4bab68f598 100644 --- a/concepts/conditionals/about.md +++ b/concepts/conditionals/about.md @@ -1,159 +1,155 @@ -# About - -The [conditionals][control flow tools] [`if`][if statement], `elif` (_a contraction of 'else and if'_) and `else` are used in Python to control the flow of execution and make decisions in a program. -Unlike many other programming langauges, Python versions 3.9 and below do not offer a formal case-switch statement, 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 in another exercise. -Conditional statements pair with expressions and objects 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 `` if `` else ``. -Since these expressions can become hard to scan, 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 -[conditional statements in python]: https://realpython.com/python-conditional-statements/ -[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 + +The [conditionals][control flow tools] [`if`][if statement], `elif` (_a contraction of 'else and if'_) and `else` are used in Python to control the flow of execution and make decisions in a program. +Unlike many other programming langauges, Python versions 3.9 and below do not offer a formal case-switch statement, 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 in another exercise. +Conditional statements pair with expressions and objects 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 `` if `` else ``. +Since these expressions can become hard to scan, 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 +[conditional statements in python]: https://realpython.com/python-conditional-statements/ +[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