Skip to main content

Command Palette

Search for a command to run...

Python Operators: A Day 8 Exploration

Updated
11 min read
Python Operators: A Day 8 Exploration
A

I'm Archana, pursuing Graduation in Information technology and Management. I'm a fresher with expertise in Python programming. I'm excited to apply my skills in AI/ML learning , Python, Java and web development. Looking forward to collaborating and learning from industry experts.

Introduction:

Welcome to Day 8 of my Python journey!

Today, I ventured into the realm of Operators.

These concepts are essential for code writing

operators :

what is expression

what is operator

what is operand

what is operators rules for operation difference between c, c++, java & python operators ?

collection of operator & operand is called as Expression.

ex : res = 5 + 3 *6 - 8

Operator : it is symbol which perform some task using operand.

ex : =, +, -,*

Operand : it may be a variable or constant

ex : res, 5, 2, 3

constant : value can't be changed

ex : pi, log2, 5

variable : variable can be change

ex : res

* The rules to naming any variable, function, class & object is called as identifier.

rules to make a variable / function / class / object :

. using A - Z, a - z, 0 - 9

. no space & symbol are allow, only one can be allow _ (underscore)

. 1st letter must be a char but not a digits .

. all keyword can't be a identifiers.

Variable has no fix length (fix in tcc compiler) choose the max option.

Types of Operator ;

. using operands :

. unary : it works with one operand

+, -, not, ~

. binary : it works with two operands

+, -, , /, %

. ternary : it works with three / more than two operands

Conditional operators

syntax :

< True value> if condition else <False value > #(T, F capital)

. using operation :

. Arithmetic : +, -, ** , \*, /, //, %

. relational : >, <, >=, <=, ==, !=

. bitwise : &, |, ^, ~, >>, <<

. conditional :

. logical : and, or, not

. assignment : =

&

. compound assignment : +=, -=, *=, %=, <<=, >>=, !=, /=, ^=, &=

. membership :

in , not in

. identity :

is, is not


extra operators which is not support by c, c++, java

**, //

in, not in

is, is not

slicing

. Arithmetic: +, -,

* , /, % (modular, mod), // (floor division)

** \ (power)

difference between /, //, %

example :

print ("5/2")

5/2 (ans)

print (" 5/2 =", 5/ 2)

5/2 = 2.5 (ans)

print ("4/2 =", 4/ 2)

4/2 = 2.0 (ans)

division vs floor operator :

print ("5/2 =", 5/2)

5/2 = 2.5 (ans)

print ("5//2 =", 5//2)

5//2 = 2 (ans)

  • mod cannot be works with float value in c, c++, java but in python it will be allow.

print ("5 % 2 =", 5 % 2)

5 % 2 = 1 (ans)

print ("14 % 3 =", 14 % 3)

14 % 3 = 2 (ans)

print ("5.0 % 2 =", 5.0 % 2)

5.0 % 2 = 1.0 (ans)

difference between & *

** is more powerful than *

**works from RIGHT to LEFT

example 1 :

print ("2 **2** 2 = ", 2 ** 2 ** 2)

2 **2 ** 2 = 16 (ans)

example 2 :

print ("2 *3 ** 2 = ", 2*3 ** 2)

2 *3 ** 2 = 18 (ans)

print (" (2 * 3) ** 2 = ", (2**3 )** 2)

(2**3)** 2 = 36 (ans)

print ("2*( 3 ** 2 ) = ", 2*( 3 **2) )

2 *( 3 ** 2 ) = 18 (ans)

print ("2 ** 3 * 2 ) = ", 2 ** 3 * 2) )

8 * 2 = 16 (ans)

example :

#wap to find last digit of a number where number is given by user , after find last digit delete that digit & display new number .

no = 67891230 (SUPPOSE)

lastdigit : 0

no = 6789123

PROGRAM :

no = int (input ("enter a number"))

ld = no % 10

no = no // 10

print ("last digit", ld)

print ("after delete last digit number =", no)

OUTPUT :

enter a number1234890

last digit 0

after delete last digit number = 123489

. relational / comparison operators :

\>, <, >=, <=, ==, !=

. it return bool type of objects.

. in python True & False is bool object.

. any non zero value is represent as True object & zero is represent as False object.

. python relational operator support chainning concept.

example :

print ("5 > 6 ", 5 > 6) # False

print ("5 == 5", 5 == 5) # True

print ("6 != -6") # False

print ("6 == 6") # True

print ("java" > "python") # False

print ("cat" > "tiger") # False

print ("cat" > "Tiger") # True

chainning :

print ("1==1 ==1", 1==1 ==1) #(1==1) and (1==1) #True

print ("2==0 ==0", 2==0 ==0) #(2==0) and (0==0) #False

print ("5==5 ==5", 5==5 ==5) #(5==5) and (5==5) #True

print (5 > 3 != 4 == 4 < 10 > 6 == 6 != 6 ) #False (T T T T T T F)

  • In AND operator if one statement is FALSE then result will be False.

  • A: 65, Z : 90 (ascii value)

  • a : 97, z : 122

Bitwise operator :

& : bitwise and

| : bitwise or

^ : bitwise xor

~ : 1s complement

\>>: right shift

<< : left shift

. bitwise means binary digits.

(i/p) input (decimal) -> convert to binary -> operation -> convert to decimal -> output(o/p)

& : if all bit are 1 then 1 otherwise 0

| : if any bit are 1 then 1 otherwise 0

^ : if all bit are same then 0 otherwise 1

~ : 1s complement

res = -n -1 (n is given no)

\>>: right shift

res= no / 2 ** shift bit

<< : left shift

res= no *2 ** shift bit

example 1 :

print (" 5 & 3", 5 & 3)

print (" 5 | 3", 5 | 3)

print (" 5 ^ 3", 5 ^ 3)

output

5= 101

3= 011

output analysis :

& 001 = 1

| 111 = 7

^ 110 = 6

example2 :

print (" 68 & 8", 68 & 8) # 0

print (" 12 | 17", 12 | 17) #29

print (" 21 ^ 29", 21 ^ 29) #8

output

68= 1000100

8= 0001000

output analysis :

& 0000000 = 0

17= 10001

12= 01100

| 11101 = 29

21= 10101

29= 11101

^ 01000 = 8

example 3 :

print ("~ 23=", ~23 ) # -23 -1 = -24

print ("~ -57=", ~ -57) # - (-57) -1 = 56

print (" 5 >> 2=", 5 >> 2) # 1

print (" 5 << 2=", 5 << 2) #20

output

no / 2 ** shift

5 / 2 ** 2

5 / 4 = 0

5 * 4 = 20

Conditional operator

(not actually avaliable in python) :

( if else means condition making but we can write in one line so we are saying conditional opeartor )

C / C++ / java syntax :

condition ? True value : False value

python syntax :

<True expression> if condition else <False condition>

example 1 ;

#check a number is +ve or -ve

no = int (input ("Enter a number "))

print ("+ve" if no > 0 else "-ve")

example 2 :

#check max between two number

n1 = int (input ("Enter no1 value "))

n2 = int (input ("Enter no2 value "))

print ("n1 is max" if n1 > n2 else "n2 is max")

logical ;

. if group of conditions are given then used logical operators

. in logical and if all groups are true then value will be True otherwise False

. in logical or if any groups is True then value is True otherwise False

. in not it alter the case

example 1 :

print ("5 > 3 and 5 != 6", 5 > 3 and 5 != 6) #True

print ("5 > 3 or 5 != 6", 5 > 3 or 5 != 6) #True

print ("5 and 7=", 5 and 7) #7

print ("5 or 7=", 5 or 7) #5

print ("0 or 7=", 0 or 7) #7

print ("0 and 7=", 0 and 7) #0

Assignment

-> =

&

Compound Assignment

-> +=, -=, \=, %=, /=, *=, //=, &=, =, ^=, >>=, <<=

it works right to left.

#swap between two number (using 3rd variable)

no1 = int (input ("enter no1 value "))

no2 = int (input ("enter no2 value "))

print ("before swap no1 =", no1, "no2 =", no2)

temp = no1

no1 = no2

no2 = temp

print ("after swap no1 =", no1, "no2 =", no2)

OR (using arithmetic operator )

no1 = int (input ("enter no1 value "))

no2 = int (input ("enter no2 value "))

print ("before swap no1 =", no1, "no2 =", no2)

no1 = no1 + no2

no2 = no1 - no2

no1 = no1 - no2

print ("after swap no1 =", no1, "no2 =", no2)

OR (using bitwise operator )

no1 = int (input ("enter no1 value "))

no2 = int (input ("enter no2 value "))

print ("before swap no1 =", no1, "no2 =", no2)

no1 = no1 ^ no2

no2 = no2 ^ no1

no1 = no1 ^ no2

print ("after swap no1 =", no1, "no2 =", no2)

OR (using positional mechanism / one line )

no1 = int (input ("enter no1 value "))

no2 = int (input ("enter no2 value "))

print ("before swap no1 =", no1, "no2 =", no2)

no1, no2 = no2, no1

print ("after swap no1 =", no1, "no2 =", no2)

OR (using user input )

no1, no2 = 6,7

print ("before swap no1 =", no1, "no2 =", no2)

no1, no2 = no2, no1

print ("after swap no1 =", no1, "no2 =", no2)

OR (using compound assignment operator )

no1 = int (input ("enter no1 value "))

no2 = int (input ("enter no2 value "))

print ("before swap no1 =", no1, "no2 =", no2)

no1 ^= no2

no2 ^= no1

no1 ^= no2

print ("after swap no1 =", no1, "no2 =", no2)

Compound assignment

x = 10

print (x) #10

#x = x + 1

x += 10

# print (x) #20

#Increment operator in python x += 1

#Decrement operator in python x -= 1

Membership operators :

in , not in

. it check / search object is aval or not, if aval then return True otherwise False.

. it return bool type object in python True & False is bool object

. it works with iterable object, in iterable object if member is aval then return True otherwise False.

. , but for noniterable object it raise an error (object is not iterable)

. list, tuple, bytes, bytearry, set, frozenset, dictonary(dict) & string are iterable object.

but int, float, complex, None, bool are non iterable object.

. type () is used to get data type / class type

example1 :

obj = [10, 20, 4.5, 7, 0, -3, "pi", 10 ] #list

print (type (obj), obj) # <'class type'>

obj = (10, 20, 4.5, 7, 0, -3, "bbsr", 20 ) #tuple

print (type (obj), obj) # <'class type'>

obj = {10, 20, 4.5, 7, 0, -3, "java" , 20} #set

print (type (obj), obj) # <'class type'> # no repetation, orderly manner

obj = "hello python" #string

print (type (obj), obj) # <'class type'>

obj = 13246882469 #integer

print (type (obj), obj) # <'class type'>

iterable & non iterable

print ('P' in "python") #False

print ('p' in "python") #True

print ('on' in "python") #True

print ('no' in "python") #Flase

print (20 in [10, -20, 30, 40, 0, 100]) #False

print (20 not in [10, -20, 30, 40, 0, 100]) #True

#print (2 in 12339835445) #TypeError: argument of type 'int' is not iterable

identity operator

. is , is not

. it return bool type of value

. in python True & False is bool object

. it return True if two object address are same otherwise False

. if two object value will be same and both are immutable object then address will be same.

. if two object are muttuable then value may be same or different but return False .

. in C, C++, Java for n number of object will be same then it allocate N times memory but in python for N number of object value will be same then it allocate one time memory.

( if all objects are immutable.)

. in python int, float, complex, None, bool, tuple, string are immutable object.

. but list [ ], set { } , dictionary are muttable object.

example :

x = 10

y = 20

z = 10

print (x is z) #True

print (y is z) #False #immutable

x = 1000

y = 1000

print (x is y) #True

x = -20000

y = -20000

print (x is y) #True

x = [10, 20, 30]

y = [10, 20, 30]

print (x is y) #False #muttable

x = "python"

y = "java"

z = "java"

print (x is y) #False

print (z is y ) #True #immutable

x = 5+5

y = 100 - 90

print (x is y) #True

identity operator is used to compare id ( ADDRESS ) upto -5 tp 256 the address will be same in interactive mode. But there is no any range for script mode.

Rules for Operator

PRECEDENCE

if multiple operators aval in an expression which operator will execute first that will be decided by precedence.

ASSOCIATIVITY

if multiple operators enjoy same precedence then the order of evaluation will be decided by associvity.

left to right

right to left

PRECEDENCE TABLE

challenges:

  1. Understanding operators.

  2. Handling errors.

Resources:

  1. Official Python Documentation: Operators

  2. W3Schools' Python Tutorial: Operators

  3. Scaler's Python Course: operators

Goals for Tomorrow:

  1. Explore data types.

  2. Learn about exceptions.

Conclusion:

Day 8 was a success! operators are now under my belt.

What are your favorite Python operators & uses? Share in the comments below.

Connect with me:

LinkedIn: [https://www.linkedin.com/in/archana-prusty-4aa0b827a/ ]

GitHub: [https://github.com/p-archana1]

Join the conversation:

Share your own learning experiences or ask questions in the comments.

Next Post:

Day 9: data type

Happy learning!

thank you!!

More from this blog

A

Archana's blog

41 posts

Python Insights: Exploring the world of Python programming, one code snippet at a time. Follow along for tutorials, projects, and insights on machine learning, web development, and data science.