Python Conditional Statements
Python Conditional Statements
Statements
[Opening Scene: Upbeat music, Python logo, and a blank IDE on screen]
Narrator:
"Welcome to our Python Video Course! Today, we’re diving into one of the most
powerful tools in programming: Conditional Statements. Imagine you’re deciding
what to wear—if it’s cold, grab a jacket; if it’s hot, pick a t-shirt. That’s exactly
what conditionals do in Python—they let your code make decisions. This is going
to be a comprehensive journey, packed with examples and a fun project, so let’s
jump right in!"
Narrator:
"First, what are conditional statements? They’re instructions that only run if a
condition is true. Think of them as the brain of your program, deciding what
happens next. In Python, we use three main keywords: if, elif, and else. Let’s start
with the simplest—the if statement."
Narrator:
"Here’s the basic syntax: if condition:—notice the colon—and then indent the code
that runs if the condition is True. Type this: temperature = 25. Now, if temperature
> 20: print('It’s warm!'). Run it—what do you see? 'It’s warm!'—because 25 is
greater than 20. Conditions always evaluate to True or False, using operators like
> for greater than or == for equal."
Narrator:
"Next, let’s add options with elif—short for 'else if'. It checks another condition if
the first fails. Update the code: if temperature > 30: print('It’s hot!'), then elif
temperature > 20: print('It’s warm!'). Run it—still 'It’s warm!'—because 25 fits the
second condition. Now, add an else: else: print('It’s cold!'). The else catches
anything that doesn’t match."
Narrator:
"Type this: age = 18, is_student = True. Now, if age < 20:, indent and add if
is_student: print('Student discount!'). Run it—you get 'Student discount!' because
both conditions are True. Nesting lets us check multiple criteria, like a filter
within a filter."
Narrator:
"Now, let’s spice things up with logical operators: and, or, and not. Try: time = 14,
then if age < 20 and time < 15: print('Matinee discount!'). Both must be True—18 is
less than 20, and 14 is less than 15, so it works! Swap and with or—now it runs if
either is True. Add not like if not time > 15: to flip a condition."
Narrator:
"Python also has a shorthand called a conditional expression—or ternary
operator. Type: grade = 85, then result = 'Pass' if grade >= 60 else 'Fail'. Print it
—'Pass'! It’s a compact if-else in one line, perfect for simple choices."
Narrator:
"Let’s apply this with a practical example: a weather outfit selector. Type:
weather = 'rainy'". Then, if weather == 'rainy': print('Take an umbrella'), elif weather
== 'sunny': print('Wear sunglasses'), else: print('Just a jacket'). Run it—'Take an
umbrella'! Change weather to 'sunny' and try again."
Narrator:
"Time for a mini-project: a Traffic Light Simulator! Type: light = 'green'". Then, if
light == 'green': print('Go'), elif light == 'yellow': print('Slow down'), else: print('Stop').
Test it with 'red'—'Stop'! This could control a real traffic system!"
Narrator:
"Let’s wrap up with a challenge! Write a program to guess a number between 1
and 10. Set secret = 7, take a guess = 5, and use if-elif-else to print 'Too low', 'Too
high', or 'Correct'. Pause here and give it a shot!"
Narrator:
"That’s Python Conditional Statements! You’ve learned if, elif, else, nesting,
logical operators, and more—everything to make your code smart and decisive.
Next, we’ll explore loops to repeat actions. See you soon!"
Syntax:
if condition:
# Code to execute when condition is True
num = 10
if num > 0:
print("The number is positive.")
if-else Statement
Syntax:
if condition:
# Code if condition is True
else:
# Code if condition is False
num = 7
if num % 2 == 0:
print("Even number")
else:
print("Odd number")
elif Statement
The elif (short for "else if") statement allows multiple conditions to be
checked in sequence.
Syntax:
if condition1:
# Code if condition1 is True
elif condition2:
# Code if condition2 is True
else:
# Code if none of the conditions are True
score = 85
if score >= 90:
print("Grade: A")
elif score >= 80:
print("Grade: B")
elif score >= 70:
print("Grade: C")
else:
print("Grade: F")
match operation:
case "+":
print("Result:", num1 + num2)
case "-":
print("Result:", num1 - num2)
case "*":
print("Result:", num1 * num2)
case "/":
if num2 != 0:
print("Result:", num1 / num2)
else:
print("Error: Division by zero")
case _:
print("Invalid operation")
2. E-commerce Pricing: