ALL MODULESmodule_03.py

Strings & Conditional Statements

~60 minTheoryLive CodingChallengeQuiz
theory.md

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:

PYTHON
WHAT HAPPENED HERE
We picked out individual characters using square brackets []. [0] grabbed the very first character, and [-1] counted backward from the end to grab the last one.
THE TECHNICAL NAME
Text in quotes is a string. Finding a character by its position number is called indexing.
TAKEAWAY
Access single letters using square brackets starting at zero: 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:

PYTHON
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
Extracting a portion of text is slicing. Built-in actions attached to text (like .upper() or .replace()) are called string methods.
TAKEAWAY
Slice strings with [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:

PYTHON
WHAT HAPPENED HERE
Python checked if age >= 18 was True. Since 20 is greater than 18, it ran the indented print statement beneath it.
THE TECHNICAL NAME
Checking a condition to decide whether code runs is an if statement. The check itself (age >= 18) is a boolean condition.
TAKEAWAY
Use 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:

PYTHON
WHAT HAPPENED HERE
Python checked the first condition (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
Testing multiple paths in order uses elif (short for else-if) and else (the default fallback).
TAKEAWAY
Chain 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:

PYTHON
WHAT HAPPENED HERE
The word and checked that BOTH conditions were True before allowing the code block to run.
THE TECHNICAL NAME
Keywords like and, or, and not are logical operators used to combine or flip boolean checks.
TAKEAWAY
Combine checks using 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().

live_coding — Username Validator & Access Control

Try changing username or password_length to see how the conditional branch responds.

Interactive Control Flow Tracing

TrueFalseEnter LoopCondition?Loop BodyExit
Python not loaded yet.
demo — Demonstrate string indexing, slicing, methods, and conditional branching.

Demonstrate string indexing, slicing, methods, and conditional branching.

challenge_1

Warm-up 1: Print the first and last character of a string.

Steps:

  1. Set word = "Developer".
  2. Print First: D.
  3. Print Last: r.
Python not loaded yet.
challenge_2

Warm-up 2: Check if a number is positive.

Steps:

  1. Given num = 42.
  2. If num > 0, print Positive number.
Python not loaded yet.
challenge_3

Warm-up 3: Convert text to uppercase and slice the first 3 letters.

Steps:

  1. Given code = "python".
  2. Uppercase the string and slice first 3 characters.
  3. Print Prefix: PYT.
Python not loaded yet.
challenge_4

Apply-it 1: Grade evaluator using if-elif-else.

Steps:

  1. Given score = 88.
  2. If score >= 90 print Grade: A.
  3. Else if score >= 80 print Grade: B.
  4. Else print Grade: C.
Python not loaded yet.
challenge_5

Apply-it 2: Password strength checker.

Steps:

  1. Given password = "secret123".
  2. If length >= 8 AND contains numbers (check any(char.isdigit() for char in password)), print Strong Password.
  3. Otherwise print Weak Password.
Python not loaded yet.
challenge_6

Apply-it 3: Text cleaning and word count check.

Steps:

  1. Given raw_text = " Python programming is fun! ".
  2. Strip surrounding whitespace using .strip().
  3. Print Cleaned: Python programming is fun!.
  4. If length > 10, print Long sentence.
Python not loaded yet.
challenge_7

Stretch Challenge: Discount Ticket Calculator.

Steps:

  1. Given age = 65 and is_student = False.
  2. If age <= 12 or age >= 60 or is_student, ticket price is 10.
  3. Otherwise ticket price is 20.
  4. Print Ticket Price: $10.
Python not loaded yet.
quiz — 9 questions
30s question timers are paused. Start individually or all together.
# question 1

What is the index of the first character in a Python string?

# question 2

What does `'Python'[1:4]` return?

# question 3

Which string method removes whitespace from both ends of a string?

# question 4

Which operator checks if two values are equal in an `if` condition?

# question 5

What will `print('cat'.upper())` display?

# question 6

When does an `and` expression evaluate to True?

# question 7

What is the output of `bool('')` (an empty string)?

# question 8

What does negative indexing `text[-1]` access?

# question 9

Which keyword acts as a catch-all fallback when all `if` and `elif` checks fail?

discussion

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