in ,

Python Operators

Python Operators

Python Operators: In this tutorial, you’ll learn of everything about various sorts of operators in Python, their syntax, and how to use them with examples.

What are operators in python?

Operations are special symbols in Python that do number-crunching or sensible calculation. The worth that the administrator works on is known as the operand.

For instance:

>>> 2+3
5

Here, + is the operator that performs addition. 2 and 3 are the operands and 5 is the output of the operation.


Arithmetic operators

Arithmetic operators are used to performing mathematical operations like addition, subtraction, multiplication, etc.

OperatorMeaningExample
+Add two operands or unary plusx + y+ 2
Subtract right operand from the left or unary minusx – y- 2
*Multiply two operandsx * y
/Divide left operand by the right one (always results into float)x / y
%Modulus – the remainder of the division of left operand by the rightx % y (remainder of x/y)
//Floor division – division that results in the whole number adjusted to the left in the number linex // y
**Exponent – left operand raised to the power of rightx**y (x to the power y)

Example 1: Arithmetic operators in Python

x = 15
y = 4

# Output: x + y = 19
print('x + y =',x+y)

# Output: x - y = 11
print('x - y =',x-y)

# Output: x * y = 60
print('x * y =',x*y)

# Output: x / y = 3.75
print('x / y =',x/y)

# Output: x // y = 3
print('x // y =',x//y)

# Output: x ** y = 50625
print('x ** y =',x**y)

Output

x + y = 19
x - y = 11
x * y = 60
x / y = 3.75
x // y = 3
x ** y = 50625

Comparison operators

Comparison operators are used to comparing values. It returns either True or False according to the condition.

Operator Meaning Example
> Greater than – True if the left operand is greater than the right x > y
< Less than – True if the left operand is less than the right x < y
== Equal to – True if both operands are equal x == y
!= Not equal to – True if operands are not equal x != y
>= Greater than or equal to – True if the left operand is greater than or equal to the right x >= y
<= Less than or equal to – True if the left operand is less than or equal to the right x <= y

Example 2: Comparison operators in Python

x = 10
y = 12

# Output: x > y is False
print('x > y is',x>y)

# Output: x < y is True
print('x < y is',x<y)

# Output: x == y is False
print('x == y is',x==y)

# Output: x != y is True
print('x != y is',x!=y)

# Output: x >= y is False
print('x >= y is',x>=y)

# Output: x <= y is True
print('x <= y is',x<=y)
Output
x > y is False
x < y is True
x == y is False
x != y is True
x >= y is False
x <= y is True

Logical operators

Logical operators are the and, or, not operators.

OperatorMeaningExample
andTrue if both the operands are truex and y
orTrue if either of the operands is truex or y
notTrue if the operand is false (complements the operand)not x

Example 3: Logical Operators in Python

x = True
y = False

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

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

print('not x is',not x)

Output

x and y is False
x or y is True
not x is False

Bitwise operators

Bitwise operators act on operands as though they were strings of parallel digits. They work a little bit at a time, subsequently the name.

For example, 2 is 10 in binary, and 7 is 111.

In the table below: Let x = 10 (0000 1010 in binary) and y = 4 (0000 0100 in binary)

OperatorMeaningExample
&Bitwise ANDx & y = 0 (0000 0000)
|Bitwise ORx | y = 14 (0000 1110)
~Bitwise NOT~x = -11 (1111 0101)
^Bitwise XORx ^ y = 14 (0000 1110)
>>Bitwise right shiftx >> 2 = 2 (0000 0010)
<<Bitwise left shiftx << 2 = 40 (0010 1000)

Assignment operators

Assignment operators are used in Python to assign values to variables.

a = 5 is a simple assignment operator that assigns the value 5 on the right to the variable an on the left.

There are different compound administrators in Python like a += 5 that adds to the variable and later allots the equivalent. It is equivalent to a = a + 5.

OperatorExampleEquivalent to
=x = 5x = 5
+=x += 5x = x + 5
-=x -= 5x = x – 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5
%=x %= 5x = x % 5
//=x //= 5x = x // 5
**=x **= 5x = x ** 5
&=x &= 5x = x & 5
|=x |= 5x = x | 5
^=x ^= 5x = x ^ 5
>>=x >>= 5x = x >> 5
<<=x <<= 5x = x << 5

Special operators

Python language offers some special sorts of operations like the identity operator or the membership operator. They are described below with examples.

Identity operators

is and is not are the identity operations in Python. They are used to check if two qualities (or variables) are situated on a similar piece of the memory. Two factors that are equivalent doesn’t suggest that they are identical.

Operator Meaning Example
is True if the operands are identical (refer to the same object) x is True
is not True if the operands are not identical (do not refer to the same object) x is not True

Example 4: Identity operators in Python

x1 = 5
y1 = 5
x2 = 'Hello'
y2 = 'Hello'
x3 = [1,2,3]
y3 = [1,2,3]

# Output: False
print(x1 is not y1)

# Output: True
print(x2 is y2)

# Output: False
print(x3 is y3)

Output

False
True
False

Here, we see that x1 and y1 are whole numbers of similar qualities, so they are equivalent just as indistinguishable. The same is the situation with x2 and y2 (strings).

In any case, x3 and y3 are recorded. They are equivalent yet not indistinguishable. It is on the grounds that the translator finds them independently in memory despite the fact that they are equivalent.


Membership operators

in and not in are the membership operators in Python. They are used to test

whether a value or variable is found in a sequence (string, list, tuple, set, and dictionary).

In a dictionary we can only test for the presence of a key, not the value.

OperatorMeaningExample
inTrue if value/variable is found in the sequence5 in x
not inTrue if value/variable is not found in the sequence5 not in x

Example #5: Membership operators in Python

x = 'Hello world'
y = {1:'a',2:'b'}

# Output: True
print('H' in x)

# Output: True
print('hello' not in x)

# Output: True
print(1 in y)

# Output: False
print('a' in y)

Output

True
True
True
False

Here, ‘H’ is in x but ‘hello’ is not present in x (remember, Python is case sensitive). Similarly, 1 is key and ‘a’ is the value in dictionary y. Hence, ‘a’ in y returns False.


Please feel free to give your comment if you face any difficulty here.

For More Latest Articles Click on Below Link

salman khan

Written by worldofitech

Leave a Reply

Python Input, Output and Import

Python Input, Output, and Import

Python Namespace and Scope

Python Namespace and Scope