In this Python article we want to learn about Python Control Structures, so as we know that Python is popular programming language and it provides different control structures for program flow management. Control structures helps us in making decisions and executing code based on certain conditions. these structures provides a way to control the flow of program execution.
In this article we want to talk about different control structures that Python provides and how they can be used.
-
Python if-else statement
Python if-else statement is one of the most commonly used control structures in Python. Using that you can make decisions based on certain conditions.
This is the syntax of if-else statement:
1 2 3 4 |
if condition: # Code to execute when the condition is True else: # Code to execute when the condition is False |
For example you want to check if a number is even or odd. you can use Python if-else statement for this:
1 2 3 4 5 |
number = 6 if number % 2 == 0: print("The number is even") else: print("The number is odd") |
This will be the result
-
Python for loop
Python for loop is another commonly used control structure in Python. and using that you can iterate over a sequence of values and execute code for each value.
This is syntax of the for loop:
1 2 |
for variable in sequence: # Code to execute for each value in the sequence |
For example you want to print the elements of a list. you can use for loop to like this:
1 2 3 |
my_list = [1, 2, 3, 4, 5] for element in my_list: print(element) |
This will be the result
-
Python while loop
Python while loop is another control structure that allows you to execute code repeatedly as long as a condition is true. this is the syntax of the while loop:
1 2 |
while condition: # Code to execute while the condition is True |
For example you want to print numbers from 1 to 5. you can use while loop like this:
1 2 3 4 |
number = 1 while number <= 5: print(number) number += 1 |
-
Python break and continue statements
Python break and continue statements are used to modify the behavior of loops. Using break statement you can exit a loop prematurely, and continue statement allows you to skip over certain iterations of a loop.
For example we want to print numbers from 1 to 10, but we want to skip number 5. you can use the continue statement like this.
1 2 3 4 |
for number in range(1, 11): if number == 5: continue print(number) |
-
Python try-except statement
Python try-except statement is used to handle exceptions that may occur during program execution. it allows you to catch and handle errors without causing the program to crash.
This is syntax of the try-except statement:
1 2 3 4 |
try: # Code that may raise an exception except ExceptionType: # Code to execute if the exception is raised |
For example if you want to read a file but the file does not exist. you can use try-except statement to handle this:
1 2 3 4 5 |
try: with open("my_file.txt", "r") as f: # Code to read the file except FileNotFoundError: print("The file does not exist") |
Subscribe and Get Free Video Courses & Articles in your Email