<jupyter_start><jupyter_text>Variable Types - Boolean In the last lesson we learnt how to index and slice strings. We also learnt how to use some common string functions such as the len() function.In this lesson, we'll learn about Boolean values. Boolean values evaluate to either True or False and are commonly used for comparisons of equality and inequality, as well as controlling the flow of a program.  Boolean Values Boolean values evaluate to True or False. You can see how Jupyter highlights these keywords:<jupyter_code>boolTrue = True
boolFalse = False<jupyter_output><empty_output><jupyter_text>Integers, floats and strings can be evaluated to a boolean value by using the bool() function.All non-zero numbers evaluate to True whilst 0 evaluates to False:<jupyter_code>bool(0)
bool(0.00000000001)
bool(10)<jupyter_output><empty_output><jupyter_text>All non-empty strings evaluate to True, whilst an empty string evaluates to False:<jupyter_code>bool("hi")
bool(' ')
bool('')<jupyter_output><empty_output><jupyter_text>And, Or and NotYou can also create more complex boolean expressions by using and, or and not to compare multiple expressions.and only returns True if all the boolean expressions evaluate to True:<jupyter_code>print(True and True)
print(True and False)
print(True and True and True and True and False)<jupyter_output>False<jupyter_text>or returns True if at least one expression evalutes to True:<jupyter_code>print(True or False)
print(False or False)
print(False or False or False or False or True)<jupyter_output>True<jupyter_text>not inverts the boolean expression; True becomes False and vice versa:<jupyter_code>print(not True)
print(not False)
print(not(True and True))<jupyter_output>False<jupyter_text>You can use brackets to create complex expressions. What do you think this evaluates to?<jupyter_code>print(not (True or False) and not(False and True))<jupyter_output>False<jupyter_text>Logical comparisonsWe've just seen the basics of boolean values, however comparing True and False isn't particularly useful. The power of boolean values lies in how we can use them to compare values.In Python, we can test for equality by using the == symbol; this test for equality returns a boolean value:<jupyter_code>5 == 5
5 == 10<jupyter_output><empty_output><jupyter_text>We can also test for inequality using !=; this also returns a boolean value:<jupyter_code>1 != 2
1 != 1<jupyter_output><empty_output><jupyter_text>== and != both work on strings:<jupyter_code>'hi' == 'hi'
'hi' != 'ho'<jupyter_output><empty_output><jupyter_text>We can test if a number is greater than another number using >:<jupyter_code>5 > 4
5 > 10<jupyter_output><empty_output><jupyter_text>We can test if a number is greater than or equal to another number by using >=<jupyter_code>5 >= 4
5 >= 5
5 >= 6<jupyter_output><empty_output><jupyter_text>We can also test if a number is less than another number by using &lt;:<jupyter_code>5 < 4
10 < 100<jupyter_output><empty_output><jupyter_text>An we can test if a number is less than or equal to another number by using &lt;=:<jupyter_code>5 <= 7
5 <= 5
5 <= 4<jupyter_output><empty_output><jupyter_text>We can also do these comparisons on strings. Python compares the first two items in the string and if they differ, this determines the outcome of the comparison. If they're the same, the next two items are compared:<jupyter_code>'a' < 'b'
'hi' < 'ho'
'z' < 'x'<jupyter_output><empty_output><jupyter_text>Combining comparisons with And, Or and Not We now know how to create complex comparisons of strings and numbers. For the following examples, try to figure out what the answer will be before you run the cell.<jupyter_code>1 != 3 and 2 == 5
"test" != "testing" and 5 == 5
"one" == 1
not (True and False)
not (4 == 4 and 0 != 10)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08226355776788054, 'fraction_numerical': 0.015981137018600995, 'mean_word_length': 6.176691729323308, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '609602', 'license': 'MIT', 'n_tokens_mistral': 1221, 'n_tokens_neox': 1154, 'n_words': 480, 'path': 'PlotlyandPython/Lessons/(01) Intro to Python/Notebooks/(05) Variable Types - Boolean.ipynb', 'repo_name': 'peternewman22/Python_Courses', 'size': '3817'}