[no audio]
Friends here we are going to
discuss about variable or variables, and we
are going to see, what is a variable, how to declare a variable
and use, and displaying a variable value using print, re-declare
a variable, and how to delete a variable, and finally
we are going to see rules to define a variable.
Let me start with very first one, that is what is a variable.
So before going to enter with your Python variable, let me
explain your known thing, that is in your mathematics suppose
if you write x=2,
and then some y=3 x + 2, what you are doing?
Any number 3, or 4, or 5, whatever it may be
so if x=2, what is y value?
Then you are saying, y=3 x 2=> 6=>6+4=>10, right
Now here x is a variable, y is a variable, so you are assigning
2 to x, and finally you are getting y value as 10, or simply
10 assigned to Y.
Now you can say finally after completion of this equation,
you can say simply x value is 2, and y value is 10.
Right. Now here
we are saying that x and y are variables. Same meaning in
your mathematics also.
Right. But here in your mathematics simply you are assigning
2 to x, and you are saying x=2 on paper, but here
whenever if you define x=2 in your Python program
or script what will happen means, your python will assign
some memory location for your x,
and in that memory location the value will be stored, and
whenever if you want to access this value always you have to use x
That's it.
[no audio]
So let me go with simple meaning for your variable.
So a variable is nothing but a reserved memory location to
store values or value.
So whenever if you want to do some action, right, on your variable,
yes, you can do it by taking that variable value. Right. How to
take and how to use we will see. Then how to declare and use a variable?
[no audio]
No need to declare a variable before using them.
That means, suppose guys
I want to store x value as 2 in my Python program, or I want
to store y value equals some 4.5. Now if you observe here
x is an integer and y is a float point.
So before going to assign 2 to x or 4.5 to y, you don't
need to tell to your Python,
I'm going to take x as an integer, 'int x', or y as integer, 'float y'.
You don't need to tell this information to your Python. Python
will automatically identify which type of data you are storing
into your 'x',
and that's why Python is called dynamic type language.
Okay. Now let me show you this on your command line, or maybe through
program, through script, or using your Python command line, right?
So what you have to do is just open some editor, guys
you can use either command line editors or if you have some GUI editors
you can also use that, right. There write
some line as 'a' equals to some 10, right. And if you want
to print your 'a' value, you have to write 'print (a)', so that
the value of 'a' will be printed, and already we have seen, right
if you want to print a string using 'print' statement you are
writing 'print', parentheses, inside of quotation some string,
but whenever if you are printing a variable value don't use
quotations. If you use quotations it will print as it
is. I mean, let me do one thing.
I am going to open my command line.
[no audio]
Okay.
[no audio]
Now I'm entering into my Python terminal.
So guys whatever the line I am typing here the same lines
you can write in your program as well.
Let's say I am writing 'x' equals to some 4 number.
Now, this is very first line in your program. Next line
I am writing 'print(x)'. Now you can see the result.
But if I write 'print("x")', it will print as it is,
I mean 'x' will be printed as it is.
So if 'x' is a variable you have to print without quotations
for your variable. If it is not a variable and if you want
to feel that as a string itself, right, you can print it in this way
also. If you print in this way, you will get only 'x' not the 'x' value.
[no audio]
First of all, if you print in this way there is no relationship
between your 'x' and this "x", because this is a
string and this is our variable.
Right. See if I print simply "Hello',
[no audio]
it is printing your string as it is because you are providing
your message, or string, or text inside of quotations
that's why your 'print' will print as it is.
[no audio]
Fine. Now, let me take 'y' equal to some number 5.6. Now I am
going to print 'y' value.
Yes, you are getting. And see that
I want to know the type of your 'x' and 'y'. 'type(x)', integer
'print', same lines
we can write all lines in a program, and you can execute guys.
'y', float.
[no audio]
Right. Let me open an editor.
So I am going to use here Sublime Text.
Of course, you can use 'vim' or 'PyCharm', right,
whatever editor you prefer, right.
So now let me use Sublime Text,
[no audio]
sorry, yeah Sublime Text.
[no audio]
So I'm going to open Sublime Text. Now using that Sublime
Text whatever the lines you executed on your Python terminal,
Python Shell, Python command line, same lines
I am going to execute.
Right. Let me copy line-by-line, and let me paste it here somewhere.
So I'm going to save this as,
[no audio]
let me save first my program.
Okay. Let me go to Desktop, because there we have some directory
called 'PythonScripts'.
So I am going to write here
'working_with_variables.py', any program name you have
to take, right?
So 'x=4', is the first line you've done on your terminal,
then 'print(x)'.
[no audio]
That's it. Now save it and run it. To run your program from
your script from Sublime Text
you have to run 'Ctrl + B'.
You can see the output. So you know the advantage by
using GUI editors is you can see your program here itself
and also output, right. Then
let me take one more thing called 'print("x")'.
So guys, you have to know the difference between command
line and your Python program.
So command line what you are doing?
You are executing line by line by typing your required line.
Program, you're going to execute all lines one by one
[no audio]
with your Python. Let's say 'y=5.6'.
I am going to declare 'y' value,
right? Don't provide any spaces before starting your line.
If you provide, it will give an indentation. Now
let me print your 'y', then let me print your 'type(x)', and
let me print your 'type(y)' as well, right.
Now I save and I am going to run. Guys whenever if you run
your Python program it will run from top to down.
It will take first line, 'x=4'.
There is no print statement.
You won't get any output. Simply 4 value is assigned to
'x'. Now after first line in your remaining program 'x' value
is 4. Now because of second line, it will print the 'x'. 'x'
value not 'x'. 'x' value means 4. Third line
it will print 'x' as it is because you are writing 'x' as a string.
Even fourth line "hello" will be displayed.
Because of Fifth Line 'y' value is 5.6. Now 'print(y)', 'y' value will
be printed. 'type(x)', what type of data you're storing into
'x', and what type of data you're storing into 'y' that type
of data will be displayed because of last two 'print' statements.
And if you observe the output, final output, you're getting
'integer' and 'float', last two 'print' statements. Observe that because
of this Python is called dynamic type language.
Right. So guys if you want to define any variable you have to write
simply 'x=4', or 'y' equals some number, right. Fine.
Now next thing is, re-declare a variable. See that very first
line 'x=4', right. Now
what I am doing is in ninth line
I am writing 'x' equals some 23, and if I 'print' in tenth Line
'x' value, you modified your 'x' value from for 4 to 23.
See the output, last 'print' statement, 23 you are getting. So even
while re-declaring your variable you don't need to tell to your Python
I'm going to change my 'x' value in so and so line. Whenever if
you want to feel to change your variable value, you are allowed.
Directly you can change your 'x' value,
right, in this way. Suppose very first line I define 10, then
after some lines I'm modifying 'x' value,
yes, you are allowed, right. Then, delete a variable.
See first of all why we need to delete a variable value or
variable? Just assume that guys, you're having some 1000s
of lines of program, right,
so assume these are the 500 lines, and these are
the 500 lines. Somewhere here
you define some variable value called 'x equals some 10, and
you know that after 500 lines,
I mean from 501 to 1000 lines,
so here you are, no where you're using your 'x' variable value,
right? But whenever if you run your Python program, whatever
the variables you are defining, and whatever the operations
you are performing, all those things will be performed from RAM memory.
Right. So if you are not going to use here your 'x' variable value
why should we keep that in my program?
Why should we keep that in RAM memory? Unnecessarily I am going
to waste my RAM memory, right?
You can delete, you can free up your RAM memory by deleting
your variable value if you are not using that, right. Simply
'del x'. See that after tenth line nowhere I am using variable
called 'x'. Then in eleventh line what I will do? Simply I will
delete 'x'. In case after deleting
if you try to print your 'x' value, there is no more 'x', you
will get an error. "name 'x' is not defined", because by this line
your Python is removing from its history your 'x'. Now there
is no 'x' in your python,
right, that's why you are getting an error.
So if you are not using then only you have to delete it, right,
so simply remember how to delete a variable value. Simply 'del x'.
[no audio]
Fine. Now next thing is, rules to define variable names.
So as of now guys, we have taken variable names as like 'x'
and 'y', but in real time you may take, suppose you are going
to find Java version then instead of 'x' and 'y',
I will take a variable called 'java_version=', something.
I will write some logic to find my Java version.
Right. So likewise, you can take some variable names. No need to
take like a 'x' and 'y'. Suppose I am going to install some Java,
right? So 'install_pkg', I will take simply "java",
so this is my variable name.
'java_version' is my variable name.
[no audio]
Right. So now while selecting your variable names you have to follow
some rules, right.
Let me go with that rules one by one.
It contains letters, numbers, and underscore.
So whatever the variable name you are selecting,
it may be simply 'xyz' or 'pq', right. But also you can take
some lengthy variable names. While taking lengthy variable
names, your variable names should include only this group
of, not include, it may contain either letters,
or numbers, and underscore.
Don't take anything
other than from these three groups.
Suppose '$' is not there in our three, either in this, or
in this, or in this, so don't take '$' in your variable
name. Suppose
I want to define 'xy', 'xy' equals some number 10. It's fine.
But 'x$' or '$x',
don't take in that way.
'$x' or 'x$',
they are invalid variable names.
Don't take them.
Right. Then second one,
it should not be a keyword.
See actually, if we take simply 'if', right, 'if' is a valid variable
name only based on your three groups because it is containing only letters.
Right. But the thing is 'if' is already predefined word for
your Python to do some particular action,
that's why don't take predefined words as your variable names
like 'print'. You know, what is the purpose of 'print'. 'print' is
used to print something. And don't take 'print' as suppose some
variable name.
Let me take 'print' equals some number 40. Don't take 'print'
as your variable name because 'print' is already predefined
word for your python. Right.
So guys, you know need to mug up about your predefined words
or keywords, while practicing you will come to know, right.
So after completion of your complete Python, you will come
to know which are the keywords.
[no audio]
Right. Then, can't contain spaces.
You should not include a space in your variable name.
Let's say, let me take command line.
Anything is fine, no.
I am taking 'my name' equal to suppose "narendra".
So 'my name', my variable name contains a space.
So it is invalid.
[no audio]
Right. So don't take any spaces in your variable names. Then,
it should not start with a number.
Yeah, one more thing guys, don't take,
don't take space in your variable name. Whenever if you want
to feel to take a space, instead of space just club those
two in this way, 'my_name'. Now it is like two words, but we
are clubbing these two words with underscore.
Right. So underscore is allowed in our variable name, right. From
here underscore is allowed,
so that's why I can take simply 'my_name',
or 'my_value', 'my_number', anything you can. No problem.
Okay. So whenever if you want to take a space inside of your
variable just replace that space with underscore. Then fine.
Next one is, it should not start with a number. Suppose guys
You can write 'x1' equal to some number, and 'x2' equal to some,
one more number, right?
And if you print simply 'x1', and if you print simply 'x2', it's
working perfectly.
But if we take '1x', that is invalid variable name.
So don't start your variable name with number.
Right. And it is case sensitive.
Case sensitive means small 'x=4' and 'X' is
some different variable.
Let me print both 'x' and 'X'. Guys they are
different. Case sensitive means 'x' and 'X', they are different.
You can use 'x' as one variable and 'X' as one more variable.
Okay, and the last one is guys,
[no audio]
You should not start with the number, right but in your valid,
for your valid variable names you have these three groups -
letters, numbers, and underscore.
You can also start your variable with underscore.
Let's say '_x' equals some number.
It is valid but don't start with a number.
[no audio]
Okay. So guys,
these are the important points to remember while defining
variable name, or while selecting a variable name whenever
if you are writing your Python scripts.
[no audio]
Right. Okay guys,
thank you for watching this video.
[no audio]