Easy implementation of Operators in Python – Pract 1

Operators in Python

Programs on Operators in python gives practical implementation of arithmetic, assignment, bit wise, membership, logical, identity and comparison operators.
Operators are special symbols that represent calculations and values which operator uses are called operands.

Arithmetic Operator

Practical a : Program to implement Arithmetic operator in python.

"""
Author : ITVoyagers (itvoyagers.in)
Date :20th August 2020
Description : Program to implement Arithmetic operator in python.
"""

#arithmetic operator in python
a=int(input("Enter any number"))
b=int(input("Enter any number"))

#addition
print("1.value of a+b",a+b)

#subtraction
print("2.value of a-b",a-b)

#multiplication
print("3.value of a*b",a*b)

#division
print("4.value of a/b",a/b)

#exponent
print("5.value of a**b",a**b)

#floor
print("6.value of a//b",a//b)

OUTPUT

Output of Arithmetic Operators in Python (itvoyagers.in)
Output of Arithmetic Operators in Python (itvoyagers.in)

Assignment Operator

Practical b : Program to implement Assignment operator in python.

"""
Author : ITVoyagers (itvoyagers.in)
Date :20th August 2020
Description : Program to implement Assignment operator in python.
"""

#assignment operator in python
a=int(input("Enter any number"))
b=int(input("Enter any number"))

a+=b
print("1.value of a+=b",a)

a-=b
print("2.value of a-=b",a)

a*=b
print("3.value of a*=b",a)

a/=b
print("4.value of a/=b",a)

a**=b
print("5.value of a**=b",a)

a//=b
print("6.value of a//=b",a)

OUTPUT

Output of Assignment Operators in Python (itvoyagers.in)
Output of Assignment Operators in Python (itvoyagers.in)

Comparison or Relational Operator

Practical c : Program to implement Comparison or Relational operator in python.

"""
Author : ITVoyagers (itvoyagers.in)
Date :20th August 2020
Description : Program to implement Comparison or Relational operator in python.
"""
#Comparison / Relational operator in Python
a=int(input("Enter any number"))
b=int(input("Enter any number"))
#equal to
if a==b:
          print("a and b are equal")
else:
          print("a and b are not equal")
#greater than
if(a>b):
          print("a is greater than b")
else:
          print("a is not greater than b")
#greater than equal to
if(a>=b):
          print("a is greater than or equal to b")
else:
          print("a is not greater than or equal to b")
#less than 
    
if(a<b):
          print("a is less than b")
else:
          print("a is not less than b")
#less than equal to
if(a<=b):
          print("a is less than or equal to b")
else:
          print("a is not less than or equal to  b")
#not equal to
if(a!=b):
          print("a and b are not equal")
else:
          print("a and b are equal")

OUTPUT

Output of Comparison or  Relational Operators in Python (itvoyagers.in)
Output of Comparison or Relational Operators in Python (itvoyagers.in)

Logical Operator

Practical d : Program to implement Logical operator in python.

"""
Author : ITVoyagers (itvoyagers.in)
Date :20th August 2020
Description : Program to implement Logical operator in python.
"""

#logical operators in python

t=True
f=False
print("value of t is : ",t,"value of f is : ",f)

print("t and f is",t and f)

print("t or f is",t or f)

print("not t is",not t)

x=1
y=1
print("value of x is : ",x,"value of y is : ",y)

print("x and y is",x and y)

print("x or y is",x or y)

print("not x is",not x)

x=0
y=""
print("value of x is : ",x,"value of y is : ",y)

print("x and y is",x and y)

print("x or y is",x or y)

print("not y is",not y)

OUTPUT

Output of Logical Operators in Python (itvoyagers.in)
Output of Logical Operators in Python (itvoyagers.in)

Bitwise Operator

Practical e : Program to implement Bitwise operator in python.

"""
Author : ITVoyagers (itvoyagers.in)
Date :20th August 2020
Description : Program to implement Bitwise operator in python.
"""

#Bitwise operators in Python
a=int(input("Enter any number"))
b=int(input("Enter any number"))
print('a=',a,':',bin(a),'b=',b,':',bin(b))
c=0
c=a&b
print("result of &(AND) is",c,':',bin(c))
c=a|b
print("result of |(OR) is",c,':',bin(c))
c=a^b
print("result of ^(XOR) is",c,':',bin(c))
c=~a
print("result of ~(COMPLEMENT) is",c,':',bin(c))
c=a<<2
print("result of <<(LEFT SHIFT) is",c,':',bin(c))
c=a>>2
print("result of >>(RIGHT SHIFT) is",c,':',bin(c))

OUTPUT

Output of Bitwise Operators in Python (itvoyagers.in)
Output of Bitwise Operators in Python (itvoyagers.in)

Membership Operator

Practical f : Program to implement Membership operator in python.

"""
Author : ITVoyagers (itvoyagers.in)
Date :20th August 2020
Description : Program to implement Membership operator in python.
"""

#Membership operators in Python
a=10
b=20
print("Value of a is : ",a ,"Value of b is : ",b)
my_iterable=[1,2,3,4,5]
print("My iterable consist of", my_iterable)

if(a in my_iterable):
    
          print('a is in the list')
else:
    
          print('a is not in the list')


if(b not in my_iterable):

          print('b is not in the list')
else:

          print('b is  in the list')

c=b/a

if(c in my_iterable):

          print('c is in the list')
else:

          print('c is not in the list')

OUTPUT

Output of Membership Operators in Python (itvoyagers.in)
Output of Membership Operators in Python (itvoyagers.in)

Identity Operator

Practical g : Program to implement Identity operator in python.

"""
Author : ITVoyagers (itvoyagers.in)
Date :20th August 2020
Description : Program to implement Identity operator in python.
"""

#Identity  operators in Python
a=int(input ("Enter a number"))
b=int(input ("Enter a number"))
print("a=",a,":",id(a),'b=',b,":",id(b))

if(a is b):
    
          print("a and b have same identity")
else:

          print("a and b do not have same identity")

if(id(a) == id(b)):

          print("a and b have same identity")
else:

          print("a and b do not have same identity")

if(a is not b):

          print("a and b do not have same identity")
else:

          print("a and b  have same identity")

OUTPUT

Output of Identity Operators in Python (itvoyagers.in)
Output of Identity Operators in Python (itvoyagers.in)

NOTE: Location will be same for same value with different variables.

For other python practicals:

Easy implementation of Operators in Python – Pract 1
Easy implementation of factorial, Fibonacci series, Armstrong, palindrome, recursion in Python pract 2
Easy Function definition and calling (Python)- Pract 3
Easy Python program on pangram & function definition pract 4
Python program on list comprehension, enumerate Pract 5
Python program on unorderd datatype dictionary Pract 6
Python programs for file handling Pract 7
1 – Best Python program to check Armstrong Number
2 – Easy and simple To-Do-List program in python
3 – Best python program to print all prime numbers in an Interval
4 – Best python program to print Fibonacci sequence
5 – Best python program to check if number is palindrome or not
6 – Best python program to find factorial using recursion
7 – Python program to check if a number is even or odd
8 – Python program to convert Celsius to Fahrenheit and vice versa
9 – Best Python program to find LCM
10 – Best Python program to find HCF
11 – Python program to find simple interest
12 – Best Python program to find Compound Interest
13 – Best python program to create class and objects
14 – Easy python program to access global variable in class
15 – Program on Labels and Buttons with Relief Styles in tkinter in easy way
16 – Python program to demonstrate PhotoImage in tkinter in easy way
17 -Python program to demonstrate Button in tkinter and its event in easy way
18 – Python program to show use of motion event in tkinter in easy way
19 – Python program to draw line and rectangle using methods in canvas in easy way
20 – Python program to demonstrate Label and its attributes in tkinter in easy way
21 – Python program to draw line in using create_line in canvas is easy way
22 – Python program to draw oval using create_oval method in canvas in easy way
23 – Python program to draw triangle using create_polygon method in canvas in easy way
24 – Python program to demonstrate create_bitmap method in canvas in easy way
25 – Python program to demonstrate create_arc method in canvas in easy way
26 – Python program to read data from file and count characters, digits, spaces, etc. in easy way
27 – Python program to create registration form using tkinter in easy way
28 – Simple and easy program to check if one list is reverse of another
More practicals coming soon...stay tuned

For other python basics related posts:

Best post on programming language and its types – 1
Python Programming (Basics) – History & Features
Python Programming (Basics)- Debugging & Types of errors
Python Programming (Basics) – Variables, Values, Types & Keywords.
Best post on built-in functions in python – 1
Python Programming (Basics) – Type Conversion
Python Programming (Basics) – Comments,Indentation,Built-in Number Data types and Expressions
Best post on IDLE & script mode in Python – 1
Python Programming (Basics)- Operators in Python
Best post on Order of Operations – Python
Simple and compound statements of python in easy way
Best post on conditional statements in python – Part 1
Best post on conditional statements in python – Part 2
Best post on looping in python (for loop)-Part 1
Best post on looping in python (while loop)-Part 2
Best post on nested loop in python(looping) -Part 3
Best post on infinite loop in python(looping) -Part 4
Best post on control statements(break,continue,pass)-1
Best post on Function definition & calling in python -1
Easy flow of function execution,parameters,arguments-2
Easy fruitful & void function,return values,stack diagrams-3
Best post on types of function arguments in python- 4
Best post on recursive function, scope of variable-5
Best post on import and from statement in python – 1
Best post on modules and built-in modules math,random & time-2
Best post on user defined modules in python-3
Best post on string datatype in python – 1
Best post immutable string,string operations python-2
Best post on string methods in python – 3
Best post on list datatype in python – 1
Best post on mutable list, list operations python – 2
Best post on List methods in python – 3
Best post on dictionary datatype in python – 1
Best post on dictionary methods and operations-2
Best post on tuple datatype in python – 1
Best post on tuple operations and immutable tuple- 2
Best post on tuple methods and built-in functions-3
17 -Python program to demonstrate Button in tkinter and its event in easy way
New posts coming soon.......

For other advanced python related posts:

File Systems and File Handling in Python
Types of file modes and attributes of file object in Python
How to read,write and append in a file in python?
Remaining file methods in Python
File Positions in Python
Directory in Python and its methods
Iterator and Iterables in Python
Exceptions in Python
Exception Handling in Python (Part I)
Exception Handling in Python (Part II)
Regular Expressions
Metacharacters or Regular Expression Patterns
Functions in 're' module(Part I)- Match vs Search
Functions in 're' module(Part II)-findall(), split(), sub()
Flags for regular expressions(Modifiers)
GUI programming in Python and Python GUI Library
What is Tkinter ?
Layout Manager (Geometry Manager) in Python
Events and Bindings in Python along with Widget configuration and styling
Fonts Names, Font Descriptors, System Fonts, Text formatting, Borders, Relief Styles in Python
Best post:Dimensions, Anchors, Bitmaps & Cursors in Python
Canvas widget of tkinter module – Python
Widgets in tkinter module – Python
Label, Text, Entry & Message Widget in Python
Button, Checkbutton & Radiobutton Widget in Python
Best post on Menu and Menubutton Widget of tkinter
Best post- Listbox and Scrollbar(Slider) Widget-Python
Best post on Frame Widget of tkinter in Python
Best post: message box widget and its methods
Best post- LabelFrame, Toplevel, PanedWindow widgets
Best post on Spinbox and Scale Widget in Python
Best post : Database connectivity in Python (Part 1)
Best post : Database Connectivity in Python (Part 2)
Best post on Create and Insert query : Python + MySQL
Best post on Select Query to Search & Read: Python + MySQL-4
Best post on Update query and Delete query : Python + MySQL-4
New posts coming soon.......

Leave a Comment