Day 3: Python Newbie?

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:

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:

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:

Comparison operators:

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

Logical operators:

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:

  • 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:

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 🙂

Data Scientist who loves to teach machine through data.

Love podcasts or audiobooks? Learn on the go with our new app.