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.
After tourism was established as a motivator of local economies (country, state), many governments stepped up to the plate.
Sentiment analysis can determine the polarity of sentiments from given sentences. We can classify them into certain categories.
Traverse a dictionary with for loop Accessing keys and values in dictionary. Use Dict.values() and Dict.keys() to generate keys and values as iterable. Nested Dictionaries with for loop Access Nested values of Nested Dictionaries How useful was this post? Click on a star to rate it! Submit Rating
For loop is one of the most useful methods to reuse a code for repetitive execution.
These all metrics are revolving around visits and hits which we are getting on websites. Single page visits, Bounce, Cart Additions, Bounce Rate, Exit rate,
Hypothesis testing is a statistical method for determining whether or not a given hypothesis is true. A hypothesis can be any assumption based on data.
A/B tests are randomly controlled experiments. In A/B testing, you get user response on various versions of the product, and users are split within multiple versions of the product to figure out the “winner” of the version.
This article covers ‘for’ loops and how they are used with tuples. Even if the tuples are immutable, the accessibility of the tuples is similar to that of the list.
MANOVA is an update of ANOVA, where we use a minimum of two dependent variables.
You only need to understand two or three concepts if you have read the one-way ANOVA article. We use two factors instead of one in a two-way ANOVA.