- Hi, this is Jeff Heaton, welcome to Applications
of Deep Neural Networks with Washington University.
In this video, we're going to look at
an introduction to Python, see some of the
preliminaries that will help you
make use of the Python programming language
as you work through this course this semester.
For this course, we use Python 3, Tenserflow, and Keras.
For the latest on my AI course and projects
click subscribe and the bell next to it
to be notified of every new video.
In this part, we will take a look at
the Python programming language.
This part starts at the very beginning
with Python so we look at how to print out
strings, numbers, looping, and if statements.
The next part we'll get into lists
and maps and how to deal with those
so if you're already very familiar with
just the introduction to Python
you may well want to skip this part
and proceed with the next one.
All of module one deals with an
introduction to the Python programming languages
but we do get into more advanced topics
such as pandas and MapReduce in
the later parts of module one.
We are using the Python Three programming language
so it's important that you're using that.
Python Two, while is still a very viable
Python version, simply has differences in syntax
that will give you issues with some of
the programming code in this class.
Like most programming language tutorials
I'm going to start with hello world
and in Python, you have functions, so
print open parentheses closed parentheses
and you pass a string to it, now in older
versions of Python, you didn't always
have to put the parentheses around this.
You could just do print space hello world in quotes.
That was deemed to be not consistent with
function handling in Python and eventually went away.
So you'll want to have the parentheses.
If you ever deal with print where there's not
parentheses, that's older Python Two stuff.
You can also have comments in Python.
Comments are great because they
let me know what you're doing.
They let you know what you're doing
later on when you might have forgotten.
When you submit assignments to me
comments will always help, if your code is
not working correctly but I see in the comment
that you had a general idea what was
supposed to be happening, that's a real
encouragement for me to give you at least partial credit.
So running this with the comment in there.
it works just the same as the first one
but the comment's simply ignored.
The triple quote, so three double quotes
in a row, is pretty cool in Python.
This will allow you to have a multiple line string.
So if you run this, it's like having
three print statements and all the
line breaking is handled for you.
So you'll frequently see these in Python.
Now if you're putting in a large large
block of text, you might really wanna
think about storing that text in a file rather
than hard-coding it right into your source code.
But this is a good way if you are
going to truly have carriage returns
and line breaks in your code to
encode it all into one string.
Now these are also sometimes used as comments
because if you just have a string by itself
so say I got rid of the open parentheses
got rid of the closed parentheses, now that
would be almost a multi-line comment because
the string is not going to really do anything for you.
Now in Python, like a lot of languages
you can have single quotes or apostrophes
or straight line quotes, whatever
you want to call those, they work
exactly the same as the double quote
so you can choose interchangeably between those.
This is just gonna print out
hello world just like the other one.
As far as picking which to use
sometimes the single quote is suggested
for more symbolic type of strings.
Characters, but not checked, if it's
truly human readable text then
you probably want to use the double quote.
Now, another technique that I will use
that I find kind of useful is say
I wanted to put a double quote
right in the middle of this, this would be
perfectly valid, and I wouldn't have to
do the normal escapes that you put around them.
Now, this is valid too, this will
put the W in quotes, but since
we have the string wrapped around
single quotes, this also works.
Numbers do not need quotes and you can use variables.
Variables in Python do not have types
so it's simply A equals 10, B equals 10 in quotes
and Python figures out that the first one
is an integer and the second one is a string
so you don't need to define those types
ahead of time, it's a dynamic.
If you run this, it prints out 10 and ten.
So you can see that both of those two
variable types are being accounted for.
What's nice about variables is they can vary
so you can change the variable as you go.
Here we started out with A is equal to 10
and then we added one to 10, now in Python Two
you will sometimes see a plus equals one.
This does exactly the same thing as that first one.
It's just a difference in syntax.
You've probably seen things like this too.
That does not exist in Python.
So if you haven't seen that, don't worry.
Doesn't exist in Python, now when you
want to print out a string and a variable
together, there's a variety of ways to do this in
Python, but the recommended way for this course
is to use something called an F string.
An F string is just the letter F
with either a single or double quote next
and then you start out just having
a normal string, but then you can put
anything you want to between curly braces
and whatever you put between curly braces are
basically expressions that would be printed out.
They can be single variables like this.
This will print out the value of A is 10.
You can also put expression type values in here.
So A plus five, this will evaluate
that expression and put out that value for you.
Python has many ways to print numbers.
These are all correct, however
for this course, we will use F strings.
IF you submit something to me in one of
these other formats, that's perfectly fine.
Python has a lot of ways to print out
numbers and strings together because
Python's been around for a while.
So it's picked up quite a few different ways.
If we run this, we're going to see that
they all print out A is equal to five.
So we've got the F strings, we've talked about that.
This is a method that I used a lot
until I started using F strings.
A is equal to open closed curly brace
and then you pass in all the values.
So if you had another curly brace in there
you would do something like A comma B.
You can also simply can catenate strings together.
This is the somewhat more primitive way
of doing this, it's okay, this can
get you into trouble sometimes.
You have to usually put the str around A
if A is not a string, otherwise you'll get an error.
If you've ever worked in the C
programming language you got used
to a function called S print F or print F.
This does basically that, that percent D
means print out a decibel number and then
you do a percent sign and you pass in
as many values as you made use of.
Here we just have one, so we print that out.
If statements, Python has if statements.
These are quite useful to you.
This also brings up one of the
most hated features of Python for some people.
I used to be one of the haters
but now I somewhat like it, this is
the fact that in Python, white space is syntax.
White space affects how the programs run.
So blocks, this if statement here
everything that is indented after that colon
is part of that if statement, so we can
put in a second print statement if we wanted to
so long as we indented it properly
it would be part of that if statement.
The else works the same way, if the
if is not true, then the else is executed.
And this program will run and since five is
not greater than five, it's going to
print out the second one, now you can
use tabs, you can use spaces, you can
use any white space you want for these blocks.
But be careful, if you're using
tabs and spaces both because that's
a recipe for frustration for both you and Python.
Well, Python won't really be frustrated.
It will just give you an error.
It's going to make your code very
hard to manage if you're using both of those.
If you're using tabs, use tabs.
If you use spaces, use a consistent amount.
Pick maybe two spaces or four spaces
to represent that tab, I typically use tabs.
It's another great programming debate
as far as should you use tabs or spaces
and not likely to be solved any time soon.
Different programmers prefer different ones.
For this class, I don't care, use spaces, use tabs.
Use whatever you want so long as your code works.
Now we can also get into multiple levels.
If A equals five, then it's going to
execute that block, but notice the elif, else if.
That basically means almost as if
you had that that but it's shorter to type that.
So else if A equals six, this is kind of like
a case statement in Python, so if
A equals five, and notice too you have
those double equals, in Python
the programming language, the double equal
basically means that it is equal to.
Single equal means an assignment,
So here will count to nine in Python.
Looks like we're counting to 10.
It's a common source of confusion.
We're using something called range.
This essentially means that we're
looping from one up to but not including 10.
By the way, if you ever see xrange
that's an old Python Two thing for efficiency.
Don't worry about that, we are using range.
If you see code with xrange you're
dealing with an earlier version of Python.
And this will just print out the loop.
We're looping from one up to nine.
You can also print numbers and strings together
in these loops, so here I'm creating
an accumulator, starting it out as zero.
And we're going to loop for X in range one to 10.
The accumulator plus equals x so
we're accumulating, we're adding
x to it each time, and then we print out
what we're adding and what the sum is so far.
If you run this program, it will say
adding one, so of course the sum is one.
Then two gets added, so it gets to three
and six, one, 10, 15, and so the series continues.
It finally stops at 45, thank you for
watching this video on the introduction
to the Python programming language.
In the next video we will take a closer look
at how lists and other data structures
in Python are used, this content
changes often, so subscribe to the channel
to stay up to date on this course
and other topics in artificial intelligence.