What are Operators in Python | Check all types of Operators in Python?
Topics covered
- Mutable and Immutable Types in Python
- Operators in Python
- Expressions in Python
Mutable and Immutable Types in python
Mutable Types
What are Mutable types in python?
The Mutable types are those that can change their value or modify them are mutable types in python.
Given below are the mutable types of python
- Lists
- Dictionaries
Immutable Types
What are Immutable types in python?
Those that can not change their value or can not alter them are the immutable types in python.
Given below are the immutable types of python
- Integers
- Floating point numbers
- Booleans
- strings
- tuples
- sets
Python object's key attributes
What are objects in python?
An object is an entity that has certain properties and shows a certain type of behavior.
How many Object's Key Attributes are there in python?
There are three object's key attributes in python:-
- type
- value
- id
The type of an object
It determines the operations that can be performed on the object.
The Built-in function type() returns the type of an object in python.
for example :-
let
>>> a=5
>>> type(a)
<class 'int'>
The value of an object
It is the data-item contained in the object.
The print statement can display the value of an object.
for example:-
let,
>>> topic="What are python data types"
>>> print(topic)
What are Python data types
The id of an object
It gives the memory location of the object.
The Built-in function id() return the id of an object in python.
for example:-
>>> a=5
>>> id(a)
30899120
Operators
What are operators in python?
Operators are the symbols that trigger some operation/action on data in python.
Here we will discuss only the following operators in python:-
- Arithmetic operators
- Relational operators
- Identity operators
- Logical operators
Starting from arithmetic operators in python
Here we will discuss only the following operators in python:-
- Arithmetic operators
- Relational operators
- Identity operators
- Logical operators
Starting from arithmetic operators in python
Arithmetic operators
Python uses arithmetic operators to perform arithmetic operations.
Python has the following arithmetic operators:-
Symbol
Operators
+
Addition
-
Subtraction
*
Multiplication
/
Division
//
Floor division
%
Remainder
**
Exponentiation
Arithmetic operators in python are also of two types:-
- Unary operators
- Binary operators
Unary operators
What are the Unary operators in python?
The operators which operate on one operand are called unary operators in python.
There are two unary operators in python
Unary +
This operator precedes an operand.
for example
>>> a=5
This means +a is 5
>>> c=-2
This means +c is -2
Unary -
This operator also precedes an operand.
for example
>>> a=6
This means -a is -6
>>> k=-1
This means -k is 1
This operator reverses the sign of the operand's value.
Binary Operators
What are Binary operators in python?
Those Operators which operate upon two operands are called binary operators in python.
The table above shows the binary operators present in python.
Addition operator in python
The symbol for addition operator is +.
This operator adds the value of operands and the result is the sum of the two operands.
for example
>>> a=6
>>> b=5
>>> c=a+b
>>> print(c)
11
Subtraction operator in python
The symbol for subtraction operator is - .
This operator subtracts the second operand from the first and the result is the diffrence between the two operands.
for example
>>> a=10
>>> b=4
>>> c=a-b
>>> print(c)
6
Multiplication operator in python
The symbol for multiplication operator is *
This operator multiplies the values of operands and the result is the product of the values of operands.
for example
>>> a=10
>>> b=6
>>> c=a*b
>>> print(c)
60
Division operator in python
The symbol for division operator is /
This operator divides its first operand by the second one and always result a float value.
for example
>>> a=57
>>> b=3
>>> c=a/b
>>> print(c)
19.0
>>> l=17
>>> m=3
>>> d=l/m
>>> print(d)
5.666666666666667
Floor Division operator in python
The symbol for Floor division operator is //
This operator also divides the first operand by the second one but the result is an integer not a float.
for example
>>> a=16
>>> b=3
>>> c=a//b
>>> print(c)
5
Modulus operator in python
The symbol for the modulus operator is %
This operator also divides the first operand by second but here the result is the remainder left after division and the reuslt is may be integer of float.
for example
>>> a=16
>>> b=3
>>> c=a%b
>>> print(c)
1
>>> k=1.6
>>> b=4
>>> c=a%b
>>> print(c)
1.6
Exponentiation operator in python
The symbol for exponentiation operator is **
This operator performs the power operation and the result is a number raised to a power .
for example
>>> a=4
>>> b=3
>>> c=a**b
>>> print(c)
64
Some important Augmented Assignment Operators
Operation
What is does.
a+=b
a=a+b
a-=b
a=a-b
a*=b
a=a*b
a/=b
a=a/b
a//=b
a=a//b
a%=b
a=a%b
a**=b
a=a**b
Python uses arithmetic operators to perform arithmetic operations.
Python has the following arithmetic operators:-
Symbol
|
Operators
|
+
|
Addition
|
-
|
Subtraction
|
*
|
Multiplication
|
/
|
Division
|
//
|
Floor division
|
%
|
Remainder
|
**
|
Exponentiation
|
Arithmetic operators in python are also of two types:-
- Unary operators
- Binary operators
Unary operators
This operator precedes an operand.
for example
>>> a=5
This means +a is 5
>>> c=-2
This means +c is -2
Unary -
This operator also precedes an operand.
for example
>>> a=6
This means -a is -6
>>> k=-1
This means -k is 1
This operator reverses the sign of the operand's value.
Binary Operators
What are Binary operators in python?
Those Operators which operate upon two operands are called binary operators in python.
The table above shows the binary operators present in python.
Addition operator in python
The symbol for addition operator is +.
This operator adds the value of operands and the result is the sum of the two operands.
for example
>>> a=6
>>> b=5
>>> c=a+b
>>> print(c)
11
Subtraction operator in python
The symbol for subtraction operator is - .
This operator subtracts the second operand from the first and the result is the diffrence between the two operands.
for example
>>> a=10
>>> b=4
>>> c=a-b
>>> print(c)
6
Multiplication operator in python
The symbol for multiplication operator is *
This operator multiplies the values of operands and the result is the product of the values of operands.
for example
>>> a=10
>>> b=6
>>> c=a*b
>>> print(c)
60
Division operator in python
The symbol for division operator is /
This operator divides its first operand by the second one and always result a float value.
for example
>>> a=57
>>> b=3
>>> c=a/b
>>> print(c)
19.0
>>> l=17
>>> m=3
>>> d=l/m
>>> print(d)
5.666666666666667
Floor Division operator in python
The symbol for Floor division operator is //
This operator also divides the first operand by the second one but the result is an integer not a float.
for example
>>> a=16
>>> b=3
>>> c=a//b
>>> print(c)
5
Modulus operator in python
The symbol for the modulus operator is %
This operator also divides the first operand by second but here the result is the remainder left after division and the reuslt is may be integer of float.
for example
>>> a=16
>>> b=3
>>> c=a%b
>>> print(c)
1
>>> k=1.6
>>> b=4
>>> c=a%b
>>> print(c)
1.6
Exponentiation operator in python
The symbol for exponentiation operator is **
This operator performs the power operation and the result is a number raised to a power .
for example
>>> a=4
>>> b=3
>>> c=a**b
>>> print(c)
64
Some important Augmented Assignment Operators
Operation
|
What is does.
|
a+=b
|
a=a+b
|
a-=b
|
a=a-b
|
a*=b
|
a=a*b
|
a/=b
|
a=a/b
|
a//=b
|
a=a//b
|
a%=b
|
a=a%b
|
a**=b
|
a=a**b
|
Relational Operators
Python uses Relational operators to show relations between two operands.
Python has the following relational operators:-
Symbol
Operators
<
Less than
>
Greater than
<=
Less than or equal to
>=
Greater than or equal to
==
Equal to
!=
Not equal to
Relational operator compares the two operands using their ordinal values
What are the Ordinal values in python?
Ordinal values are the ASCII codes of the characters.
for example
the ordinal value of A is 65
the ordinal value of a is 97
the ordinal value of space is 32
In python you can use In-Built ord() function to find the ordinal value of an object.
Capital letters are considered lesser than small letters.Space is considered small than letters.
Let's understand these using some examples:-
>>> 5<6
True
>>> 2<1
False
>>> a>b
False
>>> a>A
True
>>> 'abc' > ' abc' # space has less ordinal value
True
>>> 1<=2
True
>>> 'abc' < 'abcd' # second string is greater than the first after c
True
>>> 1==True # The boolean True is equal to 1
True
Python uses Relational operators to show relations between two operands.
Python has the following relational operators:-
Symbol
|
Operators
|
<
|
Less than
|
>
|
Greater than
|
<=
|
Less than or equal to
|
>=
|
Greater than or equal to
|
==
|
Equal to
|
!=
|
Not equal to
|
Relational operator compares the two operands using their ordinal values
What are the Ordinal values in python?
Ordinal values are the ASCII codes of the characters.
for example
the ordinal value of A is 65
the ordinal value of a is 97
the ordinal value of space is 32
In python you can use In-Built ord() function to find the ordinal value of an object.
Capital letters are considered lesser than small letters.Space is considered small than letters.
Let's understand these using some examples:-
>>> 5<6
True
>>> 2<1
False
>>> a>b
False
>>> a>A
True
>>> 'abc' > ' abc' # space has less ordinal value
True
>>> 1<=2
True
>>> 'abc' < 'abcd' # second string is greater than the first after c
True
True
>>> 2<1
False
>>> a>b
False
>>> a>A
True
>>> 'abc' > ' abc' # space has less ordinal value
True
>>> 1<=2
True
>>> 'abc' < 'abcd' # second string is greater than the first after c
True
>>> 1==True # The boolean True is equal to 1
True
True
Identity Operators
Python has two identity operators:-
- is
- is not
These operators are orange in color like keywords.
is
is operator return True if both its operands are pointing to same value or object or simply both referring to the same memory location , return False otherwise .
for example
>>> A=10
>>> B=10
>>> A is B
True
is not
is not operator return True if both its operands are pointing to diffrent value or object or simply both referring to the diffrent memory location , return False otherwise .
for example
>>> A=10
>>> B=10
>>> A is not B
False
Comparision between == and is operator
is operator checks the memory location on the other hand == operator only checks the value of object not its location.
Python has two identity operators:-
- is
- is not
These operators are orange in color like keywords.
is
is operator return True if both its operands are pointing to same value or object or simply both referring to the same memory location , return False otherwise .
for example
>>> A=10
>>> B=10
>>> A is B
>>> A is B
True
is not
is not operator return True if both its operands are pointing to diffrent value or object or simply both referring to the diffrent memory location , return False otherwise .
for example
>>> A=10
>>> B=10
>>> A is not B
>>> A is not B
False
Comparision between == and is operator
is operator checks the memory location on the other hand == operator only checks the value of object not its location.
Logical Operators
There are three logical operators present in python:-
- and
- or
- not
and operator in python
In python, The and operator evaluates to True if both its operands are True , if any of them results to False the it evaluates to False.
The and-operator will only test the second operand if and only if the first operand is True, otherwise ignore it.
for example
>>> 5>2 and 1>2
False
>>> 5>2 and 2>1
True
or operator in python
In python, The or opeartor evaluates to True if any of its operands evaluates to True and evaluates to False when both its operands evaluates to False.
The or operator only test the second operand only if the first operand evaluates to False, otherwise ignore it.
for example
>>> 2<1 or 10>20
False
>>> 5>2 or 2>1
True
>>> 5>2 or 1>2
True
not operator in python
The not operator in python reverse or negates the truth value of the expression.
If the expression evaluates to True, not operator evaluated it to False and vice versa.
for example
>>> not (2 < 5)
False
>>> not (10 < 5)
True
There are three logical operators present in python:-
- and
- or
- not
and operator in python
In python, The and operator evaluates to True if both its operands are True , if any of them results to False the it evaluates to False.
The and-operator will only test the second operand if and only if the first operand is True, otherwise ignore it.
for example
for example
>>> 5>2 and 1>2
False
False
>>> 5>2 and 2>1
True
True
or operator in python
In python, The or opeartor evaluates to True if any of its operands evaluates to True and evaluates to False when both its operands evaluates to False.
The or operator only test the second operand only if the first operand evaluates to False, otherwise ignore it.
for example
>>> 2<1 or 10>20
False
>>> 5>2 or 2>1
False
>>> 5>2 or 2>1
True
>>> 5>2 or 1>2
True
True
not operator in python
The not operator in python reverse or negates the truth value of the expression.
If the expression evaluates to True, not operator evaluated it to False and vice versa.
for example
>>> not (2 < 5)
False
>>> not (10 < 5)
True
False
>>> not (10 < 5)
True
Expression
What is an Expression in python?
In python, An expression is a valid combination of operators, variables, literals, and identifiers.
The operator's literals used in expression make the types of expression.
So the expression is of following types:-
- Arithmetic Expression
- Logical Expression
- Relation Expression
- String Expression
The starting three expressions we have already discussed so let's discuss the String expressions.
What is an Expression in python?
In python, An expression is a valid combination of operators, variables, literals, and identifiers.
The operator's literals used in expression make the types of expression.
So the expression is of following types:-
So the expression is of following types:-
- Arithmetic Expression
- Logical Expression
- Relation Expression
- String Expression
The starting three expressions we have already discussed so let's discuss the String expressions.
String Expression in python
Python uses two operators + and * with strings to form String expressions.
The + operator performs the task of the concatenation of strings. i.e., simply joining two or more strings.
for example
>>> 'Chammy' + 'Code'
ChammyCode
The * operator performs the task of multiplication or replication of strings.
for example
>>> 'ChammyCode' * 3
ChammyCodeChammyCodeChammyCode
Download Links
To download the short compact notes of Operators in python and Mutable and Immutable types in python click on the download button
The + operator performs the task of the concatenation of strings. i.e., simply joining two or more strings.
for example
>>> 'Chammy' + 'Code'
ChammyCode
The * operator performs the task of multiplication or replication of strings.
for example
>>> 'ChammyCode' * 3
ChammyCodeChammyCodeChammyCode
Download Links
To download the short compact notes of Operators in python and Mutable and Immutable types in python click on the download button
0 Comments