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 jupyter 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 on
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 jupyter notebook
So jupyter 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
Jupyter notebook through something called anaconda and it's fairly easy to use as
Well so here's the way, jupyter notebook, works there are two components to jupyter notebook
First of all when you launch jupyter notebook on your computer you might see a command line interface like this
This, represents the jupyter notebook server
You can, think of it sort of like the core of jupyter 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 jupyter notebook might stop working so just be careful about that
Now, when you launch jupyter 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 jupyter notebook server
this
Browser window or tab is basically the user interface for jupyter notebook and you can write and execute your code
Here but it's actually executed on the jupyter 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 jupyter 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 jupyter notebook so you
Don't have to install python or jupyter notebook separately on your computer
Let's now install python and jupyter 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 jupyter you need to launch an application called
Anaconda navigator
Just launch that in the usual way you launch any other application, and then find jupyter 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 jupyter 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 jupyter 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 jupyter notebook the navigation
Menu and if you already, close the tab
Just open localhost colon 8080 eight eight or just relaunched
jupyter 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 you in the next video?