Transcripts
1. Introduction: Hello, and welcome
in this tutorial, we're going to learn
Python basics, and I'm Manjanat your
instructor for this course. Here are the
learning objectives. Please make sure you have the programming
paradigm of choice opened alongside while watching this tutorial and your
learning is guaranteed. So we shall look at and
understand how do we make use of comments and use
indentation in Python. We shall also
understand the ideas of identifiers and
keywords in Python. We shall look at for a good
measure Python types and various categories of data
types Python supports. We shall also deal with
basic types in Python. We shall spend some
time understanding variable type and the
assignment in Python.
2. Comments & Indentation: Welcome to Jupiter Notebook, the perfect tool for writing running Python code
interactively. So what I'm going
to do is I go to this new option and select
Python three IP kernel. And by default, the Jupiter
Notebook is named untitled. I shall click on
this and let's give a meaningful name for
this Jupiter notebook. I want to call it
as Python basics. Notice dot IPYNB extension, which means it's a
interactive Python notebook. I shall rename that. And we are good to go. This is how the interface
would look like, and we shall cover all of the items that I
listed under learning objectives and go in a hands on mode for learning and making sure we learn it in a
systematic fashion. As you can see here, we have cells these let you execute
code in these cells. You can see instant outputs and document your
work with markdown. You can see your options
for code, markdown and raw. Code is what you are going
to write and execute, markdown and raw is more for documentation and for
generating literature. Ideal for data science and experimentation, the
Jupiter notebook, we can test idea step by step without re running everything. Each cell runs independently, making deugin and
exploration seamless. We will dive in
and start coding. So as I mentioned, first let us
understand how do we implement comments and
indentation in Python. I shall put that comment
and indentation. You must have noticed
I have prefixed this statement with
the asterisk symbol. Comment always begin
with asterisk in Python. If I execute line of
code, needless to say, nothing happens because Python recognizes this to be a comment. This is a single line comment. So how do we go about implementing multiline
comments in Python? Let's say multi line
comments in Python. Instead of keeping it as code and prefixing
with asterisk, I could also go
ahead and select as markdown and I shall execute. This is the run command.
I shall ho over my mouse, run this cell, and advance. Shift Enter also, you can use if you are
comfortable using the keys or using the menu
driven items either way. So you can see a
multiline comments in Python would look
like a broad header. So if I prefix with one
more Asterisk symbol, and if I execute,
see what happens. So the farm size gets reduced. Nevertheless, this is how
you Start using comments. So make sure it always begin
with an asterisk symbol. So now let's export the
multiline comments. You should write it in
a pair of like this. Either you could write multi line commands using
three single codes, or you can have multiple
double codes like this. So that is how you're going to implement the multi lane
commments in Python. Let's just look at one
multi lane comment. Let's say I'm going to start
with three single codes. Okay. This is a, I'm going to write
it as a multi line. Let's say Enter. I'll say comment, and I shall
close with three codes. Okay, this is how
you're going to execute multiline comments in
Python. You can see this. This is how you're going to
execute a multiline comment. You could also give these
things in double codes. So I shall copy this to
this and replace this with three double codes
at the beginning and three double codes at
the end and say execute. It does exactly the same thing. This is a multi line comment
is implemented in Python. And we have the other piece
to learn called indentation. Let's just look at
understanding indentation. This is important to understand. So please be attentive. In Python, you cannot be casual about using indentation.
What I mean is this? Let's say I'm going to assign value 20 to variable called X. And say enter, and let's say another variable Y is assigned. Let's say 60 it.
And you know what? If I put my cursor here pointing to Y and then tire and I
execute, see what happens. So this is expected. The error is expected. What I mean by that
is, you cannot use casualty
indentation in Python. Make sure not to
use indentation. Casually, let's
get this spelling right in Python.
One other thing. This is called grammatic
or semantic error. It specifically
tells the category at sorry, indentation error. It greatly gives the
meaningful information called unexpected indent. So how do we
rectify? First thing is to recognize the error. Second thing is to
learn to rectify. That's where the learning is. All I'll do is I'll
make sure it is properly indented and I'm not going to use
indentation casually. If I execute this it
will not give an error. I can print it by saying
X comma Y and execute. You will see 20 comma
six tetas, the output. This is the expected behavior. Hope everybody is clear.
3. Identifiers: Let's continue, and
now we shall learn something called
identifiers and keywords, an important topic in Python
that you must understand. Okay, so I'll just
leave that comment. Let's understand. First up, I'm going to put them
as comments, okay? Or I'll show you how to use raw. I'm going to put a iPhone and then leave some space.
I'm going to write it. Python is a case
sensitive language. Most important to understand,
don't miss this idea. Python is a gas
sensitive language. What I mean by
identified in a Python is simply a name that is used used to identify a variable function. A class. I could
also be let's say, a module or any other
object in Python. So this is what I meant when I said identifier. I shall repeat. When I say identifier, I'm talking about a
Python identifier which is nothing but a name used to
identify a variable. Okay, let me just get the spelling mistakes
were corrected. Variable, function, class module or any
other objects in Python. That's what I mean
by identifiers. I'm going to use the option a, again, from the
dropdown and say, exude, see what happens. It gives you raw information
depicted in the notebook. So this is equivalent
of a comment, but it is good to use this
as a best practice to generate the Python
code along with the comments embedded so that it increases the not
just the readability. It brings in clarity, and subsequently it brings
in ease of maintenance. That's one other important
idea of writing Python code. It must be easy to maintain. Turns out for writing identifiers,
there are some rules. So let's call it out. Rules for creating identifiers in Python. This is, again, another
important dimense. Let's say I'm going
to select it as martyr and leave it
there as a header. You must always start with
a letter of an alphabet. Okay? So start always with
a letter of an alphabet. Okay? Start with any
letter in the alphabet. There is an exception.
You can start with an underscore. Okay. The underscore symbol. So for creating the
identifiers in Python, the identifier, if it's
a variable, for example, must always start with a letter of an alphabet
or an underscore. Then subsequently, you could
have mix of multiple things. You could follow it
by more letters, for example, more
letters or even zero. Even zero, like I
already mentioned, you could also use
the underscore and you could also
make use of digits. Let us understand
this with an exam, and see what happens
if I don't start with a letter or an underscore. Let's say, I'm going to
start with eight were one. If I exude this, you will see that it gives syntax or
invalid decimal literal. But if I simply put
one, what happens? Let's check it out. There
is nothing happening. It's not defined, it says, but it is not a problem
with the variable. Naming or the identifier itself. So if I execute this,
everything is fine. Okay? So let's go back. And let's say instead of starting with a letter
of an alphabet, I would use just
an iPhone and say, VAR one, see what happens. So it gives you error. It clearly says name error. Name one is not defined. Okay? So you cannot use a digit or you cannot use a
special character like IP. Can I use a star? Let's
see what happens? No, CT's star expression
here is the message. I mentioned, if you notice here, underscore is the only exception of using some special character. So if I use underscore
are one and say is equal to
103 and exit this, turns out it works fine. What does it tell you?
For creating identifiers, you have to follow some rules. Start always preferably
with a letter of an alphabet or an underscore.
Here is an example. You can follow it by more zeros, I could give like this, it'll work or I could give
variable one, some XY z. This will also work. I could
remove the underscore. This will also work.
But you know what? When you are having words as
part of the variable name. Let's say this is variable
one, underscore salary. If a variable is bolding some salary and
you want to name, it has one underscore R. Make sure to always separate the words
with an underscore. This will work well
and it brings in a lot of readability. Okay? Supposing you're going
to use keywords, for example, what
are the keywords? We'll talk more.
Think of this way. Int, if I write, see
the color changes. If I write care, what happens? Nothing happen. So what I'm telling is
something like this. Let's say I'm going to say math. Anything happens,
nothing happens. See, for example,
Abritton Int, okay? Check something else.
Se falls, turned green. Okay? If I say from,
it turns green. What I'm saying is, these are
all the keywords in Python. Okay? These are all
the keywords in Vital. So another bit of
information is all keywords, if you notice, there
are many more. I've written few. All keywords, if you notice, like
in falls from. You could write, for example, if there could be many, we can talk about
it, not, et cetera. They are all in lower case. So another thing to
understand is that from this, let's draft a comment
here for reference. All the keywords. In Python or lower case. Hope this is clear.
These things, if you follow, you will have the basic understanding
of Python really well. Yeah, I'm going to save
this and we shall continue.
4. Keywords: Now let's continue to
understand more about keywords. As you can notice here,
It falls from if not, et cetera, keywords in Python. Like I already mentioned, all the keywords
are in lowercase. Some of you may be
curious, right? You want to know a list of
keywords available in Python. Is there a way to see the list? Yes, let us print the
list of keywords, print the list of keywords. This is interesting, okay? So how do we do that? We learn a few other things by looking
at this implementation. There is a module called keyword that is loaded
into runtime already, but you need to
import that module called keyword itself, okay? You have to use the
keyword called Import. Notice here, Import is written in green or
depicted in green rather, meaning it's a keyword as well. So one other word of caution when you see keywords like this, int falls from if not, please avoid using these
keywords for variable names. I don't want you to
experiment this, but I want you to
be aware of this. If I say It is equal to ten, unfortunately,
unfortunately in Python, it doesn't throw up
any error saying you're using keyword
for a variable name, but it messes up the default behavior of
this variable called It. It messes up big time. So don't even experiment by using the keywords
for variable names. Don't make that
cardinal mistake. A word of caution there
from a practitioner. Okay? So Ipot is a
keyword. Leave a space. You can type keyword, and this is the module. In Python, which
will help you to list the set of keywords. Keyword is the module. What we're doing with
this executable line of executable line of code called import keyword
is to be able to import the functionality of this module at
runtime in Python. I shall execute. Yeah,
there are no errors. Then I'll use this keyword, copy this and put it dot. There is something
called KW list, which gives me keyword list. So I shall print them. So I shall put it in parenthesis and use the function
called print and execute. So you can see here, these are all the keywords
available in Python. These are all the list
of keywords like false, none, true, and as ast. Is Avid, Bray, class. There are many Okay, can count it and see how
many it's after all, a list now, so you can see
the length and figure out. So I want you to tell me without counting the number of
keywords using functionality, built in functionality,
I want you to tell me what is the total
number of keywords in Python. Okay? This is how you
can list the keywords. We will spend more
time on the built in libraries and the built in modules in Python
a little later. I think this would
suffice for now how to understand to print the list of keywords because
this is important. So one more time, a word
of caution if you like. Make sure none of these
keywords listed here, you're going to make use
for variable names, okay? Otherwise, it'll mess up the depot behavior of those keywords in the
Python environment. Yeah.
5. Python Types: Now, let us focus on learning
different Python types. Another interesting
topic to learn. Maybe I'll make this into a Marton and create
it as a header. Python basically supports
three categories, okay? Supports. Very important to understand this supports
three categories. Okay. Three categories
of data types. What are those?
Let's understand. First is called basic types, and the second one is
called container types. Last but not the least, the third is called
usual five types. We shall ex prime on each one of them and also see a bit
of a code in action. Basic types, you can
see here some of these like Id, okay? Like Id. Float. We'll just
talk more on these things. For example, even complex. Quite often we don't
use this datatype, but it's interesting string. You could think of bool, right? You could also understand something called bytes,
which we'll talk later. So these are the basic types. One of the categories of
datatypes as basic types, Itch in float complex string
bool bytes fall into. Okay? Then the other category
of data type is called contener You will have the popular data
structure call list. You will have tuple you will have set and you will
have dictionary. Then they use the
different types is simply put it
is user defined. You could think of them as
let's say classes in Python. So broadly, just to summarize, Python supports three broad
categories of data types. First one is called basic types into which data types like int, float, complex, string, bull, bytes, et cetera fall into. Second important category
is column container type, and very, very important
category at that, which includes list, sample, set dictionary, and
the third category is user defined type. Typically I classes to this. Okay. So we're
going to talk a lot about the basic types
in greater details, and we should also spend some time to understand
the container types. And, of course, we'll
also look at a little bit later in this tutorial, the user defined types as well. So first, let us now explore
the basic types as one of the data types the Python
suppose. We'll do that now.
6. Basic Types: Let us now explore
basic types as one of the categories that Python
supports using some examples. First thing is to recognize
these basic types can be expressed as binary
can be a decimal, and therefore can also be exa decimal and can even be octal
representation of the data. Okay? For example, if you have binary, it typically starts with
something like this. So this is for binary. If you have, let's say, odcimal, then you will
have something like this. Okay. This is exodimal
representation. Then you will have
octave which will be something like this. This is octal representation. Okay, that means
binary starts with, let's say zero, small letter
B upon zero captleter B, and octal could be with zero with small
leterO upon zero with captleterO and Xa could be with zero small letter X
upon zero captleter X. Okay? You could also
have, for example, here something like this. Let me just write a few more examples
and see if it works. Let's say, we'll
take it as zero, zero, one, one, one would
be another example. Okay. Then you could also
have integers, which is simply, let's
say, one, seven, eight. Okay. You could have various
other representations. I think I'm going
to leave it at this that will help you to understand
a few things clearly. Okay. All of them, the examples here is integer representations. When it comes to float, let's look at float as
one of the basic types. Float is typically expressed as a fraction or even
in exponential form. You could express as a fraction. Or even in exponential form. So if you look at some example,
then you will understand. Let's say X is equal to, I'm going to say, one, two, 3.456 and turns out if
I print X, it'll work. And if I check for
the type of X, use the keyword
and say type of X. And you will see this is a
float representation. Okay? So this is a positive floating
point representation. So I could also take, let's say, for example, let's call
the variable name X one, and I could prefix
with minus sine, and it shall work with the negative floating
point representation. It turns out this is also float. The value std is minus one, two, three, 0.456 in the
variable called X one. So this is floating
point representation that we typically represent. I also mentioned you
can represent this as a fraction or exponential form. Let us say pile, you know
how to represent, right? It is 3.14 16, et cetera. And I can represent
this as expensalFm. Let's call this as X three and see if it works in this exponentiation
form. Yes, it does. Let us check this type of X
three. So this is a float. So this makes it
amply clear you could have float represented in
exponential form as well. Okay. And it turns out, I could also represent this exponential representation or
exponential representation. Even if I write here E, it should still work. Let me just show you by
executing this goal. So here, if I print this X four, you can see it is 313.16, and this is the
exponentiation form. Okay? So we have talked about this basic types can be
represented in various ways. It could be binary when
it has, for example, different ways of representation
for a data type or a basic type like decimal
or decimal octal like this. Float would be expressed as a fraction or even
in exponential form. So you see here, it is represented in decimal representation
because it's float. Here it is represented
using exponential form. Notice here, I've
used the letter smallly for exponential form. I've used the letter capitally
for exponential form. Then we set another basic
type would be complex. Let us explore
complex data type. So one more type already Itt, we quite often don't use in our data science or
analytics endeavors. She could have this
complex pipe like this. Typically, this is how the complex
representation would be. I'm going to store
the two value in a variable called
Xpi and let's say four plus ten J. Okay? And I'm going to print xi, and I shall also print
rather the type. So it turns out it is complex. So it's by convention. It is small letter J is used. Anything other than this, if we use, you will get the
error. I'll just show you. Even if I use J, let's explore. Okay? What happens? It works. Anything other than J, let's say I'm going to use
K, you will get error. I'll use the small letter
K. You'll get the error. So please remember
only capital or small letter J is allowed
for complex numbers. I'll leave it as part
of the recording. This is not because error. It's not because it
is not remediated. It is by design, and
this is expected. To demonstrate, you
will not be able to use anything
other than letter J, either smaller capture letter for representing
complex data type. Hope this is clear. So what this has is both
real and imaginary part. Four is real and ten J
is the imaginary part. So that's what a
complex number is. So more write it here. Complex will have we'll have real and imagine
the real part. You need not have to
worry too much about this because it can
be rest assured. As a practitioner, I'm saying, very rarely we use
complex numbers in typical data science or business analytics or
data mining endeavors. Okay? Let's go up and see what
else we have on our plate. Yes, we have string, we
have bool, we have bytes. Let's explore for
the basic types. Let's go one after the other. So let's explore string. So we shall explore
in detail later, but we'll go to the extent
required for this tutorial. Okay. String is collection, basically. It is an immutable collection. Okay? What is a string? String is animutable collection. What's all it could contain? It could contain uni code. Okay, characters, which
are typically enclosed within single code like
this or double code, or you could also have
triple codes like this. Any way you represent, it's all recognized
as a string in Python environment. So
please understand this. String is an
immutable collection. Basically contains
unicode characters which are enclosed within
single codes, double codes or triple codes. All these are presentation of stringed data type in Python. I'm going to put it as a
markdown and execute this. Okay? So you can see some examples. I can put in a single code. This is Python, basic tutorial. Okay? Let's say, I'm
going to put it as STR one is equal to this, and I'm going to
print this STR one, and I'll also check for this type so that
you will see both. Okay, I can see her
data type is string, and it prints exactly what
is given within single code. This is Python basic tutorial. Turns out I've just represented the string
using single codes. I could do the same thing using what I call tea
within double codes. Let's say I will replace the single code
with a double code. Probably, we can overwrite
the same variable, but just to make sure
you are quite clear, I'm going to change
the variable name to another variable name
called string two Walla, exactly same output, but
the data type is string. You would recognize by this, it can be done using
double codes too. One last variation to this, let's look at this using triple codes and
see if it works. As in the previous example, let's make this into three
and see what happens. Any guesses, what happens? We can pause the video
and execute this, then see in the
video, what happens? You know what? I'll
not keep you waiting. Sh shall run this. Yes. So this amply
demonstrates string could be represented within single codes using double codes,
using triple codes. So if I wonder, when do we use double codes
versus single code? If I have to use
apostrophe representation. Okay? This is a basic tutorial. Let's take this. For
example, let's take this. This is, let's say, I'm going to put it
deliberately unja unjnas Python basic tutorial. Notice here, within
two single quotes, we also have a single quote
as part of the string. If I execute this,
it will get hell. That's because
what in its wisdom Python thing says up
to this is the string. After that, it doesn't
recognize what it is. It simply says unterminated
string literal. So whenever you want to use some representation of a string, which has single apostrophe inside of the string
representation, make sure not to go with
single quote representation. You shall go with double quote
representation like this, and everything
will be hunky try. Hope that's clear to everybody. Yeah.
7. Boolean Data Type: Now let's explore. Bool. Okay. Let's see
how to represent. What is the variable count
here? Okay, X six we can take. We can take any variable name and write that's not an issue. Xix. Let's say I'm bound to
say true. Will this work? Let's say this. No, this
is the expected behavior. Name true, not defined. So it has to be
represented as a keyword. The moment I spell it correctly, as in the casing, T become captive letter, TRUE for the Boolean, it works. So we can check this by
checking for the tip. So similarly, you could check for Boolean is typically
what? True or false, right? So you could check this and probably I'll change
the variable name. So you can print this
and there you go. This is a basic data type called Boolean is being used for
assignment and printing. Notice here, if I use the keyword type and then reference the object
or the variable, it shall print category
of the data type. In this case, it's a basic
data type and of type Bool. Notice here, everything is implemented as class
inside of Python. So if you want to
check this out, if I use only type X seven, it simply says Bool. If I prefix it with print, and check this out, and
let's see what happens. As I alluded to,
even though it's a Boolean data type for the basic type as one
of the categories, instead of Python, it's
implemented as class, everything int, care,
whatever, right? It's all implemented as
class instead of Python. So you could use,
for example, here, the type is here, so we could take and then print this. Okay? If I print this or execute
this, you'll see string. But if I prefix it
with print command, you'll see that also as
a class called string. Hope that is clear to everybody. This is an important idea that you must
remember in Python. Every data type is implemented as class
instead of Python. Okay, so I hope this is clear. Any questions? Feel free, if you don't ask me.
I'm happy to help you. And let's learn
together in this. Yeah.
8. Integer & Float Ranges: Let's continue and we can discuss something with
respect to numbers, particularly that's called
integer and float ranges. And just explain
what it is in a bit integer and float ranges. Okay. So it's an important idea that I want you to understand. Clearly, please follow along. Let's just say,
I'm going to look at assigning variable
X with value 100. Okay. I want to look
at the same variable. I'll write it in the next cell. Same variable, 100, 200, 300. Okay? Or let me just assign it. So I'm going to look
at in the next cell, same variable, I shall copy and add a few more
digits to this. Okay, randomly, I
add few more digits. So here is the fundamental
question to think about when we talk
about integer, okay? If I exude this and if
I can check the type of this type of this.
It is integer. If I execute this cell and check for the type,
it is an integer. If I execute the X assigned a large value
and check for the type, it is also integer. So
what is the learning here? Integer can be integer. Let's put it this
way. Integer can be of any arbitrary size. Hope you understand this. Okay. That simply
means Python will provide Arbitrary
precision for integers. Okay? You can create a large
integer that is of length, whatever I'm not counting. Or you can create
integer that is on the three digit length or little more than three
digits and quite a number. In fact, maybe 15
digits or longer. I can count. Okay? So it doesn't have
restriction on the size of the integer. Integer can be of
any arbitrary size. And therefore, you can work with arbitrary precision of
integers in Python. Another important idea is, you can also perform
on these integers, certain arithmetic
operations, right? You do not have to worry about do not have
to worry about. I'll put this into
the recording here. Oflown underflow, you
don't have to worry. So while performing
arithmetic operations, operations do not worry about overflow or underflow. This is a big advantage in Python as compared to other programming paradigms from what I've experienced. Okay? So an important idea
that I want you to be comfortable
with is that you do not have to worry
about overflow or underflow while performing arithmetic operations
of integer, which can be of any size
arbitrarily speaking. I've also put something
as float ranges. Let's just examine floats. Please understand this. This is an important idea. When it comes to
floats, typically, it is represented as binary 64 bit double
precision values. Okay? Typically, represented
internally. As 64 bit binary double precision values. There is some nuance
to this. I'll just explain that in a bit. Okay? This is aligned
with IE standard. Okay? This is aligned
with IE standard. So race is this
standard you can think of float ranges like this. So based on this standard, what do you think would
be the maximum value a float can take, Okay? Float can take. Okay, let me just put it into
next sentence properly. Float can take. Okay? This is kind
of approximation. Approximately, Okay? 1.8 times ten raised
to the power. 308, if I'm not mistaken. So that means you can represent fairly large value for
floating point representation. Okay? If the number is
greater than this, if the number is greater than
the above representation, meaning 1.8 multiplied
by ten raised to the point of 308 if the number is greater
than this representation. Okay, then it is considered we represent it as
IN F, which means infinity. So that essentially means you
could represent in Python, infinitely large values for floating point
representation. That's what I meant by
float ranges. Okay? Just to summarize this, integer can be of any size
arbitrarily speaking, and you can see
the demons station here in the form of live demo. And for floats, it is typically
represented internally as a binary 64 bit double
precision values as per IAE standard. Based on this standard, float can take approximately larger values which
are typically to the tune of 1.8 times ten rays to the
par of three not eight. If the number that
is represented as a floating point representation is greater than this
representation, that is one point time eight
times ten rays to the p308, it is considered
infinitely large number. Okay? Having said what I said, here is a small nuance. Quite often, float you will not be able to represent
exactly in binary form. Floats quite often
cannot be represented. Represented in binary
form exactly. Okay? So there is a bit of
a nuance to this. So because of this,
what happens is when I have a floating point
representation, internally what happens inside
Python environment is that it is often an approximation
of the actual value. So it is often an approximation of
the actual value. Hope this is clear to everybody. When you look at as an
approximation of the actual value, because quite often you cannot quite often you cannot
represent in binary form. Let me just clarify
this once and for all. Quite often, floats cannot be represented in
binary form exactly. So what you see as
a representation is oftentimes an approximation
of the actual value. The difference between
this represented value that is approximation
of the actual value, and the real large
representation of the float point
representation is so small, for all practical purposes, this is quite insignificant, does not pose any challenges. Okay? Hope that's clear.
9. Variable Type & Assignment: Let's continue
understanding in Python, something that's
very, very important. I've called it variable
type and assignment, and I'll explain the
beauty of Python, how it differs from other
programming languages per se. Okay? If you take, for example, C or C plus plus, or even Java, you need to define the type of
the variable in advance. It could be integer,
character, string, et cetera, but you have to define the variable
type in advance. Whereas in Python, no need of
defining the variable type. That's the beauty.
Then how does Python recognize some variable
of certain data type? I'm just going to talk about it. What happens inside of Python? At the time of execution, the type of the variable is
inferred from the context. Okay? So at the time of the execution execution type of the variable. Is inferred from the context in which the variable is used. If this is mouthful, I shall simplify and explain this with a concrete example. Okay? What does these two tell you before we
go to the examples? No need of defining
the variable type. At the time of the execution, type of the variable is inferred from the context in which
the variable is used. So this essentially
points to what is called dynamically
typed language. So Python is dynamically typo language. Okay? It simply mats this
no need of defining the variable type at
design time at runtime, that's the time of execution. Type of the variable is inferred from the context in which
the variable is used. Okay? This is important
distinctions. Visa, for example, the other
paradigms of the world. Okay? So I think it's worthwhile to get these things understood in its entirety. So as I intended, let's just look
at some examples. Let's say, I'm going to assign
value X is equal to 100. Notice here, I've simply named the variable X and used the assignment operator with
a single equal to sine. And to the right of equal to
sine, we have the value 100. So this is assignment, okay? I'm assigning value
100 to X. I did not define the type of
variable X. I said, at the time of execution,
it is inferred. So how do we check this? So I can check this by simply typing. We've already seen this,
but putting into context. So if I run this line, these two lines of code, turns out it is integer. If I can print X for you, the value of the variable
is also printed. Variable holds the value 100
and it is of type integer. If I take same variable and
do a different assignment, simply put, I'll say 100.6
X and see what happens. Now it has become float.
What does it tell you? At the time of designing, I have not defined
the variable type. At the time of execution, based on the context in
which the variable was used, the variable assumes
the data type. In the first instance, same
variable assigned value of hundred turned out to be integer
as understood by Python. In the second example,
same variable assigned with the floating
point representation, it has assigned float as the data type
for the variable X. Just to give you
another flavor of this, if I go ahead and put this
hundred in codes. Okay? That's it. Then execute this. Turns out, it becomes string. So they should amply
clarify things that Python is a highly
dynamically typed language, and therefore, there is no need of defining the variable type. And therefore, at the
time of execution, the variable is inferred from the context in which
the variable is used. And also notice these
instances of three examples that I alluded to would all be about simple
variable assignment, meaning, I've assigned one
value for one variable. So next we shall see how do we do multiple variable assignment? Hope everybody is great about variable type and
assignments. Yeah?
10. Multiple Variable Assignment: Let us now understand
how do we do multiple variable
assignment in Python. There are two ways
to look at this. One is, let's say
I have variable X, variable Y, and variable Z. All of them would
require a single value. Let's say all of them
should be assigned 100, or let's say 200. I can do it very elegantly
in Python, like this. X is equal to, sorry.
X is equal to. Y is equal to Z and alla, I can print all the three
and you get 200 assigned. This is multiple
variable assignments. Where X, Y, and Z are
multiple variables. Supposing in the previous
case X was assigned 100, same variable was assigned a
floating representation of 100.66 and same variable
was assigned string 100. So instead of that, if I have
to take X is equal to 100, let's say, and I'll make some space and say
Y is equal to, let's say, 100.25
and Z is equal to, let's say, string hundred
how will I do this? I can do this. If I
execute, see what happens. I'll execute this. You will not be able to use it like this. This multi multiple
variable assignment is a wrong way of using in
Python, semantically speaking. What I can do I could use a
semicolon as the separator, and therefore, if
you use that way, you can print all
the three variables and it works really well. Uh huh. Something really not happening. So
let's see this. What I've done? X
is equal to 100. Y is equal to 100.25. Z is equal to this one. Then I called out X Y and said, here you can see the
mistake is here. I corrected it,
and there you go. So in a single line of core, I have done multiple
variable assignment. As in X is assigned
integer, hundred, Y is assigned floating
point of hundred 0.25, z is assigned a string of 100. So this is how it
is done in Python. So we'll go ahead and explore some interesting operations arithmetically speaking, next.
11. Arithmetic Operators in Python: Let's now explore some interesting arithmetic
operations in Python. Let's see a few interesting
ideas of implementation. Turns out Python
provides various arithmetic operators
support, so to speak. Like, for example, you could use uniary
operator like PAS. You could use minus. You could use star. You could also use slash. You could use percentage, and you could use double slash, and you could also
use double star. If you're comfortable using these operators in the
context of Python, it's good enough to do this. So let's go ahead and explore
few of these operators. Okay? Let's say X is equal to four plus three,
I shall print X. No points for
guessing. X is seven. So this is using plus
operator for addition. Let us just say
I'm going to take five by two, sorry, 5/2. What do you expect? So it performs what's called true division and yields what? So let's print this. Any
guesses for what is the output? Let's say this, 2.5. So this is called true division, which has resulted in 2.5
as a float representation. Okay? So this is
the slash operator. Let's just look at some
other example of okay? Randomly, I'll take something. By two. Instead of this, I think we've tried that. Let's use percentage here
and two. What do you expect? 11/2, you will see
the remainder. Let's see if I print
x, we should get one. Yes, 11/2 results
in remainder one. Okay? This is the
percentage operator. Let's go ahead and explore
this starstr operator. Let's say, I'm going
to have Y is equal to two times not two times two
raise to power of three. Okay? So this should
give you eight. Yes. So this is exponentiation. That means two is raised
to the power three. So let's explore this
double slash operator. Let us say, I'm going
to say set is equal to three slash slash two. Okay? So what it
does, let's run this. So it has given one.
What does it mean? It has given the tient to one, and it completely ignores
the fractional part. That's the whole idea
of this apparatus. Okay. Conventionally,
this is how we use. Can I use this exponeniation
slightly differently? Instead of assignment operator, I could use some shortcut
for arithmetic operations. Okay, I'll just copy this. And instead of this
representation for exponenation, I could use something like this. Okay? Let me try star star
equal to three. And let's let's see what
happens to this. Will it work? Yeah. Yes, it does. So why is 88 raised
to the pore of three? This is same as this one, but instead here, two rays to the pair of three is
what we have represented. Here, eight raised to
the pore of three is what we are getting. Okay. Then let's use some
interesting things we will use. Okay. Let's say, I'm going to put
it as X one is equal to. Okay, I'll use the
short notation. I'll say percentage equal to. Then Okay, and print X one.
So what do you think happens? Let's see this. X one is not defined because
we have not defined X one. Let's say we're going
to take X and see this. Yes, there you go. It is same as this is same as just
writing for reference. X is equal to X percentage ten. If I run this in the
next line of code, you will see exactly
the same output because it's a different
representation. There is no difference
in the output. Okay? This is using
assignment operator. This is in place of
assignment operator. We have got shortcut for
arithmetic operations. So the long story short, I'll put it just above here. You'll be able to use the various shortcut for
arithmetic operations. Okay, shortcuts for
arithmetic operations. So what are those?
You could use. You could use plus equal to, you could also use
minus equal to. You could also use
star equal to. Similarly I could use
percentage equal to. I could also use slash equal to. I could also use, for example, double slash equal to. You can explore more
as a practice, okay? I could also use
double star equal to. All these are possible. Okay. I think hope
everybody's clear on this. Shortcuts, four is what I meant. Let's go ahead and look at this. Yeah, I want to save this. Hope you're enjoying
this learning.
12. Floor Division Operator: Continue. I want
you to understand very interesting operator
in Python called for Arithmetic Operations called
floor division Operator. Floor division operator. So let's explore this because
it's interesting, okay? I'll show you some
variations of this, which will be really worth everybody's while to follow
and understand this. Let's say I'm going to use
a function called print, and let's say I'm going
to say ten slash three. What do you think is the output? Okay? So let's say this. It has given the output three. So what does it even mean?
What does it even mean? Can somebody explain? Go to the comment section of the description wherever
you want to interact. Go ahead and explain
this operator. Bring some subtle variations to this and then talk about it. Let's say I'm going to make
it minus ten slash three. Will the answer be
something different? Than this, or will
the answer be same? So when in doubt,
please execute and see. Here, it has given me three without any
negative value for ten. When I had given minus ten,
it has given me three. Okay? So one more time, I want you to explain
why it is three here, and I want you to
think about why it is minus four.
I shall explain. In this example of minus
ten slash slash three, what it has done, Python H, it has looked for
multiple of three, which will yield minus ten. So it turns out, it'll be
multiple of three which yield minus ten would be -3.33 33. We don't need to be too precise. I'm just stopping
at the fifth demu. Floor value for this, okay? Floor value for
this is minus four, and that's why you
have seen this. Okay. Is everybody clear
on this? Let us look at some variations. So what are the
other possibilities? We have ten positive,
ten negative. We have three positive
here, three positive here. Let's make three negative
here and see what happens. Any guesses for the output? It's a matter of execution. So minus four. So
what has it done? Similar to what I
explained in this example, cell number 25, cell 26
is something like this. Multiple of minus three
that will yield ten is Let's put it here. Okay. Multiple off. Let's put it completely here, multiple of minus three. That eels. Ten is -3.3 point
sorry, 3.33 33. Whose floor value
again is minus four. And that's why you see
the answer minus four. So what other variation
possible to this example? You can make both minus, minus ten, minus three, again. Any guesses, how does it work? It's a matter of execution. One more time. Learners.
Same argument can be made. I'll just copy this and whatever is required to be
changed, I'm going to change. Okay. So multiple of minus three that yields minus
ten because we have the numerator minus
ten is now it becomes plus 3.3 point sorry,
3.33 33, whatever. Whose floor value is three. That is the reason you
see the output three. Hope that is clear to everybody. Okay? If I have any doubts,
feel free to ask me. I'm more than happy to clarify or bring more examples. Yeah.
13. Operations On Boolean Data Type: Learners, I'll scroll up to show you something
interesting. We looked at several examples in this tutorial and we explore the datatype called
Boolean true and false. Is a mathematical operation possible on bull and data type. Let's understand some
interesting ideas of operations on
bull and data type. Operations on Boolean Pat? Follow along. This is another
interesting idea here. Let's say, I'm going
to say X is equal to. So true, we can represent it
like this. Can we print X? Any guesses per value X? Let's execute. It is true. Okay? If I Print X is equal to false. No point for guessing
it. False is the answer. Can I go ahead and
add True like this. What do you think is the output? You can pause this
tutorial and then think and come up
with an answer. Better still execute it to
find the answer for yourself. You'll be surprised
to see the answer no. Instead of true and false here, I'm executing cell
number 28. S two it is. If you're wondering why
True is considered one. Okay? And false is considered zero. I'll
just put it here. False is zero. What do I mean by that is if I say false plus true true
is one, false is zero. If a print is, you will get one. Okay? So another variation
to this will make, let's say, false plus false. No points but guessing
the output would be zero. So another variation
you can make to this. Of course, it's very obvious, but I'm still making
it for thoroughness. Okay? So this will also be one. So everybody is
clear. Since true is treated as one and false
is treated as zero, they can be added. Okay. Everybody's clear, right?
14. Precedence & Associativity: Learners. By now, you're comfortable, I suppose, with the
arithmetic operations. We have done operations
on Boolean data type, and we have used four
division operator and we have used several
arithmetic operations, right? So here is the fundamental idea of understanding
for thoroughness. When you are using operators in an arithmetic
expression in Python, is there any priority of
the operators is used? Put it another way. Is there any order for
the operators to be used when it is used as part of arithmetic
expression in Python. And that's what is called
precedence and associativity. I just want you to
understand this. These are some of
the specific things not many people would cover, but these are important
to understand. Someone will give you an
abbreviation to start with, which goes something
like this, P E. Yeah, ma'am, DAs. This gives you the order of priority of the
operators, okay? Order of the priority
of the operators. Priority of the operators arithmetic
operators, if you want to. Yeah. So explain.
Don't worry on that. Would I would go in and qualify this decreasing order of the priority of the
arithmetic operators. So remember this. When you use multiple operators as part of arithmetic
expression in Python, it's always evaluated based on some priority of the operators. And it is always in the decreasing order of the priority of the
arithmetic operators, which is specified
or abbreviated as PEMDAS. So what does this? We'll just simplify this. Okay. This stands
for parenthesis. P stands for parenthesis. So remember, it is the operators in the decreasing
order of priority, which is this, parenthesis. Then the next would
be exponentiation. The E stands for
exponentiation. Okay. I'm also giving
you the operators, which is represented
typically as double star. Next decreasing order would
be several operators. You will have multiplication,
you will division. Sorry, you will have
multiplication. You will have division, you will have floor division, and you will have this
percentage operator. Okay? So all this can be
put under this, which stands for D rather, which stands for
multiplication division. Okay. So I'll just put it
like this multiplication. And division. These
are the operators. Then we are left with AS. So no points for
guessing. By now, it should be crystal
clear to everybody. You have addition and
you have subtraction. So that's what it is. So
addition subtraction. So please remember
when you're using multiple arithmetic
operators as part of arithmetic expression,
it's always evaluated. I mean, operators
are always evaluated in the decreasing order of the priority of the
arithmetic operators. First one, highest priority goes to parenthesis, followed
by exponentiation, followed by multiplication
and division operators, and the least priority goes to the addition
and subtraction. Okay. I don't know if it has occurred
to you in your mind. Some of you might be
thinking ahead of what I'm going to say or ask rather. Let's say there is a tie between operators of same precedence. Okay? I ever there is a tie between operator
of same precedence, then it will always be addressed using associativity
of operators. Okay? So just put this when there is a tie between operators of same precedence. Then it is addressed
using associativity, associativity of the
operators. Okay. So slightly deeper concept, please understand
that every operator would have either left or right associativity or right
to left associativity. Let me phrase it properly. Every operator would
have either left to right associativity or right
to left associativity. Okay? So it can be thought
of like this for an example. If X is equal to, let's say, x one multiplied
by X two, divided by X. Okay. I I like this. In this example, the star is done much before
everything else. Star is done before
the slash operator, since alphabetic
operators will have left to right associativity
Think of it this way. Star and slash are part of multiplication and division in the decreasing
order of priority. So they both have same
precedence, star and slash. So look at this.
Whenever there is a tie between operators
of same precedence, then it is addressed using
associativity of operators. That simply means
every operator, either star or slash, would have either left to right associativity or right
to left associativity. If I have this example
where X is equal to X one multiplied by
X two divided by X, this multiplication
is done before this division because
arithmetic operators have left to right
associativity. I'll put it here as a
comment that will clarify. Okay? Star is
performed before since arithmetic operators have left to write
associativity. Hope everybody is
clear on this. Okay? So I want you to be
comfortable with these ideas, which is important in the
context of Python Basics. Okay. Hope you're enjoying. Continue watching. We will have some more interesting Python
basics coming your way. Keep at it. You'll
get better at it.