Learn Python
- Python basic
- Introduction to File Handling
- Basics of List Comprehension
- Introduction to Matplotlib
- Classes and Objects
- Introduction to Functions
- Python Numbers
- Creating Basic Plots
- Opening and closing files
- Function parameters and arguments
- Advanced Techniques
- Attributes and Methods
- Python Strings
- Scope and lifetime of variables
- Advanced Plotting
- Reading from files
- Performance and Limitations
- Encapsulation
- Python List
- Specialized Plots
- Writing to files
- Return statement and output
- Inheritance
- Python Tuple
- Advanced Customization
- Working with different file formats
- Lambda Functions
- Polymorphism
- Practical Applications
- Higher-Order Functions
Flow control
Conditional statements
1. The if statement:
The if statement is the foundation of conditional statements in Python. It allows you to execute a block of code only if a certain condition is met. Here's the basic syntax:
if condition: # code to execute if the condition is True
condition: This is any expression that evaluates to True or False. Common expressions involve comparisons using operators like == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to).
Example: simple if statement
if age >= 18: print("You are eligible to vote.")
In Python, indentation is crucial. The code block indented after the if statement (usually 4 spaces or 1 tab) is only executed if the condition is True. This indentation style is essential for defining code blocks within control flow statements.
2. The if-else statement:
The else clause provides an alternative block of code to execute if the condition in the if statement is False. Here's the syntax:
if condition: # code to execute if the condition is True else: # code to execute if the condition is False
Example:
if age >= 18: print("You are eligible to vote.") else print("You are not eligible to vote.")
3. The if-elif-else chain:
When you have multiple conditions to check, you can use an if-elif-else chain. The elif (else if) clause allows you to specify additional conditions to check if the previous if conditions weren't met. Here's the general structure:
if condition1: # code to execute if condition1 is True elif condition2: # code to execute if condition1 is False and condition2 is True # You can have multiple elif blocks else: # code to execute if all conditions are False
Example:
grade = "B" if grade == "A": print("Outstanding!") elif grade == "B": print("Very good!") elif grade == "C": print("Good!") else: print("Please work harder next time.")
4. Nested if statements:
For complex decision-making, you can nest if statements within each other. However, make sure the indentation is clear to represent the nesting structure. Be cautious of overly nested statements as they can make code harder to read.
By effectively using conditional statements, you can write Python programs that make decisions and respond accordingly based on various conditions.
It's time to take a quiz!
Test your knowledge and see what you've just learned.
What is the basic syntax of an if statement in Python?
Dif: condition then code
Aif condition: code to execute
Bif condition code: execute
Cif (condition) { code }
Check Answer
What does the else clause do in an if statement?
AExecutes code if the if condition is False
BAlways executes code
CExecutes only when there are no conditions
DActs as a second if statement
Check Answer
What is the purpose of the elif clause?
DTo end the if statement
ATo provide an alternative if the first if condition is True
BTo chain multiple if statements
CTo check additional conditions if previous conditions were False
Check Answer
What is a potential drawback of nested if statements?
AThey make the code run faster
BThey are easier to read
CThey simplify decision-making
DThey can make code harder to read if overly nested
Check Answer
Why is indentation important in Python conditional statements?
CIt is required for all programming languages
DIt improves performance
AIt determines the order of execution
BIt defines code blocks
Check Answer