Conditional Statements
What is a conditional statement?
In Python, conditional statements allow you to execute different blocks of code
based on certain conditions. These conditions are evaluated using logical
operators, and the result of the evaluation determines which block of code will be
executed. Conditional statements in Python are typically implemented using the
"if" statement, which is followed by an expression that evaluates to either True or
False.
Types of conditional statements: If statements
if condition:
# Code block to be executed if the condition is True
The condition is an expression that is evaluated to determine its truth value. If the
condition is True, the code block indented below the "if" statement is executed. If
the condition is False, the code block is skipped, and the program moves on to the
next statement.
If-else statement
if condition:
# Code block to be executed if the condition is True
else:
# Code block to be executed if the condition is False
If the condition is True, the first code block is executed,
and if the condition is False, the second code block
(after the "else" keyword) is executed.
If-elif-else statements
if condition1:
# Code block to be executed if condition1 is True
elif condition2:
# Code block to be executed if condition1 is False and condition2 is True
else:
# Code block to be executed if both condition1 and condition2 are False
In this example, if condition1 is True, the first code block is
executed. If condition1 is False and condition2 is True, the
second code block is executed. Otherwise, if both condition1
and condition2 are False, the third code block (after the "else"
keyword) is executed.
Nested If Statements
if condition1:
# program you want to execute without the second condition needing to be met
if condition2:
# program you want to execute with the nested if statement
Nested If statements are If statements inside of If statements, they are useful when you want
to check for multiple conditions but you still want to run other code when only some of the
conditions are met, which you cannot do with an “and” operator
Logical Operators
Python provides several logical operators that can be used to form conditions:
1. Comparison Operators:
● Equal to: ==
● Not equal to: !=
● Greater than: >
● Less than: <
● Greater than or equal to: >=
● Less than or equal to: <=
2. Logical Operators:
● Logical AND: and
● Logical OR: or
● Logical NOT: not
Example of operators being used
x=5
y = 10
if x > 0 and y > 0:
print("Both x and y are positive.")
elif x > 0 or y > 0:
print("At least one of x and y is positive.")
else:
print("Both x and y are non-positive.")
In this example, the first condition x > 0 and y > 0 checks if both x and y are positive. The second
condition x > 0 or y > 0 checks if at least one of them is positive. If neither condition is true, the code
block in the "else" section is executed.
Examples in my code #1: Logical Operators
This is an example of Conditional Statements and Logical Operators being used in my code. This code is
for the computer controlled hitman's sniper attack which tracks the x and y location of the player and if
the sniper is not locked on to the player’s location it will move to track the player.
if snipeX + 800 >= pX + 50:
snipeX -= snipeSpeed
elif snipeX + 800 <= pX + 50:
snipeX += snipeSpeed
if snipeY + 600 >= pY + 60:
snipeY -= snipeSpeed
elif snipeY + 600 <= pY + 60:
snipeY += snipeSpeed
Example in my code #2: Using if statements with Pygame
An example of if statements being used with pygame is as follows
if [Link] == [Link]:
[Link]()
[Link]()
This code checks for the event of the user clicking the X button in the top right
corner and then closes the pygame window when this condition is met.
Example in my code #3: Using conditional statements to animate my character
if animCount >= 10 and not freezePlayer:
player = frames[playerFrame]
playerFrame += 1
player = [Link](player, (100, 120))
if playerFrame > 2:
playerFrame = 0
animCount = 0
This code checks for the animCount variable (which increases when you move with the arrow keys) and
once it reaches 10 or more it increases the playerFrame variable which is used to select which object in
the frames list will be updates as the current frame of the player’s animation cycle.