0% found this document useful (0 votes)
5 views

Operators in Python

The document provides an overview of various operators in Python, including arithmetic, relational, assignment, logical, bitwise, membership, and identity operators. It explains the function of each operator type, their precedence, and associativity. Additionally, it includes exercises to apply the concepts learned about operators.

Uploaded by

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

Operators in Python

The document provides an overview of various operators in Python, including arithmetic, relational, assignment, logical, bitwise, membership, and identity operators. It explains the function of each operator type, their precedence, and associativity. Additionally, it includes exercises to apply the concepts learned about operators.

Uploaded by

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

Programming in Python

(Operators: Precedence andAssociativity)

Dr. Faisal Anwer


Department of Computer Science
Aligarh Muslim University 1
Copyright © Dept of Computer Science, AMU, Aligarh. Permission required for reproduction or display.
Recap
• Data Types

• Identifiers

• Variables

• Simple Input/Output
Contents
• Arithmetic Operators

• Relational Operators

• Assignment & Compound Operators

• Logical Operators

• Bitwise Operators

• Membership Operators

• Identity Operators
Operators and Operands
 The operators are special symbols that represent computations

 The values/variables applied in the operators are known as


operands.

 Types of Operators
 Arithmetic Operators
 Relational Operators
 Assignment & Compound Operators
 Logical Operators
 Bitwise Operators
 Membership Operators
 Identity Operators
Note: The bool data type can be used in arithmetic, relational,
assignment and logical operators. (Ture is 1 and False is 0)
4
Arithmetic Operator
 Arithmetic operators are the symbols that represent arithmetic math
operations.

 Integer division is also known as Floor division.


Operator Name Example
+ Addition x+y

- Subtraction x-y

* Multiplication x*y

/ Division x/y

% Modulus x%y

** Exponentiation x ** y

// Integer Division(Floor division) x // y


5
Relational Operators
 The relational operators are binary. These operators compare the
values on either sides of them and returns the value either “True” or
“False”.

 == Equality
 != Not equal
 > Greater than
 < Less than
 >= Greater than and Equal to
 <= Less than and Equal to

6
Assignment & Compound Operators
 The assignment (& compound ) operators are binary. These operators
perform operation on two operands and assign the result to left side
operand.

 For all operators (except =), the variable must be initialized before
the use in compound operators.

 = Assignment
 += Add & Assignment
 It adds right operand to the left operand and assign the result
to left operand.
 /= Divide & Assignment
 It divides left operand with the right operand and assign the
result to left operand
 etc.

7
Logical Operators
 The logical operators are binary except not.
 The logical operators considers operators either True or False
 These operators compare the values on either sides of them and returns
the value either “True” or “False”.
 The basic rule is…
 False is “zero” or “empty” string
 True is anything else (“non-empty” or “not zero”)
 Operators
 and (Logical AND)
 If both the operands are True, then condition becomes true.
 or (Logical OR)
 If any of the two operands are non-zero then condition
becomes true.
 not(Logical NOT)
 It is used to reverse the logical state of the operand.

8
Bitwise Operators
 The bitwise operators are binary ( except complement ~) and are
applied on corresponding bits in integers.

 & (Binary AND)


 If both bits in both operands are one, then it copies bit one to
the result otherwise it copies zero bit.

 | (Binary OR)
 If both bits in both operands are zero, then it copies bit zero to
the result otherwise it copies one.

 ^ (Binary XOR)
 If one bit is one and other is zero, then it copies bit one to the
result otherwise it copies zero bit.
9
Bitwise Operators
 << (Binary Left Shift)
 The left operand's value is moved left by the number of bits
specified by the right operand.

 >> (Binary Right Shift)


 The left operand's value is moved right by the number of bits
specified by the right operand.

 ~ (2’s complement)
 It is a unary operator that complements the operand.

10
Membership Operators
 Python’s membership operators test for membership in a
sequence, such as strings, lists, or tuples.

 in
 Evaluates to True if it finds a value in the specified
sequence and False otherwise

 not in
 Evaluates to True if it does not finds a value in the
specified sequence and False otherwise.

11
Operator’s Precedence
 () Parenthesis
 ** Exponentiation (raise to the power)
 ~, +, - Complement, unary plus and minus
 *, /, %. // 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
 =, %=, /=, //=, -=, +=, *=, **= Assignment operators
 is, is not Identity operators
 in, not in Membership operators
 not, or, and Logical operators

12
Operator’s Associatively
 Different operators have different priorities.
 When two operators have the same precedence, associativity helps
to determine the order of operations.
 Almost all the operators have left-to-right associativity.
 Exponent operator ** has right-to-left associatively.
 Assignment operator = has right-to-left associatively.

 Non associative operators


 Some operators like comparison operators do not have
associativity in Python.
 There are separate rules for sequences of this kind of operator and
cannot be expressed as associativity.
 For example, x < y < z neither means (x < y) < z nor x < (y < z).
 x < y < z is equivalent to x < y and y < z, and is evaluated from
left-to-right. (Example- 3 < 6 < 2).
13
Exercises
1. Write a program to read an integer and use bitwise operators
to multiply it by 2 ( << operator ).

2. Write a program to read an integer and use bitwise operators


to divide it by 4 (>> operator ).

3. Write a program to read the values of two integer variables


and use bitwise operators to exchange the values of the
variables (^ operator).

4. Write a program to read the values of two integer variables


and exchange the values (Use addition and subtraction
operators).

5. What should be the value of 3<<3^2&1+3


14
Summary
• Arithmetic Operators

• Relational Operators

• Assignment & Compound Operators

• Logical Operators

• Bitwise Operators

• Membership Operators

• Identity Operators

You might also like