<jupyter_start><jupyter_text>Logical expressions In this lecture- [Outcome](Outcome)- [Computer logic](Computer-logic)- [The keywords "true" and "false"](The-keywords-"true"-and-"false")- [Logical operators and logical expressions](Logical-operators-and-logical-expressions)- [Comparison operators create "true" or "false"](Comparison-operators-create-"true"-or-"false")- [Operator precedence for logical expressions](Operator-precedence-for-logical-expressions) OutcomeAfter this lecture, you will be able to: - Say what computer logic is- Explain the role of ``true`` and ``false`` in Julia- Form logical expressions using ``!``, ``&&`` and ``||``- State the order of preference of Julia's elementary arithmetical and logical operators- Work out exactly how Julia would evalutate an logical expression [Back to the top](In-this-lecture) Computer logicLogic is the study of how to transform true sentences into other true sentences.Famous example 1: Socrates is a man. All men are mortal. Therefore Socrates is mortal.Famous example 2: If it rains, the grass is wet. It is raining. Therefore the grass is wet.This rigid and formal way of working may appear to be inadequate, but computer programs cannot do otherwise.In fact, computer logic is even more limited than the famous examples above: it can only evaluate logical expressions. [Back to the top](In-this-lecture) The keywords "true" and "false"In Julia, there are only two logical values: ``true`` and ``false``. They are Julia language reserved words, which means their meaning is fixed and they can be used for no other purpose.The meaning they have in Julia is pretty much the meaning they have in ordinary human life. [Back to the top](In-this-lecture) Logical operators and logical expressionsJulia has quite a few logical operators (as do computers languages in general). But three of them are more important than the rest: ``!``, ``&&`` and ``||``- ``!`` is the NOT operator- ``&&`` is the AND operator- ``||`` is the OR operatorThe ! operator changes the truth of whatever it is applied to.<jupyter_code>!true         # NOT true is of course false
!false       # likewise, NOT false is true<jupyter_output><empty_output><jupyter_text>The other two operators combine two logical values, let's call them p and q. They could be something like "Socrates is a man" for p and "Socrates is mortal" for q. The ``&&`` operator makes a compound statement that is true if and only if both parts are true.<jupyter_code>true && true       # compound statement: for p AND q to be true, p must be true, q must also be true
false && true      # so if one part of an AND compound is false, the compound as a whole is false
true && false      # it doesn't matter which of the logical values comes first
false && false      # obviously this must be false<jupyter_output><empty_output><jupyter_text>The ``||`` operator needs only one of the parts to be true. Clearly "Black is red OR Julia is a language" is true, but "1 and 1 is seventy OR 10 divided by 2 is zero" is false.In other words, the ``||`` operator makes a compound statement that is false if and only if both parts are false.<jupyter_code>true || true       # compound statement: for p OR q to be true, one of them has to be true
false || true       # so if either part is true, the compound statement is true
true || false       # still true
false || false     # compound with OR: both parts false means the compound is false.<jupyter_output><empty_output><jupyter_text>In real life, we often reason like this: "Either the train is late or the station clock is wrong". We would then hope to eliminate one of them by showing it is false so the other one is what we go by. For this to work, at least one of them must be true.That is, we reason correctly only in the case that they are not both false. But this is exactly the definition given above: the ``||`` operator makes a compound statement that is false if and only if both parts are false. [Back to the top](In-this-lecture) Comparison operators create "true" or "false"The basic comparison operators are ````. (NB! Note particularly the ``==`` operator!).<jupyter_code>1 < 22    # this is obviously true
-10 > 3    # this is obviously false
2 == 4-2    # this is obviously true<jupyter_output><empty_output><jupyter_text>These operators are used all the time in writing programs. You have to learn them very well! [Back to the top](In-this-lecture) Operator precedence for logical expressionsIf you write computer code, you will often have to construct logical expressions very similar to the example of "one plus one is seventy OR 10 divided by 2 is zero". Here is a longer example:``3-4 > 1 && 2 + 2 == 4 || 10 - 5 > 2``    This is an example of what can make things hard in computer programming$^1$. As before, in Julia this expression can mean only one thing.$^1$I said learning to program in Julia is easy, and it is. But any computer language can be used to make easy things hard. Even Julia!<jupyter_code>( 3-4 > 1 ) && ( 2+2 == 4 ) || ( 10-5 > 2 )    # This is how Julia reads the expression above 
#                                           ... and it is how you should write it!! 
#        Arithmetic operators have higher precedence than logical operators; comparisons 
#  Here's the above just in terms of its logical values
false      &&   true       ||  true           # NB: evaluated from left to right<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08088642659279778, 'fraction_numerical': 0.007571560480147738, 'mean_word_length': 3.991705069124424, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '340493', 'license': 'MIT', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1359, 'n_words': 786, 'path': 'Week1_5-LogicalExp.ipynb', 'repo_name': 'chs132/studious-octo-umbrella', 'size': '5415'}