The first big Python programming concept that we're going to talk about are variables. When you think 1
about programming and software at a high level, 2
it's all about manipulating data. 3
For example, if you create a computer game, then you'll need to track the player score, the number of lives 4
that the player has and what level the player is on. 5
Of course, all this data changes over time as we play the video game and the program runs. 6
So where do you as the programmer store this data and how do you track it? The Python programming language 7
has a very convenient solution. 8
You would store the game score in a variable. What is a variable? 9
A variable is just a container for data. 10
The important thing to understand about Python variables is that they both have a name and a value. 11
For example, if I wanted to track my age inside a program I can simply create a variable called "myAge". 12
"myAge" is the name of the variable, but because "myAge" is a container for data I can also give it a value, 13
so I can stick the number 32 inside "myAge". In programming lingo 14
this is called assigning a value to a variable. Now, as time passes and our program runs, 15
we might need to update this variable, and we can do that simply by updating the value inside "myAge" 16
with a new value. So we can replace the value 32 with the value 33. Now, the question is what would this 17
look like in Python syntax? Inside the Python intro notebook that we created, 18
we're going to create a variable called "myAge" and give it a value just like this" 19
myAge = 32. This name "myAge" is now how we can refer to the number 32. To display the value inside 20
the "myAge" variable below the cell, 21
I'm simply going to write "print(myAge)". 22
Hitting Shift+Enter on the keyboard will evaluate the code in the cell and then display this value 23
contain inside the "myAge" variable. 24
Now suppose our program is running and we need to update the value inside this variable. 25
We can change this value of "myAge" simply by assigning a new value to it say with "myAge = 33". 26
If I write "print(myAge)" now and hit Shift+Enter we'll see the value 33 printed out. 27
No surprises there. Now, 28
variables are super handy because you can do calculations with them. 29
For example, if we write "print(myAge/3)" 30
and hit Shift+Enter, we'll see 11.0 printed out below the cell. 31
Also, we can take these two concepts that we just covered and take it up a step in complexity. 32
Let's say we want to update the value of myAge like this. 33
myAge = myAge + 1 34
Now if it's your first time learning to program, this line of code is going to look very strange, but 35
what's going on is that we're taking the previous value of myAge, namely the value 33 on the right hand 36
side of the equals sign and then adding one to it. With that equals sign 37
operator we are storing this new value - 34 (33 plus 1) inside of myAge on the left hand side. 38
In other words, we are overwriting the previous value stored inside the 39
myAge variable. If we write "print(myAge)" now and hit Shift+Enter, we'll see our value update to 34. 40
So let's do a quick exercise on using and manipulating variables in Python. As a challenge, 41
can you create a variable called "restaurantBill" and set its value equal to 36.17? 42
Then create a variable called "serviceCharge" and set its value equal to 0.125. 12 and 43
a half percent seems to be the standard rate that restaurants suggest as a tip in London these days. 44
Finally, print out the amount of tip below the cell that you would need to add to the bill. 45
I'll give you a few seconds to figure this out. 46
So pause the video. Okay, 47
so here's the solution. For part 1, 48
we'll write "restaurantBill = 36.17". For part 2, 49
we'll write 50
"serviceCharge = 0.125"; and for part 3 we'll write 51
print() 52
restaurantBill 53
Times serviceCharge. 54
When I hit Shift+Enter, 55
I'll see that the value of the tip is 4.52. 56
Now all that's left to do is asking the waiter if the restaurant is pocketing the money, or if the tip 57
really does go to the staff. 58
Now, one thing that could happen to you at this point is that you've made a typo. In programming, 59
everything is case sensitive. 60
And if our variables names didn't match exactly to how they were defined, we're gonna get some unexpected 61
errors. 62
For example, if this capital B in my print statement instead was a lower case b and we had hit Shift+Enter 63
we would have gotten the following error - "NameError: 'restaurantBill' 64
is not defined", and this is because this restaurantbill and this restaurantBill are considered to 65
be completely different entities. 66
So you want to make sure that you never have any typos in your variable names. 67
The same of course is true 68
if we miss out a letter. 69
So for example, if we wrote "seviceCharge" instead of "serviceCharge", then we would get exactly the same 70
error - our variable is not defined. 71
In other words, Python can't find something that matches this name. 72
So let's put that "r" back where it belongs. 73
Now that we've learned a thing or two about Python programming and variables, let's revisit the code 74
that we wrote when we were estimating our movie revenue. Since I saved my previous work in a Python notebook 75
and added it to the MLProjects folder, 76
I'm just going to open that now. 77
Now, 78
even though this code is a little bit more complex than what we have written just now, 79
can you spot the variables in this piece of code? 80
Now there's actually quite a few. 81
And you'll notice that the variables are often to the left hand side of an equal sign. 82
So for example data is a variable, the capital X is a variable, the lower case y is also a variable. 83
All of these variables are holding on to data, but in contrast the variables myAge and restaurantBill, 84
these X and y variables are not holding on to a single value, they are holding onto lots and lots of 85
values at the same time. 86
And I'm going to show you guys how that works in a little bit. But before I do that, 87
let's talk about another key concept that we need to understand when working with variables, namely data 88
types. In machine learning and programming, more generally, we'll be working with different kinds of data. 89
We'll be working with text, decimal numbers, tables of data, columns of data, images, sounds, video, all sorts. 90
And a programming language like Python will categorize this data. 91
In other words different kinds of data, like text or decimal numbers and whole numbers, will have a different 92
category. 93
In other words, they will have a different data type. 94
Now you can think of data types like this children's toy where you have to fit the right shape into 95
the hole. 96
So for example when you try to put a decimal number somewhere where Python expects to have a string, 97
you'll often find that you have a problem. 98
And this makes sense when you think about it intuitively. 99
So you have some Python code that makes a calculation. 100
So you're adding two things together. 101
If those two things are numbers then you're good; 5+10 is 15. 102
But if one of those things is another kind of data, like, I don't know, a home address you have a problem. 103
Your program will crash or it's going to do something very unexpected. 104
Trying to evaluate 5 + 21 James Street. 105
It doesn't make any sense. Now for the most part, 106
Python will actually take care of the data types behind the scenes, so it's not something that's kind 107
of at the forefront of the programming syntax of the Python code. 108
But let me show you how you can actually see the data type in Jupyter notebook because we can ask Python 109
what category something belongs to by writing "type()" and then putting something between those 110
two parentheses. So for example, if I write "type(33)" and hit Shift+Enter, the type of this whole number is 111
int. Int stands for integer, which makes sense. 112
But there's also quite a few other types with maybe less intuitive names. 113
So let me introduce a couple of them; if we write "type(33.6)" and hit Shift+Enter, we get 114
to see that decimal numbers are classified differently. 115
Decimal numbers are classified as floats or floating point numbers. Floating point numbers are the type 116
that you will usually be working with every time you're dealing with numbers that have a decimal point. 117
Now let me show you what the type for text is called so can write "type()" and then between the parentheses 118
I'm going to open the single quotes and add my name. When I hit Shift+Enter, 119
I can see that type for text is called "str", and "str" stands for string. The word string is just computer 120
jargon for a piece of text or a sequence of characters. And the way that you can tell that something 121
is considered to be a string is by the fact that strings are always, always surrounded by either single 122
or double quotes and Jupyter notebook is actually also very helpful with the syntax highlighting so 123
that the Python code that is considered to be a string actually has a different color. 124
So in this case the string is marked as red. 125
Let's take another look at the Python code that we wrote previously and see if we can spot a string. 126
Here we can see that when we were writing "pd.read_csv()", the value 127
that we specified between these two parentheses was a string. 128
It was a piece of text marked by the single quotes. And the same is true for the column names - "production_budget_usd"; 129
and it's also true for the X label on our graph and all the other 130
parts where the code is highlighted in red. 131
Okay, so far so good. 132
We've just introduced you to a whole bunch of new programming jargon - variables, data types, int for integers, 133
float for decimal numbers, str for pieces of text or strings. 134
But how do data types relate to variables? 135
What's the connection between the two? Well, if the data has a certain type then so does the variable 136
containing the data, because after all a variable is just a container. Now, for example, let's see what 137
the data type is for the variable myAge so I can write type and then between the parentheses I'm going 138
to supply myAge and hit Shift+Enter and here we can see that, because myAge holds on to whole numbers, 139
the data type of the variable is also integer or int. 140
Now let's take a look at the data type for restaurantBill, 141
and I'm sure it's no surprise that, because restaurantBill is holding on to a decimal number 142
36.17, 143
it's a type float. If I was to create a new variable called "myName" and set it equal to the string 144
Philip, I can write "type(myName)", hit Shift+Enter and I can see that the myName variable is a string. 145
Now let's take another look at the notebook where we ran our regression, and let me ask you this: what 146
do you think is the data type of the variables data, capital X and lowercase y? 147
This is the mystery that we're gonna be exploring over the next couple of lessons. I'll see you there.