The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Beautiful is better than ugly
. . .
Explicit is better than implicit
. . .
{'userId': 1,
'id': 1,
'title': 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit',
'body': 'quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto'}
Simple is better than complex
. . .
Readability Matters
Curly Bracket Languages: Define code blocks using { and }. (Some also define the end of a statement using ;) (aspects of semantics are defined by syntax)
if(a > b) {print("a is greater than b")} else {print("a is not greater than b")}
Python (Indentation)
if a > b:print("a is greater than b")else:print("a is not greater than b")
Readability matters
. . .
Clear (Javascript Example):
// This function takes a name and logs// a string which greets that namefunctionsayHi(name) {console.log("Hi "+ name +", nice to meet you.")}sayHi("Sam");
. . .
Not Clear (“Minified” Javascript):
functionf(o){console.log("Hi "+o+", nice to meet you.")}f("Sam");
Readability matters
. . .
General Rules:
Make your code easy to read.
Include lots of comments
Keep lines of code short
Avoid single character variable names.
Call your functions with named parameters where applicable.
When python says SyntaxError, you should read this as I don't know what you want me to do!?
Exceptions
An Exception happens the code you have written violates the Python language specification.
This code is trying to divide zero by 0. Its syntax is correct. But what happens when we run it?
>>>print( 0/0 )
It didn't work because you tried to divide by zero
When python says anything other than SyntaxError, you should read this as You are asking to do something I can't do
In this case, the ZeroDivisionError is raised because the Python language specification does not allow for division by zero.
Types of Exceptions
Python has a lot of builtin Errors that correspond to the definition of the Python language.
. . .
A few common Exceptions you will see include TypeError, IndexError, and KeyError.
TypeError
A TypeError is raised when you try to perform a valid method on an inappropriate data type.
. . .
IndexError
An IndexError is raised when you try to access an undefined element of a sequence. Sequences are structured data types whose elements are stored in a specific order. A list is an example of a sequence.
. . .
---------------------------------------------------------------------------IndexError Traceback (most recent call last)
Cell In[14], line 3 1# IndexError Example: 2 my_list = ['a', 'b', 'c', 'd']
----> 3my_list[4]IndexError: list index out of range
KeyError
A KeyError is raised when you try to perform a valid method on an inappropriate data type.
. . .
---------------------------------------------------------------------------KeyError Traceback (most recent call last)
Cell In[16], line 4 1# KeyError Examples: 3 my_dict = {'column_1': 'definition 1', 'another_word': 'a second definition'}
----> 4my_dict['column1']KeyError: 'column1'
Deciphering Tracebacks
When an exception is raised in python the interpreter generates a “Traceback” that shows where and why the error occurred. Generally, the REPL has most detailed Traceback information, although Jupyter Notebooks and iPython interactive shells also provide necessary information to debug any exception.
---------------------------------------------------------------------------NameError Traceback (most recent call last)
Cell In[17], line 7 4print(results)
6# calling the function----> 7multiply(10,2)
Cell In[17], line 4, in multiply(num1, num2) 2defmultiply(num1, num2):
3 result = num1 * num2
----> 4print(results)
NameError: name 'results' is not defined