Day 3: Python Newbie?

Adnan Khan
3 min readNov 27, 2019

--

Have you prepared a cup of coffee?Go get a cup of coffee before reading this because today we are going to discuss very interesting topic the type of operators which are mostly used in python.Let dive in types of operators first.

Operators are used to perform operations on variables and values.

Python divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators

Arithmetic Operators:

Arithmetic operators are used with numeric values to perform operations like addition, subtraction, multiplication, division, %modulus.

var1 = 20
var2 = 5
res_add=var1 + var2 #Addition among variables
res_sub=var1 - var2 #Subtraction among variables
res_mul=var * var2 #multiplication among variables
res_div=var / var2 #division among variables
res_mod=var % var2 #modulus among variables
res_mul=var // var2 #floor division among variables

Firstly we have declared two variables var1 and var2 then we perform arithmetic operations over it like addition,subtractions etc.But the point here is what’s the difference between division( / ) and floor division( // )?.The main difference between them is division( / ) will give you answer in form of float where floor division will give you answer in type integer and follow the concept of floor function. Floor function is used to return the closest integer value which is less than or equal to the specified value.

Assignment operators:

Python assignment operators are used for assigning the value of the right operand to the left operand. Assignment operators in python are (=, +=,%= , *=, /=,//= etc).

var1 = 20
var1 += 5
var1 -= 2

That’s how we use assignment operators to assign the value to left operand(var1).Lets take an example to make it more clear we already have declared var1 with value of 20.Now we perform this operation over it var1+=5 so it would simply add 5 in the existing value of var1.So, if you printout the value of var1 it would show 25 similarly,these all operation will effect the existing value of var1.

Note:

when you apply one operation on var1 then the existing value will change like when we applied var+=5 operation on var1 now the value of var1 is 25.so when you apply var1-=2 it will print 23 instead of 3.

Comparison operators:

Comparison operator are used to compare two values.These operators are some how similar to assignment operator in sign.Comparison operators are >,<,≥,≤,==,!=.These types of operators will give you output in boolean form like True or False.

var1 = 5
var2 = (2+3)
Equal_to = (var1 == var2) #True
greater_then =(var1 > var2) #False
greater_then_equal_to = (var1 >= var2)#True

Logical operators:

Logical operators in python are used for checking the conditional statement whether it is True or False.Logical operators in python are AND,OR and NOT.

AND operator → It return True only if all the conditions are true

OR operator →It returns True when any of the condition is true

NOT operator →Return true when the operand is False

var1 = 15
var2 = 25
result1= (var1 > 10 and var2 < 30) #True
result2= (var1 >5 or var2 < 20)#True
result3= not(Var > 5 and var2 < 30 ) #False

result3 here is returning False because the value of result3 is giving us True so NOT will convert it into False and vice versa.

Identity operators:

To compare the memory location of two objects, Identity Operators are used. The two identify operators used in Python are (is, is not).

  • Operator is: It returns true if two variables point the same object and false otherwise
  • Operator is not: It returns false if two variables point the same object and true otherwise
var1=[1,2,3]
var2=[1,2,3]
var3=var1
print(var3 is var1) #True
print(var3 is var2) #False

Second print statement is going to print False because identity operators same objects with same memory locations.However in this example we’re using List which we will discuss later.

Membership operators:

These operators test for membership in a sequence such as lists, strings or tuples. There are two membership operators that are used in Python.Membership operators are “in, not in”.

list1 = ["one","two","three"]
print("one" in list1)#True
print("Five" in list1) #False

So that’s it for today get practice over that and try to more examples on your own.In very first tutorial i recommended a book where a lot of examples are given solve as many as you can because if you’re not going to solve examples then these articles didn’t going to effect your skills.I’m providing the link of first articles where you can find book and some recommended course where you can learn basics of python as well.

Don’t forget to share your feedback 🙂

--

--

Adnan Khan

Data Scientist who loves to teach machine through data.