Strings & Conditional Statements
3. Strings & Conditional Statements
3.1Working with Text: String Indexing
Think of a string like a row of assigned seats at a theater — each letter sits in a numbered position starting at zero.
SHOW:
WHAT HAPPENED HERE
[]. [0] grabbed the very first character, and [-1] counted backward from the end to grab the last one.THE TECHNICAL NAME
TAKEAWAY
name[0].3.2Slicing & String Methods
Imagine slicing a loaf of bread into a smaller piece, or using a text filter to make letters uppercase.
SHOW:
WHAT HAPPENED HERE
[0:5] extracted characters from position 0 up to (but not including) position 5. Then .upper() created a new version of the string with all capital letters.THE TECHNICAL NAME
.upper() or .replace()) are called string methods.TAKEAWAY
[start:stop] and transform text using methods like .upper() or .replace().3.3Making Decisions: if Statements
Think of a automatic door sensor: IF a person steps close, THEN open the door.
SHOW:
WHAT HAPPENED HERE
age >= 18 was True. Since 20 is greater than 18, it ran the indented print statement beneath it.THE TECHNICAL NAME
age >= 18) is a boolean condition.TAKEAWAY
if condition: to run code only when a condition is True.3.4Handling Multiple Choices: elif & else
Think of a traffic light: IF green then go, ELSE IF yellow then slow down, ELSE stop.
SHOW:
WHAT HAPPENED HERE
score >= 90). It was False, so it moved to elif score >= 80, which was True! It printed "Grade: B" and skipped the rest.THE TECHNICAL NAME
TAKEAWAY
if, elif, and else to handle multiple choices.3.5Combining Conditions (and, or, not)
To log into your account, you need BOTH a valid username AND a correct password.
SHOW:
WHAT HAPPENED HERE
and checked that BOTH conditions were True before allowing the code block to run.THE TECHNICAL NAME
and, or, and not are logical operators used to combine or flip boolean checks.TAKEAWAY
and (both True), or (at least one True), or not (reverses True/False).GOTCHA 1
Using a single '=' instead of '==' when comparing values inside an if condition.
GOTCHA 2
Forgetting the colon ':' at the end of an if, elif, or else line.
GOTCHA 3
Mismatched indentation inside an if block.
WHERE THIS SHOWS UP IN REAL CODE
Web servers inspect incoming text requests (like usernames and URLs) and use conditional checks to grant access or show error pages.
TRY THIS YOURSELF
Create a string variable text = 'python', convert it to uppercase, and check if text.isupper().
Try changing username or password_length to see how the conditional branch responds.
Interactive Control Flow Tracing
Demonstrate string indexing, slicing, methods, and conditional branching.
What is the index of the first character in a Python string?
What does `'Python'[1:4]` return?
Which string method removes whitespace from both ends of a string?
Which operator checks if two values are equal in an `if` condition?
What will `print('cat'.upper())` display?
When does an `and` expression evaluate to True?
What is the output of `bool('')` (an empty string)?
What does negative indexing `text[-1]` access?
Which keyword acts as a catch-all fallback when all `if` and `elif` checks fail?
# comments · strings-conditionals
- @priya · 2h ago
The typewriter hero is such a nice touch.
- @alex · 1h ago
Totally agree! Made it super engaging.
- @sam · 1d ago
Finally a course where I can run code without leaving the page.