In this Python article we are going to learn about Python Control Structures for Conditional Statements, so Python is high level programming language that provides developers with different control structures for building complex software applications. one of the most essential control structures in Python is conditional statement, and that is used to execute code based on specific condition, so first of all let’s talk about conditional statement.
What is Conditional Statement ?
Conditional statement is a control structure that executes specific code based on whether a given condition is true or false. in Python conditional statements are created using if keyword.
This is the syntax for Python Conditional Statement, so in here condition part of the statement is a Boolean expression that evaluates to either True or False. if the condition is true, the code block indented below the if statement will be executed. if the condition is false, the code block will be skipped.
1 2 |
if condition: # code to execute if condition is true |
If-Else Statements
In many case you may want to execute a different block of code if the condition is false. for this, Python provides if-else statement.
This is the syntax for an if-else statement:
1 2 3 4 |
if condition: # code to execute if condition is true else: # code to execute if condition is false |
Nested If Statements
In some cases you may want to test multiple conditions in sequence. for this Python provides the ability to nest if statements within one another.
This is the syntax for nested if statement:
1 2 3 4 5 6 7 8 |
if condition1: # code to execute if condition1 is true if condition2: # code to execute if both condition1 and condition2 are true else: # code to execute if condition1 is true but condition2 is false else: # code to execute if condition1 is false |
elif Statements
Python also provides elif statement, which is short for else if. this statement allows you to test multiple conditions in sequence without nesting multiple if statements.
1 2 3 4 5 6 |
if condition1: # code to execute if condition1 is true elif condition2: # code to execute if condition1 is false and condition2 is true else: # code to execute if both condition1 and condition2 are false |
This is a practical example on this concept
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Prompt user to enter their username and password username = input("Enter your username: ") password = input("Enter your password: ") # Define correct username and password correct_username = "geekscoders" correct_password = "geekscoders" # Check if the input matches the correct username and password if username == correct_username and password == correct_password: print("Access granted Welcome, " + username) else: print("Access denied. Incorrect username or password.") |
In the above example we have used input function to prompt the user to enter their username and password. after that we have defined the correct username and password using variables.
Next we have used a conditional statement with if keyword to check if the input matches the correct username and password. if both conditions are true, we print a message granting access to the system. if either condition is false, we print a message denying access to the system. this is just a simple example, but it illustrates how conditional statements can be used in real world scenarios to control the flow of your code based on specific conditions.
Run the code and this will be the result