Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

logic

Logic allows programmers to write code that can make decisions based on certain conditions. It enables what is called "control flow," which is to direct the order of code execution throughout a program.

Logic works by inserting conditions into the code. If the condition evaluates to be true, then a desired expression can execute. If the condition evaluates to be false, then another expression (or no expression at all) can execute.

Logic structures are very useful when working with text, because we can specify what kinds of text should pass the condition, thus filtering out parts of the text.

boolean

First, let's take a closer look at a very important data type, booleans. If you remember, a boolean is a True or False value.

But why do these values need a separate data type? The answer is because computer programs make decisions based on whether expressions are true or false.

To evaluate whether expressions are true or false, we can use certain operators, like >, <, or == to check equivalence. See w3schools for a full list of operators.

Spend some time getting to know the operators.

Note that the equivalence operator == is different from the assignment operator = which would be used to assign a variable say in x = 3.

if statement

Using boolean logic, we can write a conditional statement that checks whether certain conditions are true. This type of statment is called the if statement

This conditional statement will print a string if the condition evaluates to True. Python will first check if the integer 10 is greater than 5, and if it is, it will print the string contained within the print() function's parentheses.

Notice that the code here indents automatically on the second line (like the for loop). This creates what is called a "code block," which is Python's way of grouping code into one logical unit, whose expressions should be executed together, in the order that they are written.

The if statement can also be expanded to specify more conditions, with the elif expression, or to include a catchall for cases that do not meet the conditions, with else

congratulations! you can vote

We can add as many elif expressions as we like, to account for many conditions in our code.

looping with conditions

In this next and final section, we will add logic to our loop from the previous section, using the text from our opening script. Remember that a loop goes through a collection of items, like strings in a list, and does something to each item.

Now let’s save our text as “text” variable. Then, we can split the text from a long string into a list of sentences.

Notice something new: we've split the text by sentence rather than word. How did we do that? The split() method enables us to specify (if we choose to) a delimiter to split the string. If we do not specify a delimiter, then it defaults to whitespace. For this case, we will split the string by period, which we will put between quotes '.'. The output will then be a list of sentences.

Next, we will write a new loop that includes a condition. This condition will check whether a sentence from the text contains the word "refuse." If it does contain the word, it will add it to the new list, which we will call "results."

Notice that we are using list slicing to print only the first twenty words from the output, so we can avoid printing out a large list, which takes a lot of space on the screen.

Now, try running the same code, but alter the word ‘refuse’ to be all capitals. The output should show an empty list (with empty brackets). Why do think it did that?