Introduction to Linux - A Hands on Guide | Linux Bible | Linux From Scratch | A Newbie's Getting Started Guide to Linux | Linux Command Line Cheat Sheet | More Linux eBooks



Sunday, 16 October 2016

Python - Arithmetic, Relational, Logical and Bitwise Operators

Hello readers! This is the 4th article of our tutorial series on Python - 'Python on Terminal' and in this article, we are going to learn about different types of operations in Python. In the last article, we have had a brief overview on Constants, Variables and Data types in Python and in this article, we will be needing them. Most of the part of this article is very simple and self-explanatory, this will be a refresher for you. So, without much a do, we will start our discussion on different kind of operators in Python, with which corresponding operations can be performed. Here we go !

python-arithmetic-relational-logical-bitwise-operator

As you know, the basic building blocks of any programming language are variables and constants, using which we create a series of expressions. In order to write an expression, we require operators and operands. Operators are the symbols that are used to perform different type of computational tasks, while operands are the values (variables, constants, Python objects, etc.) on which we are performing operations. We are discussing operators in this tutorial.

Depending on the type of operations they do, operators are classified as -

  • Assignment operators
  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Bitwise operators
Lets see them one-by-one.

Arithmetic operators

We can assign a value to a variable using assignment statement. We can use the operator = for assigning values to variables.

>>> myNum = 5
>>> myNum
5

>>> myStr = "I Love Python <3"
>>> myStr
'I Love Python <3'

Arithmetic operators

Basic arithmetic operations are addition, subtraction, multiplication, division, modulus and exponentiation. In Python, the operators (or symbols) used for these operations are as below:

  • + - Addition
  • - - Subtraction
  • * - Multiplication
  • / - Division
  • % - Modulus
  • ** - Exponentiation
Example :

# Addition
>>> print 15 + 4
19

# Subtraction
>>> print 15 - 4
11

# Multiplication
>>> print 15 * 3
45

# Division
>>> print 15 / 3
5

# Modulus
>>> print 15 % 4
3

# Exponentiation
>>> print 15 ** 3
3375

Relational operators

Relational operator uses two operands and returns a bool type, either True or False. We have six relational operators in Python (not only in Python, but everywhere else), which are as below:

  • < - Less than
  • <= - Less than or equal to
  • > - Greater than
  • >= - Greater than or equal to
  • == - Equal to
  • != - Not equal to
Lets see each of them in examples below.

Examples :

# Less than
>>> print 15 < 16
True

# Less or equals to
>>> print 15 <= 12
False

# Greater than
>>> print 15 > 12
True

# Greater or equals to
>>> print 15 >= 16
False

# Equals to
>>> print 15 == 16
False

>>> print 15 == 15
True

# Does not equal to
>>> print 15 != 15
False

>>> print 15 != 16
True

Logical operators

There are three kinds of logical operations in Python. Each of them operates on one or more bool type and returns a bool type. They are listed as below:

  • and - Output is True, if all the operands are True, else False
  • or - Output is True, if any one of the operands is True, else False
  • not - Output is True if operand is False, else False.
Example:

>>> x = True
>>> y = False

# NOT Operation
>>> print not x
False

>>> print not y
True

# AND operation
>>> print x and y
False

>>> print x and not y
True

>>> print not x and y
False

# OR Operation
>>> print x or y
True

>>> print not x or y
False

Bitwise operators

So far, we have come across different kinds of operands and operators, but these operators are special ones. Bitwise operators process bits or bit patterns, unlike other operators. Basic bitwise operations are listed as below:

  • >> or Right Shift - Shift bits to right and adds '0's on left (Divide by 2 operation).
  • << or Left Shift - Shifts bits to left and add '0' on right (Multiply by 2 operation)
  • & or Bitwise AND - Performs AND operation on every bit and produces result
  • | or Bitwise OR - Performs OR operation on every bit and produces result
  • ^ or Bitwise XOR - Performs XOR operation on every bit and produces result
  • ~ or Bitwise NOT - Inverts all the bits
Example :

# Right Shift
>>> print 15 >> 3
1                 # '1111' >> 3 = '0001' 

>>> print 15 >> 1
7                 # '1111' >> 1 = '0111'

# Left Shift
>>> print 15 << 3
120               # '1111' << 3 = '1111000'

>>> print 15 << 1
30                # '1111' << 1 = '11110'

# Bitwise AND
>>> print 15 & 3
3                 # '1111' & '0011' = '0011' 

>>> print 15 & 1
1                 # '1111' & '0001' = '0001'

# Bitwise OR
>>> print 15 | 3
15                # '1111' | '0011' = '1111'

>>> print 15 | 1
15                # '1111' | '0001' = '1111'

# Bitwise XOR
>>> print 15 ^ 3
12                # '1111' ^ '0011' = '1100'

>>> print 15 ^ 1
14                # '1111' ^ '0001' = '1110'

# Bitwise NOT
>>> print ~15
-16               # ~ '0000 1111' = '1111 0000'

>>> print ~3
-4                # ~ '0000 0011' = '1111 1100'

>>> print ~1
-2                # ~ '0000 0001' = '1111 1110'

Thus, we can come to an end of this article. In this article, we learned about various operations/operators in Python. In the next article, we would be learning about some more data types (or built-in data structures) in Python. Please let us know about your views and opinions in the comment section below and stay tuned. Thank you.

This article is originally published at www.codeninja.in - Assignment, Arithmetic, Relational, Logical and Bitwise Operations in Python

1 comment:

  1. Good article on operators, congratulations!

    ReplyDelete