Here are additional resources about Python grammar:
Let’s go over those one by one. A literal is data—things like an integer, a string, a float, a Boolean value, or even a list mixing these things. Names are variables, something that can be assigned. So in the statement
x = 3,
x is the name.
x = 3 is a statement and not an expression. The assignment itself can’t be used in
eval() but the
x can be. Attributes are similar to names but instead of being global, they’re part of a named object.
If you had an object named
part with an attribute
.serial_no, you could access that inside of
eval(). Operations are expressions that combine literals with an operator. Common ones are things like math.
value == result is an expression. Finally, you can also use any function that returns a value. You can write one for Fibonacci, you can use functions from the
math library, or methods from your objects as long as they return something. Those were expressions.
eval() method has a cousin called
exec(). The signature is very similar to the
exec() will run any kind of code—statements and expressions included, so it’s the superset. Unlike
exec() has no return result.
Even if the contents of the
exec() are a function call that returns something,
exec() itself will not return a result. These two built-in functions have similar purposes—running code—but slightly subtle different calling structures, so you can do different kinds of things with them.
I’m going to spend a little time going off on a tangent here. I want to talk a little more about expressions and statements inside of Python. What I’ve covered so far is good enough for your understanding moving forward with
04:39 If you’re bored by the details of the compiler and how parsers work, feel free to skip on to the next lesson. Otherwise, hang on and I’ll show you some deep-dive stuff in the language of Python.
05:11 3.9 is a transition. And the concepts that I’m going to show you also exist inside of earlier versions, they’re just notated differently. A grammar specification indicates to a language how to parse the code file.
05:26 You can find the full specification for Python’s grammar at this URL. In the following few slides, I’m going to highlight a few key parts of the PEG and show you the differences between expressions and statements inside of the grammar specification.
05:44 Grammars aren’t specified themselves in Python. The language here that specifies the grammar is specific to the PEG. A common language for specifying coding grammars is something called BNF, Backus–Naur form. Prior to Python 3.9, Python used something called the EBNF, the extended Backus–Naur form.
Everything shown in light blue is like the concept of a variable or a declaration. The third line down here defines something specific for
eval() in the grammar, saying that it will take expressions as well as newlines and endmarkers.
Compare that to the line above it, which is interactive, like the REPL, which takes a statement. So already here at the beginning of the PEG, you can see that the language breaks down into this concept of expressions and statements. Further down in the grammar, you’ll find the definition of
It’s a compound one based on the definition of
expression, singular, and
expression can be combined with other
expression declarations to create the plural. Below that is the definition of the singular
It can be comprised of a lambda, a disjunction, or a conditional disjunction. The conditional disjunction is one where
else is happening inside of the same line without a colon. These kinds of single-line conditionals aren’t that common in Python, so you may not have run into this before, but this would be something like
x if y > 3 else y.
Continuing further into the grammar, you can see definitions for a
disjunction is comprised of conjunctions. Conjunctions are comprised of inversions, inversions are comprised of inversions or comparisons, and this definition just keeps drilling down. Further down, you get into the concept of terms. Moving on, you’ll see that a
term consists of a
term and certain kinds of operators.
These are things like text inside of your file that are keywords. Booleans—
None placeholder, literals like numbers, lists, tuples, dictionaries—all those things that I described earlier that can be used inside of an expression inside of
By contrast to expressions, let’s look at the definition of an
assignment. There’s a lot going on here. I don’t want to go into every last little piece, but you can see that the
assignment is comprised of names, expressions, and then other kinds of sub-components like
statement declaration consists of
compound_stmt (compound statements) or
simple_stmt (simple statements).
simple_stmt consists of
small_stmt (small statements) and some punctuation to go off with that. Digging into
small_stmt, and you’re starting to see things here that actually feel familiar to a Python programmer. A
small_stmt can be an
return_stmt (return statement), an
import_stmt (import statement),
nonlocal. Grammar further defines all of these concepts, but you get a sense here that the
small_stmt is kind of like the
atom inside of the
Just like the
atom where you start to see components like literals and
False that seem familiar from the programming language, the
small_stmt consists of declarations that also feel like things that you’re probably used to—returning and importing, et cetera.
10:29 If you’re interested in the PEG grammar or more details about how all of this stuff works, there are some further reading links inside of the description. For now, I just wanted to give you a rough taste as to how all these pieces fit together and a better feeling as to the difference between an expression and a statement.
Become a Member to join the conversation.