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 this quiz for testing Manova Basics
Test your knowledge on pandas groupby with this quiz
Observe the dataset and try to solve the Visualization quiz on it
To perform ANCOVA (Analysis of Covariance) with a dataset that includes multiple types of variables, you’ll need to ensure your dependent variable is continuous, and you can include categorical variables as factors. Below is an example using the statsmodels library in Python: Mock Dataset Let’s create a dataset with a mix of variable types: Performing…
How useful was this post? Click on a star to rate it! Submit Rating
Complete the code by dragging and dropping the correct functions
Python functions are a vital concept in programming which enables you to group and define a collection of instructions. This makes your code more organized, modular, and easier to understand and maintain. Defining a Function: In Python, you can define a function via the def keyword, followed by the function name, any parameters wrapped in parentheses,…
Mastering indexing will significantly boost your data manipulation and analysis skills, a crucial step in your data science journey.
Stable Diffusion Models: Where Art and AI Collide Artificial Intelligence meets creativity in the fascinating realm of Stable Diffusion Models. These innovative models take text descriptions and bring them to life in the form of detailed and realistic images. Let’s embark on a journey to understand the magic behind Stable Diffusion in a way that’s…