Python Operators

What is an Operator ?

An operator is a symbol that performs an operation. An operator acts on some variables, called operands to get the desired result.

Operator of Python

If an operator acts on a single variable, it is called unary operator; if it acts on two variables, it is called binary operator; and if it acts on three variables, then it is called ternary operator.

Operators in Python

  • Arithmetic operators
  • Assignment operators
  • Unary minus operator
  • Relational operators
  • Logical operators
  • Boolean operators
  • Bitwise operators
  • Membership operators
  • Identity operators
Arithmetic Operators

These operators are used to perform basic arithmetic operations like addition, subtraction, division, etc. There are seven arithmetic operators available in Python. Since these operators act on two operands, they are called 'binary operators' also. Let's assume a = 7 and b = 3 and see the effect of various arithmetic operators in the Table

Arithmetic Operators

These operators are used to perform basic arithmetic operations like addition, subtraction, division, etc. There are seven arithmetic operators available in Python. Since these operators act on two operands, they are called 'binary operators' also. Let's assume a = 7 and b = 3 and see the effect of various arithmetic operators in the Table

Operator

Meaning

Example

Result

+

Addition operator. Adds two values.

a+b

10

-

Subtraction operator. Subtracts one value from another.

a -b

4

*

Multiplication Operator. Multiplies values on either side of the operator.

a*b

21

/

Division Operator. Divides left operand by the right operand.

a /b

2.3333333333333335

%

Modulus Operator. Gives remainder of division.

a%b

1

**

Exponent Operator. Calculates exponential power value. a**b gives the value of a to the power of b.

a**b

343

//

Integer Division. This is also called as Floor Division and gives only integer quotient.

a //b

2

 

When there is an expression that contains several arithmetic operators, we should know which operation is done first and which operation is done next. In such cases, the following order of evaluation is used:

  1. First parentheses are evaluated.
  2. Exponentiation is done next.
  3. Multiplication, division, modulus and floor divisions are at equal priority.
  4. Addition and subtraction are done afterwards.
  5. Finally, assignment operation is performed.

Let's take a sample expression: d = (x+y)*z**a//b+c.

Assume the values of variables as: x=1; y=2; z=3; a=2; b=2; c=3.

Then, the given expression d = (1+2)*3**2//2+3 will evaluate as:

  1. First parentheses are evaluated. d = 3*3**2//2+3.
  2. Exponentiation is done next. d = 3*9//2+3.
  3. Multiplication, division, modulus and floor divisions are at equal priority. d = 27//2+3 and then d = 13+3.
  4. Addition and subtraction are done afterwards. d = 16.
  5. Finally, assignment is performed. The value 16 is now stored into 'd'. Hence, the total value of the expression becomes 16 which is stored in the variable 'd'.

For order of precedence, just remember this PEMDAS (similar to BODMAS)

 

Problem Statement

Given an amount, find the minimum number of notes of different denominations that sum upto the given amount. Starting from the highest denomination note, try to accommodate as many notes possible for given amount.


Input

Output

Hints - Calculation

9800

9

4x2000 + 3x500 + 1x200 + 1x100, 4+3+1+1

4500

3

2x2000 + 1x500, 2+1

5500

5

2x2000 + 3x500, 2+3

2000

1

1x2000

 

a=int(input())

t=a//2000

r=a%2000

f=r//500

r=r%500

th=r//200

r=r%200

h=r//100

print ("2000's = ",t)

print ("500's = ",f)

print ("200's = ",th)

print ("100's = ",h)

print ("Total no of notes = ",t+f+th+h)

 

Assignment Operators

These operators are useful to store the right side value into a left side variable. These operators are shown in Table. In Table, let's assume the values x = 20, y = 10 and z = 5:

 

Operator

Example

Meaning

Result

=

z = x + y

Assignment operator. Stores right side value into left side variable, i.e. x+y is stored into z.

 

z = 30

+=

z += x

Addition assignment operator. Adds right operand to the left operand and stores the result into left operand, i.e. z = z+x.

 

z = 25

-=

z -= x

Subtraction assignment operator. Subtracts right operand from left operand and stores the result into left operand, i.e. z = z-x.

z = -15

*=

z *= x

Multiplication assignment operator. Multiplies right operand with left operand and stores the result into left operand, i.e. z = z *x.

z = 100

/=

z /= x

Division assignment operator. Divides left operand with right operand and stores the result into left operand, i.e. z = z/x.

 

z = 0.25

%=

z %= x

Modulus assignment operator. Divides left operand with right operand and stores the remainder into left operand, i.e. z = z%x.

z = 5

**=

z **= y

Exponentiation assignment operator. Performs power value and then stores the result into left operand, i.e. z = z**y.

z= 9765625

//=

z //= y

Floor division assignment operator. Performs floor division and then stores the result into left operand, i.e. z = z// y.

z = 0

 

It is possible to assign the same value to two variables in the same statement as:

a=b=1

print(a, b) #will display 1 1

Another example is where we can assign different values to two variables as:

a=1; b=2

print(a, b) #will display 1 2

The same can be done using the following statement:

a, b = 1, 2

print(a, b) #will display 1 2

A word of caution: Python does not have increment operator (++) and decrement operator (--) that are available in C and Java.

Unary Minus Operator

The unary minus operator is denoted by the symbol minus (-). When this operator is used before a variable, its value is negated. That means if the variable value is positive, it will be converted into negative and vice versa.

For example, consider the following statements:

n = 10

print(-n) #displays -10

num = -10

num = - num

print(num) #displays 10

Relational Operators

Relational operators are used to compare two quantities. We can understand whether two values are same or which one is bigger or which one is lesser, etc. using these operators. These operators will result in True or False depending on the values compared, as shown in Table. In this table, we are assuming a =1 and b = 2.

Operator

Example

Meaning

Result

> 

a > b

Greater than operator. If the value of left operand is greater than the value of right operand, it gives True else it gives False.

False

>=

a >= b

Greater than or equal operator. If the value of left operand is greater or equal than that of right operand, it gives True else False.

False

< 

a < b

Less than operator. If the value of left operand is less than the value of right operand, it gives True else it gives False.

True

<=

a <= b

Less than or equal operator. If the value of left operand is lesser or equal than that of right operand, it gives True else False.

True

==

a == b

Equals operator. If the value of left operand is equal to the value of right operand, it gives True else False.

False

!=

a != b

Not equals operator. If the value of the left operand is not equal to the value of right operand, it returns True else it returns False.

True

 

Relational operators are generally used to construct conditions in if statements. For example,

a=1; b=2

 if (a>b):

          print("Yes")

else:

         print("No")

will display 'No'. Observe the expression (a>b) written after if. This is called a 'condition'. When this condition becomes True, if statement will display 'Yes' and if it becomes False, then it will display 'No'. In this case, (1>2) is False and hence 'No' will be displayed.

Relational operators can be chained. It means, a single expression can hold more than one relational operator. For example,

x=15

10<x<20   #displays True

Here, 10 is less than 15 is True, and then 15 is less than 20 is True. Since both the conditions are evaluated to True, the result will be True.

x=15

10>=x<20    #displays False

Here, 10 is greater than or equal to15 is False, and then 15 is less than 20 is True.

10<x>20 #displays False

Solve the following

1<2<3<4       #displays True

1<2>3<4       #displays False

4>2>=2>1     #displays True

Logical Operators

Logical operators are useful to construct compound conditions. A compound condition is a combination of more than one simple condition. Each of the simple condition is evaluated to True or False and then the decision is taken to know whether the total condition is True or False. We should keep in mind that in case of logical operators, False indicates 0 and True indicates any other number. There are 3 logical operators as shown in Table.

Let's take x = 1 and y=2 in this table.

Operator

Example

Meaning

Result

and

x and y

and operator. If x is False, it returns x, otherwise it returns y.

2

or

x or y

or operator. If x is False, it returns y, otherwise it returns x.

1

not

not x

not operator. If x is False, it returns True, otherwise False.

False

 

x = 100 y = 200

print(x and y) #will display 200

print(x or y) #will display 100

print (not x) #will display False

Boolean Operators

We know that there are two 'bool' type literals. They are True and False. Boolean operators act upon 'bool' type literals and they provide 'bool' type output. There are three Boolean operators as mentioned in Table.

Let's take x = True and y = False in Table

Operator

Example

Meaning

Result

and

x and y

Boolean and operator. If both x and y are True, then it returns True, otherwise False.

False

or

x or y

Boolean or operator. If either x or y is True, then it returns True, else False.

True

not

not x

Boolean not operator. If x is True, it returns False, else True.

False

 

Solve the Following

a= True b= False

a and a         #displays True

b and b         #displaysFalse

a and b         #displaysFalse

a or a            #displays True

a or b            #displays True

b or b            #displays False

not a             #displays False

not b             #displays True

Bitwise Operators

These operators act on individual bits (0 and 1) of the operands. We can use bitwise operators directly on binary numbers or on integers also. When we use these operators on integers, these numbers are converted into bits (binary number system) and then bitwise operators act upon those bits. The results given by these operators are always in the form of integers.

There are 6 types of bitwise operators as shown below:

  • Bitwise Complement operator (~)
  •  Bitwise AND operator (&)
  •  Bitwise OR operator (|)
  •  Bitwise XOR operator (^)
  •  Bitwise Left shift operator (<<)
  •  Bitwise Right shift operator (>>)

 

Bitwise Complement Operator (~)

This operator gives the complement form of a given number. This operator symbol is ~, which is pronounced as tilde. Complement form of a positive number can be obtained by changing 0's as 1's and vice versa. The complement operation is performed by NOT gate circuit in electronics. Truth table is a table that gives relationship between the inputs and the output. The truth table is also given for NOT gate, as shown in Figure

Shortcut for finding the complement of a given number is ~x = -(x+1)

~ 25 = - ( 25 + 1) = -26

~ -31 = - ( -31 + 1) = 30

Bitwise AND Operator (&)

This operator performs AND operation on the individual bits of numbers. The symbol for this operator is &, which is called ampersand. To understand the bitwise AND operation, see the truth table given in Figure

                     X = 10 = 0000 1010

Y = 11 = 0000 1011

X & Y = 0000 1010            (x & y = 10)

If   int x=10, y=12. Find the value of x&y

x = 10  =  0000  1010

y = 12  =  0000   1100         (x&y=8)

Bitwise OR Operator (|)

This operator performs OR operation on the bits of the numbers. The symbol of bitwise OR operator is |, which is called pipe symbol. To understand this operation, see the truth table given in Figure. From the table, we can conclude that by adding the input bits, we can get the output bit. The OR gate circuit, which is present in the computer chip will perform the OR operation.

                     X = 10 = 0000 1010

Y = 11 = 0000 1011

X | Y = 0000 1011              (x|y=11)

 

If   int x=10, y=12. Find the value of x|y

x = 10  =  0000  1010

y = 12  =  0000   1100         (x|y=14)

Bitwise XOR Operator (^)

This operator performs exclusive or (XOR) operation on the bits of numbers. The symbol is ^, which is called cap, carat, or circumflex symbol. To understand the XOR operation, see the truth table given in Figure.


X = 10 = 0000 1010

Y = 11 = 0000 1011

X ^ Y   =  00000001           (x ^ y = 1)

          If   int x=10, y=12. Find the value of x^y

x = 10  =  0000  1010

y = 12  =  0000   1100         (x^y=6)

Bitwise Left Shift Operator (<<)

This operator shifts the bits of the number towards left a specified number of positions. The symbol for this operator is <<, read as double less than. If we write x<<n, the meaning is to shift the bits of x towards left n positions.

If x = 10, calculate x value if we write x<<2.

Shifting the value of x towards left 2 positions will make the leftmost 2 bits to be lost.

The value of x is 10 = 0000 1010. Now, x<<2 will be 0010 1000 = 40 (in decimal). The procedure to do this is explained, as shown in Figure

Bitwise Right Shift Operator (>>)

This operator shifts the bits of the number towards right a specified number of positions. The symbol for this operator is >>, read as double greater than. If we write x>>n, the meaning is to shift the bits of x towards right n positions.

If x = 10, then calculate x>>2 value.

Shifting the value of x towards right 2 positions will make the rightmost 2 bits to be lost. x value is 10 = 0000 1010. Now x>>2 will be: 0000 0010 = 2 (in decimal)

Membership Operators

The membership operators are useful to test for membership in a sequence such as strings, lists, tuples or dictionaries. For example, if an element is found in the sequence or not can be asserted using these operators. There are two membership operators as shown here:

  •   in
  •   not in

The in Operator

This operator returns True if an element is found in the specified sequence. If the element is not found in the sequence, then it returns False.

The not in Operator

This works in reverse manner for 'in' operator. This operator returns True if an element is not found in the sequence. If the element is found, then it returns False.

Identity Operators

These operators compare the memory locations of two objects. Hence, it is possible to know whether the two objects are same or not. The memory location of an object can be seen using the id() function.

This function returns an integer number, called the identity number that internally represents the memory location of the object.

For example, id(a) gives the identity number of the object referred by the name 'a'.

There are two identity operators:

  • is
  • is not

The is Operator

The 'is' operator is useful to compare whether two objects are same or not. It will internally compare the identity number of the objects. If the identity numbers of the objects are same, it will return True; otherwise, it returns False.

The is not Operator

This is not operator returns True, if the identity numbers of two objects being compared are not same. If they are same, then it will return False.

The 'is' and 'is not' operators do not compare the values of the objects. They compare the identity numbers or memory locations of the objects. If we want to compare the value of the objects, we should use equality operator (==).