Precedence of operators and converstion in python programming with subtable example
Precedence of operators : Precedence determine a series of steps carried out to be accomplished a task. The way we commonly represent expressions, in which operators appear between their operands, it is referred to as infix notation. For example the expression 4 + 5 is in infix notation since the + operator appears between its two operands, 4 and 5. There are other ways of representing expression called prefix and postfix notation in programming language, which operators are placed before and after their operands, respectively.
The expression 4 + (3*5) is also in infix notation. It contains two operators, + and *. The parentheses denote that (3*5) is sub expression. Therefore, 4 and (3*5) are the operands of the addition operator, and thus the overall expression evaluates to 19. What if the parentheses were omitted, given below
4 + 3 * 5
These are two possibilities:-
4 + 3 * 5 -> 4 +15 = 19
4 + 3 * 5 -> 7 * 5 = 35
However each programming language has its own rules for the other that operators are applied on task, that is called operator precedence. In the following table, higher-priority operators are placed above lower-priority ones.
|
Operator |
Associativity |
|
** (exponentiation) |
Right-to-left |
|
- (negation) |
Left-to-right |
|
*(mult), / (div), // (truncating div), % (modulo) |
Left-to-right |
|
+ (addition), - (substracatin) |
Left-to-right |
Table: Operator precedence of arithmetic operators in python
For Example: Consider the following examples (Write on directly on python shell)
>>> 2+3*4
Output: 14
>>> 2*3+4
Output: 10
>>> 2*3/4
Output: 1
>>> 2*3//4
Output: 1
>>> 5+42%10
Output: 7
>>> 2*2**3
Output: 16
Note: operator precedence is the relative order that operators are applied in the evaluation of expressions.
Conversion:
Python programming language has many built-in data types. You have seen integer, float, string, list, and dictionary. Data conversion in python programming can happen in two ways, implicitly and explicitly.
These are two types of conversion:
1) Implicit Conversion: Implicit conversions are done automatic by python. When data type conversion takes place either during compilation or during run time and is handled directly by Python for you.
For Example: Write a program to demonstrate the use of implicit conversion.
x= 1 #Here x is integer
y = 1.0 # Here y is float
z = x + y
print(" Addition is:",z)
print("Output datatype of z is:",type(z))
Output:
Explicit Conversion: Explicit conversion is done by programmer. Type conversion is the explicit conversion of operands to specific type. Type conversion can be applied even if loss of information results.
|
function |
Example |
Result |
|
int() |
int(9.9) |
9 |
|
int(‘9’) |
9 |
|
|
int(’9.7’) |
ERROR |
|
|
float() |
float(9) |
9.0 |
|
float(‘9’) |
9.0 |
|
|
float(’9.9’ |
9.9 |
Table:
conversion function int() and float() in python programming
Syntax:
(required_data_type)(expression)
For Example: Write a program to demonstrate the use explicit conversion.
a
= 5.5 #Here a is float type
b=
2.0 #Here b is float type
c = int(a + b) # Explicit type conversion float into integer
print("Output in Integer :", c)
c = a + b
print("Output in Float :", c)
Output:


Comments
Post a Comment