0% found this document useful (0 votes)
31 views14 pages

L4:Python

The document discusses Python operator precedence and associativity. It explains that Python follows PEMDAS order of operations, with operators of the same precedence evaluated from left to right based on their associativity. Some operators like assignment are nonassociative and have special evaluation rules. The document also covers type conversions in Python, both implicit and explicit, and functions for converting between types like int, float, oct, hex, and bin.

Uploaded by

Mayur Kinra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
31 views14 pages

L4:Python

The document discusses Python operator precedence and associativity. It explains that Python follows PEMDAS order of operations, with operators of the same precedence evaluated from left to right based on their associativity. Some operators like assignment are nonassociative and have special evaluation rules. The document also covers type conversions in Python, both implicit and explicit, and functions for converting between types like int, float, oct, hex, and bin.

Uploaded by

Mayur Kinra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 14

Computer Programming with Python

06/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 1


Operator precedence
2+3*6
evaluates to 20 (where the multiplication is done first)
or
30 (where the addition is done first)

>>> 2 + 3 * 6
20
>>> (2 + 3) * 6
30

06/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 2


Operator precedence
>>> 6-3+2
5
>>> 6-(3+2)
1
>>> 2*5//2
5 [2*(5//2) gives 4]
>>> -3 ** 2
-9 [-(3 ** 2) = -9] [To get 9, you have to write (-3)**2]
>>> 5 * 5 // 5 + 5 - 4 ** 2
-6 [5 * 5 // 5 + 5 – 16 = 25 // 5 + 5 – 16 = 5 + 5 – 16 = 10 – 16 = -6]

Operators with the same precedence are evaluated from left-to-right.


07/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 3
Operator precedence
Python follows the PEMDAS (Parentheses, Exponentiation, Multiplication, Division,
Addition and Subtraction)order of operations.

06/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 4


Operator Precedence

5
06/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University
Operator Associativity
Whenever two or more operators have the same precedence or an expression
contains more than one same operators, then associativity defines the order of
operations. Hence, associativity is the order in which Python evaluates an
expression containing multiple(or same) operators of the same precedence.

Almost all operators except the exponent (**) support the left-to-right
associativity.

5 * 2 // 3 = 3 // Left-right associativity

2 ** 3 ** 2 = 2 ** 9 = 512 // Right-to-left associativity


(2 ** 3) ** 2 = 8 ** 2 = 64
06/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 6
Nonassociative Operators
Python does have some operators such as assignment operators and comparison
operators which don’t support associativity. Instead, there are special rules for
the ordering of this type of operator which can’t be managed via associativity.
For example, the expression 5 < 7 < 9 does not mean (5 < 7) < 9 or 5 < (7 < 9). Also,
the statement 5 < 7 < 9 is same as 5 < 7 and 7 < 9, and gets evaluated(True) from
left-to-right.
>>> 5 < 7 > 9
False
A chained comparison like a < b < c is interpreted as (a < b) and (b < c), not
equivalent to either (a < b) < c or a < (b < c)

06/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 7


Nonassociative Operators
Since assignments are statements, not operations, the assignment operator does
not have a value and is not associative.
Moreover, chaining of assignments operators like a = b = c is perfectly alright
whereas the ‘a = b += c’ will result in an error.
>>> x,y,z=11,12,13
>>> x = y += 12 // not legal in Python, though they are legal C
SyntaxError: invalid syntax

06/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 8


Type Conversions/casting
Conversion of value from one data type into another.
Mixed-mode arithmetic
Expression of int produces an int,
Expression of float creates another float. But
Expression that mixes an int with a float - produce what?
How Python should handle these situation?
In mixed-typed expressions, Python will automatically convert ints to floats and
perform floating-point operations to produce a float result.
a)Implicit(perform a type conversion automatically) and
b)Explicit(perform a type conversion ourselves)
>>> 3.14*3**2
28.26 // the value 9 is converted to 9.0 before the multiplication
06/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 9
Type Conversions/casting function
>>> int (4.5)
4
>>> float (int (3.3) )
3.0
>>> int (float (3.3))
3
>>> float(3)
3.0
>>> 2 + 4.0, 2.0 ** 2
(6.0, 4.0)
>>> oct(10)
'0o12'
>>> hex(10)
'0xa'
>>> bin(10)
'0b1010'06/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 10
Type Conversions/casting function
>>> x=-4.66
>>> round(x)
-5
>>> int(x)
-4
>>> int(round(x))
-5
>>> round(3.1429, 2)
3.14
>>> abs(x)
4.66
>>> math.floor(x)
-5
>>> math.ceil(x)
06/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 11
-4
Type Conversions/casting
>>> math.trunc(x)
-4
>>> round(x)
-5
>>> round(3.4)
3
>>> round(3.7)
4
>>> pi = 3.141592653589793
>>> round (pi, 2)
3.14
>>> round (pi, 3)
3.142 06/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 12
Type Conversions/casting
What are the differences between round() and trunc() function?
1]round() function may take one or two arguments but trunc() takes exactly one argument
2] One need to import math module to use trunc() function, which is not the requirement for
round() function
3] round() function rounds off to the given number of digits and returns the floating point
number, if no number of digits is provided for round off , it rounds off the number to the nearest
integer, trunc() function remove the decimal values from specified expression and return the
integer value.
trunc() function behaves as a ceiling function for negative number and floor function for positive
number

06/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 13


Print a number upto a specific digit
Which one is the correct syntax for printing a number upto 2 digit?
(a) print("%5.2f", 3.1429)
(b) print(%5.2f, 3.1429)
(c) print('%5.2f', 3.1429)
(d) print("%5.2f%", 3.1429)
(e) print("%.2f" % 3.1429)
Ans : (e) print("%.2f" % 3.1429)

07/08/2018 Debajyoti Ghosh, Asst. Prof, BML Munjal University 14

You might also like