Example 1: Arithmetic operators in python -Source Code
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
a=9
b=4
#Addition of numbers
add=a+b
#Subtraction of numbers
sub=a-b
#Multiplication of numbers
mul=a*b
#Division (float) of numbers
div1=a/b
#Division (floor) of numbers
div2=a//b
#Modulo of both numbers
mod=a%b
# Power
p=a**b
#print results
print(add)
print(sub)
print(mul)
print(div1)
print(div2)
print(mod)
print(p)
a=9
b=4
#Addition of numbers
add=a+b
#Subtraction of numbers
sub=a-b
#Multiplication of numbers
mul=a*b
#Division (float) of numbers
div1=a/b
#Division (floor) of numbers
div2=a//b
#Modulo of both numbers
mod=a%b
# Power
p=a**b
#print results
print(add)
print(sub)
print(mul)
print(div1)
print(div2)
print(mod)
print(p)
a=9
b=4
#Addition of numbers
add=a+b
#Subtraction of numbers
sub=a-b
#Multiplication of numbers
mul=a*b
#Division (float) of numbers
div1=a/b
#Division (floor) of numbers
div2=a//b
#Modulo of both numbers
mod=a%b
# Power
p=a**b
#print results
print(add)
print(sub)
print(mul)
print(div1)
print(div2)
print(mod)
print(p)
Example 1: Arithmetic operators in python-Output
Example 2:Assignment operators in python -Source Code
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#Assign value
a=10
#assign a variable
b=a
print(b)
#Add and assign value
b +=a
print(b)
#Substract and assign value
b -=a
print(b)
#multiple and assign
b *=a
print(b)
#bitwise left shift operator
b <<=a
print(b)
#Assign value
a=10
#assign a variable
b=a
print(b)
#Add and assign value
b +=a
print(b)
#Substract and assign value
b -=a
print(b)
#multiple and assign
b *=a
print(b)
#bitwise left shift operator
b <<=a
print(b)
#Assign value
a=10
#assign a variable
b=a
print(b)
#Add and assign value
b +=a
print(b)
#Substract and assign value
b -=a
print(b)
#multiple and assign
b *=a
print(b)
#bitwise left shift operator
b <<=a
print(b)
Example 2:Assignment operators in python -Output
Example 3:Logical operators in python -Source Code
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
a=True
b=False
#print a and b is false
print(a and b)
#print a or b is true
print(a or b)
#print not a is false
print(not a)
a=True
b=False
#print a and b is false
print(a and b)
#print a or b is true
print(a or b)
#print not a is false
print(not a)
a=True
b=False
#print a and b is false
print(a and b)
#print a or b is true
print(a or b)
#print not a is false
print(not a)
Example 3:Logical operators in python -Output
Example 4:Relational operators in python -Source Code
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
a=13
b=33
# a > b is false
print(a > b)
# a < b is true
print(a < b)
# a == b is false
print(a == b)
# a != b is true
print(a != b)
# a >=b is false
print(a >=b)
# a <= b is true
print(a <= b)
a=13
b=33
# a > b is false
print(a > b)
# a < b is true
print(a < b)
# a == b is false
print(a == b)
# a != b is true
print(a != b)
# a >=b is false
print(a >=b)
# a <= b is true
print(a <= b)
a=13
b=33
# a > b is false
print(a > b)
# a < b is true
print(a < b)
# a == b is false
print(a == b)
# a != b is true
print(a != b)
# a >=b is false
print(a >=b)
# a <= b is true
print(a <= b)
Example 4:Relational operators in python-Output
Example 5:Identity operator in python -Source Code
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
a=10
b=20
c=a
print(a is not b)
print(a is c)
a=10
b=20
c=a
print(a is not b)
print(a is c)
a=10
b=20
c=a
print(a is not b)
print(a is c)
Example 5:Identity operator in python -Output
Example 6:Membership operator-Source Code
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# python program to illustrate not ' in' operator
x=24
y=20
list=[10,20,30,40,50]
if(x not in list):
print("x is NOT present in given list")
else:
print("x is present in given list")
if(y in list):
print("y is present in given list")
else:
print("y is NOT present in given list")
# python program to illustrate not ' in' operator
x=24
y=20
list=[ 10,20,30,40,50 ]
if (x not in list):
print("x is NOT present in given list")
else:
print("x is present in given list")
if(y in list):
print("y is present in given list")
else:
print("y is NOT present in given list")
# python program to illustrate not ' in' operator
x=24
y=20
list=[ 10,20,30,40,50 ]
if (x not in list):
print("x is NOT present in given list")
else:
print("x is present in given list")
if(y in list):
print("y is present in given list")
else:
print("y is NOT present in given list")
Example 6: Membership operator-Output
Example 7: Bitwise operator-Source Code
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
a=10
b=4
#print bitwise AND operation
print(a&b)
#print bitwise OR operation
print(a/b)
#print bitwise NOT operation
print(~a)
#print bitwise XOR operation
print(a^b)
#print bitwise right shift operation
print(a>>2)
#print bitwise left shift operation
print(a<<2)
a=10
b=4
#print bitwise AND operation
print(a&b)
#print bitwise OR operation
print(a/b)
#print bitwise NOT operation
print(~a)
#print bitwise XOR operation
print(a^b)
#print bitwise right shift operation
print(a>>2)
#print bitwise left shift operation
print(a<<2)
a=10
b=4
#print bitwise AND operation
print(a&b)
#print bitwise OR operation
print(a/b)
#print bitwise NOT operation
print(~a)
#print bitwise XOR operation
print(a^b)
#print bitwise right shift operation
print(a>>2)
#print bitwise left shift operation
print(a<<2)
Example 7: Bitwise operator-Output
Example 8: operator precedence-Source Code
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#Examples of operator precedence
#precedence of '+'&'*'* has higher precedence than +'
v=10+20*30
print(v)
#precedence of 'or'&'and' and has greater precedence than or so and is executed first
name = "kumar"
age= 0
if name == "kumar" or name == "ravi" and age >=2:
print("Hello! Welcome.")
else:
print("Good Bye!!")
#Examples of operator precedence
#precedence of '+'&'*'* has higher precedence than +'
v=10+20*30
print(v)
#precedence of 'or'&'and' and has greater precedence than or so and is executed first
name = "kumar"
age= 0
if name == "kumar" or name == "ravi" and age >=2:
print("Hello! Welcome.")
else:
print("Good Bye!!")
#Examples of operator precedence
#precedence of '+'&'*'* has higher precedence than +'
v=10+20*30
print(v)
#precedence of 'or'&'and' and has greater precedence than or so and is executed first
name = "kumar"
age= 0
if name == "kumar" or name == "ravi" and age >=2:
print("Hello! Welcome.")
else:
print("Good Bye!!")
Example 8: operator precedence-Output
Example 9:Operator Associativity-Source Code
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#left-right associativity
#100/10*10 is calculated as
#(100/10)*10 and not
#as 100/(10*10)
print(100/10*10)
#left-right associativity
#5-2+3 is calculated as
#(5-2)+3 and not
#as 5-(2+3)
print(5-2+3)
#left-right associativity
print(5-(2+3))
#right-left associativity
#2**3**2 is calculated as
#2**(3**2) and not
#as(2**3)**2
print(2**3**2)
#left-right associativity
#100/10*10 is calculated as
#(100/10)*10 and not
#as 100/(10*10)
print(100/10*10)
#left-right associativity
#5-2+3 is calculated as
#(5-2)+3 and not
#as 5-(2+3)
print(5-2+3)
#left-right associativity
print(5-(2+3))
#right-left associativity
#2**3**2 is calculated as
#2**(3**2) and not
#as(2**3)**2
print(2**3**2)
#left-right associativity
#100/10*10 is calculated as
#(100/10)*10 and not
#as 100/(10*10)
print(100/10*10)
#left-right associativity
#5-2+3 is calculated as
#(5-2)+3 and not
#as 5-(2+3)
print(5-2+3)
#left-right associativity
print(5-(2+3))
#right-left associativity
#2**3**2 is calculated as
#2**(3**2) and not
#as(2**3)**2
print(2**3**2)
Example 9: Operator Associativity-Output
Example 10: Ternary operator-Source Code
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#simple method to use ternary operator
#program to demonstrate conditional operator
a,b=10,20
#copy value of a in min if a<b else copy b
min=a if a<b else b
print(min)
#simple method to use ternary operator
#program to demonstrate conditional operator
a,b=10,20
#copy value of a in min if a<b else copy b
min=a if a<b else b
print(min)
#simple method to use ternary operator
#program to demonstrate conditional operator
a,b=10,20
#copy value of a in min if a<b else copy b
min=a if a<b else b
print(min)
Example 10: Ternary operator-Output
Direct method by using tuples,dictionary,and lambda
Example 11: Tuple Type-Source Code
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#python program to demonstrate ternary operator
a,b=10,20
#use tuple for selecting an item
#if [a<b] is true it returns 1, so elements with 1 index will print
#else if [a<b] is false it returns 0, so elements with 0 index will print
print((b,a)[a<b])
#use dictionary for selecting an item
#if[a<b] is true then value of true key will print
#else if [a<b] is false then valse of false key will print
print({True:a,False:b}[a<b])
#lambda is more efficient than above two methods
#because in lambda we are assure that
#only one expression will be evaluated unlink in tuple or dictionary
print((lambda:b,lambda:a)[a<b]())
#python program to demonstrate ternary operator
a,b=10,20
#use tuple for selecting an item
#if [a<b] is true it returns 1, so elements with 1 index will print
#else if [a<b] is false it returns 0, so elements with 0 index will print
print((b,a) [a<b])
#use dictionary for selecting an item
#if[a<b] is true then value of true key will print
#else if [a<b] is false then valse of false key will print
print({True:a,False:b} [a<b])
#lambda is more efficient than above two methods
#because in lambda we are assure that
#only one expression will be evaluated unlink in tuple or dictionary
print((lambda:b,lambda:a)[a<b]())
#python program to demonstrate ternary operator
a,b=10,20
#use tuple for selecting an item
#if [a<b] is true it returns 1, so elements with 1 index will print
#else if [a<b] is false it returns 0, so elements with 0 index will print
print((b,a) [a<b])
#use dictionary for selecting an item
#if[a<b] is true then value of true key will print
#else if [a<b] is false then valse of false key will print
print({True:a,False:b} [a<b])
#lambda is more efficient than above two methods
#because in lambda we are assure that
#only one expression will be evaluated unlink in tuple or dictionary
print((lambda:b,lambda:a)[a<b]())
Example 11: Tuple Type-Source Code-Output
Ternary operator can be used as nested if-else also:-Source Code
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#python program to demonstrate nested ternary operator
a,b=10,20
if a !=b:
if a>b:
print("a is grater than b")
else:
print("b is greater than a")
else:
print("both a and b are equal")
#python program to demonstrate nested ternary operator
a,b=10,20
if a !=b:
if a>b:
print("a is grater than b")
else:
print("b is greater than a")
else:
print("both a and b are equal")
#python program to demonstrate nested ternary operator
a,b=10,20
if a !=b:
if a>b:
print("a is grater than b")
else:
print("b is greater than a")
else:
print("both a and b are equal")
Ternary operator can be used as nested if-else also:-Output
To use print function in ternary operator-Source Code
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#Find the larger number among 2 using ternary operator in python3
a=5
b=7
#[statement_on_True] if [condition] else [statement _on_False]
#Find the larger number among 2 using ternary operator in python3
a=5
b=7
#[statement_on_True] if [condition] else [statement _on_False]
print(a, "is greater")if (a>b) else print(b,"is Greater")
#Find the larger number among 2 using ternary operator in python3
a=5
b=7
#[statement_on_True] if [condition] else [statement _on_False]
print(a, "is greater")if (a>b) else print(b,"is Greater")