HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>We already know how to write functions that check individual conditions. We will learn how to build compound conditions n this lesson.</p>
1 <p>We already know how to write functions that check individual conditions. We will learn how to build compound conditions n this lesson.</p>
2 <p>Suppose a site needs a password to be longer than eight characters and shorter than twenty characters when registering. You see 8 &lt; x &lt; 20 in mathematics, but you cannot do that in many programming languages.</p>
2 <p>Suppose a site needs a password to be longer than eight characters and shorter than twenty characters when registering. You see 8 &lt; x &lt; 20 in mathematics, but you cannot do that in many programming languages.</p>
3 <p>Let us try to write two separate logical expressions and connect them with the operator AND:</p>
3 <p>Let us try to write two separate logical expressions and connect them with the operator AND:</p>
4 <blockquote><p>The password is longer than 8 characters AND the password is shorter than 20 characters</p>
4 <blockquote><p>The password is longer than 8 characters AND the password is shorter than 20 characters</p>
5 </blockquote><p>Here is a function that takes the password and tells you whether it matches the conditions (True or False):</p>
5 </blockquote><p>Here is a function that takes the password and tells you whether it matches the conditions (True or False):</p>
6 <p>In mathematics, this is called a<strong>conjunction</strong>. It has<strong>operands</strong>- compound expressions.</p>
6 <p>In mathematics, this is called a<strong>conjunction</strong>. It has<strong>operands</strong>- compound expressions.</p>
7 <p>The whole expression is true if each operand is true. In other words, AND means both.</p>
7 <p>The whole expression is true if each operand is true. In other words, AND means both.</p>
8 <p>The priority of this operator is lower than that of comparison operators. So, the expression length &gt; 8 AND length &lt; 20 works correctly without parentheses.</p>
8 <p>The priority of this operator is lower than that of comparison operators. So, the expression length &gt; 8 AND length &lt; 20 works correctly without parentheses.</p>
9 <p>In addition to AND, we also have OR. It is a disjunction, meaning one or the other or both. The a or b expression is true if one or both operands are true. Otherwise, it is false.</p>
9 <p>In addition to AND, we also have OR. It is a disjunction, meaning one or the other or both. The a or b expression is true if one or both operands are true. Otherwise, it is false.</p>
10 <p>We can combine operators in any number and any order. You can use parentheses to specify the priority when and and or occur in the code.</p>
10 <p>We can combine operators in any number and any order. You can use parentheses to specify the priority when and and or occur in the code.</p>
11 <p>Here is an example of an advanced function that determines if a password is correct:</p>
11 <p>Here is an example of an advanced function that determines if a password is correct:</p>
12 <p>Suppose we want to buy an apartment that meets these conditions: at least 100 square meters on any street OR at least 80 square meters but on Main Street.</p>
12 <p>Suppose we want to buy an apartment that meets these conditions: at least 100 square meters on any street OR at least 80 square meters but on Main Street.</p>
13 <p>We will write a function to check an apartment. It takes two arguments:</p>
13 <p>We will write a function to check an apartment. It takes two arguments:</p>
14 <ul><li>The area as a number</li>
14 <ul><li>The area as a number</li>
15 <li>The street name as a string</li>
15 <li>The street name as a string</li>
16 </ul><p>Here is the code:</p>
16 </ul><p>Here is the code:</p>
17 <p>The area of mathematics that deals with logical operators is<strong>Boolean algebra</strong>.</p>
17 <p>The area of mathematics that deals with logical operators is<strong>Boolean algebra</strong>.</p>
18 <p>Below, you will see truth tables. You can use them to determine what result you get if you apply the operator.</p>
18 <p>Below, you will see truth tables. You can use them to determine what result you get if you apply the operator.</p>
19 <h2>The AND operator</h2>
19 <h2>The AND operator</h2>
20 <p>Here is the truth table:</p>
20 <p>Here is the truth table:</p>
21 <h2>The OR operator</h2>
21 <h2>The OR operator</h2>
22 <p>Here is the truth table:</p>
22 <p>Here is the truth table:</p>
23 <h2>The negation</h2>
23 <h2>The negation</h2>
24 <p>Along with the logical operators AND and OR, there is also an operation called<strong>negation</strong>. It changes the logical meaning to the opposite. In programming, negation corresponds to the unary operator not:</p>
24 <p>Along with the logical operators AND and OR, there is also an operation called<strong>negation</strong>. It changes the logical meaning to the opposite. In programming, negation corresponds to the unary operator not:</p>
25 <p>For example, if there is a function that checks if a number is even, then you can use negation to check if a number is odd:</p>
25 <p>For example, if there is a function that checks if a number is even, then you can use negation to check if a number is odd:</p>
26 <p>In the example above, we added not to the left of the function call and got the opposite action.</p>
26 <p>In the example above, we added not to the left of the function call and got the opposite action.</p>
27 <p>Negation is a tool with which you can express intended rules in code without writing new functions.</p>
27 <p>Negation is a tool with which you can express intended rules in code without writing new functions.</p>
28 <p>If you write not not is_even(10), the code will still work:</p>
28 <p>If you write not not is_even(10), the code will still work:</p>
29 <p>In logic, double negation means positive:</p>
29 <p>In logic, double negation means positive:</p>
30 <p>Now you know how to work with AND, OR, and NOT operators. They allow you to specify compound conditions with two or more logical expressions.</p>
30 <p>Now you know how to work with AND, OR, and NOT operators. They allow you to specify compound conditions with two or more logical expressions.</p>