Tuesday, June 25, 2024

Secrets of Efficient Code, Everything You Need to Know About Operators in the Python Programming Language

If you are a beginner in the world of programming and remember some basic mathematical concepts, you might think; I roughly know what operators are in mathematics, but what are operators in programming? Operators are key elements in programming because they enable the execution of various operations on data, allowing programmers to build complex logical structures and perform a variety of tasks. Operators in the Python programming language are symbols or special functions used to perform operations on values or variables. They allow data manipulation in different ways, such as mathematical operations, comparisons, logical operations, and so on.

A Girl Learns Operators in Python Programming Language

A Girl Learns Operators in Python Programming Language

Essentially, it is the same thing as in mathematics, except that in computer science it is somewhat expanded for the needs of programming languages and better computer functionality. Likewise, if you compare operators in the Python programming language with those in other programming languages, you will see that operators have a similar purpose in most programming languages, but their exact implementation and syntax may vary between programming languages. For example, in the Python programming language, the identity operator is is not the same as is in the C# programming language. In the Python programming language, the is operator is used to check if two variables refer to the same object, while in the C# programming language, the is operator is used to check a variable's type.

Understanding Operators: An overview of the most important operators in Python

The Python programming language defines the division of types of operators into the following seven groups:

1. Arithmetic operators

2. Assignment operators

3. Comparison operators

4. Logical operators

5. Identity operators

6. Membership operators

7. Bitwise operators

In addition to the mentioned division of types of operators, it is essential to pay attention to operator precedence. As in any other programming language, operators in the Python programming language have precedence that determines the order of operations. They are mostly the same as in mathematics. However, when it comes to the Python programming language, there are additional operators. See the following picture of the operator precedence table.

Table of Operator Precedence in the Python Programming Language

Table of Operator Precedence in the Python Programming Language

If you have complex expressions with multiple operators, it is always good practice to use parentheses to clearly define the order of operations and avoid mistakes, even when the operator precedence is known.

arithmetic operators

Arithmetic operators are operators used to perform mathematical operations on numbers. They allow the execution of basic arithmetic operations such as addition, subtraction, multiplication, division, and modulus. When it comes to the Python programming language, this group also includes exponentiation and floor division. To demonstrate this, launch your IDE - Integrated Development Environment, find the project python_tutorial, and add a new file to the project named arithmetic_operators.py. Then, type the following code. 

"""
ARITHMETIC OPERATORS
MANUEL RADOVANOVIC - 2024-06-25
"""

number1 = int(input("Enter the first number: "))
number2 = 
int(input("Enter the second number: "))
print()

print(f"Addition: {number1} + {number2} = {number1 + number2}")
print(f"Subtraction: {number1} - {number2} = {number1 - number2}")
print(f"Multiplication: {number1} * {number2} = {number1 * number2}")
print(f"Division: {number1} / {number2} = {number1 / number2}")
print(f"Modulus: {number1} % {number2} = {number1 % number2}")
print(f"Exponentiation: {number1} ** {number2} = {number1 ** number2}")
print(f"Floor division: {number1} // {number2} = {number1 // number2}")

Run the program and if you enter the input parameters 25 and 5, you will get the following result:

Enter the first number: 25

Enter the second number: 5

 

Addition: 25 + 5 = 30

Subtraction: 25 - 5 = 20

Multiplication: 25 * 5 = 125

Division: 25 / 5 = 5.0

Modulus: 25 % 5 = 0

Exponentiation: 25 ** 5 = 9765625

Floor division: 25 // 5 = 5

What this coding looks like; you can watch it in the following video:


Python - 3. Arithmetic Operators

assignment operators

Assignment operators in the Python programming language are specific operators used to assign values to variables. The basic assignment operator is the equals sign =. It is used to assign the value on the right-hand side to the variable on the left-hand side. For example, with assignment operators, you can shorten the writing of arithmetic operations. It is best to create a small program that demonstrates assignment operators and their usage. Therefore, in our already existing project python_tutorial, create a new file and name it assignment_operators.py. Then, type the following code.

"""

ASSIGNMENT OPERATORS
MANUEL RADOVANOVIC - 2024-06-25
"""

number = int(input("Enter a number: "))
print()

x = number
print(f"Simple assignment operator: X = {number}")
number += 
5
print(f"Add and equal operator: X += 5 ... {number}")

number = x
number -= 
5
print(f"Subtract and equal operator: X -= 5 ... {number}")
number = x
number *= 
5
print(f"Asterisk and equal operator: X *= 5 ... {number}")
number = x
number /= 
5
print(f"Divide and equal operator: X /= 5 ... {number}")

print()

number = x
number %= 
5
print(f"Modulus and equal operator: X %= 5 ... {number}")
number = x
number //= 
5
print(f"Double divide and equal operator: X //= 5 ... {number}")
number = x
number **= 
5
print(f"Exponent assign operator: X **= 5 ... {number}")

print()

number = x
number &= 
5
print(f"Bitwise And Operator: &= 5 ... {number}")
number = x
number |= 
5
print(f"Bitwise OR Operator: X |= 5 ... {number}")
number = x
number ^= 
5
print(f"Bitwise XOR Operator: X ^= 5 ... {number}")

print()

number = x
number >>= 
5
print(f"Bitwise right shift assignment operator: X >>= 5 ... {number}")
number = x
number <<= 
5
print(f"Bitwise left shift assignment operator: X <<= 5 ... {number}")

Run the program and if you enter the input parameter 24; you will get the following result:

Enter a number: 24

 

Simple assignment operator: X = 24

Add and equal operator: X += 5 ... 29

Subtract and equal operator: X -= 5 ... 19

Asterisk and equal operator: X *= 5 ... 120

Divide and equal operator: X /= 5 ... 4.8

 

Modulus and equal operator: X %= 5 ... 4

Double divide and equal operator: X //= 5 ... 4

Exponent assign operator: X **= 5 ... 7962624

 

Bitwise And Operator: &= 5 ... 0

Bitwise OR Operator: X |= 5 ... 29

Bitwise XOR Operator: X ^= 5 ... 29

 

Bitwise right shift assignment operator: X >>= 5 ... 0

Bitwise left shift assignment operator: X <<= 5 ... 768

What this coding looks like; you can watch it in the following video: 


Python - 4. Assignment Operators

comparison operators

Comparison operators in the Python programming language are operators used to compare values and return a result in the form of a boolean value: True or False. These operators simply allow the comparison of different variables and values. They are extremely useful and you will often use them for conditional code execution, as well as for comparing values in control structures like if-else statements and all types of loops. Add a new file to our project python_tutorial and name it comparison_operators.py. Then, type the following code.

"""

COMPARISON OPERATORS
MANUEL RADOVANOVIC - 2024-06-25
"""

number1 = int(input("Enter the first number: "))
number2 = 
int(input("Enter the second number: "))
print()

print(f"Equal: {number1} == {number2} ... {number1 == number2}")
print(f"Not equal: {number1} != {number2} ... {number1 != number2}")
print(f"Greater than: {number1} > {number2} ... {number1 > number2}")
print(f"Less than: {number1} < {number2} ... {number1 < number2}")
print(f"Greater than or equal to: {number1} >= {number2} ... {number1 >= number2}")
print(f"Less than or equal to: {number1} <= {number2} ... {number1 <= number2}")

Run the program and if you enter the input parameters 25 and 5, you will get the following result:

Enter the first number: 25

Enter the second number: 5

Equal: 25 == 5 ... False

Not equal: 25 != 5 ... True

Greater than: 25 > 5 ... True

Less than: 25 < 5 ... False

Greater than or equal to: 25 >= 5 ... True

Less than or equal to: 25 <= 5 ... False

What this coding looks like; you can watch it in the following video:


Python - 5. Comparison Operators

other operators

Logical operators – Logical operators in the Python programming language are used to perform logical operations on boolean values True and False. Logical operators simply combine and manipulate boolean values using the operators and, or, or not.

Identity operators – Identity operators in the Python programming language are used to compare two identities, i.e., whether two variables or objects have reference to the same object. This is done using the operators is and is not. It's important to note that these operators are different from the equality operator ==, so be mindful not to confuse them in your coding. Identity operators are commonly used when comparing variables that reference instances of classes or complex objects.

Membership operators – Membership operators in the Python programming language are operators used to check if an element belongs to a collection such as lists, arrays, sets, etc. There are only two operators for this purpose: in and not in. They are often used when coding searches or filtering.

Add a new file to our project python_tutorial and name it logical_identity_membership_operators.py. Then, type the following code.

""

LOGICAL, IDENTITY AND MEMBERSHIP OPERATORS

MANUEL RADOVANOVIC - 2023-07-07

"""

number1 = int(input("Enter the first number: "))


number2 = int(input("Enter the second number: "))

print()

# Logical operators


print(f"and: X < 10 and Y < 100 ... {(number1 < 10) and (number2 < 100)}")

print(f"or: X < 10 or Y < 8 ... {(number1 < 10) or (number2 < 8)}")

print(f"not: not(X < 10 and Y < 100) ... {not(number1 < 10) and (number2 < 100)}")

print()


# Identity operators


print(f"is: X = Y ... {number1 is number2}")

print(f"is not: X = Y ... {number1 is not number2}")

print()


# Membership operators

Y = [1, 3, 5, 8]


print(f"in: X in Y ... Y = [1, 3, 5, 8] {number1 in Y}")

print(f"not in: X not in Y ... Y = [1, 3, 5, 8] {number1 not in Y}")


Run the program and if you enter the input parameters 25 and 4 you will get the following result:


Enter the first number: 25

Enter the second number: 4

 

and: X < 10 and Y < 100 ... False

or: X < 10 or Y < 8 ... True

not: not(X < 10 and Y < 100) ... True

 

is: X = Y ... False

is not: X = Y ... True

 

in: X in Y ... Y = [1, 3, 5, 8] False

not in: X not in Y ... Y = [1, 3, 5, 8] True

What this coding looks like; you can watch it in the following video:


Python - 6. Logical, Identity and Membership Operators

As for Bitwise operators – shift operators, we will cover them in an advanced tutorial when we learn about bit shifting. For now, it will be great once you fully understand the text and examples provided.











No comments:

Post a Comment