Comprehensive Python

Table Of Contents:

Python Operators and Methods

Many are the times you guys heard me talk about the term Operator in the previous lessons.
Oh, I have just started with the noise, let me first say : I welcome you all to this other part of the python series... I hope you will be focused as I dismantle the Python Operators piece-by-piece.

What Are Operators?

Python Operators are the Symbols and terms that we use to perform or cordinate the operations in the Python Program. Every Programming language comes with the operators, and more importantly, most operators are the same in most programming languages. In this tutorial, we will look at Python operators one-by-one in detailed explaination including the presidence.

Python Operators

Operators are categorized into different groups depending on the operation they perform and some of which might be
Numeric/Mathematical Operators used to perform mathematical computations within the program; Comparison Operators for comparing the similarities or differences of two different elements or statements etc.
In below verses, let's dismantle each and everyone of them respectively.

Assignment Operators ( =, +=, -=, *=, /=, **=, //=, &=, |=, ^=, >>= and <<= )

The Assignment Operators are used to assign values to a variable. Care must be taken when usign the assignment operators,
because they do not actually mean equal to as that used in mathematics at the high school.
For instance: >>> a = 20 does not actually mean a letter 'a' is literally equal to 20, rather, it gives the value of 20 to a variable name 'a'.

As can be seen on the above bracket, Assignment operators may require their own chapter, so we will not cover the whole thing here.
If you are an active learner, you may search the provided hint.

Numeric Operators

As already said in the begining, the Numeric Operators are actually designed to work as the name suggests.
We use them when performing mathematical computations/calculations. No program ever exists except the numeric operators are included in them. The numeric operators may include the symbols below:

Addition operator ( + )
Without hesitate, I am sure everyone reading this article is familiar with addition or plus operator.
Actually, it is exactly the same operator as that we use at the secondary school to add two values, e.g numbers.
The only difference in programming is that we use addition operator for many purposes, including concatenating Strings and many of which we will see them as we go along our course.

Minus operator ( - )
Just like the additional operator above, I am pretty sure that we all are aware of the "Minus" Operator at our high schools.
This one, unless used with logical reasoning works exactly that learnt in our early academic years and are used to decrement values.
To understand it, look at the below example:

# Assign value to a variable number
>>> number = 50
# Subtract 20 from 50 and print results
>>> print (number - 20) #30 is the answer

Multiplication operator ( * )
Be careful with the multiplication operator! The multiplication operator in programming is not as that we know in our early mathematical classes.
In programming, we do not use the X for multiplication and rather uses the Asteric (*) to do multiplications.
In python, multiplication operators carry so many operations than just multiplying numbers, and they include Repeating Strings etc.
The proper usage of the operators is as in below example:
print (10 * 2) # Answer is 20
Doubling the multiplication operator will lead to mathematical power (Exponential Operator), as in 2^3 can be said to be >>> 2**3

Division operator ( / )
What is mathematics if division is not included? The division operator carries the division operations over two numbers or values.
The division operator in programming is the forward slash (/) and not that of which we are familiar with, the ( รท ).
Below is an example of how we would use the division operator to devide two values:

# Assign value 40 to a variable number
>>> number = 40
# devide 40 by 2 and print results
>>> print (number / 2) #20 is the answer

Be notified that in python, there is also floor division, sometimes called integer division used to discard dismal points. That is: 5 // 2 = 2 and not 2.5

Modulus operator ( % )
Modulus operator (Percentage sign) is also a mathematical operator. Despite being used for many purposes in Python, the sign basically calculates the remainder of two integer divisions.
Hey sir, what the heck are you supposed to mean by that?:
I understand! Assuming we have a sum of 5 mangoes, that, being kind, we would like to share among 3 innocent kids. Do we except that will fit? a big NO! Two mangoes will remain, as they will not meet the whole lot of kids, therefore...

# Assign value 5 to a variable mangoes
>>> mangoes = 5
# devide 5 by 3 and print results
>>> print (mangoes % 3) #2 is the remainder

Comparison Operators

Lucky we... we are at the point where we can mould our logic. With comparison operators, we are lucky we can achieve just that.
Comparison operators gives us the fortune to check if the entities passess the test. Below is a collection of the Python's comparison operators:

Equality Operators ( ==, !=, >, <, >=, <= )
The Equality operators are used to determine (check) whether two entities are equal or otherwise.
The == (Is Equal) operator is used to check whether two entities are quite equal.
It turns True if the entities are equal and False otherwise.
The != (Not Equal) operator is the oposed of is equal operator.
This turns True if two entities are never equal, and if they are equal, then False will be thrown. Taken:

# Assign value 50 to a variable number and do comparisons
>>> number = 50
>>> print (number == 2) #False because 50 is never equal to 2
>>> print (number != 2) #True because 50 is not 2

The > actually checks if the left entity is greater than the right one. Where as < does the opposite of that.
The >= actually checks if the left entity is greater than or equal to the right entity. Where as <= checks if left entity is less than or equal to the right one.

Logical Operators ( and, or, not )
Python's logical operators are used for logical computaions.
The and operator is used to join propotions of logical statements.

# Assign value 50 to a variable number
>>> number = 50
>>> print (number == 2 and number < 100) #False because 50 is never equal to 2, even though last one is true
>>> print (number != 2 and number > 20) #True because 50 is not 2 and 50 is greater than 20

This means, all the statements joined by and must be true.
If you want to check if atleast one of the passed statements is true, then that's where or operator falls into the scene.
The not on the other hand is used to negate the result of an expression.
On our above example:

# Assign value 50 to a variable number
>>> number = 50
>>> print (number == 2 or number < 100) #True because 50 is less than 100 though first expression is False
>>> print (number != 2 and not number > 20) #False, even though it is true that 50 is greater than 20, but "not" operator negates it

Identity Operators ( is, is not )
The Identity operators, just as the name suggests, checks the identity of the entities.
This operator is not same as the comparison operators, neither does it mean equal or not equal, rather it checks if an entities points to the same object (memory location).
The is operator checks if two entities all have simillar memory address, and return True if it is so, or false otherwise.
Whereas is not does the opposite.

x = 20
>>> y = 20
>>> print ( x is y) #True because all points to memory address 20 (1667606752)
>>> print (x is not y) #False because x and y have no different memory location

Membership Operators ( in, not in )
The membership operators are used to check if an object is available in a collection (Lists, Objects, Tuples, Sets).
If an item is available in a collection, the in operator will return True, and false otherwise.
not in, on the other hand is the opposite of the above operator.
Mote that the membership in dictionary uses Keys and not Values.

# Make a variable person with its details
>>> person = {"name" : "Amin", "surname" : "Matola"}
>>> print ( name in person ) #True
>>> print ("Amin" in person ) #False

Bitwise Operators ( &, |, ^, ~, >>, << )
This is another group of operators, which is, unfortunately not frequently used for consumers like you. ☺
As a result, we will not cover those in this article. You are not limited to search them online for further understanding of python operators.

Ladies and Gentlement, that marks the end of our operators tutorial. See you in the next tutorial