Python Basic Operators - Picone Press

[Pages:5]PYTHON BASIC OPERATORS

rialspo int.co m/pytho n/pytho n_basic_o pe rato rs.htm

Co pyrig ht ? tuto rials po int.co m

What is an operator?

Simple answer can be g iven using expression 4 + 5 is equal to 9. Here, 4 and 5 are called operands and + is called operator. Python lang uag e supports the following types of operators.

Arithmetic Operators Comparison (i.e., Relational) Operators Assig nment Operators Log ical Operators Bitwise Operators Membership Operators Identity Operators Let's have a look on all operators one by one.

Python Arithmetic Operators:

Assume variable a holds 10 and variable b holds 20, then: [ Show Example ]

O perator Desc ription

+

Addition - Adds values on either side of the

ope rator

-

Subtraction - Subtracts rig ht hand operand

from left hand operand

*

Multiplication - Multiplies values on either side

of the ope rator

/

Division - Divides left hand operand by rig ht

hand operand

%

Modulus - Divides left hand operand by rig ht

hand operand and returns remainder

**

Exponent - Performs exponential (power)

calculation on operators

//

Floor Division - T he division of operands

where the result is the quotient in which the

dig its after the decimal point are removed.

E xamp le a + b will g ive 30 a - b will g ive -10 a * b will g ive 200 b / a will g ive 2 b % a will g ive 0 a**b will g ive 10 to the power 20 9//2 is equal to 4 and 9.0//2.0 is equal to 4.0

Python Comparison Operators:

Assume variable a holds 10 and variable b holds 20, then: [ Show Example ]

O perator Desc ription

==

Che cks if the value of two ope rands are e qual

or not, if yes then condition becomes true.

!=

Che cks if the value of two ope rands are e qual

or not, if values are not equal then condition

becomes true.

Che cks if the value of two ope rands are e qual

or not, if values are not equal then condition

becomes true.

>

Checks if the value of left operand is g reater

than the value of rig ht operand, if yes then

condition becomes true.

<

Checks if the value of left operand is less than

the value of rig ht operand, if yes then condition

becomes true.

>=

Checks if the value of left operand is g reater

than or equal to the value of rig ht operand, if

yes then condition becomes true.

b) is not true.

(a < b) is true.

(a >= b) is not true.

(a ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download