0% found this document useful (0 votes)
8 views26 pages

5.operators in Python

Uploaded by

ltmonu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views26 pages

5.operators in Python

Uploaded by

ltmonu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

OPERATORS IN PYTHON

PYTHON OPERATORS

• Operators are used to perform operations on


variables and values.
• Python divides the operators in the following
groups:
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Identity operators
• Membership operators
• Bitwise operators
PYTHON ARITHMETIC
OPERATORS

Arithmetic operators are used with numeric values to perform


common mathematical operations:
PROBLEM 1

ABC company Ltd. is interested to


computerize the pay calculation of their
employee in the form of Basic Pay, Dearness
Allowance (DA) and House Rent Allowance
(HRA). DA is 80% of Basic Pay, and HRA is
30% of Basic pay. They have the deduction in
the
Inputsalary as PF which is 12%
Processing of Basic
Output pay.
Solution
Alternative
Basic Calculate Salary Salary Nothing
Pay
PROBLEM 1

ABC company Ltd. is interested to


computerize the pay calculation of their
employee in the form of Basic Pay, Dearness
Allowance (DA) and House Rent Allowance
(HRA). DA is 80% of Basic Pay, and HRA is
30% of Basic pay. They have the deduction in
the
Inputsalary as PF which is 12%
Processing of Basic
Output pay.
Solution
Alternative
Basic Calculate Salary Salary Nothing
Pay ( Basic Pay + ( Basic
Pay * 0.8) + ( Basic Pay
* 0.3 - ( Basic Pay *
0.12)
PYTHON CODE

#Enter the basic pay


bp=float (input('Enter the basic pay:'))
# net pay calucluation
netpay =bp + (bp*0.8) + (bp*0.3) -
(bp*0.12)
# display net salary
print ('Net pay :',netpay)
OUTPUT
P Y T H O N A S S I G N M E N T O P E R AT O R S

Assignment operators are used to assign values to variables:

Operator Example Same As


= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3
**= x **= 3 x = x ** 3
&= x &= 3 x=x&3
|= x |= 3 x=x|3
^= x ^= 3 x=x^3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
PYTHON COMPARISON OPERATORS

• Comparison operators are used to compare two values:

Operator Name Example


== Equal x == y

!= Not equal x != y

> Greater than x>y

< Less than x<y

>= Greater than or equal to x >= y

<= Less than or equal to x <= y


PYTHON LOGICAL OPERATORS

• Logical operators are used to combine conditional statements:

Operator Description Example

and Returns True if both statements are x < 5 and x < 10


true

or Returns True if one of the x < 5 or x < 4


statements is true

not Reverse the result, returns False if not(x < 5 and x < 10)
the result is true
EXAMPLE PROBLEM

• Write a Python program to check whether a blood donor is


eligible or not for donating blood. The conditions laid down
are as under. Use if statement.

(a) Age should be above 18 years but not more than 55 years.

(b) Weight should be more than 45 kg.


PYTHON PROGRAM

age = int(input('Enter age'))

weight = float(input('Enter weight'))

donate = 'donate' if (age > 18 and age <=55) and (weight>45)

else 'do not donate'

print(donate)
PYTHON IDENTITY
OPERATORS

• Identity operators are used to compare the objects, not if they are
equal, but if they are actually the same object, with the same memory
location:

Operator Description Example

is Returns True if both x is y


variables are the same
object
is not Returns True if both x is not y
variables are not the
same object
Example :
x = ["apple", "banana"]
y = ["apple", "banana"]
z=x

print(x is z)

# returns True because z is the same object as x

print(x is y)

# returns False because x is not the same object as y, even if they have the
same content

print(x == y)

# to demonstrate the difference betweeen "is" and "==": this comparison


returns True because x is equal to y.
Example :
x = ["apple", "banana"]
y = ["apple", "banana"]
z=x

print(x is not z)

# returns False because z is the same object as x

print(x is not y)

# returns True because x is not the same object as y, even if they have the same
content

print(x != y)

# to demonstrate the difference between "is not" and "!=": this comparison returns
False because x is equal to y
PYTHON MEMBERSHIP OPERATORS

• Membership operators are used to test if a sequence is


presented in an object:

Operator Description Example

in Returns True if a x in y
sequence with the
specified value is
present in the object
not in Returns True if a x not in y
sequence with the
specified value is not
present in the object
Example :
a = 10
b = 20
list = [1, 2, 3, 4, 5 ];

if ( a in list ):
print ("Line 1 - a is available in the given list“)
else:
print ("Line 1 - a is not available in the given list“)

if ( b not in list ):
print ("Line 2 - b is not available in the given list“)
else:
print ("Line 2 - b is available in the given list“)
• OUTPUT :
Line 1 - a is not available in the given list
Line 2 - b is not available in the given list
PY T H O N B I T W I S E O P E RAT O R S

• Bitwise operators are used to compare (binary) numbers:

Operator Name Description Example


& AND Sets each bit to 1 if both bits are 1 x&y

| OR Sets each bit to 1 if one of two bits is 1 x|y

^ XOR Sets each bit to 1 if only one of two bits is x ^ y


1
~ NOT Inverts all the bits ~x
<< Zero fill Shift left by pushing zeros in from the x << 2
left shift right and let the leftmost bits fall off

>> Signed Shift right by pushing copies of the x >> 2


right leftmost bit in from the left, and let the
shift rightmost bits fall off
O PE RAT OR PR E CED EN CE

• Operator precedence describes the order in which operations are performed.


• The precedence order is described in the table below, starting with the
highest precedence at the top:
Operator Description
() Parentheses
** Exponentiation
+x -x ~x Unary plus, unary minus, and bitwise NOT
* / // % Multiplication, division, floor division, and
modulus
+ - Addition and subtraction
<< >> Bitwise left and right shifts
& Bitwise AND
^ Bitwise XOR
| Bitwise OR
== != > >= < <= is is not in not in Comparisons, identity, and membership operators
not Logical NOT
and AND
or OR
ORDER OF OPERATIONS
Operator Operation Precedence
() parentheses 0
** exponentiation 1
* multiplication 2
/ division 2
// int division 2
% remainder 2
+ addition 3
- subtraction 3
OPERATOR PRECEDENCE

• If two operators have the same precedence, the expression is


evaluated from left to right.
• Example
• Addition + and subtraction - has the same precedence, and therefor we
evaluate the expression from left to right:

print(5 + 4 - 7 + 3)

"""
Additions and subtractions have the same precedence, and we
need to calculate from left to right.
The calculation above reads:
5+4=9
9-7=2
2+3=5
"""
TERN ARY OPERATOR IN P YTH ON

• Ternary operators also known as conditional expressions are


operators that evaluate something based on a condition being true
or false.
• It simply allows testing a condition in a single line replacing the
multiline if-else making the code compact.
• Syntax : [on_true] if [expression] else [on_false]
• Examples
# Program to demonstrate conditional operator
a, b = 10, 20

# Copy value of a in min if a < b else copy b


min = a if a < b else b

print(min)
Output:
10
BUILT-IN FORMAT
FUNCTION

• Because floating-point values may contain an arbitrary


number of decimal places, the built-in format function
can be used to produce a numeric string version of the
value containing a specific number of decimal places.

• In these examples, format specifier '.2f' rounds the result


to two decimal places of accuracy in the string produced.
• For very large (or very small)
values 'e' can be used as a format
specifier,

You might also like