Jan. 21, 2013

Python Operators

Arithmetic Operators

Python includes the +, -, *, /, % (modulus), and ** (exponentiation) operators
Assume variable a holds 10 and variable b holds 20 then:

Operator Description Example
+   Addition  a + b will give 30
-   Subtraction   b will give -10
*    Multiplication  a * b will give 200
/    Division  b / a will give 2
%    Modulus   b % a will give 0
**   Exponent  a**b will give 10 to the power 20
//   Floor Division  9//2 is equal to 4 and 9.0//2.0 is equal to 4.0

Comparison Operators

The basic comparison operators such as ==, <, >=, and so forth are used on all
manner of values. 

Numbers, strings, sequences, and mappings can all be compared.
The following table shows a list of comparison operators.

Operator Description
<     less than
<=     less than or equal
==    equal
>    greater than
>=    greater than or equal
!=     not equal
<>     not equal 

Logical Operators

The logical operators and and or also return a Boolean value when used in
a decision structure. 

There are three logical operators: and, or, and not. 

For example, x > 0 and x < 10 is true only if x is greater than 0 and less than 10

Operator Description
and     logical AND
or     logical OR
not     logical NOT 


Read more about: