Dark theme

Key Ideas


The key ideas for this part centre on making variables and using them, along with some basic syntax.


Syntax

Python is usually constructed one instruction to the computer per line.

Logical units associated with control flow are delimited by indenting and exited by dedenting.

The following should not be used as variable or function names: False; class; finally; is; return; None; continue; for; lambda; try; True; def; from; nonlocal; while; and; del; global; not; with; as; elif; if; or; yield; assert; else; import; pass; break; except; in; raise. We'll see what most of these are used for in the rest of the course.

The import command gives access to the code in libraries, for example...

import builtins

...allows us to use dir().


Variables

Variables are a combination of a label/identifier and a value. We attach an identifier to a value, or assign the variable a value, thus:

label = "value"

Everytime you see this with a literal (that is, a simple single value), a new variable label is created and attached to a new value created in memory.

Variable names are generally written in lower_case_snake_case.

As everything in the computer is just a bunch of ones and zeros (or ons and offs), we can attach more complicated stuff than just numbers and words to variable labels: we can attach other code representing complicated objects containing other variables and functions. This is the foundation of Object Oriented Programming. We'll see how to do this later in the course.

If the assignment attaches a label (on the left) to an expression (on the right), the right hand side is calculated before the assignment:

a = 2 + 2

This means that this ends up with a as 4:

a = 2
a = a + 2

There are two "agumented" assignment operators:

x += 1    # same as    x = x + 1
x -= 1    # same as    x = x - 1

The basic single value types are:

int : Integers - whole numbers.
bool: True and False
float: Floating point numbers - decimal numbers
complex: For imaginary numbers (actually two floats: one real, one imaginary)

To this can be added some basic sequence variables storing more than one thing:

str: Strings - text. Strings are collections of single-character strings.
bytes: Byte - binary. Sequences of ones and zeros.

To check the type of something, use:

type(identifier)

Infact, all these types actually contain a lot of stuff wrapped around the core data; for example, functions to work with it or convert it in some way.

>>> a = "hello world"
>>> b = a.upper()
>>> a
hello world
>>> b
HELLO WORLD

The "e" scientific notation can be used with floats: 3.14E-10. If you're going to use floats for anything critical, you need to carefully understand and follow the advice on floating point numbers.

Always use parentheses in numerical expressions to clarify the order you want them calculated in.

The following constructor functions will change x to various types ("cast" x):

a = float(x) # Where x can include a group of numerical text characters.
a = int(x) # Likewise.
a = str(x) # Convert to a string.
a = bool(x) # Convert to Boolean True or False.

The str() constructor is a good one to remember if print() refuses to print something because it won't automatically cast to a string.