4  Technically Pythonidae

Comments

I’m a prolific code commenter, or at least try to be, so I’m going to start y’all off early. Comments in Python come in two main flavours. The hash or pound character (#) starts a line comment and runs to the end of the line. A multiline comment, or “docstring”, uses three single or double quotes (""" or ''') at the start and end of a paragraph of text.

For now, you don’t need to worry too much about multi-line comments or docstrings. We’ll return to docstrings in the bonus documentation chapter; here, focus on using short comments only when they make nearby code easier to understand. A good comment is a little note from past-you, not a second copy of the code in sentence form.

White Space

Python was designed for readability and has a few syntactical similarities to English with a mathematical twist.

Python uses new lines to finish a command instead of semicolons or extra punctuation.

Python also uses indentation, either tabs or spaces, to define “scope” for loops, functions, and classes. Many other languages use braces ({ and }) for this. For example:

>>> name = input()
Buffy
>>> if name:
...     print("Hello " + name)
...
Hello Buffy

If we don’t indent the second line, we’ll get an error:

File "<stdin>", line 2
    print("Hello " + name)
    ^
IndentationError: expected an indented block

This is a helpful error once you learn to read it. Python is pointing at the line where it expected an indented block and found none.

Basic Data Types

Integers

An integer is a whole number. In everyday decimal notation, integers use base 10 digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9).

>>> 1 + 1 # addition
2
>>> 2 * 2 # multiplication
4
>>> 3 / 3 # division
1.0
>>> 4 - 2 # subtraction
2
>>> 2 - 4 # subtraction
-2

If you’re dealing with values other than base 10, you can specify the base with one of these prefixes:

Prefix Interpretation Base
0b or 0B Binary 2
0o or 0O Octal 8
0x or 0X Hexadecimal 16

You don’t need to use binary, octal, or hexadecimal every day, but they show up often enough in networking, file formats, permissions, and protocol work that it’s worth knowing they exist.

Floating-Point Numbers

The float type in Python represents a floating-point number. float values are numbers with a decimal point. For larger floating-point numbers, you can append e or E to use scientific notation:

>>> from math import pi
... pi
3.141592653589793

Strings

Strings are sequences of characters. In Python, they’re called str. String literals can use either single or double quotes (' or "):

>>> print("Pythons don't produce venom - they are non-venomous snakes")
Pythons don't produce venom - they are non-venomous snakes
>>> print('Pythons live in the tropical areas of Africa and Asia')
Pythons live in the tropical areas of Africa and Asia

A string can contain as many characters as you want, as long as the computer’s memory can handle it. That also means a string can be empty:

>>> print("")

For easier handling, though, it’s often clearer to set a missing string value to None rather than "".

Another common gotcha is including quote characters in the string itself. Your first try might look like this:

>>> print('Pythons don't produce venom - they are non-venomous snakes')
SyntaxError: invalid syntax
>>> # or
>>> print('Pythons don''t produce venom - they are non-venomous snakes')
Pythons dont produce venom - they are non-venomous snakes

That won’t give you the result you expected. If you want to include either type of quote character within the string, the simplest fix is to use the opposite quote style:

>>> print("Pythons don't produce venom - they are non-venomous snakes")
Pythons don't produce venom - they are non-venomous snakes

You can also use a string “escape” backslash (\) character:

>>> print('Pythons don\'t produce venom - they are non-venomous snakes')
Pythons don't produce venom - they are non-venomous snakes

Both approaches are fine. The tiny practical trick is to choose the version that makes the string easiest to read.

Escape Sequences

Escape Sequence Usual Interpretation of Character(s) After Backslash “Escaped” Interpretation
\' Terminates string with single quote opening delimiter Literal single quote (') character
\" Terminates string with double quote opening delimiter Literal double quote (") character
\newline Terminates line input Newline is ignored
\\ Introduces escape sequence Literal backslash (\) character

Special Escape Sequences

Escape Sequence “Escaped” Interpretation
\b ASCII backspace character
\n ASCII line feed (LF) character
\t ASCII horizontal tab (TAB) character

Examples

>>> print('Python\nKingdom: Animalia')
Python
Kingdom: Animalia
>>> print('Python\tKingdom: Animalia')
Python  Kingdom: Animalia
>>> print('Python\bKingdom: Animalia')
PythoKingdom: Animalia

Checkpoint

Create three variables in the interactive interpreter: one integer, one floating-point number, and one string. Print each value and use a comment to label its type. Then deliberately make one indentation or quoting mistake and read the error before fixing it.

Extension Activity

Use Python’s type() function on each checkpoint value. Then try one escaped string that includes a quote mark and one escaped string that includes a newline.

References