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.

type(True)
bool
type(False)
bool

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.

10 > 5
True

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.

10 == 5
False

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

if 10 > 5:
    print("This expression is true!")
This expression is true!

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

if 10 == 5:
    print("10 is equal to 5")
elif 10 == 9:
    print("10 is equal to 9")
else:
    print("10 is not equal to 5")
10 is not equal to 5

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.

for letter in 'hello':
    print(letter)
h
e
l
l
o

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

text = ''' 
  1. We refuse to operate under the assumption that risk and harm
  associated with data practices can be bounded to  mean the same
  thing for everyone, everywhere, at every time. We commit to
  acknowledging how historical and systemic patterns of violence 
  and exploitation produce differential vulnerabilities for 
  communities.
  2. We refuse to be disciplined by data, devices, and practices 
  that seek to shape and normalize racialized, gendered, and 
  differently-abled bodies in ways that make us available to be 
  tracked, monitored, and surveilled. We commit to taking back 
  control over the ways we behave, live, and engage with data and 
  its technologies.
  3. We refuse the use of data about people in perpetuity. We 
  commit to embracing agency and working with intentionality, 
  preparing bodies or corpuses of data to be laid to rest when they 
  are not being used in service to the people about whom they were 
  created.
  4. We refuse to understand data as disembodied and thereby 
  dehumanized and departicularized. We commit to understanding 
  data as always and variously attached to bodies; we vow to 
  interrogate the biopolitical implications of data with a keen 
  eye to gender, race, sexuality, class, disability, nationality, 
  and other forms of embodied difference.
  5. We refuse any code of phony “ethics” and false proclamations of 
  transparency that are wielded as cover, as tools of power, as forms 
  for escape that let the people who create systems off the hook from 
  accountability or responsibility. We commit to a feminist data 
  ethics that explicitly seeks equity and demands justice by helping 
  us understand and shift how power works.'''
  
sentences = text.split('.')

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.”

results = []
for i in sentences:
  if 'refuse' in i:
      results.append(i)

 print(results)
  File <tokenize>:6
    print(results)
    ^
IndentationError: unindent does not match any outer indentation level

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?

the end#

Congratulations on making it through the workshop! You are officially a beginner now.

In the next workshop, we build on these skills in the practice of web scraping and APIs. We will create a dataset that we will clean, process, and analyze in future sessions.