Lu 02
Lu 02
BP411 | Formatives
Formative Assessment 1
Weighing 12.5%
Duration 4 Days
Open/ Closed Book Open Book
Learning Units Covered LU 1, LU2, and LU3
Formative Assessment 2
Weighing 10%
Duration 5 Days
Open/ Closed Book Open Book
Learning Units Covered LU 4, LU5, LU 6,LU7, and LU8
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Formatives
Formative Assessment 3
Weighing 10%
Duration 5 Days
Open/ Closed Book Open Book
Learning Units Covered LU 9, LU10, LU11,LU12, and LU13
Formative Assessment 4
Weighing 20%
Duration N/A
Open/ Closed Book Open Book
Learning Units Covered All
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Summative
Summative Assessment
Weighing 50%
Duration 3 Hours
Open/ Closed Book Closed Book
Learning Units Covered All
Practical Theory
60% 40%
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Important Dates
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Prescribed Material
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Unit 1
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Objectives
Python Data Are Objects
Types
Mutability
Literal Values
Variables
Assignment
Variables Are Names, Not Places
Assigning to Multiple Names
Reassigning a Name
Copying
Choose Good Variable Names
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Introduction
Under the hood, everything in your computer is just a sequence of
bits.
One of the insights of computing is that we can interpret those bits
any way we want—as data of various sizes and types (numbers, text
characters) or even as computer code itself.
We begin with Python’s data types and the values that they can
contain. Then we see how to represent data as literal values and
variables.
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Python Data Are Objects
You can visualize your computer’s memory as a long series of shelves.
Each slot on one of those memory shelves is one byte wide (eight
bits), and slots are numbered from 0 (the first) to the end.
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Python Data Are Objects
That memory is used for the code of the program itself, and the data
that it uses.
The operating system ensures that the program cannot read or write
other memory locations without somehow getting permission.
Programs keep track of where (memory location) their bits are, and
what (data type) they are. To your computer, it’s all just bits.
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Python Data Are Objects
The same bits mean different things, depending on what type we say
they are.
The same bit pattern might stand for the integer 65 or the text
character A.
Different types may use different numbers of bits. When you read
about a “64-bit machine,” this means that an integer uses 64 bits (8
bytes).
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Python Data Are Objects
Instead of handling such raw data values directly, Python wraps each
data value—booleans, integers, floats, strings, even large data
structures, functions, and programs—in memory as an object.
For now, we’re just talking about objects that handle the basic built-
in data types.
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Python Data Are Objects
Using the memory shelves analogy, you can think of objects as
variable sized boxes occupying spaces on those shelves.
Python makes these object boxes, puts them in empty spaces on the
shelves, and removes them when they’re no longer used.
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Python Data Are Objects
In Python, an object is a chunk of data that contains at least the following:
Its type is like a factory stamp on the box, saying what it can do.
If a Python object is an integer, it has the type int, and could be added to
another int.
If we picture the box as being made of clear plastic, we can see the value
inside.
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | We’re here
Python Data Are Objects
Types
Mutability
Literal Values
Variables
Assignment
Variables Are Names, Not Places
Assigning to Multiple Names
Reassigning a Name
Copying
Choose Good Variable Names
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Types
Table 2-1 shows the basic data types in Python.
The second column (Type) contains the Python name of that type.
The third column (Mutable?) indicates whether the value can be
changed after creation, which I explain more in the next section.
And the final column (Chapter) points you to the chapter in this
book with the most details on this type.
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Types
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | We’re here
Python Data Are Objects
Types
Mutability
Literal Values
Variables
Assignment
Variables Are Names, Not Places
Assigning to Multiple Names
Reassigning a Name
Copying
Choose Good Variable Names
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Mutability
The type also determines whether the data value contained by the
box can be changed (mutable) or is constant (immutable).
By the same analogy, a mutable object is like a box with a lid: not
only can you see the value inside, you can also change it; however,
you can’t change its type.
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Mutability
The type also determines whether the data value contained by the
box can be changed (mutable) or is constant (immutable).
By the same analogy, a mutable object is like a box with a lid: not
only can you see the value inside, you can also change it; however,
you can’t change its type.
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Mutability
Python is strongly typed, which means that the type of an object
does not change, even if its value is mutable.
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Who Studies IT?
There probably is not a prototypical IT student.
But an IT student should:
Enjoy playing around with the computer—not just using it, but
learning how it works, learning how to do things on it at the
system level
Enjoy troubleshooting
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | We’re here
Python Data Are Objects
Types
Mutability
Literal Values
Variables
Assignment
Variables Are Names, Not Places
Assigning to Multiple Names
Reassigning a Name
Copying
Choose Good Variable Names
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Literal Values
There are two ways of specifying data values in Python:
Literal Variable In coming chapters, you’ll see the details on how
to specify literal values for different data types—integers are a
sequence of digits, floats contain a decimal point, text strings are
surrounded by quotes, and so on.
But, for the rest of this chapter, to avoid calloused fingertips, our
examples will use only short decimal integers and a Python list or two.
Decimal integers are just like integers in math:
A sequence of digits from 0 to 9.
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | We’re here
Python Data Are Objects
Types
Mutability
Literal Values
Variables
Assignment
Variables Are Names, Not Places
Assigning to Multiple Names
Reassigning a Name
Copying
Choose Good Variable Names
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Variables
Now, we’ve arrived at a key concept in computing languages.
Python, like most computer languages, lets you define variables—
names for values in your computer’s memory that you want to use in
a program.
Python variable names have some rules:
They can contain only these characters:
Lowercase letters (a through z)
Uppercase letters (A through Z)
Digits (0 through 9)
Underscore (_)
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Variables
They are case-sensitive:
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Variables
Within a Python program, you can find the reserved words with :
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Variables
These are valid names:
a
a1
a_b_c___95
_abc _1a
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Variables
These are valid names:
a
a1
a_b_c___95
_abc _1a
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | We’re here
Python Data Are Objects
Types
Mutability
Literal Values
Variables
Assignment
Variables Are Names, Not Places
Assigning to Multiple Names
Reassigning a Name
Copying
Choose Good Variable Names
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Assignment
In Python, you use = to assign a value to a variable.
Programs are not like algebra.
When you learned math in school, you saw equations like this:
y = x + 12
You would solve the equation by “plugging in” a value for x.
If you gave x the value 5, 5 + 12 is 17, so the value of y would be 17.
Plug in 6 for x to get 18 for y, and so on.
Computer program lines may look like equations, but their meaning is
different.
In Python and other computer languages, x and y are variables.
Python knows that a bare sequence of digits like 12 or 5 is a literal
integer.
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Assignment
So here’s a tiny Python program that mimics this equation, printing
the resulting value of y:
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Assignment
Also in programs, everything on the right side needs to have a value
(this is called being initialized).
The right side can be a literal value, or a variable that has already
been assigned a value, or a combination.
Python knows that 5 and 12 are literal integers.
The first line assigns the integer value 5 to the variable x.
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Assignment
If you started your program with the line y = x + 12, Python would
generate an exception (an error), because the variable x doesn’t have
a value yet:
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Assignment
To do this in Python, you’d need to get the literal values and initialized
variables on the right side before assigning to x on the left:
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | We’re here
Python Data Are Objects
Types
Mutability
Literal Values
Variables
Assignment
Variables Are Names, Not Places
Assigning to Multiple Names
Reassigning a Name
Copying
Choose Good Variable Names
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Variables Are Names, Not Places
Now it’s time to make a crucial point about variables in Python:
variables are just names.
This is different from many other computer languages, and a key
thing to know about Python, especially when we get to mutable
objects like lists.
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Variables Are Names, Not Places
In other languages, the variable itself has a type, and binds to a
memory location.
You can change the value at that location, but it needs to be of the
same type.
This is why static languages make you declare the types of variables.
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Variables Are Names, Not Places
This saves time, but there are some downsides:
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Variables Are Names, Not Places
Try this with the interactive interpreter.
As before, assign the value 7 to the name a.
This creates an object box containing the integer value 7.
2. Print the value of a.
3. Assign a to b, making b also point to the object box containing
7.
4. Print the value of b:
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Variables Are Names, Not Places
In Python, if you want to know the type of anything (a variable or a
literal value), you can use type(thing).
type() is one of Python’s built-in functions.
If you want to check whether a variable points to an object of a
specific type, use is instance(type):
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Variables Are Names, Not Places
Let’s try it with more literal values (58, 99.9, 'abc') and variables (a,
b):
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Variables Are Names, Not Places
A class is the definition of an object.
In Python, “class” and “type” mean pretty much the same thing.
As you’ve seen, when you use a variable in Python, it looks up the object that it refers to.
Behind the scenes, Python is busy, often creating temporary objects that will be
discarded a line or two later.
Let’s repeat an earlier example:
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Variables Are Names, Not Places
In this code snippet, Python did the following:
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Variables Are Names, Not Places
When an object’s reference count reaches zero, no names are pointing to it, so it doesn’t
need to stick around.
Python has a charmingly named garbage collector that reuses the memory of things that
are no longer needed.
Picture someone behind those memory shelves, yanking obsolete boxes for recycling. In
this case, we no longer need the objects with the values 5, 12, or 7, or the variables x and
y.
The Python garbage collector may choose to send them to object heaven, or keep some
around for performance reasons given that small integers tend to be used a lot.
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | We’re here
Python Data Are Objects
Types
Mutability
Literal Values
Variables
Assignment
Variables Are Names, Not Places
Assigning to Multiple Names
Reassigning a Name
Copying
Choose Good Variable Names
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Assigning to Multiple Names
You can assign a value to more than one variable name at the same time:
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | We’re here
Python Data Are Objects
Types
Mutability
Literal Values
Variables
Assignment
Variables Are Names, Not Places
Assigning to Multiple Names
Reassigning a Name
Copying
Choose Good Variable Names
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Reassigning a Name
Because names point to objects, changing the value assigned to a
name just makes the name point to a new object.
The reference count of the old object is decremented, and the new
one’s is incremented.
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | We’re here
Python Data Are Objects
Types
Mutability
Literal Values
Variables
Assignment
Variables Are Names, Not Places
Assigning to Multiple Names
Reassigning a Name
Copying
Choose Good Variable Names
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Copying
Assigning an existing variable a to a new variable named b just makes
b point to the same object that a does.
If you pick up either the a or b tag and follow their strings, you’ll get
to the same object.
If the object is immutable (like an integer), its value can’t be changed,
so both names are essentially read-only.
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Copying
When we assigned x to y, that made the name y point to the integer
object with value 5 that x was also pointing to.
It did not change the one containing 5, which y still points to.
But if both names point to a mutable object, you can change the
object’s value via either name, and you’ll see the changed value when
you use either name.
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Copying
A list is a mutable array of values, and Chapter 7 covers them in
gruesome detail.
For this example, a and b each point to a list with three integer
members:
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Copying
These list members (a[0], a[1], and a[2]) are themselves like names,
pointing to integer objects with the values 2, 4, and 6.
The list object keeps its members in order.
Now change the first list element, through the name a, and see that b
also changed:
When the first list element is changed, it no longer points to the object with value
2, but a new object with value 99. The list is still of type list, but its value (the list
elements and their order) is mutable
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | We’re here
Python Data Are Objects
Types
Mutability
Literal Values
Variables
Assignment
Variables Are Names, Not Places
Assigning to Multiple Names
Reassigning a Name
Copying
Choose Good Variable Names
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411 | Choose Good Variable Names
It’s surprising how important it is to choose good names for your
variables.
In many of the code examples so far, I’ve been using throwaway
names like a and x.
In real programs, you’ll have many more variables to keep track of at
once, and you’ll need to balance brevity and clarity.
For example,
it’s faster to type num_loons rather than number_of_loons or
gaviidae_inventory, but it’s more explanatory than n.
2 0 2 0 C T U Tr a i n i n g S o l u t i o n s | A l l R i g h t s Re s e r v e d | ctutraining.ac.za
BP411
Main | LU2 | Subheading
Heading
The End
2 0 22
0 0C2T0UCTr
TU a i Tr
n ia
nign iSnogl uStoi ol untsi o n
| s A l| l R
Ail g
l hRtisg hRe
t ss Re
e r vseedr v e| d c t| u tcrtaui tnrianign.iancg..zaac . z a