Python Operators Guide – Trenovision

Python – Basic Operators

Operators are those which performs operation on operand.We will know about Baisc Python operators​ in this post.

 

What is Python ?

  • Python is an Interpreter
  • Python is Interactive
  • Python is Object-Oriented programming language
  • Python focusses on simplicity and generality.
  • Reduces cost of program maintenance

 

Why Python ?

  • Easy-to-learn, read and maintain
  • Aids in rapid development of complex projects
  • Portable, Extendable and scalable
  • Supports broad range of standard modules for Networking,
  • Database Handling and  GUI Programming
  • It is Free language but very stable and mature.

Use of Python

  • Python is extensively used in Google, the New York Stock Exchange, Industrial Light and Magic
  • It can be used for :-
    • Web and Internet Development
    • Database Access
    • Desktop GUIs
    • Scientific and Numeric
    • Education
    • Network Programming
    • Software Development
    • Game and 3D Graphics

Python Operators – Types

In this post we will learn about use of below operators in Python.

  • Arithmetic Operators
  • Comparison Operators
  • Assignment Operators
  • Bitwise
  • Logical Operators
  • Membership Operators
  • Identity Operators

Arithmetic Operators

  • Arithmetic Operators

Operator

Description

Example

+

Addition

int i = 8 + 9; byte b = (byte) 5+4;

Subtraction

int i = 9 – 4;

Multiplication

int i = 8 * 6;

/

Division

int i = 10 / 2;

%

Remainder

int i = 10 % 3;

 

  • ** -> Exponential
  • // -> Floor division

Operators – Relational Operators/Conditional Operators

 

  1. Equal = =
  2. Not equal !=
  3. Less than <
  4. Greater than >
  5. Less than equal to <=
  6. Greater than equal to >=
  7. comparison cmp
  • Relational Operators

Operator

Description

Example

>

Greater than

if ( x > 4)

<

Less than

if ( x < 4)

>=

Greater than or equal to

if ( x >= 4)

<=

Less than or equal to

if ( x <= 4)

 

  • Conditional Operators ​(java || and Java &&)

Operator

Description

Example

&&

Conditional and

If (a == 4 && b == 5)

||

Conditional or

If (a == 4 || b == 5)

Assignment operators

 

  1. =
  2. +=
  3. -=
  4. *=
  5. /=
  6. %=
  7. **= -> a **= b e a = a ** b
  8. //= -> a //= b e a = a // b
  9. Multiple assignments in a line

a  =  b  =  c  =  100;

Note the addresses of a, b and c.

del  a. Observe the addresses and values of a, b  and c.

Change the value of c. Note the addresses and values of a, b  and c.

Operator Description Example
= Assigns values from right side operands to left side operand c = a + b assigns value of a + b into c
+= Add AND It adds right operand to the left operand and assign the result to left operand c += a is equivalent to c = c + a
-= Subtract AND It subtracts right operand from the left operand and assign the result to left operand c -= a is equivalent to c = c – a
*= Multiply AND It multiplies right operand with the left operand and assign the result to left operand c *= a is equivalent to c = c * a
/= Divide AND It divides left operand with the right operand and assign the result to left operand c /= a is equivalent to c = c / ac /= a is equivalent to c = c / a
%= Modulus AND It takes modulus using two operands and assign the result to left operand c %= a is equivalent to c = c % a
**= Exponent AND Performs exponential (power) calculation on operators and assign value to the left operand c **= a is equivalent to c = c ** a
//= Floor Division It performs floor division on operators and assign value to the left operand c //= a is equivalent to c = c // a

 

Bitwise Operators

 

  1. & ->   bitwise And
  2. | ->   bitwise OR
  3. ^ ->   bitwise XOR
  4. ~ ->   binary one’s complement
  5. << -> binary Left Shift Operator
  6. >> -> binary Right Shift Operator

Operator

Description

Example

&

Bitwise and

001 & 111 =  1 

|

Bitwise or

001 | 110 = 111

^

Bitwise ex-or

001 ^ 110 = 111

~

Reverse

~011 = -10

 

Logical Operators

 

  1. and
  2. or
  3. not

Operator

Description

Example

&

Bitwise and

001 & 111 =  1 

|

Bitwise or

001 | 110 = 111

^

Bitwise ex-or

001 ^ 110 = 111

~

Reverse

~011 = -10

Membership / Identity operators

 

  • Membership Operators :-

– in  -> x in y  returns True if x is a member of sequence y.

–not  in -> x not in y  returns True if x is NOT a member of sequence y.

  • Identity Operators :-

–is  -> x is y, here is results in 1 if id(x) equals id(y).

–is not -> x is not y, here is not results in 1 if id(x) is not equal to id(y).

 

Python Operator Precedence

Operator Description
** Exponentiation (raise to the power)
~ + – Complement, unary plus and minus (method names for the last two are +@ and -@)
* / % // Multiply, divide, modulo and floor division
+ – Addition and subtraction
>> << Right and left bitwise shift
& Bitwise ‘AND’
^ | Bitwise exclusive `OR’ and regular `OR’
<= < > >= Comparison operators
<> == != Equality operators
= %= /= //= -= += *= **= Assignment operators
Is  ,  is not Identity operators
in  ,  not in Membership operators
not  , or , and Logical operators

What is IronPython ? History of IronPython

Continue reading at https://www.trenovision.com/what-is-ironpython-history-of-ironpython/