Python Tutorial 12 --- "Operators In Python "
Python Tutorial 12 ---
"Operators In Python "
This PROGRAM or TUTORIAL about Python's "Break and Continue Statement In Python"
DOC
“Operators in Python can be defined as symbols that assist us to perform certain operations. The operations could be between variable and variable, variable and value, value and value”
Operators that Python Language supports are:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Identity Operators
- Membership Operators
- Bitwise Operators
We must remember most of them from our basic mathematics that we studied in school. May be except for the last one, that might be new for some of us. To understand bitwise fully, we must have the basic knowledge of the conversion of decimal into binary. For example, the binary for the first five number is:
- 0001
- 0010
- 0011
- 0100
- 0101
Arithmetic Operators:
Basic mathematical operations such as addition, multiplication, subtraction, division, etc. are performed with the help of arithmetic Operations. It contains nearly all operations that we can perform with the help of a calculator. Symbols for such operators include *, /, %, -, //, etc.
Assignment Operators:
The assignment operator is used to assign values to a variable. In some cases, we have to assign a variable’s value to another variable, in such cases the value of the right operand is assigned to the left operand. One of the basic signs from which we can recognize an assignment operator is that it must have an equal-to(=) sign. Some commonly used assignment operators include +=, -=, /=, etc.
Comparison Operators:
They are also known as relational operators. They compare the values on either side of the operator and decide the relation among them. Commonly used comparison operators include ==, >, < , >=,etc.
Logical Operators:
Logical operators perform logical AND, OR and NOT, operations. They are usually used in conditional statements to join multiple conditions. AND, OR and NOT keywords are used to perform logical operations.
Identity Operations:
Identity operator checks if two operands share the same identity or not, which means that they share the same location in memory or different. “is” and “is not” are the keywords used for identity operands.
Membership Operands:
Membership operand checks if the value or variable is a part of a sequence or not. The sequence could be string, list, tuple, etc. “in” and “not in” are keywords used for membership operands.
Bitwise Operand:
Bitwise operands are used to perform bit by bit operation on binary numbers. First, we have to change the format of the number from decimal to binary and then compare them using AND, OR, XOR, NOT, etc.
CODE
#Operators in Python---
"""Types of Operators in Python---
Arithmatic Operators
Assignment Operators
Comparison Operators
Logical Operators
Identity Operators
Membership Operators
Bitwise Operators"""
#Arithmatic Operators
print("8+4 is", 8+4) #Addition
print("8-4 is", 8-4) #Subtraction
print("8*4 is", 8*4) #Multiplication
print("8/4 is", 8/4) #Division
print("8**2 is", 8**2) #Expoential (To The Power Value)
print("8%3 is", 8%3) #Remainder (ভাগশেষ)
print("8//4 is", 8//4) #It will give us Integer Value
print(end="\n") #This isn't part of the program; just to look clean the output
#Assignment Operators
x = 10 # here '=' is an Assignment Operator
print(x)
x += 10 #Add number to the value, here '+=' is an Assignment Operator
print(x)
x -= 5 #Subtract number from the value, here '-=' is an Assignment Operator
print(x)
x /= 3 #Divide the value, here '/=' is an Assignment Operator
print(x)
x *= 3 #Multiply the value, here '*=' is an Assignment Operator
print(x)
x %= 0.10 #Show Percentage value, here '%=' is an Assignment Operator
print(x)
print(end="\n")
#Comparison Operators
n = 5
print(n==5) #Compares the equality of the value; Output=True
print(n==8) #Compares the equality of the value; Output=False
print(n!=5) #Compares the inequality of the value; Output=False
print(n>5) #Compares if the value is greater ; Output=False
print(n>=5) #Compares if the value is greater than or equal; Output=True
print(n<5) #Compares if the value is lesser ; Output=False
print(n<=5) #Compares if the value is lesser than or equal; Output=True
print(end="\n")
#Logical Operators
a = True
b = False
print(a and a) #Output=True
print(a and b) #Output=False
print(b and b) #Output=False
print(b and b) #Output=False
print(a or a) #Output=True
print(a or b) #Output=True
print(b or a) #Output=True
print(b or b) #Output=False
print(end="\n")
#Identity Operators
x = True
y = False
print(x is y) #Output = False
print(x is not y) #Output = True
#just like
print(5 is 100, 5 is 5, 5 is not 5) #Output = False True False
print(end="\n")
#Membership Operators
list1 = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
print(30 in list1) #Output=True
print(30 not in list1) #Ouput=False
print(1000 in list1) #Output=False
print(1000 not in list1) #Output=True
print(end="\n")
#Bitwise Operators
#You neeed boolean knowledge to work with bitwise operators
#0 = 00
#1 = 01
#2 = 10
#3 = 11
print(0 & 2) #Output = 0
print(0 | 3) #Ouput = 3
Comments
Post a Comment