Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Operators in Python

Uploaded by

hemlatasahu0103
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Operators in Python

Uploaded by

hemlatasahu0103
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Arithmetic Operators in Python

Operator Operation Description Example (Try in shell)


>>> num1 = 5
Adds the two numeric values on >>> num2 = 6
>>> num1 + num2
either side of the operator
11
+ Addition
>>> str1 = “Hi ”
Can also be used to concatenate two >>> str2 = “there”
strings on either side of the operator >>> str1 + str2
‘Hi there’
>>> num1 = 5
Subtracts the operand on the right >>> num2 = 6
– Subtraction >>> num1 – num2
from the operand on the left
–1
Multiplies the two numeric values on >>> num1 = 5
either side of the operator >>> num2 = 6
>>> num1 * num2
30
* Multiplication >>> str1 = ‘India’
If left operand is a >>> str1 * 3
string and right operand is an ‘IndiaIndiaIndia’
integer value, left operand is repeated
the right operand number of times
>>> num1 = 8
Divides the operand on the left by the
>>> num2 = 4
/ Division operand on the right and returns the >>> num1 / num2
quotient 2
>>> num1 = 13
Divides the operand on the left
>>> num2 = 5
% Modulus by the operand on the right and >>> num1 / num2
returns the remainder 3
Performs exponential (power) >>> num1 = 3
calculation on operands (raises >>> num2 = 4
** Exponentiation >>> num1 ** num2
operand on the left to the power of
81
the operand on the right)
>>> num1 = 13
>>> num2 = 4
Divides the operand on the left
>>> num1 // num2
Floor division / by the operand on the right and
// 3
integer division returns the quotient by removing
the decimal part. >>> num2 // num1
0
Relational Operators in Python

num1 = 10 ; num2 = 0 ; num3 = 10 ; str1 = “Good” ; str2 = “Afternoon”

Operator Operation Description Example (Try in shell)


>>> num1 == num2
If the values of two operands are False
== Equals to equal, then the condition is True,
otherwise it is False >>> str1 == str2
False
>>> num1 != num2
True
If values of two operands are not
>>> str1 != str2
!= Not equal to equal, then condition is True, True
otherwise it is False
>>> num1 != num3
False
If the value of the left-side operand >>> num1 > num2
is greater than the value of the right- True
side operand, then condition is True,
otherwise it is False
>>> str1 > str2
> Greater than In case of strings, the Unicode values True
of characters of the string are
compared (this simply means that the
string appearing later alphabetically
is greater)
>>> num1 < num3
If the value of the left-side operand
False
is less than the value of the right-
< Less than
side operand, then condition is True, >>> str2 < str1
otherwise it is False True
>>> num1 >= num2
True
If the value of the left-side operand is
Greater than or greater than or equal to the value of >>> num2 >= num3
>=
equal to the right-side operand, then condition False
is True, otherwise it is False
>>> str1 >= str2
True
>>> num1 <= num2
False
If the value of the left operand is less
Less than or equal than or equal to the value of the right >>> num2 <= num3
<=
to operand, then is True otherwise it is True
False
>>> str1 <= str2
False
Assignment, Augmented Assignment Operators in Python

Operator Description Example (Try in shell)


>>> num1 = 2
>>> num2 = num1
>>> num2
Assigns value from right-side operand to left- 2
=
side operand
>>> country = ‘India’
>>> country
‘India’
>>> num1 = 10
>>> num2 = 2
>>> num1 += num2
>>> num1
It adds the value of right-side operand to the 12
left-side operand and assigns the result to the >>> num2
+= 2
left-side operand
Note: x += y is the same as x = x + y
>>> str1 = “Hello ”
>>> str2 = “India”
>>> str1 += str2
‘Hello India’
>>> num1 = 10
It subtracts the value of right-side operand from
>>> num2 = 2
the left-side operand and assigns the result to
–= >>> num1 –= num2
left-side operand >>> num1
Note: x –= y is same as x = x – y 8
>>> num1 = 2
It multiplies the value of right-side operand >>> num2 = 3
with the value of left-side operand and assigns >>> num1 *= 3
>>> num1
the result to left-side operand
6
*= Note: x *= y is same as x = x * y
>>> a = ‘India’
If operand is a string, it is repeated right >>> a *= 3
operand number of times >>> a
‘IndiaIndiaIndia’
>>> num1 = 6
It divides the value of left-side operand by the
>>> num2 = 3
value of right-side operand and assigns the
/= >>> num1 /= num2
result to left-side operand >>> num1
Note: x /= y is same as x = x / y 2.0
>>> num1 = 7
It performs modulus operation using two
>>> num2 = 3
operands and assigns the result to left-side
%= >>> num1 %= num2
operand >>> num1
Note: x %= y is same as x = x % y 1
>>> num1 = 7
It performs floor division using two operands >>> num2 = 3
//= and assigns the result to left-side operand >>> num1 //= num2
Note: x //= y is same as x = x // y >>> num1
2
>>> num1 = 2
It performs exponential (power) calculation on
>>> num2 = 3
operators and assigns value to the left-side
**= >>> num1 **= num2
operand >>> num1
Note: x **= y is same as x = x ** y 8

Logical Operators in Python

There are three logical operators supported by Python. These operators (and, or, not) are to be
written in lower case only. Every value is logically either True or False and by default, all values are
True except None, False, 0 (zero), empty collections "", (), [], {}, and few other special values. So if
we say num1 = 10, num2 = -20, then both num1 and num2 are logically True.

Operator Operation Description Example (Try in shell)


>>> True and True
True
>>> num1 = 10
>>> num2 = -20
>>> bool (num1 and num2)
True
If both the operands are True
and Logical AND >>> True and False
then condition becomes True
False
>>> num3 = 0
>>> bool (num1 and num3)
False
>>> False and False
False
>>> True or True
True
>>> True or False
If any of the two operands are True
or Logical OR True, then condition becomes >>> bool (num1 or num3)
True True
>>> False or False
False
>>> num1 = 10
>>> bool (num1)
Used to reverse the logical True
not Logical NOT >>> not num1
state of its operand
>>> bool (num1)
False
Identity Operators in Python

Operator Description Example (Try in shell)


>>> num1 = 5
>>> type (num1) is int
True
Evaluates True if the variables on either side of
the operator point towards the same memory >>> num2 = num1
is location and False otherwise. >>> id (num1)
var1 is var2 results to True if id (var1) is 1433920576
equal to id (var2) >>> id (num2)
1433920576
>>> num1 is num2
True
Evaluates to False if the variables on either side >>> num1 is not num2
of the operator point to the same memory False
is not location and True otherwise.
var1 is not var2 results to True if id (var1) is
not equal to id (var2)

Membership Operators in Python

Used to check if a value is a member of given sequence.

Operator Description Example (Try in shell)


>>> a = [1, 2, 3]
>>> 2 in a
Returns True if the variable/value is found in
True
in the specified sequence and False otherwise.
>>> ‘1’ in a
False
>>> a = [1, 2, 3]
Returns True if the variable/value is not found >>> 10 not in a
True
not in in the specified sequence and False otherwise.
>>> 1 not in a
False
Precedence of operators in Python

When an expression contains different kinds of operators, precedence determines which operator
should be applied first. Higher precedence operator is evaluated before the lower precedence
operator.
Unary operators need only one operand, and they have a higher precedence than the binary
operators.

Order of
Operators Description
Precedence
1 ** Exponentiation (raised to the power)
2 ~,+, - Complement, unary plus and unary minus
3 *,/,%,// Multiply, divide, modulo and floor division
4 +,- Addition and subtraction
5 <=,<,>,>= Relational operators
6 ==,!= Equality operators
7 =,%=,/=,//=,-=,+=,*=,**= Assignment operators
8 is, is not Identity operators
9 in, not in Membership operators
10 not, and, or Logical operators

NOTE :
1) Parenthesis can be used to override the precedence of operators. The expression within
() is evaluated first.
2) For operators with equal precedence, the expression is evaluated from left to right.

You might also like