Operators
Operators
1
Python - Basic Operators
Python language supports following type of operators.
• Arithmetic Operators
• Comparison Operators
• Assignment Operators
• Bitwise Operators
• Logical Operators
• Conditional (or ternary) Operators
2
Python Arithmetic Operators:
Assume a=10 b=20
Operator Description Example
+ Addition - Adds values on either side of the operator a + b will give 30
* Multiplication - Multiplies values on either side of the operator a * b will give 200
/ Division - Divides left hand operand by right hand operand b / a will give 2
Floor Division - The division of operands where the result is 9//2 is equal to 4
// the quotient in which the digits after the decimal point are and
removed. 9.0//2.0 is equal to 4.0
3
Python Comparison Operators: Assume a=10 b=20
Operator Description Example
Checks if the value of two operands are equal or not,
== if yes then condition becomes True. (a == b) is False.
4
Python Assignment Operators:
Operator Description Example
c = a + b will assign
= Assigns values from right side operands to left side operand value of a + b into c
It adds right operand to the left operand and assign the result c += a is equivalent to
+= to left operand c=c+a
It subtracts right operand from the left operand and assign the c -= a is equivalent to
-= result to left operand c=c-a
It multiplies right operand with the left operand and assign the c *= a is equivalent to
*= result to left operand c=c*a
It divides left operand with the right operand and assign the c /= a is equivalent to
/= result to left operand c=c/a
It takes modulus using two operands and assign the result to c %= a is equivalent to
%= left operand c=c%a
Performs exponential (power) calculation on operators and c **= a is equivalent to
**= assign value to the left operand c = c ** a
Performs floor division on operators and assign value to the c //= a is equivalent to
//= left operand c = c // a
5
Python Bitwise Operators: a=60(111100) b=13(001101)
7
Python Membership Operators:
In addition to the operators discussed previously, Python has membership operators, which test for
membership in a sequence, such as strings, lists, or tuples.
8
Python Operators Precedence
Operator Description
** Exponentiation (raise to the power)
Complement, unary plus and minus
~+- (method names for the last two are +@ and -@)
* / % // Multiply, divide, modulo and floor division
+- Addition and subtraction
>> << Right and left bitwise shift
& Bitwise 'AND'
^| Bitwise exclusive `OR' and regular `OR'
<= < > >= Comparison operators
<> == != Equality operators
Sample Input 1:
1200
Sample Output1:
12
Sample Input 2:
242
Sample Output2:
7 13
Lucky Gifts
Krishna is Chocolate lover.
In an event, he was asked to distribute chocolates to few lucky attendees.
5
There are 'N' chocolates available. He need to decide how many Chocolates (P) to place in each pack. Each
pack must contain the same number of chocolates. Place exactly P chocolates into each pack. He should
make as many packs as possible.
Write a program that will calculate the pack size P so that he can eat as many chocolates as possible.
If multiple pack size will result in the same number of leftover candies, then print the largest pack size.
Number of hours Employee-X has worked in the weekdays is 10 more than the number of
hours he had worked during weekends. If the total salary paid to him in this month is
known, write a program to estimate the number of hours he had worked during weekdays
and the number of hours he had worked during weekends.
Given the total number of people who have attended the event in the first 3 days, find the
number of people who have attended the event on day 1, day 2 and day 3.