hey everyone my name is YK and I'm the
founder of this YouTube channel CS dojo
and I'm also a former software developer
at Google and this is my new series
Python tutorial for absolute beginners
and this is my video number 1 what are
variables so this series is gonna be
good for complete beginners who have
never done any programming before as
well as someone who's done some
programming before whether it's Python
or any other language but wants to learn
more about Python so in this video I'm
going to go through a number of
different things first of all what is
Python and what can you do with it and
then I'm gonna explain how to install
Python on your computer and then set up
a development environment we're gonna
use something called Jupiter notebook
throughout this series and then I'm
gonna explain how to use the print
function and what are variables and how
to use them and by the end of this video
if you have two variables storing
different things you'll be able to swap
the content within those two variables
with each other and I'm gonna put an
outline of this video in the comment
section below so you don't have to watch
the whole thing if you don't want to oh
and before we get started if you want to
make sure that you don't miss my future
tutorial videos sign up to my newsletter
which is available in the description
below so just quickly what is Python and
what can you do with it it's one of the
most popular programming languages out
there and it's used at smaller companies
as well as larger companies including
Google and many universities use Python
as the introductory programming language
in their computer science courses and
one advantage of python is that it's
fairly easy to learn because it has
fairly simple syntax it's used for many
different applications including
websites back-end code which is the code
that runs on your server as opposed to
the code that runs on your device
whether it's a phone or a laptop and
it's also popular for their analysis and
scientific research purposes okay so
let's now go ahead and install Python on
your computer usually when people
develop a program using Python or any
other language for that matter they
usually use something called IDE which
stands for
integrated development environment it's
an environment that includes everything
you need pretty much to develop a
program it has a nice text editor in
which you can write your code and then
the ability to run your program as well
as something called a debugger which
allows you to quickly get rid of all the
bugs in your program and if you want to
use an IDE to follow my series I
personally recommend Park charms
Community Edition so in this series
instead of using a traditional IDE I
decided to use something called Jupiter
notebook so Jupiter notebook is an
environment for writing and testing your
program quickly and it's actually
popular with the scientific community
and for data analysis purposes but I've
decided to use Jupiter notebook
throughout this series because it's very
simple and easy to install
Jupiter notebook through something
called anaconda and it's fairly easy to
use as well so here's the way Jupiter
notebook works there are two components
to Jupiter notebook
first of all when you launch Jupiter
notebook on your computer you might see
a command line interface like this this
represents the Jupiter notebook server
you can think of it sort of like the
core of Jupiter notebook and you don't
have to worry that much about how it
works exactly but just remember that if
you close this window maybe accidentally
Jupiter notebook might stop working so
just be careful about that
now when you launch Jupiter on your
computer you might also see a browser
window or tab showing up whether it's
Chrome Firefox Safari or anything else
and it'll probably be at the URL
localhost call eight eight eight eight
or something like that
and it'll be connected to the Jupiter
notebook server this browser window or
tab is basically the user interface for
Jupiter notebook and you can write and
execute your code here but it's actually
executed on the Jupiter notebook server
again you don't have to worry that much
about how it works exactly but this is
just an overview now to install Python
and Jupiter notebook we're going to use
something called anaconda anaconda is
something called a package manager
which allows you to install many
programs at once this particular package
manager' anaconda is actually used for
installing math and science libraries
but you don't have to worry about that
the only thing you need to know is that
when you install anaconda it comes with
Python and Jupiter notebook so you don't
have to install Python or Jupiter
notebook separately on your computer
let's now install Python and Jupiter
through anaconda go to anaconda org and
then click download anaconda and select
whatever platform you're using whether
it's Windows Mac or Linux I'm using Mac
here so I'm gonna click mac OS here and
there are two options here Python
three-point-something or Python 2 point
something make sure to use Python 3
point something because we're going to
use Python 3 instead of Python 2
throughout this course so click download
and then save this file wherever you
want to save it and once this file is
downloaded just open it and then click
continue continue continue agree with
everything and then select Install for
me only or install on a specific disk it
doesn't matter which one you choose and
then click install and this process will
probably take a while for you once this
process is done you should see something
like the installation was completed
successfully once you see that just
click close and you're all set to launch
Jupiter you need to launch an
application called anaconda navigator
just launch that in the usual way you
launch any other application and then
find Jupiter notebook and click Launch
like I said before you see a browser tab
or window opening up and it should be at
the URL localhost colon 8080 8th 8th or
something like that ok let's now create
our first program first go to whatever
the folder you want to create your first
program in I'm going to go to desktop
and once you click it you can see that
my current location is desktop because
it's
at the top right here before you create
your first program here I personally
recommend that you create a new folder
so I just went to my desktop I clicked
right click and then new folder I'm
gonna call this folder Python tutorial 1
and once you create that folder you
should see in the Jupiter notebook
interface to click that and you see that
the current location is Python tutorial
one within desktop right here at the top
to create a new file or a new notebook
file as I say just click new at the top
right hand corner and then click Python
3 so a new notebook has been created
here change the name of this notebook
from untitled to let's say what are our
variables click rename and once you do
that if you go back to desktop and the
folder that we just created python
tutorial 1 you'll see a new file what
our variables
dot I PI and B and that I PI and B is
just an extension for algebra notebook
now in Jupiter notebook there's
something called a cell each cell
represents a set of code and the box
you're looking at right now is one cell
so you can type in any Python code here
for example print parentheses double
quotes hello world and when you run this
cell using this button at the top it
executes all the code within the cell so
let's run this cell and you see that the
string hello world is printed so this
text hello world is something called a
string it's usually enclosed by either
double quotes or single quotes and it's
basically just a set of characters so
assuming that this is your first time
coding in your life I'm going to explain
this line a little bit more carefully so
this says print whatever is in the
parentheses these two parentheses and
that happens to be hello world a string
and when you run it again it's printed
right below this cell how the world and
the important thing to note here is that
if you don't type in the exact
set of characters that you see on the
screen it might not go right so for
example if you forget to close the
parentheses and run the cell you'll get
an error and if you use for example
curly brackets instead of regular
parentheses
you also get an error and if you forget
to close the quotation marks it's the
same thing so let's try using this print
function a little bit more in this cell
that you see right here in the green box
you can type in print double quotes more
string and actually instead of using
double quotes you can also use single
quotes in Python and then you can run
this cell and the string more string is
printed so you can use single quotes and
double quotes pretty much
interchangeably in Python and then you
can also print not just a string but
also a number so you can say print
parenthesis 3 and then when you run the
cell the number 3 is printed and you
could also have multiple lines within
the same cell as well so in this cell
you can write print let's use double
quotes here more string and then print 3
so when you run this cell these two
lines are executed one by one so you see
more string and three print it just one
after another
so let's now dive into our main topic
here namely what are variables before I
explain what our variables exactly in
Python I'm going to show you some sample
code you can write a equals 1 and what
this means is that assign this value 1
this number to the variable named a you
can think of it it sort of like the
variable a containing the value 1 that's
not 100% technically accurate as I'll
explain later but that's one way to
think about it so when you run this cell
you won't see anything printed this time
but the variable a now contains the
value 1 or a more technically accurate
way of thinking about it is that the bar
a refers to the value one and you can
check what's inside the variable a by
printing the variable with print
parentheses a and note here that there
are no double quotes or single quotes
around this character a and that's
because a is not a string it's a
variable and when you run the cell you
see the value a is referring to which is
one and you can do the same thing with
different variables so you can write B
equals two so the convention here is you
write B space and then equal space and
to this line says assign the value 2 to
the variable B and when you run the cell
again the variable B refers to the value
2 now so if you haven't for example C in
this code and if you want to know what's
inside the variable or what the variable
refers to you can write just like before
print parentheses be no single quotes or
double quotes run this cell with this
button right here and then you'll see
the value B is referring to just like
before we can write multiple lines
within a single cell right here by
writing print parentheses a print
parentheses B and when you run this cell
you'll see one and two so one is what a
is referring to and two is what B is
referring to so a variable doesn't
necessarily refer to a number it could
refer to a string so if you write for
example C equals either double quotes or
single quotes hello there and when you
run the cell C now we first see the
value or the string hello there and when
you print C you should see how low
they're printed so I just ran this cell
and the string hello there has been
printed
so let's now quickly talk about what
variables are in Python when you write a
equals one in a different language for
example C or C++ the correct way to
think about it might be as a box so you
have a box
a containing the value one but in Python
this is not the accurate way to think
about it the more accurate way to think
about it is that a is more like a name
tag and this can refer to any value you
want and when you say a equals one
you're saying a refers to the value one
now these two ways of thinking about it
they might not seem that different right
now but it's going to be more important
later and in Python you can create a new
variable by writing as we saw B equals 2
and this says the name B or the variable
B now we first see the value 2 and just
like you saw a variable can refer to a
string as well so if you write C equals
hello there
you're saying the variable C refers to
the string hello there once you master
that basic concept you'll be able to
move on to a slightly more advanced
topic let's say you run these lines of
code and then you want to run more code
for example D equals 2 then what happens
is the variable D of course refers to
the value to which the variable B also
refers to so it's totally possible for
two or more variables in Python to point
to the same value in this case 2 and
then it's also possible for you to
reassign an existing variable to another
value so if you write B equals 1 after
executing this line of code B equals 2
after this line B refers to two of
course
but after writing B equals 1 B refers to
1 which a also refers to and you can
even reassign an existing variable for
example B which once referred to a
number one right here to something else
for example a string so if you write for
example B equals double quotes ah the
string the variable B now refers to the
string ah so let's see how this actually
works out in code okay we're gonna
continue on the code we had earlier
earlier we had B equals
so if you print be the variable we get
the value to and you can reassign this
variable to another value for example 1
by writing B equals 1 and then when you
run the cell and print B again you get
one printed the new value and what
happens if we try to print a variable
that doesn't exist yet
so if you try to print print parenthesis
a what happens let's run this cell and
see what happens it'll actually give you
an error because he doesn't exist yet
and the error says name error name e is
not defined yet so that makes sense
let's fix that by writing e equals this
is a string and this way the value this
is a string is assigned to the variable
E and the variable E exists in the
system so if you print E with print
parenthesis E and when you run this cell
you don't get the error anymore and
let's address another question you might
have here is it possible for us to
assign a variable to another one the
answer is yes so here if you write
equals 1 a of course refers to 1 and
then C equals hello world C refers to
hello world what happens if you write F
equals a what happens then is this means
the variable F the name F refers to the
value a is referring to so F refers to
one now and so f doesn't refer to the
variable a instead it refers to the
value a is referring to and this is
important for example when you do a
equals two if you write that a now
refers to the value 2 instead of 1 but F
stays at the value 1 and this might not
be the case if F was referring to the
variable a let's take a look at another
example here if you write G
Iko see the variable G will refer to
whatever the value C refers to which is
how it world and then once you write C
equals hello C will refer to the new
value hello right here but the variable
G will stay at the value hello world
okay let's go back into the demo earlier
in the demo we had a equals one and then
C equals hello there so once we print a
and C with print parenthesis a and print
parenthesis C we should see one for a
and hello there for see what happens if
we write F equals a F should now refer
to whatever a refers to so if we print F
we should see the number one printed and
what happens if we assign a to two then
the value of a should now be two and
then the value of F should stay as I
said earlier at one okay now I'm gonna
give you a quick practice problem to
work on in order to solidify your
understanding of variables and to do
that go back to the previous tab of
Jupiter notebook the navigation menu and
if you already close the tab just open
localhost colon 8080 eight eight or just
relaunched Jupiter notebook and then go
back to the same folder as before we had
python tutorial one click new in the top
right corner click python 3 again and
let's rename this notebook by clicking
untitled and by changing it to swapping
two variables and here's the problem
you're given two variables let's say v1
equals first string in double quotes and
v2 equals second string again in double
quotes
how can you swap the values within these
two variables with each other the
easiest way to solve this problem is
just to write v1 equals second
string and then b2 equals first string
but this might not be the best solution
because if you had a much longer string
it would be kind of cumbersome to repeat
these strings and then if one of the
strings changes so if first string
becomes first two strings that you need
to rewrite your code right here to match
that to write first two strings again so
ideally you want to be able to solve
this problem without repeating the
strings think about it for a second and
then I'm gonna show you what a good
solution might look like when you see
this problem you might say well it's
actually pretty simple once we have b1
equal to first string and v2 equal to
second string so that's v1 referring to
this string and v2 referring to this
string we can just write v1 equals v2
and then v2 equals v1 and we're done but
actually this solution is wrong because
when we execute this line v1 equals v2
v1 now refers to whatever v2 refers to
of course which is second string and
then when we write v2 equals v1 v2 will
now refer to what v1 refers to now which
is second string so at the end of these
lines of code will have v1 and v2 both
refer to the second string which is not
what we want let's now see what a good
solution might look like I'm going to
give you a few different options here
the first option is to use two temporary
variables we're gonna call them temp 1
and temp 2 temp 1 will be equal to what
v1 refers to so that's first string and
then temp 2 will be equal to what v2
refers to which is of course second
string and then we'll just need to swap
them together
so v1 will be equal to temp 2 which
refers to second string so v1 will now
refer to second string after this line
right here and then v2 will be equal to
temp 1 which
of course first string so v2 will refer
to first string after these lines so
this is one potential solution but
actually we don't have to use two
temporary variables okay here's a
solution that only uses one temporary
variable instead of two the first thing
we're gonna do is we're gonna set a new
temporary variable called temp - what v1
refers to so temp will be this string
right here and after that we'll set v1
to v2 so v1 will no longer refer to
first string and then it will refer to
second string instead and at this point
note that the first string this string
is not lost yet because we have a
variable pointing to that value so after
that all you need to do is refer v2 back
to whatever temp refers to which is
first string so v2 will refer to first
string after these lines of code and the
v1 will refer to second string and this
little pattern of using a single
variable to switch the values of two
variables is actually really important
to know because it's a very common
pattern to use for any programmer okay
let's now test that pattern with our
Python code so in the second sale we're
going to write our solution we're gonna
create a new variable called temp which
refers to whatever v1 refers to which is
first string and then we'll set v1 to be
v2 so v1 at this point is second string
and then we'll set v2 to whatever
temporary first - which is first string
so at this point once we print v1 we
should be able to print second string we
do and then if we print v2 which would
see first string and we do okay to
download the files I created throughout
this video just go to CS dojo io /
Python 1 or if you just want to
subscribe to my newsletter so that you
don't miss my future tutorial videos
just go to CS dojo dot io / news and as
always let me know in
comment below when you thought of this
video I'm YK from CS dojo and I'll see