This article provides a overview of expression evaluation in Python programming language along with easy to understand examples.
Contents
Introduction to Expression Evaluation
A Python program contains one or more statements. A statement contains zero or more expressions. Python executes a statement by evaluating its expressions to values one by one. Python evaluates an expression by evaluating the sub-expressions and substituting their values.
Literal Expressions
A literal expression evaluates to the value it represents. Following are some examples of literal expressions:
10 => 10
‘Welcome to Python’ => ‘Welcome to Python’
7.89 => 7.89
True => True
Binary Expressions
A binary expression consists of a binary operator applied to two operand expressions. Examples:
2*6 => 12
8-6 => 2
8==8 => True
1000 > 100 => True
‘hello’ + ‘world’ => ‘helloworld’
Unary Expressions
An unary expression contains one operator and single operand. Examples:
-(5/5) => -1
-(3*4) => -12
-(2**4) => -16
-10 => -10
Compound Expressions
In a binary or unary expression, if an operand itself is an expression, such expression is known as a compound expression. Examples:
3 * 2 + 1 => 7
2 + 6 * 2 => 14
(2 + 6) * 2 => 16
Variable Access Expressions
A variable access expressions allows us to access the value of a variable. Examples:
>>>x = 10
>>>(x + 2) * 4
48
>>>x
10
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Content of the page is good, but it is less.
Provide more content to understand the topic briefly.
Thank you
Hi Dhaval,
I am rewriting the entire Python tutorial. But due to time constraints, I don’t know when it will be completed.
Better to write example programs also