Skip to content

Commit 6fe8692

Browse files
committed
improved section on None
1 parent 37a7745 commit 6fe8692

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Die Slides zum Kurs in deutscher Sprache können unter <https://thomasweise.gith
3434
9. [Zwischenspiel: Python Dokumentation und Informationsquellen](https://thomasweise.github.io/programmingWithPythonSlidesDE/09_dokumentation.pdf)
3535
10. [Der Datentyp `bool`](https://thomasweise.github.io/programmingWithPythonSlidesDE/10_bool.pdf)
3636
11. [Der Datentyp `str`](https://thomasweise.github.io/programmingWithPythonSlidesDE/11_str.pdf)
37+
12. [`None`](https://thomasweise.github.io/programmingWithPythonSlidesDE/12_none.pdf)
3738

3839
### 2.3. The Slides in English
3940
The slides for the course are available at <https://thomasweise.github.io/programmingWithPythonSlides> and also listed below.

text/main/basics/simpleDataTypesAndOperations/none/none.tex

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
\Cref{exec:none} illustrates some of the things we can do with \pythonilIdx{None}.
1818
If we write \pythonilIdx{None} into the \python\ console, then nothing happens.
19-
In the past, we just wrote values, such as \pythonil{34} and, after we hit \keys{\enter}, they would be printed again.
19+
In the past, we just wrote values, such as \pythonil{34} and, after we hit~\keys{\enter}, they would be printed again.
2020
Not so~\pythonilIdx{None}.
21-
If we want to print~\pythonilIdx{None}, we have to force it by using the function \pythonilIdx{print}.
21+
If we want to print~\pythonilIdx{None}, we have to force it by using the function~\pythonilIdx{print}.
2222
\pythonil{print(None)} then indeed prints~\pythonilIdx{None}.
2323

2424
The value \pythonilIdx{None} has many use cases.
@@ -29,9 +29,18 @@
2929
\pythonilIdx{None} would be much clearer, as no arithmetic calculation could ever return~\pythonilIdx{None}.
3030

3131
For this to work we need to be able to check whether a value is~\pythonilIdx{None}.
32-
And we can do this using the operator~\pythonilIdx{is}.\footnote{%
33-
\pythonilIdx{is} is a bit similar to \pythonilIdx{==}, but instead of comparing whether the contents of an object are the same, it compares whether two values reference the same object.}%
34-
\pythonil{1 is None} gives us \pythonil{False} and so does \pythonil{"Hello World!" is None}.
32+
We can use the \pythonilIdx{==}~operator for it, but its use is discouraged~\cite{PEP8}.
33+
We do it here anyway, just for demonstration purposes, because you are already very familiar with~\pythonil{==}.
34+
\pythonil{1 == None} gives us \pythonil{False} and so does \pythonil{"Hello World!" == None}.
35+
\pythonil{None == None}, however, is \pythonil{True}.
36+
This is a bit interesting, because \pythonil{nan == nan} would give us~\pythonil{False}, but then again, \pythonil{nan} means \inQuotes{undefined} and \pythonil{None} means \inQuotes{nothing.}%
37+
%
38+
\bestPractice{cmpWithNone}{%
39+
Comparisons to singletons like \pythonil{None} should always be done with \pythonil{is} or \pythonil{is not}, never the equality operators~\python{==} or~\pythonil{!=}~\cite{PEP8}.}%
40+
%
41+
As stated above, the right way to compare values if \pythonil{None} may be involved is using the operator~\pythonilIdx{is}~\cite{PEP8}.
42+
\pythonilIdx{is}~is a bit similar to~\pythonilIdx{==}, but instead of comparing whether the contents of an object are the same, it compares whether two values reference the same object.
43+
As expected \pythonil{1 is None} gives us \pythonil{False} and so does \pythonil{"Hello World!" is None}.
3544
\pythonil{None is None}, however, is \pythonil{True}.
3645

3746
Also, functions that do not return any result do, in fact, return~\pythonilIdx{None}.

0 commit comments

Comments
 (0)