In this lesson we're going to wrap up our introduction to Python programming with something a little 1
bit more fluffy. 2
A hot topic on the Internet is always coding style. 3
But what do I mean by coding style? Coding style is whether you use capital or lowercase letters, whether you 4
use tabs or spaces, where you put your curly brackets and so on. 5
It's how you arrange your code and what sort of syntax you use. Now the thing about programming style 6
is that it's the classic bike shedding problem, right? 7
Everybody has an opinion on what color the bike should be. 8
And so everyone gets in a heated and lengthy argument about it in the same way. 9
People also have very strong feelings on how code should be formatted. 10
Now the irony is that the standards vary hugely between programming languages. 11
So mainstream Python programming style is a little different from the mainstream Java programming style, 12
which again is different from C++ or Swift. 13
So the question is how do we find out what good Python programming style is or should be? 14
And what are the guidelines that we should follow or what are the guidelines that people tend to follow? 15
Well, lucky for us there are so-called style guides out there on the Internet. 16
And the most popular style guide I think these days for Python is the official one from the Python Software 17
Foundation. 18
And it looks like this. 19
It's called PEP 8 - the style guide for python code. 20
And if you ask me this this makes excellent bedtime reading. Yeah, it's pretty long. 21
It's very comprehensive and it's also not something that needs to be followed religiously. 22
It's a suggestion for writing readable Python code, right? Readable for humans. 23
The computers don't really care. 24
The thing I'll say is that this isn't the only style guide out there. 25
In fact many companies have their own style guides too, that, you know, might be slightly different from 26
the ones of the Python Foundation for example. 27
So here is the style guide from the engineers at Google. 28
The Python style guide, to be specific. 29
Now, I was actually looking through this and trying to spot a difference with the foundation's style 30
guide and the thing I noticed is that these style guides are actually very, very similar and there are 31
maybe like some differences; Python Software Foundation says line length should be 80 characters, 32
Google says it should be 79. 33
Most of the suggestions are actually really, really similar. 34
So one of the differences I actually found seems to me more philosophical than anything else, right? 35
So at the end Google says their parting words. 36
BE CONSISTENT. 37
Right? 38
This is this is the formatting they use - all caps. 39
Right? 40
In contrast, the PEP 8 style guide from the Python Foundation says "A Foolish Consistency is the Hobgoblin 41
of Little Minds". 42
So what they're saying here is that you can be a bit more flexible in your coding style if you need 43
to be. 44
Now as I said before, in the end none of these style guides are hard and fast rules. 45
They're more about aesthetics and readability of the code. 46
So you yourself you can make a choice, right? 47
You can choose one of these style guides and just stick to it. 48
That's really all there is to it. 49
That said, let's have a look that we've written so far in the lessons and see how it stacks up against 50
the style guide from the Python Software Foundation. 51
I'm not going to ask you to read through all of PEP 8 and play dpot the difference, but truth be told 52
we've actually violated some of the style recommendations of PEP 8. 53
So what I want to do in this video is kind of give an introduction to familiarize ourselves with what 54
is considered good practice. 55
And afterwards we can return to our Python code and make changes if necessary. 56
One thing that we've already alluded to in the previous lessons is the indentation - 57
So the layout of our code. And we recommended that we use four spaces for each indentation level. 58
If we look back at our Jupyter notebook, we can see that this formatting has already been taken care 59
of for us, right? 60
So we can see that this print statement 'Open Door' is one, two, three, four spaces indented. 61
So in many ways the editor that we're using - Jupyter notebook or other text editors - modern text editors, 62
not Notepad actually shoulder some of this code formatting burden for us. 63
Another thing that I want to point out is naming conventions. 64
So I'm gonna dwell on this a little bit because it's very, very handy for reading and interpreting the 65
code that other people have written. 66
So naming conventions are handy for being able to read Python code and know a little bit more about 67
what it is that you're looking at, just from how it's typed. So let's start with variables. 68
Typically you'll see variables written as single letters, especially if you're doing math or physics 69
or machine learning, right? 70
So lowercase y, Capital X, these are variable names that you'll see very, very often. Now short variable 71
names like this are good when there's like an accepted convention in place. 72
So for example with scikit-learn, you'll often see the variable name for the training data as capital 73
X and you'll see that target values as lower case y. 74
And this is a convention that they adopt and try to be consistent with across, like, their whole code 75
base. 76
But there is also a big, big advantage from not using single letters for variable names because if you 77
have a descriptive variable name, right, as opposed to a single letter you can help make your code a lot 78
more readable. For a nice, long, descriptive variable name, 79
you'll want to write it in all lowercase and you want to separate, if there's multiple words, you want 80
to separate the words with an underscore, so lightning_mcqueen was an example of this kind 81
of formatting. And if you really, really want to use a single letter variable names then please, please 82
please do not use lowercase l or uppercase I or uppercase O as your variable name. 83
It's really, really hard to tell these apart with smaller fonts, right, 84
if you're looking at a screen. So yeah, 85
single letter variable names, maybe just exclude these ones for readability. Now module and package 86
names, they actually tend to follow the same naming conventions as variables really. 87
Right? 88
They're written in lowercase and the longer names have the words separated again with an underscore. 89
And again, for functions and methods, we typically use lower case words separated by underscores as well. 90
If the function in question has parameters, then the parameters tend to follow the same naming conventions 91
as with variables. 92
So with this in mind, we can actually quickly spot the coding style in our Python intro notebook where 93
we've run afoul of these guidelines. 94
For example, at the very top of our Python intro notebook we had some variables called myAge, restaurantBill 95
and serviceCharge and these are written in camel case. 96
So camel case means starting with a lowercase letter and then capitalizing each word. 97
And this is quite a popular style in programming languages like Java but you see it much much less in 98
Python. Cool. 99
So that pretty much covers our introduction to Python programming. 100
We'll be diving deeper into Python programming techniques and the subsequent lessons because programming 101
is actually strongly linked to both data science and machine learning. 102
And the reason is that programming goes well beyond how to call functions from scikit-learn. Knowing 103
programming develops your mindset as an engineer. 104
And to succeed in this course and with machine learning more generally, having an engineering mindset 105
is key as part of this course. 106
And in the workplace you're going to be putting a lot of different skills together from different fields. 107
And those include mathematics, statistics, design and of course programming. 108
And the reason is that outside of academia you're gonna be in a position where you're going to be trying 109
to solve a real life business problem and figuring out how to solve this problem will test your engineering 110
mindset. 111
You know ,a common misconception these days is that to solve problems you need to use the most sophisticated 112
model possible or, I don't know, artificial intelligence. 113
Everybody is talking about A.I., right? 114
Artificial intelligence. 115
However, in reality A.I. is not the only thing that matters. 116
And A.I. doesn't solve all problems. 117
Now depending on your specific problem that you're faced with, A.I. might not even add all that much 118
value, especially considering the high cost and the high amount of data required to build an A.I. system. 119
So, you know, with your engineering mindset as an engineer you're going to be thinking about how to solve 120
your problem in an efficient way. 121
So maybe a simple model will do for your purposes and this is why this course will teach you a variety 122
of tools and that way you can use the right tool for the job. 123
After all, if all you've got is a hammer then everything starts to look like a nail. 124
And coming back to programming, and programming in Python specifically, Python is actually a really, really 125
nice language to start learning to program in contrast to other languages like C or C++, because Python 126
actually takes care a lot of the error prone grunt work that goes on behind the scenes for you. 127
And I don't think this is a bad thing because, unlike with C or C++, with Python you don't have to do 128
chores like laying out the memory structure, managing the memory allocation or implementing access routines. 129
Now, don't get me wrong, 130
I think there is a time and place for all those things and becoming familiar with like how computer 131
hardware actually works will definitely, definitely help down the line but I don't think it's useful 132
to get confronted with this right away, especially if you've got so many other things to worry about. 133
Which brings me to our next topic. Where are we going next? 134
The next couple of lessons and sections will introduce you to the core of machine learning, namely loss 135
functions and a technique called gradient descent. 136
This is where the actual learning takes place and we're going to take a look at how this works under 137
the hood for regression techniques. 138
Also, we're going to be looking more closely at regressions that use more than one explanatory variable. 139
In other words, we're gonna spice up our regression model. 140
For example, if you were trying to predict a house price then you probably have to look at a number of 141
different factors like the number of bedrooms, the location, the total area of the flat or the house, the 142
proximity to amenities like a park or a good school. 143
There's a number of factors that contribute to price of a house and the technique that will tease 144
out the individual contribution of each of these factors. 145
It's called multivariable regression and on that bombshell, it's time for me to get some more coffee 146
and then I'll see you in the next lesson. 147
Take care.