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 

Recommended Python Training – Treehouse

Treehouse For Python training, our top recommendation is Treehouse.

Treehouse is an online training service that teaches web design, web development and app development with videos, quizzes and interactive coding exercises.

Treehouse has beginner to advanced Python training that programmers of all levels benefit from.

Read more about:
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, PythonForBeginners.com only recommend products or services that we try personally and believe will add value to our readers.