Basic Operators in Python for Calculations - PYTHON MADE SIMPLE (2015)

PYTHON MADE SIMPLE (2015)

Chapter 9: Basic Operators in Python for Calculations

Operators are the functions which can be used in the manipulation of the values of the operands. For instance, let us consider the expression 5 + 15 = 20. In this example: 5 and 15 are referred to as operands, while the plus sign (+) is referred to as the operator.

Operators

Python language supports the following types of operators.

· Arithmetic operators

· Comparison operators for establishing relations between values

· Assigning operators for equating values to a name

· Logic operator for determining the logic between values on either side of the operators.

· Membership operators

· Operators for influencing the identity of objects/values

Here is a list of the various forms of operators and examples on how they can be applied in a real python algorithmic development scenario in programming. They have been looked at one by one.

Python Arithmetic Operators

Let is assume that a variable named (a) has a value of 5 and variable (b) holds a value of 15, therefore:

Operator

Description

Example

+

Sums up the values of the values on either side of the operator.

a + b = 20

-

Gives the difference of the two values on either side of the operator

a - b = -10

*

Gives you a product of values on either sides of the operator

a * b = 75

/

Provides a division of the two values

b / a = 3

%

Divides left by right hand values of the operand and gives the value of the remainder

b % a = 0

**

Makes the second value the exponential power of the first value

a**b =5 to the power 15

//

This is also referred to as the Floor Division In this division, the digits after the decimal point are removed from the result of the quotient.

Operators for comparison of values

Comparison operators make a comparison of the values to the left and right of the operand and helps define relationship between the values as set by the operand. They are also referred to as the relational operators. Use the values for (a) and (b) as illustrated above.

Operator

Description

Example

==

It sets as a condition for the values to the left and right if the operand to be true only when they are equal.

It is not true when (a == b)

!=

It sets as a condition for the two values to be true only when the values are not equal

Same as above

<>

It sets the condition to be true when the values of the operands are not equal.

It is true only when (a <> b).

The application is similar to the sign !=

>

This emphasizes on the value of the left operand. When it has a higher value than the right value, It accepts the condition as true.

It is not true if (a > b)

<

It also emphasizes on the value to the left of the operand. When the value is less than the right value, it accepts the set condition as true.

It is true when (a < b).

>=

This is applied only when the value of the left operant is higher or same as the value to the right operand. This recognizes the condition as true.

It is not true when (a >= b).

<=

If the value to the left is lower than or equals to right. This enables the condition as set to be true.

It is true when (a <= b).

Python for Assigning Operators

Operator

Description

Example

=

It assigns the value of the right hand side of the operand to the left hand side.

c = a + b

+= Add AND

This operant adds values on the right to left operand and equates sum to the left operand

c += a is equal to c = c + a

-= Subtract AND

It gets the difference between the right from left operand and gives the division to left operand

c -= a is also c = c - a

*= Multiply AND

Its action is same as above but then as a product of the right and left operand and makes the result equal to the left operand.

c *= a is same as c = c * a

/= Divide AND

Same as above but makes a division

c /= a is same as c = c / ac /= a and also c = c / a

%= Modulus AND

Same as above but makes floor division and gives a quotients and equates the quotient to the left operand

c %= a is equivalent to c = c % a

**= Exponent AND

Assigns an exponential (power) on operators and equates to the value of the left operand

c **= a is same as c = c ** a

//= Floor Division

It creates a division of operators and equates the value calculated as equal to the left value of the operand

c //= a is expressed as c = c // a

Python Membership Operators

These operators evaluate membership of values in a sequence in various python data forms such as strings, lists, or tuples. There are only two forms of python membership operators:

Operator

Description

Example

is

It evaluates to be true when the variables on either side of the operators are similar or equates to the same object. But it is false otherwise.

x is y.

not in

It becomes true when a variable in the sequence is not found and otherwise, becomes false.

x not in y

Python Identity Operators

This only serves to make a comparison of two memory locations for two different objects. Similarly, just like membership operators, there are only two membership operators as explained below:

Operator

Description

Example

is

Evaluates to true if the variables on either side of the operator point to the same object and false otherwise.

x is y

is not

Evaluates to false if the variables on either side of the operator point to the same object and true otherwise.

x is not y

Python Operators ordered list

The table below ranks the operators in terms of their precedence in descending order.

Operator

Description

**

Exponential (raises one value to the power of the other)

~

Complement,

+

plus

-

unary minus

*

Multiply

/

divide

%

modulo

//

floor division

+

Addition

-

Subtractions

>>

Right shift bitwise

<<

left bitwise

&

Bitwise 'AND'

^ |

exclusive `OR' and regular `OR' bitwise

<= >

Comparison operators

< >

comparison

=

comparison

<> == !=

Equality operators

= %= /= //= -= += *= **=

Assigning values operators

Is, is not

Identity operators

in not in

Creating membership operators

not or and

Creating logical operators