Any calculative statement that has some operands and operators in it is called as expression. An expression can be arithmetic, relational, logical or string related. Like all other languages, python has its own precedence of operators. Expressions are evaluated based on this precedence. Following is the table of operator precedence in python:
Arithmetic operations are performed in the following way:
Examples:
Relational operations are all on the same priority hence what comes first is evaluated first. Result of every relational expression is a Boolean value. It is either TRUE or FALSE.
Examples:
This is because combined relational expressions are handled with Logical ‘and’. Here, it evaluates as: (a==b) and (b<8). Since first is false, entire expression results false.
Logical expressions are nothing but combined relational operations. In other words, we can say that relational operators are combined using logical operators. Three basic logical operators are: ‘and’, ‘or’ and ‘not’.
While evaluating a logical expression, python uses the following three rules:
The priority of logical operators is lower than that of the arithmetic operators. Hence if there is any arithmetic operation in the entire expression, it is evaluated first and then logical operators are applied.
Examples:
The priority of logical operators among themselves is ‘not’ then ‘and’, and then ‘or’. Hence if all three are there in an expression, first ‘not’ is evaluated then ‘and’ and finally ‘or’ is evaluated.
Examples:
If in an expression there are two relational expressions combined with or operator, the second is evaluated only if the first one is false. Similarly, if there are two relational expressions combined with end operator, the second is evaluated only if the first one is true.
Examples:
In the above example, entire expression resulted as TRUE because the (not (2 > 6)) part became TRUE hence there was no need to evaluate the other side of ‘or’.
Typecasting is also known as explicit type conversion. In most of the programming languages, typecasting is used as a tool to convert the data type of a variable temporarily into another data type. Generally implicit type conversion happens automatically from higher to lower datatypes but typecasting can change this way of work.
Syntax:
Example: