Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an expression in Python?

Tags:

I have some confusion about its meaning or definition.

Isn't that some code that produce or calculate new data values? (Says Zelle in his book)

Then I wonder if a string data type is an expression.

If it is, then what does eval() do when its argument is a string?

The book by Zelle sayseval(<string>) evaluates string as an expression, what does that exactly mean if string is already an expression?

And if string is not an expression, then how come it can occur after print?

like image 707
FloydChen Avatar asked Jan 24 '11 13:01

FloydChen


People also ask

What is an expression in programming?

An expression is a particular concept in computer science in which a number of variables or constants, and operators and functions, are put together in a single statement that is acted on by a particular programming language.

What are expressions and values in Python?

In Python, operators are special symbols that designate that some sort of computation should be performed. The values that an operator acts on are called operands. A sequence of operands and operators, like a + b - 5 , is called an expression. Python supports many operators for combining data objects into expressions.

What is an expression and statement?

In programming language terminology, an “expression” is a combination of values and functions that are combined and interpreted by the compiler to create a new value, as opposed to a “statement” which is just a standalone unit of execution and doesn't return anything.

What is expression list in Python?

An expression list containing at least one comma yields a tuple. The length of the tuple is the number of expressions in the list. The expressions are evaluated from left to right. The trailing comma is required only to create a single tuple (a.k.a. a singleton); it is optional in all other cases.


2 Answers

Expressions represent something, like a number, a string, or an instance of a class. Any value is an expression!

Anything that does something is a statement. Any assignment to a variable or function call is a statement. Any value contained in that statement in an expression.

foo = "hello" is a statement that assigns foo to the value of the expression "hello". Since the code "hello" is a simple expression, meaning it contains no operations, nothing is actually evaluated, so foo is just assigned to "hello". More complex expressions actually evaluate things, like adding numbers. Using the word expression seems like it is making things more confusing. Expressions are nothing but values, except they can have operations like addition or subtraction.

eval evaluates the string as if it were a python expression. Eval does takes an expression as an argument. However, there's nothing special about this since every single value is an expression. Saying "eval takes a value as an argument" is saying exactly the same thing, but it sounds much simpler. :D

eval( "2+2" ) passes the string "2+2" to the function. The function evaluates the expression contained in the string, which comes out to 4.

The book by Zelle says eval(<string>) evaluates string as an expression, what does that exactly mean if string is already an expression?

Any string is an expression since it represents a value. However, what is in the string has absolutely no impact on it being an expression. If its a value, its an expression. When it is "evaluated as an expression by eval", the characters inside the string are executed as if they were a python expression.

like image 56
Gordon Gustafson Avatar answered Sep 17 '22 13:09

Gordon Gustafson


TL;DR: Expressions are combinations of values and operators and always evaluate down to a single value. A statement is every other instruction. Some statements contain expressions.

An expression is an instruction that combines values and operators and always evaluates down to a single value.

For example, this is an expression:

>>> 2 + 2

The 2s are integer values and the + is the mathematical operator. This expression evaluates down to the single integer value 4.

Technically, this is also an expression:

>>> 4

As an expression, it evaluates down to the single value 4.

When I say values and operators, this isn't limited to math problems:

>>> 'You will be ' + str(int(myAge) + 1) + ' next year.'

The myAge variable evaluates to the value inside it. The function call int('5') evaluates to the function's return value, 5. All these string values are combined with the + operator (in this case, it's the string concatenation operator). No matter how big an expression is, it evaluates down to a single value: in this case, the string value 'You will be 6 next year.'

Contrast this with a statement, which is a Python instruction that does not evaluate down to a value. A Python statement is pretty much everything else that isn't an expression. Here's an assignment statement:

>>> spam = 2 + 2

Here's an if statement:

>>> if spam == 4:

Here's a while statement for an infinite loop:

>>> while True:

Note that both of these statements contain expressions (even True, which evaluates down to the single value True). But not all statements use expressions in them. Here's a break statement:

>>> break
like image 28
Al Sweigart Avatar answered Sep 20 '22 13:09

Al Sweigart