0% found this document useful (0 votes)
35 views2 pages

Python Data Handling Notes 4

Operator associativity determines the order of operations when multiple operators of the same precedence are used in an expression. All operators are left-to-right associative except for exponentiation (**), which is right-to-left associative. Expressions in Python can be arithmetic, relational, logical, string expressions, or compound expressions depending on the operators and operands. When expressions contain different data types, Python performs implicit type conversion to avoid losing information. Type casting allows explicit conversion of an operand to a specific data type using angle brackets. Evaluation of expressions follows set rules like evaluating sub-expressions and operands before applying operators.

Uploaded by

Sharad Mittal
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)
35 views2 pages

Python Data Handling Notes 4

Operator associativity determines the order of operations when multiple operators of the same precedence are used in an expression. All operators are left-to-right associative except for exponentiation (**), which is right-to-left associative. Expressions in Python can be arithmetic, relational, logical, string expressions, or compound expressions depending on the operators and operands. When expressions contain different data types, Python performs implicit type conversion to avoid losing information. Type casting allows explicit conversion of an operand to a specific data type using angle brackets. Evaluation of expressions follows set rules like evaluating sub-expressions and operands before applying operators.

Uploaded by

Sharad Mittal
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/ 2

CS Faculty:

Sharad Mittal COMPUTER SCIENCE


M: +91-9911433679
E: sharadmittal2000@gmail.com PYTHON DATA HANDLING - NOTES 4
OPERATOR ASSOCIATIVITY: - In a single expression of Python, if different operators have different precedence, then it is
quite easy to evaluate expression with higher precedence first, one after another. But if there are multiple operators of same
precedence, associativity helps determine the order of operations.
Associativity if the order in which an expression having multiple operators of same precedence, is evaluated. All operators
are left-to-right associative, except exponentiation (**), which is right-to-left associative.
For example,
7 * 8 / 5 // 2 will result into 5.0
4*5+7*2–8%3+4 will result into 36
6.7 // 2 – 1 + 4 will result into 6.0
not 2 == 4 will result into True
not 2 ** 4 > 6 * 2 will result into False
2 ** 3 ** 2 will result into 512

EXPRESSION: - An expression in Python is any valid combination of operators, identifiers, literals and values-in-closure (like
strings, tuples, lists, dictionaries, sets etc.). An expression is composed of one or more operations.
The expressions in Python can be Arithmetic Expressions, String Expressions, Relational Expressions, Logical Expressions, and
Compound Expressions etc. The types of operators and operands used in an expression determine the expression type.
1. Arithmetic Expressions- These involve numbers (integers, floating-point numbers, complex numbers) and arithmetic
operators. For example, a + b * c // d -4 7 * 8 / 5 - 13 // 2
2. Relational Expressions- These have literals and/or variables of any valid type and relational operators. For example,
x>y y <= z z == q z<y>x x == y < z
3. Logical Expressions- An expression having literal and/or variables of any valid type and logical operators is a logical
expression. For example, a or b b and c a and not b not c or not b
4. String Expressions- Python provide + (concatenation) and * (replication) operators. Concatenation (+) operator
combines/ joins two strings, both operands must be of string type. Replication operator repeats a string (one
operand) by no. of times (another integer operand). For example, “and” + “then” “and” * 2
5. Compound Expression- An expression is called compound expression, if it involves multiple types of operators. For
example, a + b > c ** d a*b<c*d

Implicit Type Conversion: - When an expression has different data types intermixed, compiler performs type conversion
automatically without programmer’s intervention, so as not to lose information. Here, Python converts all operands up to
the type of the largest operand (type promotion). If two operands are standard numeric types:
1. If one operand is a complex number, the other is converted to complex number.
2. Otherwise, if one operand is a floating-point number, the other is converted to floating-point number.
3. No conversion, if both operands are integers.

Type Casting: - The explicit conversion of an operand to a specific data type (forcibly by user in the program) is called type
casting or Explicit Type Conversion. Syntax is <datatype> (<expression>).
For example,
If we have a = 5.0, then int(a) will change its value as 5.
If we have b = 5, then float(b) will change its value as 5.0.
If we have ch = 5.78, then str(ch) will change its value as ‘5.78’.

EXPRESSION EVALUATION: -
1. Evaluating Arithmetic Expression- Here are the rules to evaluate an arithmetic expression:
a) Determine the order of evaluation in an expression, considering the operator precedence.
b) Evaluate each sub-expression as per below mentioned evaluation order:
i. Evaluate each of its operands or arguments.
CS Faculty:
Sharad Mittal COMPUTER SCIENCE
M: +91-9911433679
E: sharadmittal2000@gmail.com PYTHON DATA HANDLING - NOTES 4
ii. Perform any implicit conversion (if needed).
iii. Computer result, based on operator.
iv. Replace subexpression with result.
v. Carry on expression evaluation.
vi. Repeat the process till final result is obtained.
For example,
ch = 5 A = (ch + i) / db B = fd / db * ch / 2
i=2 = (5 + 2) / 5.0 = 36.0 / 5.0 * 5 / 2
fl = 4 = (7) / 5.0 = 7.2 * 5 / 2
db = 5.0 = 7.0 / 5.0 = 7.2 * 5.0 / 2
fd = 36.0 A = 1.4 = 36.0 / 2
A = (ch + i) / db = 36.0 / 2.0
B = fd / db * ch / 2 B = 18.0
print(A, B)
Hence, output is 1.4 18.0

2. Evaluating Relational Expression- All relational operators have same precedence (but lower than arithmetic
operations) and yield in Boolean Values (True/ False).
For chained expressions like a < b < c (which is internally equivalent to a < b and b < z), the common (middle)
expression (b, in this case) is evaluated only once and the third expression (c, in this case) is not evaluated at all, if
first comparison is False. Otherwise, second comparison is also evaluated.
For example,
a, b, c = 10, 23, 23
print(a < b) #True
print(b <= c) #True
print(a < b <= c) #True

a, b, c = 23, 10, 10
print(a < b) #False
print(b <= c) #True
print(a < b <= c) #False

3. Evaluating Logical Expression- Python follows below rules while evaluating logical expression:
i) Precedence of logical operators is lower than arithmetic operators. Hence if an expression has arithmetic
sub-expressions as well, that is evaluated first and logical operators are applied after.
ii) Precedence of logical operators are not then and then or. Hence expression a or b and not c is
equivalent to (a or (b and (not c))).
For example,
(3 < 5) or (5 < 2) will result as True
(5 < 3) or (5 > 2) will result as True
(5 < 3) or (5 < 2) will result as False

You might also like