In this Python article we want to learn about Python Control Structures for Decision Making, Python is powerful programming language that offers different control structures for decision making. Control structures allows you to control the flow of your program based on certain conditions. this helps you to create programs that can make intelligent decisions based on user input, calculations or other factors. in this article we want to talk about the most commonly used control structures in Python for decision making.
-
Python if statement
if statement is used to execute a block of code if a certain condition is true. this is the basic syntax of an if statement.
1 2 |
if condition: # code to be executed if condition is true |
And this is practical example.
1 2 3 |
x = 10 if x > 5: print("x is greater than 5") |
Run the code this will be the result
-
Python if-else statement
Python if-else statement is used to execute one block of code if a certain condition is true, and another block of code if the condition is false. this is the basic syntax of an if-else statement.
1 2 3 4 |
if condition: # code to be executed if condition is true else: # code to be executed if condition is false |
This is practical example
1 2 3 4 5 |
x = 10 if x > 5: print("x is greater than 5") else: print("x is less than or equal to 5") |
This will be the result
-
Python if-elif-else statement
Python if-elif-else statement is used to execute different blocks of code based on multiple conditions. this is the basic syntax of an if-elif-else statement.
1 2 3 4 5 6 |
if condition1: # code to be executed if condition1 is true elif condition2: # code to be executed if condition2 is true else: # code to be executed if all conditions are false |
This is our practical example
1 2 3 4 5 6 7 |
x = 10 if x > 20: print("x is greater than 20") elif x > 15: print("x is greater than 15 but less than or equal to 20") else: print("x is less than or equal to 15") |
Run the code and this will be the result
-
Python while loop
Python while loop is used to execute a block of code repeatedly as long as a certain condition is true. this is the basic syntax of a while loop.
1 2 |
while condition: # code to be executed repeatedly as long as condition is true |
This is practical example
1 2 3 4 |
x = 1 while x <= 5: print(x) x += 1 |
This will be the result
-
Python for loop
Python for loop is used to iterate over a sequence of elements, such as a list or a range of numbers. this is the basic syntax of a for loop.
1 2 |
for variable in sequence: # code to be executed for each element in sequence |
This is the practical example
1 2 3 |
languages = ["Python", "C++", "C#"] for lang in languages: print(lang) |
This will be the result
You can also use Python range() function to generate a sequence of numbers to iterate over. range() function takes three arguments, start, stop and step. if you provide only one argument, it will be interpreted as the stop value, and the start value will be assumed to be zero.
1 2 |
for i in range(1, 6): print(i) |
-
break and continue statements
So in addition to five Python control structures that we already have described, Python also provides two statements that can be used within loops to control the flow of the loop, break statement and continue statement.
Python break statement
break statement is used to exit a loop prematurely, regardless of whether the loop has finished iterating over all elements in the sequence. this is an example of break statement.
1 2 3 4 5 |
languages = ["Python", "C++", "C#"] for lang in languages: if lang == "C#": break print(lang) |
This will be the result
Python Continue Statement
Python continue statement is used to skip over an iteration of a loop, and move on to the next iteration. this is an example of Python Continue statement.
1 2 3 4 5 |
languages = ["Python", "C++", "C#"] for lang in languages: if lang == "C++": continue print(lang) |
Learn More
- Introduction to Python Classes and Objects
- Python Static Vs Instance Methods
- Python Abstraction and Encapsulation
- Python Class Inheritance and Polymorphism
- Key difference Between Python Classes and Objects
- How to Debug Python Classes and Objects
- Python Classes and Objects from Scratch
- Best Tips for Python Classes and Objects