Hello guys.
So we are going to continue the discussion with respect to Python.
And in this video we are going to discuss about variables.
Okay.
Now what exactly is variables.
And uh here I have also given the entire video outline.
So we will first of all introduce like uh the topic is introduction to variables.
Then we'll be seeing how we can declare and assign variables.
Then we'll talk about the naming convention.
Then we'll understand variable type.
We'll see type checking and conversion dynamic typing.
And finally we'll be seeing a lot of practical examples and common errors okay.
So all these things will be specifically covered in this video okay.
Now first of all, as usual let's go with the definition.
So variables are fundamental elements in programming used to store data that can be referenced and manipulated
in a program in Python.
Variables are created when you assign a value to them, and they do not need explicit declaration to
reserve memory space.
The declaration happens automatically when you assign a value to a variable.
Okay.
So, uh, the best way to give you an example is that let's say if I define a is equal to 100.
So this basically becomes the variable.
And always remember guys, whenever you start a new notebook file always make sure to select the kernel
okay.
Now if I'm going to write a is equal to 100.
This basically means that I've created a variable a and I've assigned a value to 100 okay.
Now uh, obviously in my previous video I've also defined some basic variables and all and I've assigned
some values, let's say string, numerical, uh, integers, floating point, anything, any values
you can basically assign.
Uh, we'll be learning more about it.
Uh, so first of all, let's go ahead and understanding about declaring, declaring and assigning variables.
Okay.
So here, uh, you'll be able to see that, uh, let's say I'm going to define age is equal to 32.
I'm going to probably define height is equal to six feet or 6.1.
That is specifically my height.
So I'm writing it down.
Height is equal to 6.1.
Then let's say name is equal to my name.
I will go ahead and define crush.
Uh, and then here you can also see that I can also define a boolean variable.
Let's say I will go ahead and define is underscore student okay.
I will also talk about naming convention.
Uh what is the best naming convention of defining a variable.
What are the good practices.
Everything I'll be discussing about.
So let's say I'm going to define a boolean variable as true right.
We'll learn more about the specific data types in my next video.
But here I'm just giving you an example okay.
So h is equal to 32.
This this is there.
And I have actually written it right now.
Let me quickly go ahead and print it.
And this is how we assign a variable right.
We are declaring a variable and we signing some values.
Right.
So now let me just go ahead and print printing the variables.
Right.
So here you can see.
So now first of all I'm going to print age right.
And this is where I will also show you how you can use uh different print, how you can uh, display
in your console in a different way.
So first of all, let's say I want to print age.
So age colon let's say I want to assign this age colon with some value over here.
So I will go ahead and define my variable h right.
Similarly, if I go ahead and show you the next print statement here, I'm going to basically write
height okay.
And with respect to this particular height I am going to define another variable.
Let's say this will basically be my height.
Another next thing that I'm going to print over here is my name.
And here you can probably see that I'm going to basically define my name.
And let me just go ahead and write comma.
And this will basically be my name.
Right.
So if I want to define all these things you here, you can see I'm displaying age is equal to 32.
Height is equal to 6.1.
Name is equal to crush.
That basically means see, when I'm printing I can also call this particular variable that I have actually
defined.
Okay.
So these are some of the ways.
Again we will be discussing about more different boolean type more different data types as we go ahead.
And that is how we declare different different variables.
But just a basic idea for you with respect to the variable declaration.
Okay.
Now coming to the next one, which is basically called as naming convention.
And this is the most important one.
Like how should we name a variable.
Right.
Then for this you really need to know about the naming convention.
Some of the important points that I would like to mention over here, let's say uh, variables, uh,
name.
Right.
The variable name that we define right should be should be descriptive.
Okay.
Then.
Uh, the second point that I would like to mention, they must always start with a letter.
Okay.
So this is the second condition.
Or it can also start with an underscore.
Okay.
Underscore like underscore uh this kind of underscore mark.
Right.
And uh it can contain letters numbers and underscore in between.
Right.
In between.
But always understand they must always start with a letter or underscore.
And it can contain letters, numbers and underscores.
Okay.
Within inside that okay.
And uh, one more thing.
Very important point, which I've already mentioned.
Variables names are case sensitive.
Okay.
This also I have defined it.
So these are some of the naming important naming convention with respect to the variables okay.
Now let's see some kind of example over here.
So first of all I will just go ahead and write some valid variable name.
Okay.
So valid variable names.
So first valid variable name I'm going to define it as first underscore name is equal to crush okay.
Uh last underscore name is equal to Nick.
Right.
So is this a good way of writing a variable.
Yes.
Does it start with a letter.
Yes.
It starts with a letter.
Does can it contain letter number or numbers or underscores?
Yes, it can contain.
So here you can see I am having letters or underscore.
I can also put numbers in between but not in the starting right.
So this is a good way of defining a variable.
Okay.
So here uh this absolutely works fine.
Let me say show you some invalid variable name invalid variable names okay.
So let's say that I define with a number.
Right.
And if I go ahead and define over here age is equal to 30.
See I'm getting an error saying that invalid decimal literal right.
So I cannot start with a letter.
So this is one way okay.
The another way is that, uh can I define something like this first dash name.
Right.
Can I define like this.
First dash name is equal to, let's say Krish.
Can I define like this?
Will I get an error.
So here you can see cannot assign to expression here.
Maybe you mean double equal to or instead of equal to.
So this is not being considered.
This symbol is also not being used.
Right.
Uh so obviously this is also an invalid variable name.
Right.
The third invalid variable name can I use at the rate and define a name variable.
Right.
So let me just go ahead and check it out.
So here I'll go ahead and write Chris will I get an error.
It says invalid syntax.
Maybe you meant so and so right.
So this is also not a valid variable name.
It is an invalid variable name okay.
And already I have spoken about case sensitive.
Right.
Case sensitivity.
Right.
If I define name is equal to Chris.
And let's say I have defined capital name is equal to Naic.
Right.
Both this are different, right?
It is not same.
Right.
If I really want to compare, can I compare like this num name double equal to capital name.
It'll be giving false right?
Yes.
The values.
If it is same it will give me true.
But in short you can see that these are different identifiers, right?
Later on I can do the comparison operation and I can do it right.
So obviously case sensitivity is also there.
Right.
So this was related to something related to the naming conventions okay.
Now let me just go ahead and make you understand about some of the variable types okay.
So uh we will continue the writing the code here itself.
So let me just go ahead and write understanding variable types okay.
Now one thing you need to understand Python is dynamically typed.
Okay.
Dynamically typed.
What does this mean?
This means the type of a variable.
Type of a variable is determined at runtime.
Determined at runtime right.
So this is very much important okay.
And some of the important data types are like int float string and boolean.
Right.
So let's say if I write age equal to 25 right.
So when I execute it during the runtime it will understand.
Hey fine.
Integer is basically getting assigned.
So age is an integer variable right.
And let's say that I will go ahead and define some more variables.
Right.
Let's say height right.
I'm going to basically go ahead and define height.
Let's say I'm going to write 6.1.
So this this is nothing but it is integer.
This is nothing but this is float.
See how I'm also using the single line comments.
Right.
Let's say if I'm going to define my name.
So this will basically be Chris.
So this is what this is nothing but str the string.
And finally if I go ahead and define is underscore student.
This is basically my boolean variable right.
So boolean.
Uh sorry I will assign it as true.
So this basically becomes my boolean variable.
Now if I go ahead and print the type of any of this right like age.
So I'm going to basically get integer.
The class will be integer.
During the runtime.
It is able to understand it.
If I go ahead and print what is the type of name I'm actually going to get string right.
So similarly you will be able to find out different different types like what classes it basically belongs
to Okay, now one more thing.
Right here I will discuss about one very important point and understand one thing guys.
Here I'll be showing some example.
But when I go ahead with more session, more different lessons as we go ahead, right.
I'll be reusing all this things.
Don't think that I'm just going to write for a single time over here, because this all things will
again get repeated.
Okay.
With all the different, different examples.
Now let me just go ahead and make you understand about type checking and conversion okay.
Now type checking and conversion.
What does this basically mean.
Obviously type checking.
You have known right.
So if I go ahead and write type off height.
So that basically means I'm going to get the uh data type of this height variable.
Right.
So in short I'm actually getting float over here right now.
My question rises.
Can we convert one data type to the other data type?
Is it possible?
So let me just go and show you.
Let's say if I go ahead and write age is equal to 25 right now.
You know that if I go ahead and write print type of age, so what I'm actually going to get it is an
integer, right?
But now I am going to perform some type conversion.
Type conversion basically means I am going to convert this integer value into a string value.
Let's say So if I go ahead and write h underscore str, and if I go and use this str function, write
str function is what string right.
This is what is the class that I'm actually getting.
Now if I want to convert this age into string I can use this str.
And I can use this age variable over here.
Right now if I go ahead and write print h print age underscore str okay.
And after that I will just go ahead and write print type of age underscore HDR.
Right.
So if I go ahead and execute it see initially it was int later on uh when I printed this age underscore
HDR here you can probably see that I'm getting 25 okay.
But the type of this particular value is string.
Right here you can see string.
So this this value from integer it has got converted into strings okay.
Now the question rises.
Krish.
Uh, what are different types of type conversion.
Can I do okay?
Can can I do something like this.
Can I write.
Age is equal to in string like 25.
And can I convert this into int.
Let's see.
Right.
Let's see this.
So the answer is yes.
You are able to convert it right.
So if I go ahead and print uh the type of int of age, yes, definitely you are able to convert it.
Right.
And here I'm just going to close it.
Yes the class is int.
This is fine but still I will just go ahead and use one more.
Some more examples.
Let's say if I go ahead and write my name is Krish right now do I can I convert this into an integer?
The answer is no, right?
Because obviously Krish is a complete sentence, right?
If I talk about 25 in string manner, this is basically a numerical value itself, right?
So even though it is in a string, I can convert this into an int type.
But over here this is my string complete string.
I cannot just convert this into an integer type or a float type.
So here you basically get an error saying that invalid literal for int with base ten Krish.
Right?
So this is the value that you are able to get it over here right.
Another example some more example.
Let's define some float value.
Right.
So let's say my uh my height is nothing but 6.1 okay.
Or let's say 6.0 or 6.0 or 5.11 okay.
Something.
Okay.
Now if I go ahead and see the type of height, what exactly it is, It is nothing but float, right?
Can I convert this float into an integer?
The answer is yes.
So if I go ahead and write int and I do the typecasting for this particular height variable, you'll
be able to see that I'm able to get five right.
That basically means it is just going to remove the decimal.
Right.
Can I do the opposite again?
Can I again typecast this from int to float again?
Yes I can, and it is going to give me 5.0 right.
So 5.11.
Initially I converted this into an integer I got 5.0.
Then again sorry I got five.
And then again I converted that into float.
So I got 5.0.
So this is some example with respect to the conversion right type checking conversion.
So this is what I really want to show you over here okay.
Now let me do one thing.
Let me talk about one very important concept, which is called as dynamic typing.
Okay.
And this is probably the most amazing feature in Python.
Okay.
Let's say I'm going to define a variable ten okay I'm going to define a variable ten okay.
Now let me just go ahead and write print var.
Where I'm going to print this var and I'm going to also print type of where.
Okay.
Type of where.
Perfect.
So this initially is an integer.
Then obviously it should give ten.
It should display ten.
And it should say that hey it is an integer.
Now Python allows the type of a variable to change as the program executes.
Right?
So let me write down this particular comment.
Python allows the type of a variable to change.
To change as the program executes.
Right.
Amazing, right.
Now, what does this basically mean?
Now I can go ahead and change the value of var and I can write.
Hello.
Okay.
And let's say I'm going to print var comma type of var.
Okay.
This time obviously you know what it will output will be string.
If you're not satisfied with this again you can change.
So as you keep on executing the code you can keep on changing it.
Let's say now this time I defined it as a float variable.
Now if I go ahead and write print var okay var comma type of var.
So if I go ahead and execute this here, you'll be able to see that I'm able to get this right.
So see initially it was ten class int hello class str.
And finally 3.14 class is float.
So what is basically happening.
Python is allowing this type of a variable to change as the program gets executed.
Right.
So we can basically do this.
Okay.
Now uh, let's do one amazing thing.
Let's just create a simple, uh, application with whatever things we are specifically not.
Okay.
And before that, let me just talk about input.
Okay.
Now, what exactly is input?
Let's say that, uh, a user wants to give some values, right?
Let's say age.
And I've told that hey, user, you give the value.
What is the age?
Okay.
What is the age.
So I, I am a user.
I want to probably give the value over here.
So if I use this right input function and if I print h over here, see what will happen.
So once I execute this statement now see what will happen.
So here you can see uh an input box has got opened.
So here I'm just going to give my uh, age.
Let's say the age is 23 okay.
And I'm going to press enter.
So here you can see I'm printing the age now.
One very important thing to show you right.
What will be the type of this age.
Okay.
So that basically means whenever I'm giving any input from this particular input box by using this input
function, what will be the type of it by default type.
So here I'm going to go ahead and write 23.
And here you can see it is a string type.
So let's say I want to probably get this particular input.
And then also convert this into an integer or float.
So I can again typecast this entire thing.
Right.
So now if I go ahead and execute this now, let me just go ahead and write 23 and press enter.
So here you can see I'm getting a class hint.
Right.
So this is uh, one very important functionality for uh, uh, the entire, uh, input function over
here.
And uh, this can be very handy and can be used in various places.
Okay.
So, uh, I hope you are able to execute this and, uh, you are able to understand this.
So I'm just going to show you one example again.
Now let us do one thing.
Let us simply create one simple calculator okay.
Now this simple calculator will be amazing.
So here you can just see that what this calculator is doing I'm giving two numbers.
Uh again I'm using this input uh, number one.
Again.
Number two, then some.
I'm doing num plus num one plus num two difference num one minus num two.
And product is num one multiplied by num two num one divided by num two.
We'll discuss more about this operators as we go ahead.
And we are just going to display all the sum difference and all.
So let's go execute it.
Let's say the first number is 56.
I'll go ahead and press enter again I've got my another cell.
Let's say I'm going to write 43 or 10 something and press enter.
So here you can see I'm able to see all the answers like some difference product and quotient.
Right.
So yes, uh, this was uh, amazing.
This was I guess, you, you are able to understand many things about variables.
Uh, this was it for my side.
Uh, in the next video, I am going to probably discuss more about data types.
And then finally we'll also be discussing about operators.
What are the different different data types.
Also we need to discuss about.
So yes this was it for my side.
I will see you in the next video.
Thank you.