Hi everyone. Today we will learn about
the smallest building blocks of Python
variables. Imagine you are moving to a
new house and you need to pack your
things in boxes. In one box you will put
your shoes. In another box you will put
your books. And big items like a coffee
machine will probably need their own
box. But once the boxes are closed, you
can't really see what's inside them. So
you will write labels such as shoes,
books, and coffee machine. And that way
you know exactly what each box contains
without even opening it. Now Python
variables work in a similar way. You can
think of them as labels for locations in
memory where we can put different kinds
of data. So a chunk of memory is like a
box and we give it a name. So we don't
have to look inside to understand what
it stores. For example, we can store
integers or whole numbers like five,
minus 10 or zero. We can also store
floating point numbers or floats or
numbers that have a dot inside them. For
example,.5 99.9 and even whole numbers
like
1.0. We also have strings or text or
more accurately anything between a set
of quotes. For example, apples, 500
bananas, and even the entire book of
Genesis. Then lastly, we have boolean,
which can either be true or false. There
are no other options. It's just one or
the other. And this is what we call data
types. They're just different categories
of information. And we will see shortly
why they're so important. Now variables
in terms of code are pairs of name and
value. The name comes first and it
equals the value. So for example,
rockets equals 5, burger equals yes and
hungry equals true. But we can't just
use any kind of name. There are certain
words and characters that we are not
allowed to use when we name variables.
So for example, if there's a keyword
that already exists in Python like
import, if for and so on, then don't try
to use it. Also, the only special
character we are allowed to use is an
underscore. So no dashes, no dots and so
on. Also, you can use numbers just not
in the first character. So, 4K is not
going to work, but D9 will. And finally,
Python is case sensitive. So, Batman is
not the same as Batman. Okay, but what
are we supposed to do with these
variables? Well, there's things that we
can do with integers that we can't
really do with strings. For example, we
all know that the square root of 25 is
five. But how are we supposed to know
the square root of banana? It just
doesn't make any sense. That's why we
have different kinds of actions
associated with different kinds of data.
And to have a deeper look, we will need
to learn a few new concepts. But at this
point, don't worry about understanding
the small detail. just focus on the big
picture instead. One category of actions
is called operators where operators are
a collection of symbols like plus and
minus designed to work with different
kinds of data and not just numbers. We
can use the plus operator to join
strings and we can use the multiply
operator to duplicate them. When it
comes to boolean, then true is
translated to one. False is translated
to zero. And we just treat these two
like regular numbers. But operators are
not the only type of action we're
dealing with. We also have methods that
are mostly unique for each data type.
For example, we have the upper method
that converts a string into uppercase.
or we have a method named bit length
that tells us just how much space an
integer takes in memory. We know that 25
takes only five bits while 1 million
takes 20 of them. But we cannot convert
a number into uppercase and we cannot
directly calculate the bit length of a
string and that's why these methods are
mostly unique for each data type.
Another type of action is what we call
functions. We can convert an integer
into a float using the float function or
we can find the length of a string using
the len function. And most of all, we
can find the data type of any variable
using the type function. And as you saw
earlier, knowing the type of data that
we're dealing with is very important
because it changes everything. Now,
we've only scratched the surface when it
comes to operators, methods, and
functions. There are millions of other
things we can do with them. And in
future videos, we will dive into it in
great detail. But for now, let's
practice everything that we've learned
today in code. So, let's navigate to
jupiter.org and let's scroll down and
let's try it in our browser. We will
then choose the Jupiter lab instance.
We'll of course get rid of these
annoying popups in this file tree that
we don't really need. No, close all
these things and let's create a new
Python pyodide
notebook. Go away. Okay. Now in this
notebook, Python is already installed
and we can use it in our workflow. So
let's say that my name equals Maria and
let's say that my height equals
1.67 m. Now with these two variables at
the very bottom we can say my name is to
which we will add the variable name. Now
let's give it a quick run with Ctrl
enter and beautiful. Our name variable
was replaced with the name value which
is of course Maria. Now let's quickly
add my height to the mix. So right after
our variable of name, we will add and I
am plus height plus meters tall. Now
let's go ahead and give it another run.
And oh no, we get a type error because
we are only allowed to add strings to
other strings where in reality we tried
adding a string to a float. So how are
we supposed to solve it? Well, how about
converting our float of height into a
string? Now one way we can do so is
wrapping it in a set of quotes. But this
is not fun. There's nothing fancy about
it. So, alternatively, let's wrap it in
an STR function that converts other data
types into strings. So, now that we
encapsulate
1.67 in this lovely function, we can
rerun this code and it will work like a
charm. And great. So now that we know
the basics of variables, we can move on
with collections of variables where
instead of a single name, we can store a
whole bunch of names or instead of
storing the name and height separately,
we can bundle them together. So, I will
see you soon in the next tutorial. And
in the meanwhile, bye-bye.