variables#
So we have data in a digital format, as a specific type of data, but how do we work with that data?
Before we can use that data in any kind of programming, we need to save it. To give it a name, or a label. This is where variables become important.
A variable assigns data, a value, to a label, say “x”. We could save the integer 34 to “x” like so,
x = 34
With some exceptions, we can assign variables to almost any label we want, like “x” or “y.” But, we want to make sure that anybody reading our code will have a sense of what the variable refers to. Programmers therefore encouraged highly encourage each other to use precise but meaningful variables, like “age” to describe someone’s age in years, to reflect the value most accurately and succinctly.
Variables can be used to store any data type, like strings, lists, and even booleans.
age = 34
name = 'filipa'
breakfast = ['granola', 'cashew yogurt', 'muffin', 'coffee']
truth = True
Before moving forward, practice creating all kinds of variables for a few minutes. See if you can figure out the rules for creating variables.
variables as objects#
As a label for data, variables represent what is officially called an an object in Python. Objects are ways of thinking about data in its digitized form and what can be done to data in that form. Depending on the type of object (like a string, for example) we can run different kinds of computations. When applied to objects, the computations we can do are called methods.
For example, with a string type of object, we can use the upper()
method to make all of the characters in the string uppercase.
greeting = 'hello'
greeting.upper()
'HELLO'
Here, we are using dot syntax, that is, a period .
in between the
variable (the object) and the method (the action). What comes before the
dot is the data, saved to a variable. What follows the dot in dot syntax
is the action being applied to the variable.
a note on methods vs functions#
You may have already noticed that a method seems very similar to a
function, which we have seen previously with the type()
function
that evaluates data types. And you’re correct: both methods and functions do something to
data.
The difference between them has to do with syntax and what it means about the underlying process. A function takes data in between the parentheses, like when we pass a string into
the type()
function with type('hello')
. A method, by contrast,
appends an action to the end of an object using dot syntax, like with
greeting.upper()
example above. In the first case, we are passing a bit of data into a function that stands on its own. In the second, we are taking an object and applying one of its associated actions, like uppercase, to it.
The differences in syntax indicate a deeper difference between functions and methods, which has to do with object-oriented programming, which is an advanced concept that we will discuss futher in the Web Scraping workshop. For now, though, it’s enough to know that functions and methods are recognizable by their syntax, where one takes data within parentheses and the other appends to data using dot syntax.
Let’s see how different methods work with different data types, for example. There
are string methods like split()
which
splits a long string, say a paragraph of text, into individual
strings, each string being a different word.
text = '''
1. We refuse to operate under the assumption that risk and harm
associated with data practices can be bounded to mean the same
thing for everyone, everywhere, at every time. We commit to
acknowledging how historical and systemic patterns of violence
and exploitation produce differential vulnerabilities for
communities.
2. We refuse to be disciplined by data, devices, and practices
that seek to shape and normalize racialized, gendered, and
differently-abled bodies in ways that make us available to be
tracked, monitored, and surveilled. We commit to taking back
control over the ways we behave, live, and engage with data and
its technologies.
3. We refuse the use of data about people in perpetuity. We
commit to embracing agency and working with intentionality,
preparing bodies or corpuses of data to be laid to rest when they
are not being used in service to the people about whom they were
created.
4. We refuse to understand data as disembodied and thereby
dehumanized and departicularized. We commit to understanding
data as always and variously attached to bodies; we vow to
interrogate the biopolitical implications of data with a keen
eye to gender, race, sexuality, class, disability, nationality,
and other forms of embodied difference.
5. We refuse any code of phony “ethics” and false proclamations of
transparency that are wielded as cover, as tools of power, as forms
for escape that let the people who create systems off the hook from
accountability or responsibility. We commit to a feminist data
ethics that explicitly seeks equity and demands justice by helping
us understand and shift how power works.'''
text.split()
['1.',
'We',
'refuse',
'to',
'operate',
'under',
'the',
'assumption',
'that',
'risk',
'and',
'harm',
'associated',
'with',
'data',
'practices',
'can',
'be',
'bounded',
'to',
'mean',
'the',
'same',
'thing',
'for',
'everyone,',
'everywhere,',
'at',
'every',
'time.',
'We',
'commit',
'to',
'acknowledging',
'how',
'historical',
'and',
'systemic',
'patterns',
'of',
'violence',
'and',
'exploitation',
'produce',
'differential',
'vulnerabilities',
'for',
'communities.',
'2.',
'We',
'refuse',
'to',
'be',
'disciplined',
'by',
'data,',
'devices,',
'and',
'practices',
'that',
'seek',
'to',
'shape',
'and',
'normalize',
'racialized,',
'gendered,',
'and',
'differently-abled',
'bodies',
'in',
'ways',
'that',
'make',
'us',
'available',
'to',
'be',
'tracked,',
'monitored,',
'and',
'surveilled.',
'We',
'commit',
'to',
'taking',
'back',
'control',
'over',
'the',
'ways',
'we',
'behave,',
'live,',
'and',
'engage',
'with',
'data',
'and',
'its',
'technologies.',
'3.',
'We',
'refuse',
'the',
'use',
'of',
'data',
'about',
'people',
'in',
'perpetuity.',
'We',
'commit',
'to',
'embracing',
'agency',
'and',
'working',
'with',
'intentionality,',
'preparing',
'bodies',
'or',
'corpuses',
'of',
'data',
'to',
'be',
'laid',
'to',
'rest',
'when',
'they',
'are',
'not',
'being',
'used',
'in',
'service',
'to',
'the',
'people',
'about',
'whom',
'they',
'were',
'created.',
'4.',
'We',
'refuse',
'to',
'understand',
'data',
'as',
'disembodied',
'and',
'thereby',
'dehumanized',
'and',
'departicularized.',
'We',
'commit',
'to',
'understanding',
'data',
'as',
'always',
'and',
'variously',
'attached',
'to',
'bodies;',
'we',
'vow',
'to',
'interrogate',
'the',
'biopolitical',
'implications',
'of',
'data',
'with',
'a',
'keen',
'eye',
'to',
'gender,',
'race,',
'sexuality,',
'class,',
'disability,',
'nationality,',
'and',
'other',
'forms',
'of',
'embodied',
'difference.',
'5.',
'We',
'refuse',
'any',
'code',
'of',
'phony',
'“ethics”',
'and',
'false',
'proclamations',
'of',
'transparency',
'that',
'are',
'wielded',
'as',
'cover,',
'as',
'tools',
'of',
'power,',
'as',
'forms',
'for',
'escape',
'that',
'let',
'the',
'people',
'who',
'create',
'systems',
'off',
'the',
'hook',
'from',
'accountability',
'or',
'responsibility.',
'We',
'commit',
'to',
'a',
'feminist',
'data',
'ethics',
'that',
'explicitly',
'seeks',
'equity',
'and',
'demands',
'justice',
'by',
'helping',
'us',
'understand',
'and',
'shift',
'how',
'power',
'works.']
The output shows the long string split into several smaller strings. This is an example of how a method works and how you can recognize it by its syntax.
abstraction#
We can save the output of this to a new variable, called words
.
words = text.split()
Now, what was previously a string
data type has become a list
data
type. We can easily check the data type using the type()
function.
type(words)
list
Saving the output of the method to a new variable then allows us to do more things with that dat
Not only does this code show that methods work with specific types of objects (like strings or lists), it also introduces us to a bit of abstraction, where new variables are
created by saving the output of computations, like split()
.
Abstraction is common in programming, as data is transformed into new formats, and saved with a new name. These new forms then enable further types of computations. Later in workshop, creating new variables will be very useful for the kind of work we want to do with text.