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 (more on this later)
As just mentioned, Python is dynamically typed langiage so most basic operations will attempt to coerce object to a consistent type appropriate for the operation.
Python is not quite as liberal as R when it comes to type coercion,
More on why this happens in a little bit…
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 a couple of different approaches,
strings can also be triple quoted, using single or double quotes, which allows the string to span multiple lines.
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.
By default Python does not support missing values and non-finite floating point values are available but somewhat awkward to use. There is also a None
type which is similar in spirit and functionality to NULL
in R.
Python lists are a heterogenous, ordered, mutable containers of objects (they behave very similarly to lists in R).
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?
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 a 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 functionality we just saw can be applied to them.
Because string processing is a common and important programming task, the class implements a number of specific methods for these tasks. Review the page linked on the previous slide for help.
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 2023