Operators

Python SELF EN
Level 2 , Lesson 2
Available

2.1 Mathematical Operators

We're familiar with the 4 standard operators for dealing with numbers: +, -, * and /. But there are also a few more interesting ones. Let's check out some features of mathematical operators in Python.

Regular Division — Operator «/»:

Divides the first number by the second, result is always a float. Example:


                result = 5 / 2  # result will be 2.5
        

Integer Division — Operator «//»:

Divides the first number by the second, result is an integer [whole part of the number].


                result = 7 // 2  # result will be 3
        

Remainder — Operator «%»:

Returns the remainder of the division of the first number by the second. Example:


                result = 5 % 3  # result will be 2
        

Exponentiation — Operator «**»:

Raises a number to a power.


                result = 5 ** 3  # result will be 125
        

2.2 Shortcut Assignment Operators

Besides regular assignment operations, Python allows the use of shortcut notation. Example:


                x = 5
                x += 1 
        

This is fully equivalent to the notation:


                x = 5
                x = x + 1 
        

Main shortcut assignment operators:

Addition and Assignment ( += ): increases the variable's value by the right operand.


                x = 5
                x += 3  # x is now 8

        

Subtraction and Assignment ( -= ): decreases the variable's value by the right operand.


                x = 5
x -= 3  # x is now 2
        

Multiplication and Assignment ( *= ): multiplies the variable by the right operand.


                x = 5
x *= 3  # x is now 15
        

Division and Assignment ( /= ): divides the variable by the right operand, result is always a float.


                x = 5
x /= 2  # x is now 2.5
        

Integer Division and Assignment ( //= ): divides the variable by the right operand, result is an integer.


                x = 5
x //= 2  # x is now 2
        

Remainder and Assignment ( %= ): assigns the remainder of the division by the right operand to the variable.


                x = 5
x %= 3  # x is now 2
        

Exponentiation and Assignment ( **= ): raises the variable to the power specified by the right operand.


                x = 5
x **= 3  # x is now 125
        

2.3 Comparison Operators

Besides assigning variables, Python allows you to compare them. Special comparison operators are used for this.

Comparison operators in Python compare two values and return a boolean value True or False depending on the outcome of the comparison. These operators are fundamental for making decisions in the program and controlling the flow of execution.

Equality ( == ): checks if two values are equal.


        print(5 == 5)  # Outputs: True

Not Equal ( != ): Checks if two values are different.


        print(5 != 5)  # Outputs: False

Greater Than ( > ) and Less Than ( < ): compare two values to determine if one value is greater than or less than the other.


        print(5 > 3)  # Outputs: True
        print(5 < 3)  # Outputs: False

Greater Than or Equal ( >= ) and Less Than or Equal ( <= ): Compare two values to check if one value is greater than or equal to the other, or less than or equal to the other.


        print(5 >= 5)  # Outputs: True
        print(5 <= 4)  # Outputs: False

These operators are widely used in conditional expressions, loops, and other places to create logical conditions that help decide which blocks of code should be executed based on the satisfaction of certain conditions.

2
Task
Python SELF EN, level 2, lesson 2
Locked
Height in Feet and Inches
Height in Feet and Inches
2
Task
Python SELF EN, level 2, lesson 2
Locked
Chess Problem
Chess Problem
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION