Programming in
Python
Variable Assignment
Assignment Operator
The equal sign ( = ) is the main Python assignment operator.
anInt = -12
aString = 'cart'
aFloat = -3.1415 * (5.0 ** 2)
Assignment does not explicitly assign a value to a variable.
In Python, objects are referenced, so on assignment, a reference
(not a value) to an object is what is being assigned.
Augmented Assignment
x = x + 1 can be written as . . .
x += 1
Python does not support pre-/post-increment nor pre-/post-decrement
operators such as x++ or --x
Variable Assignment
Multiple Assignment
x=y=z=1
An integer object (with the value 1) is created, and
x, y, and z are all assigned the same reference to that object.
“Multuple” Assignment
x, y, z = 1, 2, 'a string‘
two integer objects (with values 1 and 2) and one string object
are assigned to x, y, and z respectively.
when assigning variables this way, the objects on both sides of
the equal sign are tuples,
Variable Assignment
# swapping variables in Python
x, y = 1, 2
>>> x
1
>>> y
2
>>> x, y = y, x
>>> x
2
>>> y
1
Identifiers
Identifiers are the set of valid strings that are allowed as names in a computer
language.
Valid Python Identifiers
First character must be a letter or underscore ( _ )
• Any additional characters can be alphanumeric or underscore
• Case-sensitive
Keywords
and ,break, class, continue ,elif else etc.
Special Underscore Identifiers
Python designates (even more) special variables with underscores both prefixed
and suffixed.
_xxx a variable named _xxx is considered “private” and should not be
used outside that module or class . Do not import with 'from
module import *'
__xxx__ System-defined name
Variable Declaration
Variable Declarations (or Lack Thereof)
In most compiled languages, variables must be declared before
they are used.
In Python, there are no explicit variable declarations. Variables
are “declared” on first assignment.
Like most languages, however, variables cannot be accessed
until they are(created and) assigned:
>>> a
Traceback (innermost last):
File "<stdin>", line 1, in ?
NameError: a
Dynamic Typing
Dynamic Typing
In Python, the type and memory space for an object are
determined and allocated at runtime.
On creation—that is, on assignment—the interpreter creates an
object whose type is dictated by the syntax that is used for the
operand on the right-hand side of an assignment
After the object is created, a reference to that object is assigned
to the variable on the left-hand side of the assignment.
Reference Count
Reference Counting
To keep track of objects in memory, Python uses the simple
technique of reference counting.
Incrementing the Reference Count
The refcount for an object is initially set to 1 when an object is
created and (its reference) assigned.
New references to objects, also called aliases, occur when
additional variables are assigned to the same object, passed as
arguments to invoke other bodies of code such as functions,
methods
Let us say we make the following declarations:
3.14 x
x = 3.14
y
y=x
Reference Count
x is the first reference, hence setting that object’s refcount to one. The statement
y = x creates an alias y, which “points to” the same integer object as x . A new
object is not created for y.
Instead, the only thing that happens is that the reference count for this object is
incremented by one (to 2).
An object’s reference count is increased when
It (the object) is created
x = 3.14
Additional aliases for it are created
y=x
It is passed to a function (new local reference)
func1(x)
It becomes part of a container object
myList = [123, x, 'xyz']
Reference Count
Decrementing the Reference Count
When references to an object “go away,” the refcount is decreased. The
most obvious case is when a reference goes out of scope. This occurs
most often when
the function in which a reference is made completes. The local
(automatic)variable is gone, and an object’s reference counter is
decremented
A reference also goes away when a variable is reassigned to another
object.
For example:
f = 'xyz‘ // reference count of sting object’xyz’
b=f // incremented to 2
f = 123 // decremented to 1 since f is assigned to 123
Aliases for that object are explicitly destroyed
del y # or del x
Garbage Collection
Memory that is no longer being used is reclaimed by the system
using a mechanism known as garbage collection
The garbage collector is a separate piece of code that looks for
objects with reference counts of zero.