Names in Python (Names in Python), lesson, page 724624
https://www.purl.org/stefan_ram/pub/names_python (permalink) is the canonical URI of this page.
Stefan Ram
Python Course

Names in Python 

Fundamentals of names

An sequence of letters, digits and underscores is called a name (It must not begin with a digit).

A name is an expression.

A name might be bound to an object. If so, then the object is the value of the name.

An evaluation
credits
    Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
for supporting Python development. See www.python.org for more information.
Transcript
Credits
NameError: name 'Credits' is not defined

Syntactically, a name is an atom. Feel free to ignore this information if you find it confusing.

Syntax

atom

.--------------------------.
---.------------>| literal |------------.--->
| '--------------------------' |
| .--------------------------. |
'------------>| name |------------'
| '--------------------------' |
| .-. .--------------------------. .-. |
'--->( ( )--->| expression |--->( ) )---'
'-' '--------------------------' '-'

The Python Language Reference, Release 3.6.0, Section 6.2.1
When the name is bound to an object, evaluation of the atom yields that object. When a name is not bound, an attempt to evaluate it raises a NameError exception.

Names for types

The names of most types are bound to the object representing the type.

Protokoll
str
<class 'str'>
Protokoll
int
<class 'int'>
Protokoll
float
<class 'float'>

Evaluation of names

Every expression belongs to a module.

All expressions we've entered so far in the console belong to the module __main__.

Every module has a table of bindings from names to objects, a binding table.

When a name is evaluated, it is looked up in the binding table of its module.

So, when we enter a name, it's looked up in the binding table of our module __main__.

If it is found there, then its value of the name is the object associated with the name in this binding table.

Otherwise, the name is looked up in the binding table of the module builtins.

If it is found there, then its value of the name is the object associated with the name in this binding table.

Otherwise a NameError occurs.

/   Exercise

Have the value of the name »copyright« output.

/   Reserve Exercise

Have the value of the name »__debug__« output.

True‹, ›False‹ und ›None

This names refer to three prominent objects the meanings of which will become more clear later, but already can be guessed from the names.

The type of ›True‹ and ›False‹ is the class ›bool‹; the type of ›None‹ is the class NoneType.

/   Exercise

Try to have the values of the expressions (atoms) »True«, »False«, »None«, »bool« and NoneType  output.

bool‹ as a subtype of ›int

Every bool value can also double as an int value: The int value of ›True‹ is «1», and the int value of ›False‹ is «0».

An evaluation
True + 1
2

?   Practice Question

What is the value of the following expression?

An expression
True * 'abc' +( 1 - True )* 'def'

?   Practice Question

What is the value of the following expression?

An expression
False * 'abc' +( 1 - False )* 'def'

Expressions as Statements

The console expects statements.

We have entered expressions.

This worked because every expression also is a statement, the evaluation statement  that requests the evaluation of itself as an expression.

Syntax diagram

evaluation statement

.------------.
--->| expression |--->
'------------'

statement

.-----------------------.
--->| evaluation statement |--->
'-----------------------'

Evaluation statements consist of an expressions and therefore also are known as expression statements.

Effects

An evaluation might change something. Such a change then is an effect of the evaluation.

For example, in the console, most evaluations bind the name »_« to the ₍rɪˈzʌlt₎ result of the evaluation. This binding is an effect  of the evaluation.

Transcript
2 + 3
5
_ * 7
35
_ + 1
36

from imports

A from import statement is a statement that copies the binding of the name specified after »import« from the module specified after »from« to the module of the from import statement.

Transcript
digits
NameError: name 'digits' is not defined

from string import digits

digits

'0123456789'
Syntax diagram

from import statement

.----. .------. .------. .------.
--->( from )--->| name |--->( import )--->| name |--->
'----' '------' '------' '------'

statement

.-----------------------.
---.--->| evaluation statement |---.--->
| '-----------------------' |
| .-----------------------. |
'--->| from import statement |---'
'-----------------------'

Adjacent tokes must be separated by a space when they otherwise would merge into a single token.

Transcript
fromstring import digits
SyntaxError

/   Exercise

Determine the value of the name »hexdigits« of the module »string« after the import of that name.

/   Exercise

Determine the value of twice the value of the name »pi« from the math module after importing that name.

Library grade code versus ad hoc code

Code that is used for a longer period of time is called library grade code, while code that is only written for practice¹ or short-term use is called ad hoc code.

(¹Except when the purpose of the exercise is to learn how to write library grade code!)

Ad hoc code, is code that is thrown away immediately after writing.

Style rules apply only to library grade code.

Complete import statement

In general, don’t use from modulename import *. 
Python Frequently Asked Questions  (Release 3.9.0a3), Abschnitt “2.2.5 What are the “best practices” for using import in a module? 

A from import with »import *« will import all names from a module (except names beginning with an underscore »_«). But this is not recommended, unless you write ad hoc code, because too many names are confusing.

Transcript (after a restart of the console)
e
NameError: name 'e' is not defined
pi
NameError: name 'pi' is not defined

from math import *

e

2.718281828459045
pi
3.141592653589793

 

About this page, Impressum  |   Form for messages to the publisher regarding this page  |   "ram@zedat.fu-berlin.de" (without the quotation marks) is the email address of Stefan Ram.   |   A link to the start page of Stefan Ram appears at the top of this page behind the text "Stefan Ram".)  |   Copyright 1998-2020 Stefan Ram, Berlin. All rights reserved. This page is a publication by Stefan Ram. relevant keywords describing this page: Stefan Ram Berlin slrprd slrprd stefanramberlin spellched stefanram724624 stefan_ram:724624 Names in Python Stefan Ram, Berlin, and, or, near, uni, online, slrprd, slrprdqxx, slrprddoc, slrprd724624, slrprddef724624, PbclevtugFgrsnaEnz Explanation, description, info, information, note,

Copyright 1998-2020 Stefan Ram, Berlin. All rights reserved. This page is a publication by Stefan Ram.
https://www.purl.org/stefan_ram/pub/names_python