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

Operators

Uploaded by

maha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Operators

Uploaded by

maha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

Expressions

 An expression is a combination of values, variables, operators,


and calls to functions.
 If you type an expression at the Python prompt, the
interpreter evaluates it and displays the result, which is always
a value:
 >>> len('hello')
 5
 >>> 43 + 60 + 16 + 41
 160
 >>> 50 - 60
 -10
Jan 6, 2025 1
Notes :
 Python follows well accepted mathematical conventions when
evaluating mathematical expressions. In the following example,
Python adds 30 to the result of the multiplication (i.e., 120).
# Mathematical expression
>>> 30 + 2 * 60
150
And just like mathematics, expressions enclosed in parentheses have
priority. So the following multiplies 32 by 60.
# Mathematical expression
>>>(30 + 2) * 60
1920
Jan 6, 2025 2
Operators
 Operators are the constructs which can manipulate
the value of operands.

 Consider the expression 4 + 5 = 9.

 Here, 4 and 5 are called operands and + is called


operator.
Types of Operator

1. Arithmetic Operators
2. Comparison (Relational) Operators
3. Assignment Operators
4. Logical Operators
5. Bitwise Operators
6. Membership Operators
7. Identity Operators
Arithmetic operators
 Arithmetic operators: Used for performing
arithmetic operations.

Jan 6, 2025 5
 For example ,if x= 7,y=2
 addition: x+y=9
 subtraction:x-y=5
 Multiplication:x*y=14
 Division: x/y=3.5
 Floor division(Integer division): x // y=3 (rounds off the
answer to the nearest whole number)

 Modulus: x%y= 1 (Gives the remainder when 7 is divided


by 2 )
 Exponent : x**y=49 (7 to the power of 2)

Jan 6, 2025 6
Arithmetic Operators
Assume variable a holds 10 and variable b holds 20, then −

Operator Description Example


+ Addition Adds values on either a + b = 30
side of the operator.
- Subtraction Subtracts right hand a – b = -10
operand from left hand
operand.
* Multiplies values on a * b = 200
Multiplication either side of the
operator
/ Division Divides left hand b/a=2
operand by right hand
operand
Arithmetic Operators

Operator Description Example


% Divides left hand operand by b%a=0
Modulus right hand operand and returns
remainder
** Performs exponential (power) a**b =10 to the
Exponent calculation on operators power 20
Floor Division - The division of 9//2 = 4 and
operands where the result is the 9.0//2.0 = 4.0
// quotient in which the digits after
the decimal point are removed.
Comparison Operators(Relational operators)

 These operators compare the values on either sides of them


and decide the relation among them.
 Relational operators: Also known as comparison
operators, are used to compare values. Result of a relational
expression is always either true or false.
Comparison Operators(Relational operators)

Operator Description Example


== If the values of two (a == b) is not true.
operands are equal, then the
condition becomes true.
!= If values of two operands (a != b) is true.
are not equal, then condition
becomes true.
<> If values of two operands (a <> b) is true. This is
are not equal, then condition similar to != operator.
becomes true.
> If the value of left operand (a > b) is not true.
is greater than the value of
right operand, then condition
becomes true.
Comparison Operators(Relational operators)

Operator Description Example


< If the value of left operand is (a < b) is true.
less than the value of right
operand, then condition
becomes true.
>= If the value of left operand is (a >= b) is not true.
greater than or equal to the
value of right operand, then
condition becomes true.
<= If the value of left operand is (a <= b) is true.
less than or equal to the value
of right operand, then condition
becomes true.
Assignment Operators
 Assignment operators are used in Python to assign values to
variables.

 a = 5 is a simple assignment operator that assigns the value 5


on the right to the variable a on the left.

 There are various compound operators in Python like a += 5


that adds to the variable and later assigns the same. It is
equivalent to a = a + 5.
Assignment Operators
Assignment Operators
Operator Description Example
= Assigns values from right side c = a + b assigns value
operands to left side operand of a + b into c
+= Add It adds right operand to the left c += a is equivalent to
AND operand and assign the result to c = c + a
left operand
-= Subtract It subtracts right operand from c -= a is equivalent to
AND the left operand and assign the c = c - a
result to left operand
*= Multiply It multiplies right operand with c *= a is equivalent to
AND the left operand and assign the c = c * a
result to left operand
Assignment Operators
Operator Description Example
/= Divide It divides left operand with the c /= a is equivalent to
AND right operand and assign the c = c / a
result to left operand
%= It takes modulus using two c %= a is equivalent to
Modulus operands and assign the result c = c % a
AND to left operand
**= Performs exponential (power) c **= a is equivalent
Exponent calculation on operators and to c = c ** a
AND assign value to the left
operand
//= Floor It performs floor division on c //= a is equivalent to
Division operators and assign value to c = c // a
the left operand
Logical operators
 Logical operators are used to combine one or more
relational expressions.
 Logical operators are the and, or, not operators.

Jan 6, 2025 16
Logical operators
 If A and B are two relational expressions, say A = (Num1>2000),
B= (Num2>100), the result of combining A and B using logical operator is
based on the result of A and B as shown below:

Jan 6, 2025 17
Logical Operators
Operator Description Example

and If both the operands are true then (a and b) is true.


Logical condition becomes true.
AND

or If any of the two operands are non- (a or b) is true.


Logical zero then condition becomes true.
OR

not Used to reverse the logical state of Not(a and b) is false.


Logical its operand.
NOT
Bitwise Operators

 Bitwise operator works on bits and performs bit by bit


operation.
Bitwise Operators

 Bitwise operator works on bits and performs bit by bit


operation.
 Assume if a = 60; and b = 13; Now in binary format they
will be as follows −
 a = 0011 1100
 b = 0000 1101
 -----------------
 a&b = 0000 1100 #12
 a|b = 0011 1101 #61
 a^b = 0011 0001 #49
 ~a = 1100 0011 #-61
Bitwise Operators

 a = 0011 1100
 b = 0000 1101
 -----------------
 a<<2 # 240 = 1111 0000
 a >> 2 # 15 =0000 1111
Bitwise Operators
Bitwise or: a | b
Bitwise exclusive or: a ^ b # Don't confuse this with
exponentiation
Bitwise and: a & b
Shift a left or right by b bits: a << b, a >> b
Bitwise Operators

Operator Description
& Bitwise and Operator copies a bit to the result if it
exists in both operands

| Bitwise or It copies a bit if it exists in either


operand.
^ Bitwise exclusive It copies the bit if it is set in one
or operand but not both.

~ Binary Ones It is unary and has the effect of


Complement 'flipping' bits.
Bitwise Operators

Operator Description
<< Binary Left The left operands value is moved left by the
Shift number of bits specified by the right operand.

>> Binary Right The left operands value is moved right by the
Shift number of bits specified by the right operand.
Membership Operators

 Python’s membership operators test for membership in a


sequence, such as strings, lists, or tuples.

Operator Description Example

in Evaluates to true if it finds a x in y,


variable in the specified sequence
and false otherwise. here in results in a 1 if x is a
member of sequence y.

not in Evaluates to true if it does not finds x not in y,


a variable in the specified sequence
and false otherwise. here not in results in a 1 if x is
not a member of sequence y.
Identity Operators
 Identity operators compare the memory locations of two
objects.

 is and is not are the identity operators in Python.

 They are used to check if two values (or variables) are


located on the same part of the memory.

 Two variables that are equal does not imply that they are
identical.
Identity Operators

Operator Description Example


is Evaluates to true if the x is y, here is results in 1
variables on either side of if id(x) equals id(y).
the operator point to the
same object and false
otherwise.

is not Evaluates to false if the x is not y, here is


variables on either side of not results in 1 if id(x) is
the operator point to the not equal to id(y).
same object and true
otherwise.
Precedence of operators
 An expression may have multiple operators to be evaluated.
 The operator precedence defines the order in which
operators are evaluated.
 In other words, the order of operator evaluation is determined by
the operator precedence.
 If a certain expression contains multiple operators, their order of
evaluation is determined by the order of precedence.

Jan 6, 2025 28
 The operator precedence in Python is listed in the following
table. It is in descending order (upper group has higher
precedence than the lower ones).

Jan 6, 2025 29
Jan 6, 2025 30
Precedence of operators
 What do you think is the output of 5+4*9%(3+1)/6-1?
 It is done based on the precedence of the operator. Precedence

order - Brackets followed by Orders (Powers, Roots), followed


by modulo, Division and Multiplication, followed by Addition
and Subtraction.
 Brackets have the highest precedence followed by orders.
 Modulo, Division and Multiplication have the same precedence.

Hence if all appears in an expression, it is evaluated from Left


to Right.
 Addition and Subtraction have the same precedence. Hence if

both appears in an expression, it is evaluated from Left to


Right.
Jan 6, 2025 31
Associativity
 When two operators have the same precedence, associativity
helps to determine the order of operations.

 Associativity is the order in which an expression is evaluated


that has multiple operators of the same precedence.

 +,-,*, / operators have left-to-right associativity.

Jan 6, 2025 32
Precedence of operators

Jan 6, 2025 33
Comments in the program
 Comments in Python are the lines in the code that are
ignored by the interpreter during the execution of the
program.
 Comments enhance the readability of the code and help
the programmers to understand the code very carefully.
 There are three types of comments in Python –
 Single line Comments
 Multiline Comments
 Docstring Comments

Jan 6, 2025 34
Single-Line Comments

 A single-line comment begins with a hash (#) symbol and is


useful in mentioning that the whole line should be considered as
a comment until the end of the line.

 It can appear either in an individual line or inline with some


other code.

 Anything that is written in a single line after ‘#’ is considered as


a comment.

Jan 6, 2025 35
Single-Line Comments
Example :
#Defining a variable to store number.
n = 50 #Store 50 as value into variable n.

 In the above example program, the first line starts with the hash
symbol, so the entire line is considered a comment.
 In the second line of code, "N = 50" is a statement, and after the
statement, the comment begins with the # symbol.
 From the # symbol to the end of this line, the line will be treated
as a comment.
Jan 6, 2025 36
Multiline Comments
 Python does not provide the option for multiline comments.
 However, there are different ways through which we can write
multiline comments.

1. Using Multiple Hashtags (#) :

 We can multiple hashtags (#) to write multiline comments in


Python. Each and every line will be considered as a single-line
comment.

Jan 6, 2025 37
Multiline Comments

1. Using Multiple Hashtags (#) : Example

Jan 6, 2025 38
Multiline Comments

2. Using String Literals


 Python ignores the string literals that are not assigned to a

variable so we can use these string literals as a comment.

Jan 6, 2025 39
 Coding standards are the set of guidelines that can be
used to enhance the readability and clarity of the
program and make it easy to debug and maintain the
program.

Jan 6, 2025 40
Basic Syntax

Jan 6, 2025 41
QUESTIONS
1. Explain operators in detail with examples.
2. Evaluate the expression x**y**z given x=2,y=3,z=2 .
3. Explain precedence and write precedence table.
4. What you mean by associativity?
5. What do "comments" in programming refer to? How can this
be implemented in Python?
6. Describe Arithmetic operators, Assignment operators,
Comparison operators, Logical operators, and Bitwise
operators in detail with examples

Jan 6, 2025

You might also like