Beginning Python: Conditionals
2025-10-06
[ … ]list.index()[X:Y]
[:2], [-2:]"key": valuevalue: dict["key"]dict["key"] = valuedict: "key" in dictkey: value pair – del dict["key"]for loops: do this task for a set number of times or for each item in a sequencewhile loops: do this task as longer as long as a condition is trueif, elif, else+ : plus- : minus* : times/ : divided by** : to the power of// : floor division% : modulus% 2x is even if True.x = 50 on its own line, then the if statement.x = 51.
if statement?x % 2 == 0.
x % 2 is exactly equal to 0.–>
if statement:if statements are more common, as they frequently involve multiple lines of code.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.") x = 50x = 51if x % 2 == 0:
print(x, "is even.")
print("This line only prints if it's even too.")
print("This line prints no matter what.") print("This line prints no matter what.") not indented?
else and elif Statementselse Statementsif 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.") else Statementselse command:elif Statementsif statements.50.1?elif Statementselif StatementsEven % 2 == 0Odd % 2 == 1Decimal % 2 == 0.01 - 1.99elif Statementsnovel = "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 statementelif keywords in your conditional block!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")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)Create a variable for a single sound, called sound.
Next, define lists of vowels and consonants (use these or make your own):
sound and prints the result.
print(sound, "is a vowel.")# 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 removedsound = "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!")