HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>In this lesson, we will see how you can use conditional constructs to change the behavior of a program that we can make to act based on the result of a condition that it checks. It allows you to write complex programs that behave depending on the situation.</p>
1 <p>In this lesson, we will see how you can use conditional constructs to change the behavior of a program that we can make to act based on the result of a condition that it checks. It allows you to write complex programs that behave depending on the situation.</p>
2 <h2>The if construct</h2>
2 <h2>The if construct</h2>
3 <p>Here we start with an example. We consider a function that works with sentences we pass and determines their type.</p>
3 <p>Here we start with an example. We consider a function that works with sentences we pass and determines their type.</p>
4 <p>First, it will distinguish between statements and questions:</p>
4 <p>First, it will distinguish between statements and questions:</p>
5 <p>The if construct controls how we execute instructions. After the word if, we pass a predicate expression, followed by a colon at the end. Next, we pass a block of code. The program will execute it if the predicate is True.</p>
5 <p>The if construct controls how we execute instructions. After the word if, we pass a predicate expression, followed by a colon at the end. Next, we pass a block of code. The program will execute it if the predicate is True.</p>
6 <p>If the predicate is False, the program will skip the code block and execute the function. In our case, the following line of code is return 'normal'. It will make the function return the string and terminate.</p>
6 <p>If the predicate is False, the program will skip the code block and execute the function. In our case, the following line of code is return 'normal'. It will make the function return the string and terminate.</p>
7 <p>The return can be anywhere in a function, even inside a code block with a condition.</p>
7 <p>The return can be anywhere in a function, even inside a code block with a condition.</p>
8 <h2>The else construct</h2>
8 <h2>The else construct</h2>
9 <p>Now we will modify the function from the previous example so that it returns the whole string Sentence is normal or Sentence is question instead of just the sentence type:</p>
9 <p>Now we will modify the function from the previous example so that it returns the whole string Sentence is normal or Sentence is question instead of just the sentence type:</p>
10 <p>We have added a new block as well. It will execute when the if is false. You can also put other if conditions in the else block.</p>
10 <p>We have added a new block as well. It will execute when the if is false. You can also put other if conditions in the else block.</p>
11 <p>We can execute the if-else construct in two ways. Negation allows you to change the order of the blocks:</p>
11 <p>We can execute the if-else construct in two ways. Negation allows you to change the order of the blocks:</p>
12 <p>To make it easier, try choosing non-negative checks and adjust the contents of the blocks to match them.</p>
12 <p>To make it easier, try choosing non-negative checks and adjust the contents of the blocks to match them.</p>
13 <h2>The elif construct</h2>
13 <h2>The elif construct</h2>
14 <p>The function get_type_of_sentence() only distinguishes between questions and statements. Let us add support for exclamations to it:</p>
14 <p>The function get_type_of_sentence() only distinguishes between questions and statements. Let us add support for exclamations to it:</p>
15 <p>We have added an exclamation checker for exclamation sentences. Technically, this feature works, but it incorrectly interprets questions. It also has problems in terms of semantics:</p>
15 <p>We have added an exclamation checker for exclamation sentences. Technically, this feature works, but it incorrectly interprets questions. It also has problems in terms of semantics:</p>
16 <ul><li>It checks for the exclamation point in any case, even if there is already a question mark</li>
16 <ul><li>It checks for the exclamation point in any case, even if there is already a question mark</li>
17 <li>We described the branch else for the second condition but not for the first. Therefore, the question sentence becomes "normal"</li>
17 <li>We described the branch else for the second condition but not for the first. Therefore, the question sentence becomes "normal"</li>
18 </ul><p>To remedy the situation, let's look at another possibility that conditional constructions bring:</p>
18 </ul><p>To remedy the situation, let's look at another possibility that conditional constructions bring:</p>
19 <p>Now all the conditions are lined up in a single construction. The elif means:</p>
19 <p>Now all the conditions are lined up in a single construction. The elif means:</p>
20 <blockquote><p>If the previous condition is not satisfied, but the current one is satisfied</p>
20 <blockquote><p>If the previous condition is not satisfied, but the current one is satisfied</p>
21 </blockquote><p>In this scenario, we get:</p>
21 </blockquote><p>In this scenario, we get:</p>
22 <ul><li>If the last letter is ?, then it is a 'question'</li>
22 <ul><li>If the last letter is ?, then it is a 'question'</li>
23 <li>If the last letter is !, then it is a 'exclamation'</li>
23 <li>If the last letter is !, then it is a 'exclamation'</li>
24 <li>The other options are 'normal'</li>
24 <li>The other options are 'normal'</li>
25 </ul><p>The program will execute only the code blocks that refer to the entire if construct.</p>
25 </ul><p>The program will execute only the code blocks that refer to the entire if construct.</p>
26 <h2>The ternary operator</h2>
26 <h2>The ternary operator</h2>
27 <p>Look at the definition of this function, which returns the modulus of a given number:</p>
27 <p>Look at the definition of this function, which returns the modulus of a given number:</p>
28 <p>But it could be written down more succinctly. It requires an expression to the right of return.</p>
28 <p>But it could be written down more succinctly. It requires an expression to the right of return.</p>
29 <p>But if is an instruction, not an expression. In Python, there is a construction that works like if-else but is considered an expression. It is the<strong>ternary operator</strong>, and it's the only operator that requires three operands:</p>
29 <p>But if is an instruction, not an expression. In Python, there is a construction that works like if-else but is considered an expression. It is the<strong>ternary operator</strong>, and it's the only operator that requires three operands:</p>
30 <p>The general pattern looks like this:</p>
30 <p>The general pattern looks like this:</p>
31 <p>Or like this:</p>
31 <p>Or like this:</p>
32 <p>Let us rewrite the initial version of get_type_of_sentence() similarly.</p>
32 <p>Let us rewrite the initial version of get_type_of_sentence() similarly.</p>
33 <p>Before:</p>
33 <p>Before:</p>
34 <p>After:</p>
34 <p>After:</p>
35 <p>A ternary operator can be nested in another ternary operator. But we should avoid it because such code is hard to read and debug.</p>
35 <p>A ternary operator can be nested in another ternary operator. But we should avoid it because such code is hard to read and debug.</p>
36  
36