[no audio]
Friends, in this video we are going to discuss about data types of
your Python or variable data types in your Python.
See before going to start about this concept, you know already
variable, right?
Simply you are taking some 'x = 2', any number, and you
are saying that this is a variable. I can also take some other
variable like 'y =' some 4.8 right, or let's say some
'z=' some '3 +' some '4j', but instead of 'j'
I have taken 'i'. I mean we have three variables,
right, and in first variable I have, any way in all these
three variables you have number only, but strictly speaking 'x' is an integer,
'y' is some float 'z' is complex. That means based on
which type of data you are storing into your variable, based on that
we have data types or variable data types. And one more thing
guys, you know whenever if you assign something like any
variable equal to any value, right,
so Python is going to take some RAM memory and in that you're
storing a value called '4', and whatever the address is there
for this memory that is represented with 'x'.
Now, if you want to take, the value of 'x' directly simply you are taking
'x', but if you want to find what is the address of your
'x', so for that there is a small syntax.
Let me open that.
Let me show you that.
Let me enter into my Python.
You can use this from anywhere,
I mean from both Python Script or Program or Panel of your
Python. 'id(x)', will give the memory location of your variable,
okay. Guys, this point is very, very important for your online
exams. Just make a note. 'id(x)' will give your
variable memory location.
But if you want to get variable value directly you have to take
variable itself, right?
See, sorry. 'x = 5', I can directly take 'print(x)',
I can get value of 'x'. But if I want to get the 'id', memory
location of your 'x' I have to take in this way.
So otherwise I can store 'id(x)', or address of
'x' '=id(x)'.
I mean I am defining one new variable called 'id_x', and into
that I assign 'id(x)'. I can do it in this way also.
Okay, that's fine.
Now let's come back to your discussion, data types, right?
First thing guys, every value in Python has a data type.
It may be integer, or float, or complex, or some string, or some
Boolean, right?
We have different data types in your Python, right. And guys,
whatever the point we have here, right that may or may not
be understood by you as of now because if you are familiar
with some OOPS concepts of any other programming language
then you can easily understand the second point.
If you don't know just forget about second point, we will
discuss it while working with OOPS in your Python.
So that is, "Since everything is an object in Python,
so whatever you are creating, whatever you are writing no,
that is like an object in your Python Programming. So data
types are actually classes and variables, right.
Your data types are actually have classes and variables,
and they are like instances of the classes.
[no audio]
That's fine.
So based on which type of data you are storing into your
variable, based on that
we have different types of data types, right. Then we have
various, but I'm going with as of now basic data types.
They are like numbers. In numbers
you have integer data, float, and complex; strings; Boolean,
okay. Let me open a script and there I will write.
So I opened my Sublime Text.
Let me give a name for my Python Script, right. Just click
on 'Ctrl + S', just press 'Ctrl + S', so we are working with
our, let me take 'Desktop', there
we have one folder called 'PythonScripts'.
Let me create here,
[no audio]
'working_with_data_types.py', right. Fine.
So guys, first thing I am writing 'x=' some number 3,
okay, 'y=' some number 4.6, and 'z=' some number
'3+4i', right?
[no audio]
Then let me print first your variable 'x', and then let me print
your variable 'y', and let me print your variable 'z', right. Save
it and run it. So to run just to press 'Ctrl + B'.
So you're getting your all datas, right.
So guys, if you observe here I defined my variables,
three variables in three lines.
Of course, I can also do define in this way. In a single line
if you want to define multiple variables you can also go in this way.
This is also valid one.
Just observe the output, you're getting same output, right?
Fine. Now, and even if you want to display your all variables
with one single 'print' statement simply you can write
'x, y, z'.
This is also valid one,
okay. Now, let me run this and see the output.
[no audio]
You are getting same output,
right, but in a single line you are getting. So how to print in multiple lines,
we will see it.
Right. Okay, fine.
So just for clarity, just I am maintaining 'x=', and
'y=' and 'z=', and then I am printing separately, 'print(x)',
[no audio]
then 'print(y)',
[no audio]
'print(z)'.
[no audio]
Fine. At the same time now
what I am doing is I am also printing 'x, type(x)'. So
I can print with the 'type(x)', right and see the output of
your 'x' value, 'x' type. Same way 'y, type(y)', and then
already we know this what is the type, right?
[no audio]
Observe the output you're getting - integer, float, and complex.
So anyway, first of all these three variables are number data
types, specifically 'x' is integer, 'y' is float, and 'z' is complex.
So this is one data type, suppose.
Right. Now, let me comment this. You have second data type as strings,
string. String is nothing but group of characters, maybe single
character also. Let's say I am taking a variable name,
that is 'my_name' or
[no audio]
'lan_name',
right. You can take any variable.
Simply you can take 'l=' also. Just suitable variable
name, I am selecting 'lan_name="python scripting"'.
So guys, if you observe whatever you are writing on right
side of your equal symbol, that is a string. If it is a string
you have to mention that string inside of quotations,
same as your 'print'. Through 'print' you are printing some string
no? If it is a string you have to write inside of your quotations,
and that quotations maybe single quotation or double quotation.
Let me first work with single quotation
and see the output,
or I can also define like, you know single quotations as well.
[no audio]
That's it.
[no audio]
Right. And let me do one thing.
[no audio]
I am taking 'my_name=
narendra'. So 'print(my_name)'.
[no audio]
Right. So if you observe 12th line you are defining a string. If
it is a string actually you have to write inside of quotations.
It may be single or double or even maybe triple quotes also.
But as of now I am going to work with single and double.
For time being just remember only these two way of defining
your strings, we'll discuss the triple quotes somewhere else.
Fine. Now if you observe 'my_name=', string I am
writing but this string I'm not writing in quotation.
And if you run this, see the output what you are getting. You
are getting an error called "name 'narendra' is not defined".
In case if it is already a variable
then this value will be assigned to this
like, let me explain with example. Suppose 'x=2'
I have, and 'y=x' if I write. 'x' is already defined. 'x'
value is there too. Now that value you are assigning to 'y',
and in this case, this is correct.
But in our program nowhere we are defining variable called
'narendra', that's why it is giving, "name
'narendra' is not defined", but actually our intention is
'narendra' is not a variable, right?
'narendra' is a data.
That's why you have to write inside of your quotations.
Now, it will work perfectly. That's it.
Right. And if you want to know the type of your variable
always you can 'print(type())', your variable name so that you
will get your variable type, which type of data you stored
into your variable, that is a string.
[no audio]
Right. Then,
the next one is you have Boolean data types.
So guys in your strings you know, let me take simply
'my_name=' some "x" also. Now here
I am redeclaring my variable name as 'x'. Now
see the output, final output. "x", you are getting. I mean,
you can also take single character as your string, no problem.
See the output. You're getting class as 'str'. "x" is also a string.
[no audio]
Okay, this is fine. Now, let me comment multiple lines.
I'm commenting. So you know, what is the purpose of comment,
I mean triple quotes? To comment multiple lines, right? Now
if I run this program, no output because all lines are commented.
Fine. Now observe that
[no audio]
I am going to take something called 'my_value=True'.
Right. So guys, you know 'True' is not a variable here.
And suppose if you feel 'True' is a string you should
write in quotations, and in case if 'True' is not present
then what will happen?
You will get an error. If 'True' is not a variable you will
get an error but except 'True' and 'False'.
Let me take 'my_new_value
=False'.
Let me run this and see the output.
You're not getting any output, at the same time
you're not getting any error as well.
But if I take 'my_one_more_value
=true', and if I run that, see the output
what you are getting. 'true' is not defined. Small letter
't', 'true' is not defined. Same way 'T'
also not defined, right but you are not getting error here,
but you are getting error while defining in this way.
That means actually only 'True', and only 'False',
these two are for your Boolean, right.
Let me print your 'my_name', sorry 'my_value', and then 'type()'
your 'my_value'.
[no audio]
See the output. You are getting like a Boolean. Not only for
that, 'print(my_new_value, type(my_new_value))'. See the
output. Both are Boolean.
So actually whenever if you write in this way, you will feel
that 'True' is a string and 'False' is also a string,
but if you don't write quotations only for these two words
guys, not for any other words, only for 'True' and
only for 'False',
if you don't write quotations, then they will act as like
a Boolean data type.
By mistake if you write quotations for 'True', if you write
anything inside of a quotation that is always a string
even though if it is 'True', because you have written inside of
quotations now that data will be considered as a string.
Observe the output. You're getting as a string.
[no audio]
Right. So you have to remember this point. Except this 'True' and
'False', remaining all strings
you have to write quotations, then only that is a valid syntax.
Even if you want to take 'True' as a string,
yes, you can take it by mentioning inside of quotations.
Right. So guys, these are the data types, basic data types in your Python.
Right. So already we know that. The next thing is typecasting or
type conversion,
so simply converting one data type to another, is it possible?
Yes, it is possible.
Let me show you one example.
[no audio]
See as of now let me take a very simple example so that
you will be clear.
[no audio]
I am taking 'x=' some number 56, right?
And then I am printing your 'x', then its 'type(x)'.
[no audio]
Observe the output first, integer. Now
I am taking y, 'y=str(x)', then I am printing
'y, type(y)', so whatever the 'y' you have no,
that 'y' how you are creating? From 'x'
I'm creating 'y' but while assigning 'x' value to 'y' I am
converting the number into string. Now see the output. Yes,
now your '56' is a string. That means 'y' value is a string
but 'x' value is integer.
[no audio]
Right. Same way
let me take 'x=', some 'z=', boolean,
'bool(x)', and let me print your 'z' as well as 'type(z)'.
[no audio]
See the output. 'True' you're getting. 'bool(x)'. Some value
is there, that value if you convert into 'z',
I mean Boolean, that will be 'True'.
Let me take 'p=0', right and first let me print
your 'p', then 'type(p)'.
So I want to print 'p' as well as 'type(p)'.
So observe the last 'print' statement, that is a integer. Now,
what I am doing is, let me take some 'q= bool(p)'.
Now let me print in the next line 'q, type(q)'.
It is a 'False'.
[no audio]
Right.
Okay, how you are getting 'True' or 'False', I will explain to you
but before that you are converting one data type to another
data type, string, integer to string, right or string to Boolean
also you can convert.
Okay, now let me give the helping words to convert one data type
to other data type.
[no audio]
So guys in your data types, in your Numbers data type you have -
int, float, and then complex;
Strings - str; and then boolean - bool.
So these are the helping words to convert from one data type
to another data type, right?
Let's say, now I can easily explain from terminal guys.
Let me write 'x=4.6'. Now
I am printing directly 'int(x)'.
That is float. Fractional
part will be removed,
right. Now I have, let's say 'my_name=' some "python".
Now you know what is the type of your 'name'.
Right. I'm printing your name as well as 'type()', your name.
String, right. Now
I am trying to convert that string into integer.
So, how can I convert? Simply 'int()', your variable.
It is giving an "invalid
literal for int() with base 10", because, see guys number
4 if you write inside of quotations that will convert
as a string,
but if you remove quotations for "python", because this is the
alphabets, right, alphabets
you cannot convert into number but number you can convert into strings.
So this is a very, very important point.
[no audio]
First thing, any data type can be converted into Boolean, first
thing. This is always true. With the help of simply
'bool(any_data_type)'
is maybe 'True' or 'False'.
So when it will become 'True' or when it will become 'False'?
So 'bool(empty)' is always
[no audio]
'False',
[no audio]
okay and 'bool(non-empty)' data
[no audio]
is always 'True'.
Let me give the examples,
what is empty and what is non-empty. '0',
nothing right. '0' is nothing.
So let me show you on your terminal.
'bool(0)' is always 'False'.
Let's say I am defining a string which is empty. See immediately
I started and closed with quotations,
that is a, that is an empty string.
And I'm trying to convert that string into Boolean, 'False',
right. Guys,
there is one more data type called 'None', nothing.
So 'bool(None)' is also 'False', and while going forward
we will see something like this. As of now, don't worry. Just
observe. This is also empty.
I am not writing anything inside of your curly braces, right?
[no audio]
Sorry, your brace.
[no audio]
Okay, so what is that
we will see. Now for empty
I am giving example
[no audio]
'bool(0)',
[no audio]
'bool(None)', 'None' is also one of the data types, nothing,
'bool([])', some list this is,
empty list and 'bool()', let me write empty tupple,
and then 'bool()', empty dictionary.
So what is the dictionary, tupple list
we will see. So these all results guys will always give
output as 'False'. Remaining combinations, if something is there
and if you convert that into Boolean it will give 'True'.
So guys, these are very, very important in your real-time.
While writing our scripts we'll come to know how much importance
is there with conversion of any data into Boolean,
okay. Now one more point you have to remember,
[no audio]
any data type
[no audio]
can be converted
into a string but reverse is not always true.
[no audio]
The reason is number can be converted into string,
right and string which consists of alphabets, that string
you cannot convert into number.
Let's say 'x=', directly I'm writing a string called
"345". Now "345" is a string.
So what is the use of writing your number as a string means
sometimes, you know, you are going to take versions in
this way. Version is always a string guys.
You have to take as a string.
If you are going to work with, in your real time with any
version please be careful, try to define your
version always as a string.
And even while working, while writing your Python Script, right
to get some version of some particular software
you will get as a string only.
That's why try to write your version as a string,
right. That's fine.
Now see that. What will happen if I define any number inside
of quotations, right.
Now, do we have a number called 2.1.3.5
in our number systems? No. You have something like
this but you don't have multiple dots in your number systems.
That's why you cannot convert whatever the 'x' you are having
as of now into your, see suppose if I take
[no audio]
2.1.3.5 is not a base 10 number system.
It is something else.
That's why that you cannot convert into a number.
But if I take 'x=', some 45. This is a number but you
represented that as a string because you have written inside
of quotations, but I can convert that into integer directly
by writing 'int(x)' because 45 is there
even in your number systems also, that's why you can convert
that into integer, right?
So this is the case for this meaning.
I mean any data type can be converted into string but reverse
is not always true.
Sometimes it is true whenever if the string is a valid number
system data,
right. Okay guys, so this is the way how you can convert from
one data type to other data type and this is called type
casting or type conversion, okay.
Okay guys thank you for watching this video.
[no audio]