True
1
1.0
(1+1j)
'string'
Lecture 02
Like R, Python is a dynamically typed language but the implementation details are very different as it makes extensive use of an object oriented class system for implementation
As a dynamically typed langiage most basic operations will attempt to coerce object to a consistent type appropriate for the operation.
Note that the default numeric type in python is an integer rather than a double, but floats will generally take precedence over integers.
Python is not quite as liberal as R when it comes to type coercion,
Explicit casting between types can be achieved using the types as functions, e.g. int()
, float()
, bool()
, or str()
.
When using Python it is important to think of variable assignment as the process of attaching a name to an object (literal, data structure, etc.)
Strings can be defined using several different approaches,
A single \
can also be used to span a long string over multiple lines without including the newline
As of Python 3.6 you can use f strings for string interpolation formatting (as opposed to %
-formatting and the format()
method).
One other special type of string literal you will come across are raw strings (prefixed with r
) - these are like regular strings except that \
is treated as a literal character rather than an escape character.
Base Python does not support missing values. Non-finite floating point values are available but somewhat awkward to use. There is also a None
type which is similar in usage and functionality to NULL
in R.
Python lists are heterogenous, ordered, mutable containers of objects (they are very similarly to lists in R).
See here and here for a more complete listing of functions and methods.
Elements of a list can be accessed using the []
method, element position is indicated using 0-based indexing, and ranges of values can be specified using slices (start:stop:step
).
Come up with a slice that will subset the following list to obtain the elements requested:
Select only the odd values in this list
Select every 3rd value starting from the 2nd element.
Select every other value, in reverse order, starting from the 9th element.
Select the 3rd element, the 5th element, and the 10th element
Since lists are mutable the stored values can be changed,
When assigning an object a name (x = ...
) you do not necessarily end up with an entirely new object, see the example below where both x
and y
are names that are attached to the same underlying object in memory.
What are the values of x
and y
now?
To avoid this we need to make an explicit copy of the object pointed to by x
and point to it with the name y
.
What are the values of x
and y
now?
Now lets look at happens when we have a list inside a list and make a change at either level.
What are the values of x
, y
, and z
now?
lists (and other sequence types) can be unpacking into multiple variables when doing assignment,
It is also possible to use extended unpacking via the *
operator (in Python 3)
Python tuples are heterogenous, ordered, immutable containers of values.
They are nearly identical to lists except that their values cannot be changed - you will most often encounter them as a tool for packaging multiple objects when returning from a function.
It is possible to cast between sequence types
These are the last common sequence type and are a bit special - ranges are a homogenous, ordered, immutable “containers” of integers.
What makes ranges special is that range(1000000)
does not store 1 million integers in memory but rather just three 3\(^*\).
In most of the ways that count we can think about Python strings as being ordered immutable containers of unicode characters and so much of the sequence type functionality we just saw can be applied to them.
Because string processing is a common and important programming task, the str
class implements a number of additional methods for these specific tasks. See here a list of methods.
String processing - take the string given below and apply the necessary methods to create the target string.
Source:
Target:
We will discuss sets (set
) and dictionaries (dict
) in more detail next week.
Specifically we will discuss the underlying data structure behind these types (as well as lists and tuples) and when it is most appropriate to use each.
Sta 663 - Spring 2025