Day 3: Python Newbie?

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

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

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.

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

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

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

--

--

Data Scientist who loves to teach machine through data.

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store