Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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 35 to “x” like so,

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 often 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.

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.

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.

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.

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.

Saving the output of the method to a new variable then allows us to do more things with that data. It introduces 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.