Uploaded by emilychang1234

Python Lesson 8

advertisement
Lesson 8:
Conditional
Statements
Learning to Make
Decisions in Python
What We'll Cover Today
•
•
•
•
•
Recap of Previous Lessons
String Formatting
Learn how to use the input() function
Understand the basics of if statements.
Build a simple decision-making program.
Recap of Previous Lessons
•
String Formatting with f-strings:
•
Syntax:
•
Input() function:
•
•
– Use curly braces {} to insert variables into strings.
– Preface the string with f to indicate formatting.
name = "Alice"
age = 10
greeting = f"Hello, {name}. You are {age} years old."
print(greeting)
– Example: name = input("Enter your name: ")
If statement:
Example with Explanation: number = 10
if number > 5:
print("Number is greater than 5")
– Break down the example:
• ‘number > 5’ is the condition.
• The print statement is executed because the condition (number is greater
than 5) is true.
Structure of an if Statement
•
•
Breaking Down the if Statement
– ‘if’: The keyword that starts the conditional statement.
– ‘condition’: What you’re checking, which must be either true or false.
– ‘:’: The colon signifies the start of the block of code that runs if the
condition is true.
– Indented code block: The code under the if statement, which runs only if
the condition is true.
Example with Explanation: number = 10
if number > 5:
print("Number is greater than 5")
– Break down the example:
• ‘number > 5’ is the condition.
• The print statement is executed because the condition (number is
greater than 5) is true.
Importance of Indentation
The code block under an if statement must be indented!!
Indentation (usually 4 spaces or a tab) in Python signifies a block of code.
Hands-on Demonstration
• Task: Write a program that prints “This is a
positive number” if the number is positive.
•
number = 5
if number > 0:
print("This is a positive number")
Hands-On Exercise: Simple Decision Making
• Task: Write a program that asks the user for their
age and prints if they are a teenager or not.
• Pseudocode:
• Get age.
• Check if age is between 13 and 19.
• Print message based on the condition.
Exploring Conditions
• Different types of conditions (e.g., ==, >, <).
• Examples:
– if age == 13:
– if word_length > 5:
– if height >=130:
– if gender == “female”:
– If number >= 0.5:
More Hands-On Practice
• Problem 1: Write an if statement to check if a number
is even. Hint: % is the modulo operator, which returns
the remainder of the division of number by 2.
• Problem 2: Write an if statement to see if a word has
more than 4 letters. Hint: ‘len(word)’ returns the
length of the string ‘word’
Solutions
• Problem 1: number = 8 # You can replace 8 with any number you want to check
if number % 2 == 0:
print(f"The number {number} is even.")
else:
print(f"The number {number} is not even.")
• Problem 2:word = "Python" # Replace "Python" with any word you want to check
if len(word) > 4:
print(f"The word '{word}' has more than 4 letters.")
else:
print(f"The word '{word}' does not have more than 4 letters.")
Download