 ## Python Programming – Operators and Expressions

The operator is a symbol that tells the computer to perform certain mathematical or logical manipulations. Operators are used in programs to manipulate data and variables. Operators are basically used to form a part of mathematical and logical expressions. In other words, Operators are the constructs that can manipulate the values of operands. The Python language provides a rich set of operators. Python operators can be classified into a number of categories as follows:

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

Arithmetic Operators

Python language provides all the basic arithmetic operators. As the name implies, these operators are used to perform mathematical operations such as addition (+), subtraction (-), multiplication (*), division (/), and modulo (%). The arithmetic operators are listed in Table 3.1 with the description and example of each of them. Code 3.1 (in script mode) illustrates the use of all arithmetic operators. Suppose x holds the value 40 and y holds the value 20, in Table 3.1 except for floor division.

 Operator symbol Name Example Output Description + Addition x+y 40+20=60 Adds values on either side of the operator. – Subtraction x-y 40-20=20 Subtracts right-hand operand from the left-hand operand. * Multiplication x*y 40*20=800 Multiplies values  on either side of the operator / Division x/y 40/20=2.0 Divides left hand operand by right-hand operand % Modulus x%y 40%20=0 Divides left hand operand by right-hand operand and returns remainder ** Exponent 40**20 Performs exponential (power) calculation on operators // Floor Division x//y(if x=7, y=2) 7//2=3 Division – The division of operands where the result is the quotient in which the digits after the decimal point are removed.

Code: 3.1. Illustration of arithmetic operators.

 # program to represent the use of arithmetic operators x= 10 y=20 z=0z=x+y print(“Addition is “,z)z= x- y print(“Subtraction is “,z)z=x* y print(“Multiplication is “,z)z=x/y print(“Division is”, z)z= x% y print(“Modulus is “, z)x=5 y=3 z=x**y print(“Exponent is “,z) x=10 y=5 z=x//y print (“Floor division is “,z) Output Addition is 30 Subtraction is -10 Multiplication is 200 Division is 0.5 Modulus is 10 Exponent is 125 Floor Division is 2

Comparison Operators

In Python programming, certain decisions are taken based on the relation between expressions or operands. For instance, we may compare the marks of two students or the height of two persons. These comparisons can be performed with the help of comparison operators. In other words, operators compare the values of operands on either side of them and decide the relation among them. These are also termed relational operators. The comparison operators generate the output in terms of True (Non-Zero) and False (Zero). The comparison operators are listed in Table 3.2. Suppose x holds the value 40 and y holds the value 20, in Table 3.2. Code 3.2 illustrates (in script mode) the use of comparison operators.

 Operator symbol Name Example Output Description < Less than x Greater than x>y 40>20 true If the value of left operand is greater than the value of right operand, then condition becomes true. >= Greater than equal to x>=y 40>=20 true If the value of left operand is greater than or equal to the value  of right operand, then condition becomes true. = Equal to x==y 40==20 false If the values of two operands are equal, then the condition becomes true. != Not equal to x!=y 40!=20 true If the values of two operands are not equal, then the condition becomes true. <> Not equal to X<>y 40<>20 true If the values of the two operands are not equal, then the condition becomes true.

Code; 3.2, Illustration of comparison operators

 # program to illustrate the use of relational or comparison operators x= 10 y = 20 if( x = y): print(‘x is equal to y’) else: print(‘x is not equal to y’) if(x !=y): print (‘x is not equal to y’) else: print (‘x is equal to y’) if( x < y): print (‘x is less than y’) else: print (‘x is not less than y’) if( x > y): print (‘x is greater than y’) else: print (‘x is not greater than y’) if( x <= y): print (‘x is either less than or equal to y’) else: print (‘x is neither less than nor equal to y’) if( x >= y): print (‘x is either greater than or equal to y’) else: print (‘x is neither greater than nor equal to y’) Output x is not equal to y x is not equal to y x is less than y x is not greater than y x is either less than or equal to y x is neither greater than nor equal to y
 the result to left operand *= Multiplication then assignment x*=10 is equivalent to x=x*10 It multiplies right operand with the left operand and assigns the result to left operand /= Division then assignment x/=10 is equivalent to x=x/10 It divides left operand with the right operand and assigns the result to left operand %= Modulus then assignment x%=10 is equivalent to x=x%10 It takes modulus using two operands and assigns the result to left operand //= Floor division and assignment x//=10 is equivalent to x=x//10 It performs floor division on operands and assigns value to the left operand **= Exponent and assignment x**=10                    is equivalent to x=x**10 Performs exponential (power) calculation on operands and assigns value to the left operand &= Bitwise AND and assignment x&=10 is equivalent to x=x&10 Performs bitwise AND calculation on operands and assigns value to the left operand I= Bitwise OR and assignment x|=10 is equivalent to x=x|10 Performs bitwise OR calculation on operands and assigns value to the left operand ∧= Bitwise XOR and assignment xA=10 is equivalent to x=xA10 Performs bitwise XOR calculation on operands and assigns value to the left operand >>= Bitwise right shift and assignment x»=10                is equivalent to x=x>>10 Performs bitwise right shift calculation on operands and assigns value to the left operand <<= Bitwise left Shift and assignment X<<=10             is equivalent to x=x<<10 Performs bitwise left shift calculation on operands and assigns value to the left operand

The Code 3.4. illustrates the use of assignment operators, where shorthand operator is used with =, +, *, / and % arithmetic operators.

Code: 3.4. Illustration of assignment and shorthand operators

 # program to illustrate the use of assignment and shorthand operator x= 10 y =20 z =0 z = x + y print(“Value of z is “, z) z += x print(“Value of z is “,z) z*= x print(“Value of z is “, z) z/= X print(“Value of z is “, z) z=3 z%=x print(“Value of z is “, z) Output Value of c is 30 Value of c is 40 Value of c is 400 Value of c is 40.0 Value of c is 3

Logical Operators

The logical operators are used to compare Boolean expressions. The result of a Boolean expression is always a Boolean, i.e., True or False. We can combine the results of multiple relational or arithmetic operations by using logical operators. The logical operators are logical AND (&&), logical OR (||), and logical NOT (!).

The logical AND is used when all the given conditions in an expression need to be satisfied, while Logical OR is used when any of the given conditions need to be satisfied. The negation operator takes only one operand and returns a false value if the operand is true and vice-versa. The logical operators are listed in Table 3.4. Let us assume the value of x=10 and y=20.

 Operator Name Name Example Output Description && Logical AND (xy)&&(x==10) False (x>y)||(x!=l 0) False (xy) | |(x== 10) True (x>y)||(x!=10) False ! Logical NOT !(x>y) True Used to reverse the logical state of its operand. !(x

The Code 3.5. illustrates the use of logical operators. In the program, only the expressions computing to true result are coded.

Code: 3.5. Illustration of logical operators

 # program to illustrate the use of logical operator x= 10 y = 20 if( xy or x==10): print(‘true’) if (not(x>y)): print(‘true’) Output true true true true

Bitwise Operators

The bitwise operators assume operands as strings of bits and the bit operations are performed on these operands. Moreover, the python language allows us to operate directly at bit level using bitwise operators. As the
operation is performed bit by bit, let us assume the values x=35 and y=10. Their binary equivalents are given as follows:

x 00100011
y 00001010

Now the bitwise AND, OR, XOR, one’s complement, left shift and right shift operations are performed as shown in Table 3.5.

 Operator Operation Bitwise AND X&y = 00000010 Bitwise OR xIy = 00101011 Bitwise XOR x∧y = 00101001 Bitwise ones compliment ∼X = 11011100 Bitwise left shift X<<1 = 01000110 Bitwise right shift x>>1 = 00010001

The bitwise operators are listed in table 3.6. with x=35 and y=10

 Operator symbol Name Example Output Description & Bitwise AND X&y 2 Operator copies a bit to the result if it exists in both operands | Bitwise OR x|y 42 It copies a bit if it exists           in either operand. ∧ Bitwise XOR x∧y 40 It copies the bit if it is set in one operand but not both. ∼ Bitwise ones compliment ~x 120 It is unary and has the effect of ‘flipping’ bits. << Bitwise left shift X<<=l 70 The left operands value is moved left by the number of bits specified by the right operand. >> Bitwise right shift x>>=l 17 The left operands value is moved right by the number of bits specified by the right operand.

As shown in the above example in Table 3.7., if one bit is moved to the left side x«=l using bitwise left shift operator then the outcome results in a multiple of 2, i.e., the value of x becomes 70, which was initially 35. Consequently, if 2 bits are moved to the left then the result is a multiple of 4 and so on.

On the other hand, in the bitwise right shift operator, if one bit is moved to the right then the outcome is the integer division by 2. In the above example, x»=l, results in 17 where the initial value of x was 35.

Summary

In this chapter, we have learned about different operators such as arithmetic, comparison, logical, bitwise, special operators, identity, and membership operators available in the Python language. All the operators are described with appropriate examples of each. We have learned that how operators and operands form an expression, which is the basic sentence of a programming language. The precedence decides which operator will be evaluated first and associativity decides how to evaluate an expression if two operators exhibit the same precedence. Finally, we have learned how Python language evaluates expressions.

Python Tutorial