Python Data Types and Variables (with SEO Examples)
About 25 minutes
- Variable
- String
- Integer
- Float
- Boolean
- None
- type
The counter at the top counts this lesson's exercises (4 of them). Reading and running the examples don't count.
As an SEO, you look at something like https://example.com/python-seo/ and say: that's a URL.
Python doesn't see a URL the way you do. To Python, it's just "https://example.com/python-seo/", a piece of text, and that's called a String.
That's the first mindset to take with you from here: Python doesn't know this is a URL. Or a keyword, a title, a domain, or a click count. You decide what the data means, through its type and the way you use it.
Let's take one whole page
Here's a page:
Python sees four different types of data on this page. Let's go through them one by one.
Text: String
Any text between quote marks, double " " or single ' ', is a String to Python. It's the type you'll meet most in SEO work:
Whole numbers: Integer
Numbers with no decimal point, like:
Python calls this type int. You'll see it in status codes, clicks, impressions, word counts and internal link counts.
Decimal numbers: Float
If the number has a decimal point, like:
Python treats it as a float. You'll see this one a lot in Search Console data, in average position and CTR.
True or false: Boolean
This one matters a lot in SEO. For example:
The type is called bool, and its value is always one of two: True or False. It fits SEO yes-or-no questions perfectly. Is the page indexable? True. Does it have a meta description? False. Does it have an H1? True.
So what does a variable do?
When you write:
you have two things: the name clicks, and the value 1540. The variable clicks is now a name that points to the value, so instead of typing 1540 every time, you can write:
1540
Back to objects
When you write url = "https://example.com", the value "https://example.com" itself is called an object, an object of type str. When you write clicks = 500, that 500 is an object of type int. And you can ask Python to tell you the type itself, with type():
<class 'str'> <class 'int'>
Let's try it on a whole page of SEO data
Without running the code, predict the type type() will print for each variable.
What will Python print for each line?
Why should I care about the type?
Look at clicks = 100 and clicks = "100". To you they look the same. To Python they're completely different: the first is an int, the second a str. Try this:
150
But if you write:
Why does the code above throw an error?
And this is a real SEO problem. Say you pull your Search Console data, and the Clicks column looks like numbers. If Python reads that column as text by mistake, every calculation on it breaks. That's why data types aren't an academic topic: when you work with real data, you need to know what Python actually thinks that data is.
One more important type: None
Picture a page with no meta description. We can write it like this:
None means there's no value here. It isn't the text "None", it isn't 0, and it isn't False. It's simply the absence of a value, and you'll see it a lot. For example, canonical = None means no canonical was found on the page.
True or false? None means the value is the word "None" as text.
Lesson exercise
Here's a page:
Store this page's data in Python variables
Write seven variables with exactly these names: url, title, status_code, word_count, position, indexable, meta_description. Pick the right data type for each value, and notice that the meta description is missing.
In the next lesson we use the same idea, but instead of one variable per value, you'll learn to keep a whole set of pages in one place: lists and dictionaries.
You still have exercises left
0/4Exercises left: 4 of 4. The lesson counts as complete once you solve them all, and you can come back to them any time.
Well done, you reached the end of the lesson.