Python Operators

Just has we had it previously, we wrote our first line of code in python and it's the most amazing feeling more like a super power. Now we gotta hit into it more as there are plenty of things to be learnt.

So unto the next is python operators, yeah verily all programming languages have an operator syntax and just with it we're looking on how python's operators are like.

Python Operators are used in performing operation in variables and values..

A quick example

print(20 * 5)

Operators in python is divided into groups namely;

  • Arithmetic operator.
  • Comparison operator.
  • Assignment operator.
  • Logical operator.
  • Identity operator.
  • Membership operator.
  • Bit wise operator

ARITHMETIC OPERATOR

This are operators which is used with numeric values to perform mathematical operations.

   +  Addition

   -   Subtraction

   *   Multiplication

   /    Division

  //    Floor division

 **    Exponential

 %     Young modulus

You'd wonder how does this work? there's no need for much worries as i am going to show a quick example on how it works, literary if you run the below codes on your PYCHARM IDE or any code editor you use, you'd definitely see an output.

x + y           
print(2 + 3)

x - y 
print(4 - 2)

x * y
print(4 * 2)

x / y
print(4 / 2)

x // y
print(4 // 2)    
#this round the division down to the nearest whole number

x ** y
print(4 ** 2)   

x % y
print(4 % 2)    
#this outputs the reminder

NB: The Print statement is used in outputting the result.

YEAH! python is getting interesting as we move onto the next...

ASSIGNMENT OPERATOR

They are used to assign values to variables such as;

 =
 x = 7   
 #this means x is equal to 7

Now there are other assignment operators in python and we'd definitely look into it and what they do. we've known that using the equality sign means assigning a value to any variable, so now i personally call it "mixed assignment" and we look for it

+=
#if this applied to any value it means there is an addition in the variable , let us see some example below

x = 7

x += 3

#the output of this would be 10 meaning 7 plus 3 
-----------------------------------------------------
-=
#a vice versa of the above one

x = 7
x -= 3
#the output will be 4 as it subtracts 7 from 3
-----------------------------------------------------
*=
#multiply assignment

x = 7
x *= 3
#the output will be 21 as it multiply 7 by 3
-----------------------------------------------------
/=
#divisible assignment

x = 10
x /= 2
#the output will be 5. as it divides 10 by 
-----------------------------------------------------
//=
#rounding up the divisible assignment

x = 7
x //= 3
#the output will be 2 as it divides 7 by 3 and it rounds it up to the nearest whole number
-----------------------------------------------------
%=
#a vice versa of the above one

x = 7
x %= 3
#the output will be 1 as it gets the remainder

There are more to the operators and i will update this article as we learn more into it.

And as promised i will drop links that i find helpful on python beginners https://www.w3schools.com/python

https://www.codecademy.com

https://github.com/Asabeneh/30-Days-Of-Python