0% found this document useful (0 votes)
3 views4 pages

4 Python - Expression Evaluation

The document discusses Python expression evaluation, focusing on operator precedence, types of expressions, and the evaluation process. It explains type conversion, both implicit and explicit, and provides examples of accepting user input and displaying output. Additionally, it includes various methods for reading multiple inputs and converting them into different data types.

Uploaded by

Arun Sinha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

4 Python - Expression Evaluation

The document discusses Python expression evaluation, focusing on operator precedence, types of expressions, and the evaluation process. It explains type conversion, both implicit and explicit, and provides examples of accepting user input and displaying output. Additionally, it includes various methods for reading multiple inputs and converting them into different data types.

Uploaded by

Arun Sinha
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Expression Evaluation 1

1. Precedence of operators

Operator precedence refers to the priority of operators in an expression. It determines


which operation is performed first when multiple operators appear in an expression.

Operators with higher precedence are evaluated before operators with lower precedence.

Operator Name Operator Symbol Operator Operator


Precedence (High Associativity
→ Low)
Parentheses () Highest Left → Right
Exponentiation ** High Right → Left
Unary operators +x, -x, ~x High Right → Left
Multiplication * Medium Left → Right
Division / Medium Left → Right
Floor Division // Medium Left → Right
Modulus % Medium Left → Right
Addition + Medium Left → Right
Subtraction - Medium Left → Right
Bitwise Shift <<, >> Lower Left → Right
Bitwise AND & Lower Left → Right
Bitwise XOR ^ Lower Left → Right
Bitwise OR | Lower Left → Right
Comparison <, <=, >, >=, Low Left → Right
Operators ==, !=
Membership in, not in Low Left → Right
Operators
Identity is, is not Low Left → Right
Operators
Logical NOT not Very Low Right → Left
Logical AND and Very Low Left → Right
Logical OR or Lowest Left → Right
Assignment =, +=, -=, *=, /=, Very Low Right → Left
Operators //=, %= etc.

2. Expression
An expression in Python is a combination of variables, constants, operators, and
function calls that produces a value.
Example - 10 + 5, a * b, x / y, 5 ** 2

Expressions can be of below types –

Artihmatic : 5 + 2, 5 ** 2
Relational (uses comparison operator) : print (x < y)
Logical (uses AND, OR, NOT) : print (x > 3 and x <10)
Bitwise (uses bitwise operator) : print (a & b)
Python Expression Evaluation 2

3. Evaluation of an expression

Evaluation of an expression means calculating the result of the expression


according to operator precedence and associativity rules.

Python evaluates expressions using:


1. Operator precedence
2. Associativity
3. Parentheses

Associativity Associativity determines the direction of evaluation when two


operators have the same precedence.

4. Type-conversion (explicit and implicit conversion)

Type conversion means converting one data type into another data type.

There are two types:


1. Implicit Conversion
2. Explicit Conversion

Implicit conversion is automatically performed by Python when converting a smaller


data type into a larger data type.

Example : a=5
b = 2.5
c=a+b
print(c)
print(type(c))

Explicit conversion is done manually by the programmer using built-in functions.

Functi Description
on
int() Converts value to
integer
float() Converts value to
float
str() Converts value to
string
bool() Converts value to
boolean

Example : a = "10"
b = int(a)
print(b)
print(type(b))
Python Expression Evaluation 3

5. Accepting data as input from the console and displaying output


Python allows users to enter data through the keyboard using the input() function. The
input() function reads input from the user as a string.
Example name = input("Enter your name: ")
print(name)

Since input() returns a string, it must be converted to numeric type.

Example age = int(input("Enter your age: "))


print(age)
Miscellaneous Examples

1. Taking Multiple Integers in One Line


Instead of writing multiple input() statements, you can read multiple values in one line.
a, b = map(int, input("Enter two numbers: ").split())
print(a, b)

2. Taking a List of Integers


This is very common in Python programs.
numbers = list(map(int, input("Enter numbers: ").split()))
print(numbers)

3. Reading Characters of a String as a List


chars = list(input("Enter text: "))
print(chars)

4. Reading Multiple Lines of Input


n = int(input("Enter number of lines: "))
for i in range(n):
line = input()
print(line)

5. Reading Matrix Input


Example: 3×3 matrix.
matrix = []
for i in range(3):
row = list(map(int, input().split()))
[Link](row)
print(matrix)

6. Taking Input and Converting to Boolean


value = bool(int(input("Enter 0 or 1: ")))
print(value)

7. Taking Space-Separated Strings


a, b, c = input("Enter three words: ").split()
print(a, b, c)

8. Taking Comma-Separated Input


numbers = input("Enter numbers separated by comma: ").split(",")
print(numbers)
Python Expression Evaluation 4

9. Predict the output a = “Asansol”


print(bool(a)) //True

a = None
print(bool(a)) //False

You might also like