[Tutor] Python "well-formed formulas"
Steven D'Aprano
steve at pearwood.info
Tue May 28 14:26:50 CEST 2013
On 28/05/13 21:44, Citizen Kant wrote:
> Could you please help me with a simple example of a Python well-formed
> formula in order to understand "well-formed formulas" and "formation rules"
> concepts properly?
Probably not, since they aren't really Python terms, so I'll be guessing what they are.
But *in general*, "well-formed" means there are no errors according to the grammar or syntax of the language being written. For example, in English:
"Last week, I went to the restaurant and ate a pizza."
is a grammatically-correct sentence, and so is well-formed. But:
"Last weeks, me goed to a restaurants and ated a pizzas?"
is not. Similarly, this is a well-formed mathematical expression:
y = 3x² - 4(x + 5)
while this is not:
y = ² - 4(x+)
In the case of Python, there are many different varieties of well-formed expressions. Some of them look remarkably close to mathematical notation (which is not an accident):
y = 3*x**2 - 4*(x + 5)
Some obvious differences: Python uses ** for the power operator, not superscript; multiplication must be explicitly stated using * and not just implied.
Other well-formed Python expressions:
mylist and mylist[1:]
"This is a string".upper()
function(12, 3.5, {4:None})
and some which are not well-formed:
mylist mylist:[1]
This is a string.)(upper
function 12,, 3.5, {4 None)}
Basically, something is well-formed if it has the form of the syntax or grammar of the language, and is not if it doesn't.
--
Steven
More information about the Tutor
mailing list