HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>In addition to arithmetic operations, there are comparison operations in mathematics, such as 5 &gt; 4 or 3 &lt; 1. They also exist in programming. For example, when we visit a website, the username and password are compared with those in the database. If they match, we can authenticate. In this lesson, we will learn more about comparison operations.</p>
1 <p>In addition to arithmetic operations, there are comparison operations in mathematics, such as 5 &gt; 4 or 3 &lt; 1. They also exist in programming. For example, when we visit a website, the username and password are compared with those in the database. If they match, we can authenticate. In this lesson, we will learn more about comparison operations.</p>
2 <p>Programming languages have adapted all mathematical comparison operations unchanged, except for the equality and inequality operators. The regular equal sign= is used for this in mathematics, but it is less common in programming.</p>
2 <p>Programming languages have adapted all mathematical comparison operations unchanged, except for the equality and inequality operators. The regular equal sign= is used for this in mathematics, but it is less common in programming.</p>
3 <p>Many languages use the = symbol to assign values to variables. That is why we use == for comparison in Python.</p>
3 <p>Many languages use the = symbol to assign values to variables. That is why we use == for comparison in Python.</p>
4 <p>Here is the list of comparison operations:</p>
4 <p>Here is the list of comparison operations:</p>
5 <ul><li>&lt; - less than</li>
5 <ul><li>&lt; - less than</li>
6 <li>&lt;= - less than or equal to</li>
6 <li>&lt;= - less than or equal to</li>
7 <li>&gt; - more</li>
7 <li>&gt; - more</li>
8 <li>&gt;= - greater than or equal to</li>
8 <li>&gt;= - greater than or equal to</li>
9 <li>== - equals</li>
9 <li>== - equals</li>
10 <li>!= - is not equal to</li>
10 <li>!= - is not equal to</li>
11 </ul><p>These operations are not limited to numbers. For example, you can use the equality operator to compare strings. The password == text comparison works with the string values.</p>
11 </ul><p>These operations are not limited to numbers. For example, you can use the equality operator to compare strings. The password == text comparison works with the string values.</p>
12 <h2>The logical data type</h2>
12 <h2>The logical data type</h2>
13 <p>A logical operation like 5 &gt; 4 or password == text is an expression. Its result is a specific value - True or False.</p>
13 <p>A logical operation like 5 &gt; 4 or password == text is an expression. Its result is a specific value - True or False.</p>
14 <p>It is a bool - a new data type for us:</p>
14 <p>It is a bool - a new data type for us:</p>
15 <p>Along with strings, integers, and rational numbers, the bool is one of the primitive data types in Python.</p>
15 <p>Along with strings, integers, and rational numbers, the bool is one of the primitive data types in Python.</p>
16 <p>Let us try to write a simple function that takes the age of children as input and determines whether they are babies or not. We define babies as children under the age of one:</p>
16 <p>Let us try to write a simple function that takes the age of children as input and determines whether they are babies or not. We define babies as children under the age of one:</p>
17 <p>Each operation is an expression, so the only line of the function we write means "return the value that results from the comparison age &lt; 1". Depending on the argument passed, the comparison will be True or False, and return will return that result.</p>
17 <p>Each operation is an expression, so the only line of the function we write means "return the value that results from the comparison age &lt; 1". Depending on the argument passed, the comparison will be True or False, and return will return that result.</p>
18 <p>Now run the check on a child who is six months old:</p>
18 <p>Now run the check on a child who is six months old:</p>
19 <p>The result of the operation is True. So the child is a baby.</p>
19 <p>The result of the operation is True. So the child is a baby.</p>
20 <h2>Predicates</h2>
20 <h2>Predicates</h2>
21 <p>The is_infant() function is a<strong>predicate</strong>or question function. A predicate answers a "yes or no" question by returning a boolean value. Predicates in any language usually have convenient names to make them easy to parse. Python predicates start with the prefixes is or has:</p>
21 <p>The is_infant() function is a<strong>predicate</strong>or question function. A predicate answers a "yes or no" question by returning a boolean value. Predicates in any language usually have convenient names to make them easy to parse. Python predicates start with the prefixes is or has:</p>
22 <ul><li>is_infant() - is it an infant?</li>
22 <ul><li>is_infant() - is it an infant?</li>
23 <li>has_children() - do they have children?</li>
23 <li>has_children() - do they have children?</li>
24 <li>is_empty() - is it empty?</li>
24 <li>is_empty() - is it empty?</li>
25 <li>has_errors() - are there any errors?</li>
25 <li>has_errors() - are there any errors?</li>
26 </ul><p>A function is a predicate function if it returns the boolean values True or False.</p>
26 </ul><p>A function is a predicate function if it returns the boolean values True or False.</p>
27 <p>Let us write another predicate function. It takes a string and checks if it is the word 'Castle':</p>
27 <p>Let us write another predicate function. It takes a string and checks if it is the word 'Castle':</p>
28 <h2>Combining operations and functions</h2>
28 <h2>Combining operations and functions</h2>
29 <p>Logic operations are expressions, meaning we can combine them with other ones. For example, if we want to check whether a number is odd or even. The approach used in programming is to check the remainder of a division by two:</p>
29 <p>Logic operations are expressions, meaning we can combine them with other ones. For example, if we want to check whether a number is odd or even. The approach used in programming is to check the remainder of a division by two:</p>
30 <ul><li>If the remainder is 0 - the number is even</li>
30 <ul><li>If the remainder is 0 - the number is even</li>
31 <li>If the remainder is not 0 - the number is odd</li>
31 <li>If the remainder is not 0 - the number is odd</li>
32 </ul><p>Division remainders are a simple but essential concept in arithmetic, algebra, number theory, and cryptography. You need to divide the number into several equal groups. If anything remains at the end, it is the remainder of the division.</p>
32 </ul><p>Division remainders are a simple but essential concept in arithmetic, algebra, number theory, and cryptography. You need to divide the number into several equal groups. If anything remains at the end, it is the remainder of the division.</p>
33 <p>Here we split some candies equally among individuals:</p>
33 <p>Here we split some candies equally among individuals:</p>
34 <ul><li>7 candies, 2 people: 2 x 3 + remainder 1 - 7 not a multiple of 2</li>
34 <ul><li>7 candies, 2 people: 2 x 3 + remainder 1 - 7 not a multiple of 2</li>
35 <li>21 candies, 3 people: 3 x 7 + remainder 0 - 21 multiples of 3</li>
35 <li>21 candies, 3 people: 3 x 7 + remainder 0 - 21 multiples of 3</li>
36 <li>19 candies, 5 people: 5 x 3 + remainder 4 - 19 not a multiple of 5</li>
36 <li>19 candies, 5 people: 5 x 3 + remainder 4 - 19 not a multiple of 5</li>
37 </ul><p>The % operator calculates the remainder of a division:</p>
37 </ul><p>The % operator calculates the remainder of a division:</p>
38 <ul><li>7 % 2 → 1</li>
38 <ul><li>7 % 2 → 1</li>
39 <li>21 % 3 → 0</li>
39 <li>21 % 3 → 0</li>
40 <li>19 % 5 → 4</li>
40 <li>19 % 5 → 4</li>
41 </ul><p>Let us combine the equality check == and the arithmetic operator % into one expression and write a function that checks if a number is odd or even:</p>
41 </ul><p>Let us combine the equality check == and the arithmetic operator % into one expression and write a function that checks if a number is odd or even:</p>
42 <p>Arithmetic operators have higher priority than logical ones. So, there are three steps. The program:</p>
42 <p>Arithmetic operators have higher priority than logical ones. So, there are three steps. The program:</p>
43 <ul><li>Calculates the arithmetic expression number % 2</li>
43 <ul><li>Calculates the arithmetic expression number % 2</li>
44 <li>Compares the result with zero</li>
44 <li>Compares the result with zero</li>
45 <li>Returns the result of the equality check</li>
45 <li>Returns the result of the equality check</li>
46 </ul><p>Now we will write a function that takes a string and checks if it starts with the letter a.</p>
46 </ul><p>Now we will write a function that takes a string and checks if it starts with the letter a.</p>
47 <p>Here is the algorithm:</p>
47 <p>Here is the algorithm:</p>
48 <ol><li>Get the initial character of the string and assign it to the variable</li>
48 <ol><li>Get the initial character of the string and assign it to the variable</li>
49 <li>Compare whether the symbol is equal to the letter a</li>
49 <li>Compare whether the symbol is equal to the letter a</li>
50 <li>Return the result</li>
50 <li>Return the result</li>
51 </ol><p>And here is the code:</p>
51 </ol><p>And here is the code:</p>
52 <p>Try to say what is going on in the same way we decoded the process in the is_even() example to understand what is happening here.</p>
52 <p>Try to say what is going on in the same way we decoded the process in the is_even() example to understand what is happening here.</p>
53 <p>You now know that programmers use comparison operations alongside arithmetic operations. But remember that we indicate equality with ==. This way, you will not confuse this operation with assigning a value to a variable.</p>
53 <p>You now know that programmers use comparison operations alongside arithmetic operations. But remember that we indicate equality with ==. This way, you will not confuse this operation with assigning a value to a variable.</p>