Python has several types of operators. Mathematical, Assignment, Comparison, Logical, Identity, Membership, Bitwise operators.
Python has several types of operators. Mathematical, Assignment, Comparison, Logical, Identity, Membership, Bitwise operators.
Greetings, fellow data enthusiasts! Today, we’ll dive into the world of Python operators. These are the symbols that tell Python what operations to perform on variables and values. Think of them as the gears that keep the engine of your code running smoothly.
In this short read, we will learn about operators. Because there are several types of operators in Python, they will be presented in tabular form. They are self-explanatory.
Operation | Operator | Example | Output |
---|---|---|---|
Addition | + | a=8,b=4 a + b | 12 |
Subtraction | – | a=8,b=4 a - b | 4 |
Multiplication | * | a=8,b=4 a * b | 32 |
Modulus | % | a=8,b=4 a % b | 0 |
Division | / | a=8,b=4 a / b | 2.0 |
Floor Division | // | a=8,b=4 a // b | 2 |
Exponential | ** | a=8,b=4 a ** b | 4096 |
[1,3,4]
+ [1,1,1]
equals [1,3,4,1,1,1]
.# Merge two lists with '+' operator.
a=[1,3,4]
b=[1,1,1]
print(a+b)
[1, 3, 4, 1, 1, 1]
# Merge two tuples with + opertor.
a=(1,3,4)
b=(1,1,1)
a+b
(1, 3, 4, 1, 1, 1)
# Merge two Strings with + opertor.
tree='pine'
fruit='apple'
tree + fruit
'pineapple'
(1,2,3) * 3
equals (1,2,3,1,2,3,1,2,3)
.# Use * operator to
a=[1,3,4]
a*3
[1, 3, 4, 1, 3, 4, 1, 3, 4]
# multiply tuple by 3
b=(4,5,6)
b*3
(4, 5, 6, 4, 5, 6, 4, 5, 6)
Operators | Example | Explanation |
---|---|---|
= | a=10 | a = 10 |
+= | a+=10 | a = a+10 |
-= | a-=10 | a = a-10 |
%= | a%=10 | a = a%10 |
*= | a*=10 | a = a*10 |
/= | a/=10 | a = a/10 |
** = | a**=10 | a = a**10 |
//= | a//=10 | a = a//10 |
&= | a&=10 | a = a&10 |
|= | a|=10 | a = a|10 |
^= | a^=10 | a = a^10 |
>>= | a>>=10 | a = a>>10 |
<<= | a<<=10 | a = a<<10 |
# Use assignment operator
a=25
print(a)
25
# Use Addition Shorthand
a += 10
print(a)
35
# Use Multiplication Shorthand
a *= 10
print(a)
10
# Use Substraction Shorthand
a -= 10
print(a)
25
# Use modulo Shorthand
a %= 2
print(a)
1
# Use Power of number Shorthand
a **= 10
print(a)
10000000000
# Use Division Shorthand
a /= 10
print(a)
1000000000.0
Operators | Operation | Explanation |
---|---|---|
== | Equal to | a == b |
!= | Not Equal to | a != b |
> | Greater Than | a > b |
< | Less Than | a < b |
>= | Greater Than or Equal to | a >= b |
<= | Less Than or I’m to | a <= b |
# Comparing if 4 is equal to 5
4==5
False
# Comparing if 2 is not equal to 1
2!=1
True
# Comparing if 2 is greater than 1
2>1
True
# Comparing if 4 is equal to 4
4==4
True
Operators | Explanation | Example |
---|---|---|
and | It returns True if both statements are true. | a > b and b > c |
or | It returns if one or both of the statements are true. | a < b or b > c |
not | Reverses the Result | not(a > c) |
and
operator# Declaring some integers.
a=10
c=15
b=20
# chaining comparisons.
c>a and c<b
aTrue
# chaining comparisons.
c>a and c>b
False
a# Using and operator.
True and True
True
# Using and operator.
True and False
False
or
operator# Using or operator
True or False
True
# Using or operator
False or False
False
# chaining or operator
c>a or c>b
True
not
operator# Using Not Operator with and
not True and False
False
# Using Not Operator with or
not True or False
False
# Using not operator with greater than b.
not a>b
True
# Using not operator with lesser than b.
not a<b
False
Operators | Explanation | Example |
---|---|---|
is | If Both given objects do not hold the same value, it will Return True | a is c |
is not | If Both given objects do not hold the same value, it will return True | a is not c |
a=10
c=15
b=10
d=25
# Check if a is b , it's checked with id() function.
a is b
True
# Check if b is c , it's checked with id() function.
b is c
False
# Check if a is not c , it's checked with id() function.
a is not c
True
# Check if b is not c , it's checked with id() function.
b is not c
True
Operators | Explanation | Example |
---|---|---|
in | If the given value is not available in the collection,It will Return True | a in c |
not in | an in c | a not in c |
# find 'pine' in fruit
# use membership operator 'in'.
fruit='pineapple'
'pine' in fruit
True
# find 'app' in fruit
'app' in fruit
True
# find 'banana' in fruit.
'banana' not in fruit
True
# find 'ineapp' in fruit.
'inepp' not in fruit
False
Name | Operators | Description |
---|---|---|
AND | & | Returns 1 if both bits are 1 |
OR | | | Returns 1 if one or both bits are 1 |
Left Shift(Zero fill) | << | Shifts bits to left by putting zeros from right and left end bit is deleted |
Signed Right Shift | >> | Shifts bits to right by putting numbers copied from right to left and rightend bit is deleted |
XOR | ^ | Returns 1 if only one bit is 1 and returns zero if both bits are same |
NOT | ~ | Inverts Every bit |
# Using bitwise and
a = 10
b = 4
print('AND')
print( bin(a) , '\t' , bin(b) )
print("a & b =", a & b)
AND
0b1010 0b100
a & b = 0
# Using bitwise or
print('OR')
print( bin(a) , '\t' , bin(b) )
print("a | b =", a | b)
OR
0b1010 0b100
a | b = 14
# using bitwise not
print('NOT')
print( bin(a) , '\t' , bin(b) )
print("~a =", ~a)
NOT
0b1010 0b100
~a = -11
# using bitwise xor
print('XOR')
print( bin(a) , '\t' , bin(b) )
print("a ^ b =", a ^ b)
XOR
0b1010 0b100
a ^ b = 14
ANCOVA is an extension of ANOVA (Analysis of Variance) that combines blocks of regression analysis and ANOVA. Which makes it Analysis of Covariance.
What if we learn topics in a desirable way!! What if we learn to write Python codes from gamers data !!
Start using NotebookLM today and embark on a smarter, more efficient learning journey!
This can be a super guide for you to start and excel in your data science career.
Solve the task by completing the SQL script
Learn about LAG function in SQL and solve the quiz.
fill in the blanks to complete the code.
Brush up on your pandas basics knowledge. Drag and drop quizzes.
Improve your analytical skills by practicing the following tasks
Random forest trees combine multiple decision trees to obtain an output. And it is flexible enough to adapt to Classification and Regression.Â
In measures of dispersion, the standard deviation is one of the prominent tools to calculate the dispersion of the data
Let’s learn to calculate the spread of the data and measure it. with Absolute measures and Relative measures
Interquartile range is the difference between first and last quarters in a series of numbers. A Quartile range means a four-partition series of numbers.
In this article, we will learn how to utilize the functionalities provided by excel and python libraries to calculate IQR,