Conditional statements

Python Mastery: From Beginner to Expert - Sykalo Eugene 2023

Conditional statements
Basics of Python

Introduction to Conditional Statements

What are Conditional Statements?

Conditional statements are used in programming to make decisions based on certain conditions. They allow the program to execute different code depending on whether a specified condition is true or false.

Why are they important in Python?

Python, like many other programming languages, uses conditional statements extensively to control program flow. By using conditional statements, you can create programs that are more flexible and can respond to a wider range of inputs.

Conditional statements are particularly important in Python because they allow you to create programs that are easier to read and understand. Python's syntax is designed to be easy to read and write, which makes it an ideal language for beginners. However, this also means that it is easy to create complex programs that are difficult to follow. By using conditional statements, you can break your code down into smaller, more manageable chunks, making it easier to understand and debug.

If Statements

Syntax and Structure

In Python, an if statement is used to check whether a certain condition is true or false. If the condition is true, the code inside the if statement is executed. If the condition is false, the code inside the if statement is skipped.

The basic syntax of an if statement is as follows:

if condition:
 # code to be executed if condition is true

Examples of If Statements

Here is an example of an if statement that checks whether a variable is greater than 10:

x = 15

if x > 10:
 print("x is greater than 10")

In this example, the condition x > 10 is true, so the code inside the if statement is executed and the message "x is greater than 10" is printed to the console.

Nested If Statements

You can also use nested if statements to check multiple conditions. Here is an example:

x = 15
y = 20

if x > 10:
 if y > 15:
 print("x is greater than 10 and y is greater than 15")

In this example, both conditions x > 10 and y > 15 are true, so the code inside the nested if statement is executed and the message "x is greater than 10 and y is greater than 15" is printed to the console.

It is important to note that you can use as many nested if statements as necessary to check multiple conditions, but be careful not to overcomplicate your code.

Else Statements

Syntax and Structure

In Python, an else statement is used in conjunction with an if statement to execute code when the condition specified in the if statement is false. The basic syntax of an else statement is as follows:

if condition:
 # code to be executed if condition is true
else:
 # code to be executed if condition is false

Examples of Else Statements

Here is an example of an if-else statement that checks whether a variable is greater than or equal to 10:

x = 5

if x >= 10:
 print("x is greater than or equal to 10")
else:
 print("x is less than 10")

In this example, the condition x >= 10 is false, so the code inside the else statement is executed and the message "x is less than 10" is printed to the console.

Combining If and Else Statements

You can also combine if and else statements to create more complex logic. Here is an example:

x = 15

if x > 10:
 print("x is greater than 10")
else:
 print("x is not greater than 10")

print("This line of code will always execute")

In this example, the condition x > 10 is true, so the code inside the if statement is executed and the message "x is greater than 10" is printed to the console. The code inside the else statement is skipped. The final line of code is executed regardless of whether the condition is true or false.

It is important to note that an else statement must always be preceded by an if statement. If you want to execute code when multiple conditions are false, you can use an elif statement, which is covered in the next section.

Elif Statements

Syntax and Structure

In Python, an elif statement is used to check additional conditions after the if statement. If the condition specified in the if statement is false, the program moves on to the elif statement to check whether its condition is true. If the condition is true, the code inside the elif statement is executed. If the condition is false, the program moves on to the next elif statement or the else statement, if one is present.

The basic syntax of an elif statement is as follows:

if condition1:
 # code to be executed if condition1 is true
elif condition2:
 # code to be executed if condition2 is true and condition1 is false
else:
 # code to be executed if both condition1 and condition2 are false

Examples of Elif Statements

Here is an example of an if-elif-else statement that checks whether a variable is positive, negative, or zero:

x = -5

if x > 0:
 print("x is positive")
elif x < 0:
 print("x is negative")
else:
 print("x is zero")

In this example, the condition x < 0 is true, so the code inside the elif statement is executed and the message "x is negative" is printed to the console. If x were positive, the code inside the if statement would be executed. If x were zero, the code inside the else statement would be executed.

Combining If, Else, and Elif Statements

You can combine if, else, and elif statements to create more complex logic. Here is an example:

x = 15

if x > 10:
 print("x is greater than 10")
elif x < 10:
 print("x is less than 10")
else:
 print("x is equal to 10")

print("This line of code will always execute")

In this example, the condition x > 10 is true, so the code inside the if statement is executed and the message "x is greater than 10" is printed to the console. The code inside the elif and else statements is skipped. The final line of code is executed regardless of whether the condition is true or false.

It is important to note that you can use as many elif statements as necessary to check multiple conditions, but be careful not to overcomplicate your code.

Comparison Operators

Comparison operators are used to compare two values and return a Boolean value (True or False) based on the result of the comparison. The following table shows the different types of comparison operators in Python:

Operator Description
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Examples of Comparison Operators

Here are some examples of using comparison operators in Python:

x = 10
y = 5

print(x == y) # False
print(x != y) # True
print(x > y) # True
print(x < y) # False
print(x >= y) # True
print(x <= y) # False

In this example, the variable x is assigned the value of 10 and the variable y is assigned the value of 5. The comparison operators are then used to compare the values of x and y, and the results are printed to the console.

It is important to note that comparison operators can also be used to compare strings, lists, and other data types in Python. When comparing strings, Python compares the ASCII values of each character in the strings.

Logical Operators

Logical operators are used to combine multiple conditions and return a Boolean value based on the result of the combination. The following table shows the different types of logical operators in Python:

Operator Description
and Returns True if both conditions are true
or Returns True if at least one condition is true
not Returns the opposite of the condition

Examples of Logical Operators

Here are some examples of using logical operators in Python:

x = 10
y = 5
z = 7

print(x > y and x > z) # True
print(x > y or x < z) # True
print(not x > y) # False

In this example, the variables x, y, and z are assigned values, and the logical operators are used to combine conditions and return Boolean values.

It is important to note that when using logical operators, Python evaluates the conditions from left to right and stops as soon as the result of the operation is clear. This is known as short-circuiting.

By understanding and utilizing comparison and logical operators in your conditional statements, you can create more complex and powerful programs in Python.