Saturday, 10 August 2019

How to do operators work in python

How to do operators work in python

Operators: An operator indicates an operation to be performed on data. An operation is a data item on which operators perform operations.

Python supports operators are as:

  • Unary Operators
  • Arithmetic Operators
  • Bitwise Operators
  • Logical Operators
  • Identify Operators
  • Relational Operators
  • Assignment Operators
  • Membership Operators


Let’s discuss

Unary operator python 3

Unary Operators: Unary Operators are those operators that require one operand to operate on it.It means that the operators perform an action on one operand.

For Example:

Unary +

If a=6, +a means 6
If a=0, +a means 0
If a=-3, +a means -3

Unary –

If a=6, -a means -6
If a=0, -a means 0
If a=-4, -a means 4

Function in python arithmetic operators | arithmetic operator in python with example

Arithmetic Operators: These operators are used to perform basic calculations such as addition (+), subtraction (-), multiplication (*) etc. It requires two operands to perform operations.
‘+’ and ‘– ‘operators can be binary operator and unary operator. A unary operator takes only one operand. Binary operator takes two operands to perform some operation.

Let’s discuss

Arithmetic operator examples in python

Operators
Example
+ (Addition)
15+5 =20
- (Subtraction)
15-5=10
* (Multiplication)
15*5=75
/ (Float Division)
15/5=3.0
// (Integer Division)
15//5=3
** (Power/Exponentiation)
15**5=759375
49.0**0.5=7.0
% (Modulus/Remainder)
15%5=0
7.2%3=1.2
6%2.5=1.0

Screenshot:


Python operator example with answer



‘//’ Integer Division Operator returns integer value.

‘/’ Float Division Operator returns float value.

‘%’ Modulus Operator returns remainder.

‘**’ It returns the result of a number raised to a power.


Bitwise Operator in python

Bitwise Operators: Bitwise operators are used to change individual bits in an operand. These are similar to logical operators but bitwise operators work on binary representations of data.

For Example:

‘&’   Bitwise and: This is the AND operator. It compares two bits and generates a result of 1, If both bits are 1 otherwise it returns 0.

‘|’   Bitwise or: This is the OR operator. It compares two bits and generates a result of 1, If both bits are complementary otherwise it returns 0.

‘^’   Bitwise xor: This is the EXCLUSIVE-OR(XOR) operator. It compares two bits and returns 1 either of the bits are 1 and it gives 0 if both bits are 0 or 1.

‘~’   Bitwise complement: This is the Complement (~) operator. It is used to invert all the bits of the operand.

Operator
Use
& (bitwise AND)
13 & 12=12
| (bitwise OR)
13|12=13
^ (bitwise XOR)
13^12=1
~ (bitwise Complement)
~12=-13

 Screenshot

Example of Bitwise operator in python








No comments:

Post a Comment

Recent Post

Python Project | Banking System | Project on Banking System using python

  PYTHON PROJECT Banking System import csv import pandas as pd import matplotlib.pyplot as plt import sys import datetime found=False def ne...