Expression is a combination of values, variables and operators.
Expressions
•
Expression is a combination of values, variables and operators.
•
Value is considered as an expression.
•
For example: Following are expressions
a=10
a+20
30
2+3*4
14
The
first statement in above code is the assignment statement. The second statement
is an expression. The interpreter evaluates the expression and displays the
result.
Order of
execution
•
When more than one operator appears in an expression, the order of evaluation
depends on the rules of precedence.
•
Python follows the same precedence rules for its mathematical operators that
mathematics does.
The
acronym PEMDAS is a useful way to remember the order of operations.
1. P:
Parentheses have the highest precedence and can be used to force an expression
to evaluate in the order you want. Since expressions in parentheses are
evaluated first, 1 * (10‒5) is 5.
2.
E: Exponentiation
has the next highest precedence, so 2**3 is 8.
3.
MDAS: Multiplication
and Division have the same
precedence, which is higher than Addition
and Subtraction, which also have the
same precedence. So 2+3*4 yields 14 rather than 20.
4.
Operators with the same precedence are evaluated from left to right. So in the
expression 3‒2+1 will result 2. As subtraction is performed first and then
addition will be performed.
Review Questions
1. What is a numeric
literal? Give an example.
2. Outline the
operator precedence of arithmetic operators in Python.
3. Mention the list of
keywords available in python. Compare it with variable names.
4.What are the
statements? How are they constructed from variable and expressions in python?
5. What are keywords?
Give examples.
Python for Data Science: Chapter 1: Basics of Python : Tag: Computer Programming, Python, Data Science : - Python: Expressions
Python for Data Science
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Applied Physics (CSIE) II
PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Digital Principles and Computer Organization
CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electrical and Electronics Engineering
EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Python for Data Science
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Python for Data Science - Laboratory
AD25201 2nd Semester AIDS Dept | 2025 Regulation | 2nd Semester 2025 Regulation