What is an expression in Python?

I have some confusion regarding its meaning or definition.

Isn't that some kind of code that generates or calculates new data values? (Says Zelle in his book)

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

If so, what does eval() when its argument is a string?

Zelle's book says that eval(<string>) evaluates string as an expression, what does it mean if string already an expression?

And if string not an expression, how can it happen after print ?

+10
source share
4 answers

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

Everything that does something is instruction. Any assignment to a variable or function call is an expression. Any value contained in this expression in the expression.

foo = "hello" is the statement that assigns foo to the value of the expression "hello". Since the code "hello" is a simple expression, that is, it does not contain any operations, nothing is evaluated, so foo is assigned only to "hello ". More complex expressions actually value things, like adding numbers. it makes things more confusing. Expressions are nothing but values, except that they can have operations such as addition or subtraction.

eval evaluates the string as if it were a python expression. Eval makes the expression as an argument. However, there is nothing special about this, since each value is an expression. Saying "eval takes a value as an argument" says exactly the same thing, but it sounds a lot easier .: D

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

Zelle's book says that eval(<string>) evaluates a string as an expression, what does it mean if the string is already an expression?

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

+17
source

"Expression" can be a bit confusing term when you get away from thinking about how the Python script parser works. The standard documentation makes a distinction between expressions and “atoms”, but I think it makes its terminology rather restrictive (the BNF diagram on 5.11 implies that I think the expression should be either a lambda or a conditional expression. My BNF is rusty.)

Atoms, on the other hand, seem to cover, because @kynnysmatto says “everything that has“ value. ”Perhaps“ everything that can be enclosed in brackets, and then outside parentheses is indistinguishable from its corresponding value, ”may be better definition of an atom.

When Zelle discusses expressions in context, such as eval (foo), I think it implies:

  • foo is an identifier that is an atom type
  • but foo is an identifier for something
  • this "something" looks like a string, probably the actual string
  • inside the eval () internal elements, this line also represents the expression "Python", that is, something that Python can parse and understand.

tl; dr: “expression” because terminology is best understood in terms of parsing code; when you program yourself, it may seem better to think in terms of "atoms."

+2
source

TL DR: expressions are combinations of values ​​and operators and are always evaluated to a single value. A statement is any other instruction. Some statements contain expressions.

expression is a command that combines values ​​and operators and always evaluates to a single value.

For example, this expression:

 >>> 2 + 2 

2s are integer values, and + is a mathematical operator. This expression evaluates to a single integer value of 4.

Technically, this is also an expression:

 >>> 4 

As an expression, it is evaluated to a single value of 4.

When I say values ​​and operators, this is not limited to mathematical problems:

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

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

Compare this with the operator , which is a Python statement that does not evaluate a value to a value. The Python operator is almost everything that is not an expression. Here's the assignment operator:

 >>> spam = 2 + 2 

Here's the if statement:

 >>> if spam == 4: 

Here's the while statement for an infinite loop:

 >>> while True: 

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

 >>> break 
+2
source
Line

- this expression. An expression is all that matters. Like 3, "Hello world", 1 + 1, math.sqrt (9), etc. Function names are also expressions.

eval () gives you the value of the expression that you give it as a string. If you say eval ('1 + 1'), it returns 2. Thus, it returns the same that would be returned if you simply write: 1 + 1.

+1
source

All Articles