Python

Python Intermediate Conditions

Python Intermediate Conditions

This page covers the intermediate conditions of Python programming.

For Python conditions here.

It continues the previous conditions tutorial in more details. Intermediate conditions encompass:

  • operators (%, **, //, and +=)
  • “pass” statement
  • nested “if” statements
  • short-form “if” statements

Operators

Operators are mathematical operations that perform interact with variables and values.

The operators covered here are: modulus (%), exponentiation (**), floor division (//), and a short-form expression of assignment operators (+=).

Modulus (%)

The modulus operator divides a number by another and returns the remainder.

a = 9
b = 4
print(a % b)

# Output 1

Exponentiation (**)

The exponentiation operator refers to the operation of raising to a power, similar to the “pow()” built-in function.

a = 3
b = 3
print(a ** b)

# Output 27

Floor division (//)

The floor division operator divides one number by another, with the exception that it rounds down the result.

x = 21
y = 4
print(x // y)

# Output 5

Short-form operator assignment

In Python, some expressions can be simplified. This is a very common practice and many developers use the shorter version (especially with incrementing in loops).

# standard version
a = a + 1

# shorter version
a += 1

# Both are exactly the same

The above works also with other operators (e.g. -=, *=, /=, and so on).

Pass statement

In Python, in the case of an empty conditional statement, the program produces an error.

The following is an example of empty “if” statement.

x = 5
y = 1
if x > y:

# Output: IndentationError

Instead, the “pass” statement can resolve this issue. The example below will not output any errors.

x = 5
y = 1
if x > y:
    pass

# Output: 

Nested “if” statements

Nested statements simply refers to a conditional statement inside another conditional statement.

The following is an example of a “if” nested statement (“if” inside another “if”).

a = 7
if a > 0:
    print("The number is positive")
    if a > 10:
        print("The number is also higher than 10")

# Output: The number is positive
a = 15
if a > 0:
    print("The number is positive")
    if a > 10:
        print("The number is also higher than 10")

# Output: The number is positive
# Output: The number is also higher than 10

The process behind the nested “if” statements is as follows: Python checks the first condition, in the case of false, it does not execute the second “if”. But in the case where the first condition is true, then the program checks the second condition. If the second is false, it executes only the first “if” statement. If the second condition is also true, it produces both “if” statements.

Short-form “if” statement

The short-form version allows for a single line “if” statement. This is useful when the conditional statement is small. Some developers use it to improve neatness and hence readability.

a = 3
if a < 10: print (a, "is less than 10")

# Output: 3 is less than 10

If the condition is false, an empty output appears.

a = 11
if a < 10: print (a, "is less than 10")

# Output:

Next: Python Intermediate Loops

by AICorr Team

We are proud to offer our extensive knowledge to you, for free. The AICorr Team puts a lot of effort in researching, testing, and writing the content within the platform (aicorr.com). We hope that you learn and progress forward.