Learn Python Programming For Hackers – Lesson 2 – Variables


in today’s lesson we’re going to talk
about variables and how to use variables how to declare them so on and so forth so declaring a variable in Python is
very straightforward and simple I mean the variable is nothing more than
basically memory location variable allows you to store information inside
the memory so you can retrieve it modify it and do what you want with it so in
order to set that memory aside you need to declare a variable so let’s go ahead
and declare a variable now we’re going to give our variable the name of X and
we’re going to initialize it with the variable 0 rather with the value 0
so now if we were to print X it will print the value 0 let’s go ahead and try
that now and as you can see here on screen it printed 0 now let’s try something different so we’re going to cook declare another
variable actually before we do that let me demonstrate something so right now
our variable is a integer value of zero now we can just as easily turn that
variable which is X and turn it into a string variable simply by initializing it with the
string variable as we’re doing here let’s save this so as you can see X was initially an
integer variable rather an integer value of zero and then we converted it to a
string and gave it the value of this is some text so as you can see a variable can be
different types you just merely need to initialize it with the datatype that
you’d like to use so let’s try this let’s turn this into a variable called
why and let’s try to print X plus y which
means add them together and print them to the screen let’s go ahead and save
that so what this should do is basically when
it’s all said and done we should you should print to the screen something
that looks like that let’s go ahead and save this and let’s
run it and all we got an error so if we look type error unsupported operand
types for plus int and stir so what it’s telling us is that we’re trying to
combine two different variable types and in Python you can’t do that
so what we need to do is convert one of these variable types to a string or they
both need to be the same variable type so in this instance we’re going to
convert X which is the value 0 to a string and we’re going to use the STR
function and what this function does is it takes
whatever parameter you pass into it which in this case we’re passing X which
the vet X is the value of zero and it will convert that into a string so let’s
go ahead let’s run then there you go so the first
line zero is being printed from right here and then we declare our variable Y
give it the value this is some text we print that to screen we can see that
right here and then lastly we print our variable X and concatenate that with our variable Y
to get us the text 0 this is some text No this is basically taking zero and
converting it into a stream zero so that’s all that’s happening right there
now we could go the other way with this now just as a quick example
now if you look at X&Y X is zero and Y is zero so to us as a human it’s the same data
but to the computer that is not the same at all so if we try to run this we
should still get an error because even though to us both of those
are zeroes to the computer there are two completely different values so what we
could do is this time we can convert our string Y into an integer and so now they
are both integers X we can see is this integer and then we’re going to convert
Y which is a string into its integer equivalent which is zero so let’s go
ahead and run that and bam you can see we didn’t get any errors and we were
able to print both values to the screen right you so now let’s talk about global variables
so in order for this to for me to demonstrate this we need to create a
function now if right now just ignore the function don’t worry about it that’s
actually going to be our next a lesson but for right now let’s just we’re going
to need to use this for an example okay so let’s save this let’s just go
ahead and run the program so we can analyze the output all right so when the
program runs it declares X is zero and then it prints X to the screen which we
have right here zero on next we declare our function and nothing happens here
we’re just simply declaring it that gets skipped and now we move on to our next
line which is calling the test function so then our code is going to come up
here it’s going to turn X into the value of one and then print that to the screen
and that’s what we have right here and then our code is going to return back to
the text function move to the next line here X is being declared as the string
value ABC and then we’re printing X to the screen which is the text value of
ABC all right now we have something that’s called global variables so you
can see each time we change the value X as we did right here printed it to
screen did here printed it and lastly printed it every time we modified X the
value changed okay now let’s demonstrate how global
variables work you all right so let’s save that now let’s
go ahead and run it and let’s look at the output and see what’s happening so
first we’re declaring X 0 and then we’re printing that to the screen as we can
see here on the first line next our code is going to declare our function that
will then skip over that on our next line we’re actually calling our function
so our code will come back up here going to declare X is equals 1 it’s going to
print that to the screen and we can see that right here and now we’re going to leave this
function and move to the next line of code which is print X and you can see X
is printing a 0 even though we just converted it to 1 when we leave this
function and then print X it’s back to 0 as it was up here because variables
inside of a function have their own local version so this X inside this code
here is not the same X as this code here or here basically anywhere is outside
the function and that’s very important because you might want the same value to
exist all the way across the board so if that’s what you want to do what you need
to do is declare your variable X inside your function as a global variable so
now if we save this and we run the code we should get the output of 0 1 1
because we changed one here and because it’s a global variable that means X is
going to change everywhere so when we leave this function and go to our next
line X should still be 1 let’s go ahead and give that a try perfect the code executed exactly as we
expected so that’s how global variables work
they’re pretty simple you can choose to have a variable that’s local meaning it
only exists in that element of that function or you can have a variable its
global where that variable can be accessible from anywhere within that
code all right guys the last thing I’m going to show you here is that you can
delete a variable when you’re done with it if you no longer need it and you want
to free up memory or whatever you can just delete that variable basically this
should actually give us an error because what’s happening is we’re going to
delete the variable X but then we immediately try to print it and that
should obviously be a problem we can’t print something that doesn’t exist and
that’s just merely to demonstrate this delete function and how it operates so
let’s go ahead and save this code and execute and sure enough the program
worked just fine up until this last print statement we deleted the X which
that also worked and then you can see here print X global name X is not
defined and that’s because we deleted it right there so there you have a guys I
think that’s a good little introduction to variables with Python so why don’t
you go ahead and create a script and and play with these variables create some
string variables create some integer variables print them to the screen I’m
just just play around what they get comfortable with it and then tomorrow
we’re going to pick up on functions and we’re going to actually start writing
some code that actually does some function functionality does something
cool so I’ll try to come up with something that’s somewhat useful or just
something that we can code instead of just you know coding these stupid little
print statements that’s going to do it for today guys I greatly appreciate you
guys watching my videos if you enjoyed this video please give me a thumbs up
it’s very motivating for me to make new videos if you have any friends or know
anybody who might be interested in my videos please share these videos I
greatly appreciate it I will see you guys on the other side

Leave a Reply

Your email address will not be published. Required fields are marked *