Introduction
This lab provides an introduction to conditional statements in Python. This includes if, for, while. Conditional statements are fundamental constructs in programming that allow you to control the flow of execution based on certain conditions.
Achievements
- Indentation
- if-else Statements
- for Loops
- while Loops
if Statements
The if statement in Python allows you to check multiple conditions and execute different blocks of code based on the first condition that evaluates to true.
Syntax
The syntax of an if statement in Python is:
if condition_1:
statement_block_1 ## Every Block in Python is Indented
elif condition_2:
statement_block_2
else:
statement_block_3
Note: Before learning about the
ifstatement, you should have a basic understanding of indentation in Python. Indentation is crucial for structuring code blocks. Unlike other languages that use braces{}to delineate blocks, Python relies on indentation. Let's explore the fundamentals of Python indentation, its significance, and how it's applied.
Example
- In this example, the if statement checks if the variable
xis greater than zero. If the condition is true, the statementprint("x is positive")is executed.
## Example: Check if a number is positive
>>> x = 10
>>> if x > 0:
... print("x is positive")
...
x is positive
Tips: You need type four spaces before the print statement to make it part of the if block. Typing the Enter key at the end of the line will execute the block.
- In this example, the if-else statement checks if the variable
xis greater than zero. If the condition is true, the statementprint("x is positive")is executed; otherwise, the statementprint("x is negative")is executed.
## Example: Check if a number is positive or negative
>>> x = -5
>>>
>>> if x > 0:
... print("x is positive")
... else:
... print("x is negative")
...
x is negative
- In this example, the if-elif-else statement checks the value of the variable marks and prints the corresponding grade based on the conditions provided. Since marks is 75, the condition
marks >= 70evaluates to true, so the statementprint("Grade: C")is executed.
## Example: Determine the grade based on marks
>>> marks = 75
>>>
>>> if marks >= 90:
... print("Grade: A")
... elif marks >= 80:
... print("Grade: B")
... elif marks >= 70:
... print("Grade: C")
... else:
... print("Grade: Fail")
...
Grade: C
The if statement provides a flexible way to control the flow of your program based on different conditions. It's a fundamental building block for writing conditional logic in Python.
for Loops
A for loop is a fundamental control flow statement in Python used for iterating over a sequence of elements.
Syntax
The basic syntax of a for loop in Python is as follows:
for <variable> in <sequence>:
<statements>
else:
<statements>
<sequence>can be any iterable object like a list, tuple, string, or range.<variable>is a placeholder that takes on each value in the sequence one at a time.<statements>represent the block of code to be executed for each value in the sequence.
Examples
Let's explore a simple example of iterating over a list of integers and printing each value:
>>> numbers = [1, 2, 3, 4, 5]
>>> for number in numbers:
... print(number)
...
1
2
3
4
5
In this example:
numbersis a list containing integers from 1 to 5. List is an built-in data structure in Python that can hold multiple values.for number in numbers:iterates over each element in thenumberslist.- Inside the loop,
print(number)prints each value ofnumberone by one.
while Loops
A while loop is another fundamental control flow statement in Python used for executing a block of code repeatedly as long as a specified condition is true.
Syntax
The basic syntax of a while loop in Python is as follows:
while <condition>:
<statements>
- is an expression that evaluates to either
TrueorFalse. - represent the block of code to be executed as long as the condition is
True.
Examples
Let's explore a simple example of using a while loop to count from 1 to 5:
>>> count = 1
>>> while count <= 5:
... print(count)
... count += 1
...
1
2
3
4
5
In this example:
countis initialized to 1.while count <= 5:checks if the value ofcountis less than or equal to 5.- Inside the loop,
print(count)prints the current value ofcount. count += 1increments the value of count by 1 in each iteration.
The loop continues to execute until the condition count <= 5 becomes False, i.e., when count exceeds 5.
Summary
Understanding conditional statements and indentation is fundamental for writing clear and readable Python code. By using conditional statements effectively and following proper indentation practices, you can create logical and well-organized programs.



