Sunday, August 2, 2015

Python Basics - 009 - Operators in Python 3

Previous chapter: Control Statements Next Chapter: Lists and Tuples



Operators in Python 3 : Complete reference.

There are many types of operators, we have already met the arithmetic operators, we also have used some comparison operators on the previous post/chapter.

Arithmetic Operators

Operator
Description
Example
+
Addition
varSum = 10 + 5
-
Subtraction
varDiff = 10 - 5
*
Multiplication
varProd = 10 * 5
/
Division
varDiv = 10 / 5
%
Modulus, gives remainder of a division
varMod = 10 % 3
# varMod value will be 1
**
exponent, power of operator
varResult = 4 ** 2
# gives me 4 to the power of 2, i.e 16
//
Floor division: Gives the quotient of a division operation as an integer by removing the digits after the decimal point
varFloor = 4 // 3
# gives me 1


Comparison Operators


Operator
Description
==
Equal to, to check if two operands are equal to one another
!=
Not equal to : to check if operands are different
<>
Not equal to : another way of expressing !=
>
Greater than
<
less than
>=
Greater than or equal to
<=
Less than or equal to


Python Bitwise Operators

This is something we'll almost never use, but let it hang in here for reference.

These are operators which are used when the operands are in binary form or if you want to perform a bitwise operation on a variable.

Consider two variables:

var1 = 50
var2 = 10

In binary format, their values can be represented as

var1 => 0110 0000
var2 => 0001 0000

Let's use the above variable's values in the examples to get a clear picture. And as I've said before, this is almost never used in any common scenario, just know that if you want it, it will be here.

Here's a page[1] explaining bitwise operations described below and how they are calculated.


Operator
Description
Example
&
Binary AND operator
var1 & var2 => 0000 0000
|
Binary OR operator
var1 | var2 => 0111 0000
^
Binary XOR operator
var1 ^ var2 =>0111 0000
~
Binary one's complement
~var1 => 1001 1111
<<
Binary Left Shift operator
a << 2 ==> 1000 0000
>>
Binary Right Shift operator
a >> 2 ==> 0001 1000


Logical Operators

Logical operators are used to combine multiple conditional statements together, it is always an operation performed between two boolean values, or conditions or functions or statements which return boolean values.


Operator
Description
Example
and
Logical AND operator
a > 10 and b > 10
or
Logical OR operator
a > 10 or b > 10
not
Logical NOT operator
NOT ( a > 10)
# when 'a' is not greater than 10


Assignment Operators

The basic assignment operator is the equal to (=) sign, all other assignment operators are a combination of arithmetic and equal to sign.

For example a = 10, assigns the value 10 to the variable a.

a += 10, adds a value of 10 to variable a. This is a short form of expressing a = a + 10, this style is followed by using all other arithmetic operators

Here's an exhaustive list:


Operator and usage
What it means
a = 10
10 is assigned to variable a
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


Identity Operators

This is an operator type which can be used with object operands and not primitive data types. it is used to check the similarity of object operands.

In the examples below consider obj1 and obj2 as objects and we assign the obj1 to obj2 like below then both objects are one and the same.

obj2 = obj1

Now lets see the operators and use the above conditions in the examples to better make sense of it.


Operator
Description
Example
is
returns True if operands are the same object
obj1 is obj2 # result is True
is not
returns True if operands are not the same object
obj1 is not obj2 # returns False


Membership Operators

These concern Lists and tuples, doesn't make sense before learning about them, but there is nowhere else to put this information other than with the operator reference page.

These operators are used to find if an object is a part of a list or a tuple (which is also a kind of list).

Consider eggs to be a list of all kinds of eggs

eggs = ["good egg", "bad egg", "quail egg", "ostritch egg"]

Operator
Description
Example
in
to check if an object is part of the second operand which is a list
if "good egg" in eggs:
    print("yes")
not in
to check if an object is not present in the list
if "good egg" not in eggs:
    print("No good eggs")



Operator Precedence [2]

Operator precedence means which operator is considered more important when two operators are present in the same statement.

The operators are listed in the order of highest to lowest precedence.


Operator
Description
**
exponentiation, power of operator
~ + -
complement, unary plus and minus
* / % //
Multiply, divide, Modulus and Floor Division
+ -
Addition, subtraction
>> <<
Right and left bitwise shift operator
&
bitwise AND
^ |
bitwise XOR and OR
<= < > >=
comparison operators
<> == !=
equality operators
=, +=, -=, *=, /=, //=, **=
assignment operators
is, is not
Identity operators
in, not in
Membership operators
not, or, and
logical operators



References:

  1. https://en.wikipedia.org/wiki/Bitwise_operation
  2. https://docs.python.org/3.5/reference/expressions.html#operator-precedence
  3. https://docs.python.org/3.5/reference/expressions.html#the-power-operator



Previous chapter: Control Statements Next Chapter: Lists and Tuples

No comments:

Post a Comment