Control_Structures_(if_for_while_etc).md
November 28, 2023 ยท View on GitHub
Core Python Concepts: Control Structures (if, for, while, etc.)
Control structures in Python dictate the flow of execution of a program. They enable the program to make decisions (conditionals) or execute a code block repeatedly (loops). The most common control structures in Python are if, for, and while.
1. If-Else Statements
The if statement is used for conditional execution. It executes a block of code if a specified condition is true. Python also supports elif (else if) and else for additional conditional checks.
x = 10
if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is 5")
else:
print("x is less than 5")
2. For Loops
A for loop in Python is used to iterate over a sequence (such as a list, tuple, string, or range).
# Iterating over a list
for item in [1, 2, 3]:
print(item)
# Using the range function
for i in range(5): # 0 to 4
print(i)
3. While Loops
A while loop executes as long as a specified condition is true.
count = 0
while count < 5:
print(count)
count += 1
4. Nested Loops
Python allows using one loop inside another loop.
for i in range(3): # Outer loop
for j in range(2): # Inner loop
print(i, j)
5. Loop Control Statements
break: Exits the loop.continue: Skips the current iteration and continues with the next.pass: A placeholder, does nothing but can prevent syntax errors when empty code is not allowed.
for i in range(5):
if i == 3:
break
print(i)
6. List Comprehensions
List comprehensions provide a concise way to create lists using a for loop in a single line.
# List of squares using list comprehension
squares = [x * x for x in range(10)]
7. Conditional Expressions (Ternary Operator)
Python supports a one-line if-else expression, often called the ternary operator.
x = 5
result = "Greater than 2" if x > 2 else "Less than or equal to 2"
Conclusion
Control structures are fundamental in Python, allowing the creation of complex and dynamic programs. Understanding and correctly using these structures is crucial for any Python programmer, enabling the development of programs that can make decisions and perform repetitive tasks efficiently.