HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>In this lesson, we will learn the rules for converting an argument and how to work with compound expressions and double negation.</p>
1 <p>In this lesson, we will learn the rules for converting an argument and how to work with compound expressions and double negation.</p>
2 <h2>Rules of conversion</h2>
2 <h2>Rules of conversion</h2>
3 <p>Take a look at this example:</p>
3 <p>Take a look at this example:</p>
4 <p>The OR operator interrupts its execution from left to right and returns the result of the first argument, which we can convert to True. When we have no such arguments, the program returns the last one on the right.</p>
4 <p>The OR operator interrupts its execution from left to right and returns the result of the first argument, which we can convert to True. When we have no such arguments, the program returns the last one on the right.</p>
5 <p>Here we see an example with the AND operator:</p>
5 <p>Here we see an example with the AND operator:</p>
6 <p>The AND operator interrupts execution from left to right and returns the result of the first argument, which it can convert to `False'. When we have no such argument, the program returns the last one on the right.</p>
6 <p>The AND operator interrupts execution from left to right and returns the result of the first argument, which it can convert to `False'. When we have no such argument, the program returns the last one on the right.</p>
7 <p>There are two conversion rules in Python:</p>
7 <p>There are two conversion rules in Python:</p>
8 <ul><li>We should convert to False the 0, 0.0, '', and None. Those values are considered<strong>falsy</strong>. It includes other types of data that we will study in Hexlet</li>
8 <ul><li>We should convert to False the 0, 0.0, '', and None. Those values are considered<strong>falsy</strong>. It includes other types of data that we will study in Hexlet</li>
9 <li>We should convert to True everything else</li>
9 <li>We should convert to True everything else</li>
10 </ul><p>These rules are used in development, for example, to define a default value:</p>
10 </ul><p>These rules are used in development, for example, to define a default value:</p>
11 <p>When name takes one of the false values, we assign an empty string to the value variable. In this case, we can treat value as a string in the following code.</p>
11 <p>When name takes one of the false values, we assign an empty string to the value variable. In this case, we can treat value as a string in the following code.</p>
12 <p>However, there is a potential bug. Suppose name contains:</p>
12 <p>However, there is a potential bug. Suppose name contains:</p>
13 <ul><li>A falsy value</li>
13 <ul><li>A falsy value</li>
14 <li>The variable value to which we can assign values like 0, false, or none</li>
14 <li>The variable value to which we can assign values like 0, false, or none</li>
15 </ul><p>In this case, the above code will not work correctly:</p>
15 </ul><p>In this case, the above code will not work correctly:</p>
16 <h2>Compound expressions</h2>
16 <h2>Compound expressions</h2>
17 <p>If you combine logical expressions, you can get some pretty interesting ways of solving problems with code.</p>
17 <p>If you combine logical expressions, you can get some pretty interesting ways of solving problems with code.</p>
18 <p>Suppose we need to implement some code with a variable that gets:</p>
18 <p>Suppose we need to implement some code with a variable that gets:</p>
19 <ul><li>The yes string if the number is even</li>
19 <ul><li>The yes string if the number is even</li>
20 <li>The no string if it is odd</li>
20 <li>The no string if it is odd</li>
21 </ul><p>We can do this by using the knowledge gained above:</p>
21 </ul><p>We can do this by using the knowledge gained above:</p>
22 <p>These expressions work by order and priority. The assignment has the lowest priority, so it comes last. The comparison operator == has a higher priority than the logical operators and and or, so it comes earlier.</p>
22 <p>These expressions work by order and priority. The assignment has the lowest priority, so it comes last. The comparison operator == has a higher priority than the logical operators and and or, so it comes earlier.</p>
23 <p>Next, the code is executed from left to right, since the priority of and is higher than that of or. Let's look at this step by step:</p>
23 <p>Next, the code is executed from left to right, since the priority of and is higher than that of or. Let's look at this step by step:</p>
24 <p>Also, we can use it with any expression at the beginning:</p>
24 <p>Also, we can use it with any expression at the beginning:</p>
25 <h2>Double negations</h2>
25 <h2>Double negations</h2>
26 <p>Let us remember what the negation operation looks like:</p>
26 <p>Let us remember what the negation operation looks like:</p>
27 <p>With double negation, the final value is equal to the initial value:</p>
27 <p>With double negation, the final value is equal to the initial value:</p>
28 <p>The not operator always returns a Boolean value, regardless of the type of argument passed. It does not replace the value with its opposite. Therefore, double negation will also return a boolean True or False:</p>
28 <p>The not operator always returns a Boolean value, regardless of the type of argument passed. It does not replace the value with its opposite. Therefore, double negation will also return a boolean True or False:</p>
29 <h2>Selection errors</h2>
29 <h2>Selection errors</h2>
30 <p>Suppose we need to check whether a value equals one or another. For example, the variable value must contain one of two values: first or second. Beginners sometimes write this expression this way:</p>
30 <p>Suppose we need to check whether a value equals one or another. For example, the variable value must contain one of two values: first or second. Beginners sometimes write this expression this way:</p>
31 <p>However, this kind of code will lead to the wrong result. We must remember the priority of operations. The first thing to evaluate is everything in parentheses like this:</p>
31 <p>However, this kind of code will lead to the wrong result. We must remember the priority of operations. The first thing to evaluate is everything in parentheses like this:</p>
32 <p>Now we will substitute the original expression with the partly evaluated one:</p>
32 <p>Now we will substitute the original expression with the partly evaluated one:</p>
33 <p>It is not what we expected. Now we go back to the beginning and write the check correctly:</p>
33 <p>It is not what we expected. Now we go back to the beginning and write the check correctly:</p>
34  
34