Lession 2: Control Structure in Python by BlackBox AI


 BLACKBOXAI

Control structures in Python are constructs that allow you to control the flow of execution of your code based on certain conditions or the repetition of certain actions. The main types of control structures in Python include conditional statements, loops, and exception handling.

1. Conditional Statements

Conditional statements allow you to execute certain blocks of code based on whether a condition is true or false.

a. if Statement

The simplest form of a conditional statement.

python
1x = 10 2if x > 5: 3 print("x is greater than 5")

b. if-else Statement

Provides an alternative block of code to execute if the condition is false.

python
1x = 3 2if x > 5: 3 print("x is greater than 5") 4else: 5 print("x is not greater than 5")

c. if-elif-else Statement

Allows you to check multiple conditions.

python
1x = 10 2if x < 5: 3 print("x is less than 5") 4elif x == 5: 5 print("x is equal to 5") 6else: 7 print("x is greater than 5")

2. Loops

Loops allow you to execute a block of code multiple times.

a. for Loop

Used to iterate over a sequence (like a list, tuple, or string).

python
1fruits = ["apple", "banana", "cherry"] 2for fruit in fruits: 3 print(fruit)

b. while Loop

Repeats a block of code as long as a condition is true.

python
1count = 0 2while count < 5: 3 print(count) 4 count += 1

3. Loop Control Statements

These statements can alter the flow of loops.

a. break

Exits the loop prematurely.

python
1for num in range(10): 2 if num == 5: 3 break 4 print(num)

b. continue

Skips the current iteration and continues with the next iteration.

python
1for num in range(10): 2 if num % 2 == 0: 3 continue 4 print(num) # This will print only odd numbers

c. pass

A null statement that is used when a statement is syntactically required but you do not want to execute any code.

python
1for num in range(5): 2 if num == 3: 3 pass # Do nothing 4 print(num)

4. Exception Handling

Exception handling allows you to manage errors gracefully without crashing the program.

a. try and except

Used to catch and handle exceptions.

python
1try: 2 result = 10 / 0 # This will raise a ZeroDivisionError 3except ZeroDivisionError: 4 print("You cannot divide by zero!")

b. finally

A block that will execute no matter what, whether an exception occurred or not.

python
1try: 2 file = open("example.txt", "r") 3except FileNotFoundError: 4 print("File not found!") 5finally: 6 print("This will always execute.")

c. else

A block that runs if the try block does not raise an exception.

python
1try: 2 result = 10 / 2 3except ZeroDivisionError: 4 print("You cannot divide by zero!") 5else: 6 print("Division successful, result is:", result)

Conclusion

Control structures are essential for creating dynamic and responsive programs in Python. They allow you to make decisions, repeat actions, and handle errors effectively, making your code more robust and flexible. Understanding how to use these structures is fundamental to programming in Python.

Comments

Popular posts from this blog

Basic Syntax Of Python by Chat GPT | Ballu Python 835430

Day 1: ChatGPT Python Lession | Ballu Python 835430