Computation for Linguists

Beginning Python: Conditionals

Dr. Andrew M. Byrd

2025-10-06

Review

  • What did you learn last time?

Recap from Last Week

  • Variables
  • Three Value Types:
    • Numbers
    • Strings
    • Boolean

Recap from Last Week

  • Lists:
    • Ordered, can add & delete
    • Within []
    • list.index()
    • Slicing: [X:Y]
      • if you want to target margins, use blank: [:2], [-2:]

Recap from Last Week

  • Dictionaries:
    • "key": value
    • getting value: dict["key"]
    • adding/reassigning: dict["key"] = value
    • Checking dict: "key" in dict
    • Removing key: value pair – del dict["key"]

Dictionary Activity

  • Below is a dictionary pairing George Orwell’s novels with their publication years. Copy this into your qmd:
orwell_dict = {
    "Animal Farm": 1945,
    "Nineteen Eighty-Four": 1949,
    "Burmese Days": 1934,
    "Keep the Aspidistra Flying": 1936,
    "Coming Up for Air": 1939
}

Dictionary Activity

  • Print the year that Animal Farm was published using its key.
  • Check if Homage to Catalonia is in the dictionary.
  • Add Orwell’s Homage to Catalonia (1937) to the dictionary. Print the dictionary again to confirm.
  • Whoops, made a mistake! Homage to Catalonia was actually published in 1938. Fix that mistake.
  • Remove Coming Up for Air from the dictionary.

Control Structures

Loops & Conditionals = Control Structures

  • This is where programming languages truly become powerful
  • Control structures:
    • control the flow of a program
    • tell the program what to do next, and in what order
  • All programming languages have control structures, but they differ in how they’re written out

Loops & Conditionals = Control Structures

  • Loops:
    • for loops: do this task for a set number of times or for each item in a sequence
    • while loops: do this task as longer as long as a condition is true
  • Conditionals: check whether a condition is true; if so, execute the corresponding block of code
    • if, elif, else

Conditionals

Conditional Example: Odd or Even?

  • Here are the operators we know:
    • + : plus
    • - : minus
    • * : times
    • / : divided by
    • ** : to the power of
    • // : floor division
    • % : modulus

Conditional Example: Odd or Even?

  • Using % 2
100 % 2 \# 0
117 % 2 \# 1

Conditional Example: Odd or Even?

  • Let’s write a short block of code that will print x is even if True.
  • We’ll assign x = 50 on its own line, then the if statement.
x = 50
if x % 2 == 0: print(x, "is even")      # 50 is even
  • Run this code in your Quarto doc!
  • Redefine x = 51.
    • What happens when you rerun the if statement?

Conditional Example: So What’s Going On Here?

x = 50
if x % 2 == 0: print(x, "is even")      # 50 is even
  • We’re making the logical statement x % 2 == 0.
    • This statement is checking to see if the outcome of x % 2 is exactly equal to 0.
    • If we run it as its own line of code, we’ll get True.
x % 2       # 0
x % 2 == 0  # True 

–>

Conditionals: Proper Syntax

  • To generalize the syntax of an if statement:
if condition: command()     # if x is true, do y
  • We could also do:
if condition: 
    command()     # if x is true, do y
  • Note: the indent is required!
  • Indented if statements are more common, as they frequently involve multiple lines of code.

Conditionals: Proper Syntax

  • Knowing when and when not to indent is important.
  • Take the following code block:
if x % 2 == 0: 
    print(x, "is even.") 
    print("This line only prints if it's even too.") 
print("This line prints no matter what.") 
  • Run this code where x = 50

Conditionals: Proper Syntax

  • Now run this code where x = 51
if x % 2 == 0: 
    print(x, "is even.") 
    print("This line only prints if it's even too.") 
print("This line prints no matter what.") 
  • Why is print("This line prints no matter what.") not indented?
    • What would happen if we did indent it?

Python & Indentation

  • Through indentations, Python will define the scope of a control statement.
  • Lines of code with the same degree of indentation are controlled by the same control structures.
  • The greater the indentation, the narrower the scope of the code.

else and elif Statements

else Statements

  • Let’s return to our original if statement.
if x % 2 == 0: 
    print(x, "is even.") 
    print("This line only prints if it's even too.") 
print("This line prints no matter what.") 
  • Right now, this code only prompts a command if a number is even.
    • But what if we’d like it to also print a statement if the number were odd?

else Statements

  • The else command:
if x % 2 == 0: 
    print(x, "is even") 
else: 
    print(x, "is odd") 
  • Run this code block, redefining the value of x.

elif Statements

  • There’s one more keyword that we need to learn for our if statements.
  • Currently our code identifies numbers as even or odd:
if x % 2 == 0: 
    print(x, "is even") 
else: 
    print(x, "is odd") 
  • But what if the number is neither?
    • What if the number is 50.1?

elif Statements

  • Run the following code:
x = 50.1
x % 2
  • How might we capture forms that end in a decimal in Python?

elif Statements

  • You’ll note that when using the modulus operator with decimals, the output will itself be a decimal
  • For even numbers, you get between 0-1; for odd between 1-2
  • So:
    • Even % 2 == 0
    • Odd % 2 == 1
    • Decimal % 2 == 0.01 - 1.99

elif Statements

  • Let’s translate this to code:
x = 50
if x % 2 == 0:
    print(x, "is even.")
elif x % 2 == 1:
    print(x, "is odd.")
else:
    print(x, "is a decimal.")

Another Example: The Full Brontë

The Full Brontë

  • Let’s combine our knowledge of conditionals with lists.
  • Let’s say we wanted to identify which of the Brontë sisters wrote a particular novel.
  • We’d first need to create a list of novels for each sister.
charlotte = ["The Professor", "Jane Eyre", "Shirley", "Villette"] 
emily = ["Wuthering Heights"] 
anne = ["Agnes Grey", "The Tenant of Wildfell Hall"] 

Testing Novel Titles

novel = "Jane Eyre"     # we identify the novel we're looking at
if novel in charlotte: 
    print("Charlotte Brontë wrote", novel)  #we first check to see that our variable is in charlotte
elif novel in emily: 
    print("Emily Brontë wrote", novel) #then emily
elif novel in anne: 
    print("Anne Brontë wrote", novel) #then anne
else: 
    print(novel, "was not written by one of the Brontë sisters") #if the novel is in none of these, we get our else statement
  • Note that you can have multiple elif keywords in your conditional block!

Complexifying the Code

  • Let’s make our Brontë checking code a bit more complex
    • We can also keep track of how many times we’ve checked whether a particular sister wrote a book.
    • First, we need to initialize a dictionary that will keep track.
bronte_checks = {"charlotte": 0, "emily": 0, "anne" : 0}  #a starter dict, where the values of each bronte book is at 0

Complexifying the Code

  • Now, we include a line of code in each part of the if block to increase the value in the dictionary by 1.
if novel in charlotte: 
    bronte_checks["charlotte"] = bronte_checks["charlotte"] + 1
    print("Charlotte Brontë wrote", novel) 
elif novel in emily: 
    bronte_checks["emily"] = bronte_checks["emily"] + 1
    print("Emily Brontë wrote", novel) 
elif novel in anne: 
    bronte_checks["anne"] = bronte_checks["anne"] + 1
    print("Anne Brontë wrote", novel) 
else: print(novel, "was not written by one of the Brontë sisters")

The Full Brontë

novel = "Wuthering Heights" 

charlotte = ["The Professor", "Jane Eyre", "Shirley", "Villette"] 
emily = ["Wuthering Heights"] 
anne = ["Agnes Grey", "The Tenant of Wildfell Hall"] 

bronte_checks = {"charlotte": 0, "emily": 0, "anne" : 0} 

if novel in charlotte: 
    bronte_checks["charlotte"] = bronte_checks["charlotte"] + 1
    print("Charlotte Brontë wrote", novel) 
elif novel in emily: 
    bronte_checks["emily"] = bronte_checks["emily"] + 1
    print("Emily Brontë wrote", novel)
elif novel in anne: 
    bronte_checks["anne"] = bronte_checks["anne"] + 1
    print("Anne Brontë wrote", novel) 
else: print(novel, "was not written by one of the Brontë sisters")

print(bronte_checks)

Activity: Phoneme Classifier

Activity: Phoneme Classifier

  • Create a variable for a single sound, called sound.

  • Next, define lists of vowels and consonants (use these or make your own):

vowels = ["a", "e", "i", "o", "u"]
consonants = ["p", "t", "k", "m", "n", "s", "r", "l"]
  • Finally, write a conditional that classifies the sound and prints the result.
    • Hint: print(sound, "is a vowel.")

Activity Solutions

Dictionary Activity

# 1. Direct lookup
print(orwell_dict["Animal Farm"])     # Output: 1945

# 2. Membership check
print("Homage to Catalonia" in orwell_dict)   # Output: False

# 3. Adding a new key–value pair
orwell_dict["Homage to Catalonia"] = 1937
print(orwell_dict)      # Now includes: "Homage to Catalonia": 1937

# 4. Overwriting a value (fixing a mistake)
orwell_dict["Homage to Catalonia"] = 1938   # reassigns the correct year
print(orwell_dict["Homage to Catalonia"])   # Output: 1938

# 5. Delete a key–value pair
del orwell_dict["Coming Up for Air"]
print(orwell_dict)        # "Coming Up for Air" has been removed

Phoneme Classifier:

sound = "p"
vowels = ["a", "e", "i", "o", "u"]
consonants = ["p", "t", "k", "m", "n", "s", "r", "l"]

if sound in vowels:
    print(sound, "is a vowel.")
elif sound in consonants:
    print(sound, "is a consonant.")
else:
    print(sound, "is something else!")