Day 3: Python Newbie?

Operators are used to perform operations on variables and values.

  • 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

Assignment operators:

var1 = 20
var1 += 5
var1 -= 2

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:

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:

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

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
Adnan Khan

Adnan Khan

Data Scientist who loves to teach machine through data.

More from Medium

Hands on Python programming;

Advanced Concepts of Python

What is Python Bytecode?