Hello guys.
So we are going to continue the discussion with respect to Python programming language.
Now in this first video we are going to probably discuss about syntax and semantics in Python.
So I have given the video outline what all things we are going to cover over here.
So first of all we are going to understand about single line comments and multi line comments.
Uh, along with this we are going to understand what is the definition of syntax and semantics.
And then uh we'll see basic syntax rules in Python.
Then we'll also be understanding about semantics in Python.
And what are the common syntax errors and how to avoid them.
And we'll also be seeing a lot of practical code examples.
Okay.
Now first of all let's go ahead with the definition over here.
So here you can clearly see that syntax refers to the set of rules that defines the combination of symbols
that are considered to be correctly structured program in a language.
In a simpler terms, syntax is about the correct arrangement of words and symbol in a code.
Now understand when we are doing coding right, uh, we usually face some kind of syntax error.
Why it usually happens.
It may be because let's say I'm not assigned a variable and I've directly used it, you know, so I
may get a syntax error.
So that is the reason it is written over here, that it is a correct arrangement of words and symbols
in a code.
Right.
Now, uh, this is a basic definition of syntax.
I would also like to give you some more definition for semantics.
Okay.
So let's go ahead and write it over here.
So here you can see semantics refers to the meaning or interpretation of the symbol character and commands
in a language.
It is about what the code is supposed to do and it runs.
Syntax is how we are able to arrange the words so that we write a code and semantics refers to the interpretation.
So whatever code, we are specifically writing what it is supposed to do right when it runs, like when
we are trying to run it, what it is supposed to do, it is more about the task that we are trying to
do, right?
And that is what semantics basically refer to.
Now this is some kind of basic definition.
I know you usually don't go through this, but, uh, I really wanted to explain you in a way that a
basic anyone can actually be able to understand this who is also starting Python programming language?
Okay, now let's go ahead and see some of the basic syntax in Basic syntax rules in Python.
Okay.
Now first first and the foremost important thing is about case sensitivity.
Okay.
So I'm going to write a comment over here.
Case sensitivity.
Now before I go ahead you'll be able to see that whenever I use double hash in a code.
Uh, see, whenever we create a coding cell.
Right.
And if I want to write some kind of comments, a single line comment, I can basically use this double
hash.
So this double hash basically means that whenever I want to use a single line comment, I can just use
this.
And I can write any kind of comments that I want.
Like I have written over here.
Let's say that in this code cell I'm going to discuss about basic syntax rules in Python.
So I have just written double hash over here okay.
Let's say and there is also an option to use multi-line comments okay.
Now multi-line comments.
Let's say that I have created one test.py file over here.
Now for multi-line comment let's say I'll write.
this is a multi-line comment or sorry, this double hash is a single line comment.
And in case of multi line comment I have to use triple quotes right.
Something like this triple quotes.
So I will say welcome to the right to the Python course.
So that basically means I'm using 2 to 3 lines.
And I'm writing this particular lines.
Right.
I'm, I'm, I'm giving some kind of comments over here.
Now whenever I have comments which, which expands to multiple lines, I can definitely use this triple
quotes.
So these are the two ways of writing comments in Python programming language okay.
The reason, uh, why I have used this test dot p, y and c again in Python, you can actually use ipynb
file, which is the Jupyter notebook file, or.py file, which is a Python file Now, uh, this triple
quotes usually does not work in Jupyter notebook.
It only works in test.py file, right?
Other than that, single line comments definitely work for both Jupyter notebook and the py file.
Basically the python file.
So I hope you got an idea of understanding about single line comments and multi line comments.
Now let's go ahead and see some of the basic syntax in Python.
So first point that I really want to talk about is case sensitivity.
And to give you a good comment over here Python is case sensitive.
So guys now let me go ahead and define a variable.
Let's say this is a variable.
Um and what exactly is a variable.
Again I will give a definition.
But let's consider that a variable is something that stores some information.
Okay.
Let's say in this particular variable I define my name.
That is Chris okay.
And let's say I again define a variable with capital n and I write the same variable name that is name.
And here let's say that I am just going to use another name.
Let's say Nick okay.
Now one thing that you really need to know if I print this particular name and if I print the next name
right over here.
Both this name are different variables, right?
It is not the same variable, even though even though the variable meaning is same.
Right.
Name name.
Right.
But I've told over here that Python is case sensitive, right?
That basically means both these variable are different identifiers.
Okay.
So if I am going to execute this here, you can see the first name is displaying Krish and the second
name is displaying Nayak.
Right.
So that is the reason we say that hey python is case sensitive right.
Both this variable.
Right.
If you are writing, we always need to make sure that, uh, you know, let's say that, uh, I have
defined this name over here and again, I've defined name, uh, unless and until both this, both this
variable are having that case sensitive things.
Right.
That basically means if I, even though I'm writing with the small, uh, small n later letter over
here, then I will be considering both this variable as same.
Otherwise this will be treated as a different identifier.
Right.
So this is one of the very much important information.
Now coming to the second thing right.
Second point, let's talk about something called as indentation right now.
What exactly is indentation okay.
And this is also very much important to understand.
I'll just write a comment over here.
Python uses indentation to define blocks of code.
Consistent use of space.
Commonly four spaces or a tab is required.
Okay, now what exactly is indentation?
Okay.
And, uh, if you really want to have a definition for indentation.
Okay.
Uh, so we can also define indentation over here.
So let me just go ahead and give you some kind of definition okay.
I will be using a markdown cell.
And let me just go ahead and write over here right.
And let's go through this particular indentation definition.
So let me just go ahead and execute.
Indentation in Python is used to define the structure and hierarchy of the code.
Unlike many other programming language that uses braces to delimit block of codes, right, Python uses
indentation to determine the grouping of statement.
Okay, now indentation is something that is very much necessary whenever you are writing some clauses.
Let's say if block or for loop or something else.
Right.
So let's say I'll give you an example over here, I will I will define a variable.
Let's say age is equal to 32.
And let me just go ahead and write one if condition.
Don't worry if you don't know about if condition how do we write it?
What is the syntax and all?
I will, uh, explain this as we go ahead, but I just want to give you an idea about indentation.
So let's say if I write if age is equal to greater than 30.
Now when I write, if age is greater than 30, and if I just use this colon now see what will happen
if I press enter, it is going to start inside this particular block and automatically it did not start
from here.
Instead it started from here.
Right?
So it is what it is basically doing.
It is using indentation, right?
It is using consistent use of space commonly for or tab.
Right.
And here it is for a block of statement right.
Block of code right.
So here if I'm going to right let's say that I'm going to print 30 over here I'm just going to print
age right now what is basically happening over here I've just used this uh, if block.
Right.
And because of indentation, we know that this code is only going to get executed when this is true.
Okay.
If I go ahead outside and if I print it print age, that basically means this is not inside the if clause,
right?
Usually in other programming languages, we usually write this kind of braces.
Right?
We we try to define this kind of braces.
If you know other programming languages like C plus plus or C sharp.
Right.
But in Python, instead of using this braces automatically it will be aligned with indentation.
Now let me just go ahead and execute it.
This time.
Two times is basically getting displayed 32.
So one is this and the other one is over here.
Understand this age is basically getting displayed but this is outside the if block okay.
So this is giving you an idea about indentation okay I've already explained you about comments, but
let me just go ahead and write about single comments.
So first single comment.
Let's say this is a single line comment.
I'll go ahead and write single line comment.
Let's say that I'm going to print Hello World.
Okay, now if I go ahead and execute this, you can see only Hello World is getting displayed.
This this comment section is not getting printed right again, why do we write comments?
Let's say that I want to define a function.
I want to give some information about this particular function.
I can write all those info in this comment section.
Okay.
Now, uh, this is important.
Okay.
Now let me just go ahead and talk more about different, different syntaxes.
One more syntax is something called as line continuation.
So here I'm going to basically write line continuation.
Now what exactly is line continuation okay.
Let's say I am going to create a variable total okay.
And I'm doing some mathematical operation like one plus two plus three plus four plus five plus six
plus seven.
Let's say this line is becoming bigger and bigger, right?
So this line is really becoming bigger and bigger.
Let's say I want to go to the next line.
So what I can actually do, let's say I will write plus and now I will use a black slash.
Now if I use a backslash if I press enter then what is going to happen?
It is just going to continue in the new line.
Okay, so here I will just use this and then let me go ahead and press backslash.
And let's say I go ahead and add more elements like four plus five plus six.
Now if I go ahead and print total you will be able to see this will also do all the operation.
So if I do the calculation 1318, 22, 25, 27, 28, 28 and uh 28, plus six, 34 3943.
Right.
And I'm able to get the answer so you can use right in case of uh, basically line continuation, you
can basically use a backslash.
Right.
And you can continue the statement to the next line.
Okay.
So this is very much important.
Again one of the most amazing feature.
And again let me just show you one more thing.
One more amazing thing with respect to syntax and semantics.
Uh, that is nothing but multiple statements on a single line.
On a single line.
Now, what does this basically mean?
Let's say I want to assign one value.
Let's say x is equal to five.
I have defined I want to define another variable.
Let's say y is equal to ten.
Again if I want to define another variable I'll use a colon.
And I'll go ahead and write x is equal to x plus y.
Now what is basically happening in a single line of uh in a single line I've used multiple statement
I have assigned x is equal to five, y is equal to ten, Z is equal to x plus y.
Now if I go ahead and print z you see I will be able to get the answer.
See ten plus five is equal to nothing but 15 right?
So guys now you here you can see that this kind of operation can also be performed right.
Now let's go ahead and understand semantics okay.
And here also I'm going to put some points.
So understanding semantics in Python.
Okay.
Now the first thing uh that we are going to understand is about variable assignment.
So what exactly is variable assignment?
Let's say that I define a variable like age is equal to 32.
Okay.
So here when we run this particular command right.
When we run this line of code then in other programming language you usually define the data type beforehand
like int age float age.
But here in Python the kernel automatically understands during the runtime.
So here the kernel will understand that age is an integer, because here we are assigning some integer
value.
Similarly, let's say I go ahead and write name is equal to Chris.
So here you'll be able to see that name will be a string right.
So name is a string.
So I will just go ahead and execute this.
If you want to understand whether I am probably how to probably check the type of this.
So I will go ahead and write type of H and let me just go ahead and execute it.
So here you'll be able to see that hey I'm getting an integer value.
So this basically indicates that h is an integer.
Then if I go ahead and execute type off name.
So here you'll be able to see that hey it is an HDR type that is nothing but string type.
Now uh, let me do one thing.
Okay.
Uh, this the type that we have actually run, right?
It is also called as something called as type inferencing.
So here I'm going to basically write type inference.
Now Python infers the type of a variable at the runtime.
And this is basically allowing dynamic typing okay.
So what does it basically mean over here.
Is that uh, okay, I will just use this in the other code.
Let's see.
So if I go ahead and write over here I'll create a variable.
Variable is equal to ten okay.
And let's say I go ahead and write print type of var write as.
Sorry this will be variable okay.
Now what is basically going to happen over here?
Once you probably execute this you are able to get an answer saying that hey, it is of int type and
the class will be int okay.
But if I go ahead and use the same variable and assign some other name or some other value, let's say
Chris.
Now if I go ahead and print the type of variable now what I am actually going to get right.
So here you'll be able to see that I will be able to get SDR for the same variable itself.
Right.
So here you'll be able to see that whenever we refer to the type of this specific variable, we will
be also able to see something very amazing as we keep on changing.
This particular variable will be also able to see the inferencing type.
Right.
So basically when we are searching for type of this it is basically showing different different things.
So that basically means what during the runtime you can change the type of the variable with different
different values.
Okay.
Now uh this was one of the most amazing thing, uh, with respect to semantics.
Okay.
Um, and uh, as, as, you know, something, uh, over here is that we also need to understand that
what happens, uh, in the case of indentation error, let's say if I go ahead and write, age is equal
to 32.
And if I write if age is greater than 30, okay.
And if I go ahead and execute it and let's say I don't use this indentation.
Instead I start from here and I go ahead and write print age.
So here clearly you'll be getting an error saying that hey indentation error expected and indented block
after if statement on line two.
So this is how you will be able to understand it.
All I have to do is that start from this and automatically you'll be able to see the answer.
Okay, uh, this is important.
Uh, otherwise, uh, understand?
Without indentation, things will not work out.
Even though you create classes, even though you create functions, whenever you write a block of codes
within some kind of blocks itself, like an if statement for loop or while loop.
So indentation is specifically required there.
You do not explicitly need to use brackets.
Now, uh, one more thing, one more syntax error, which usually people do.
Let's say I have defined a is equal to B and you know that B is not defined.
So if I go ahead and execute this this is also kind of syntax error.
Because name B is not defined over here.
Right.
So this basically becomes a unknown variable.
Right.
So this is nothing but this is basically called as a name error.
Okay.
I'll be showing you as we go ahead, uh, with respect to exception handling and how do we handle all
these things?
We'll be talking more about it.
Okay.
Now, uh, let me do one thing.
Uh, let me just show you one amazing example of indentation.
Some code example of indentations.
Okay.
And again, uh, after this session, also, I will be giving you a lot of, uh, quizzes, assignments,
practice, uh, sessions and all.
You just need to go ahead and do it.
Okay.
Practice it.
So here, if I write if true, say true is a kind of a boolean variable, I will be talking more about
variables in our next session.
Let's say I go ahead and print.
This is my correct indentation.
Right.
Indentation.
So here uh this this basically becomes my statement.
And print is just to print anything in your output console okay.
Now inside this true let's say I have another condition if false.
Now if I want to write any print statement inside this block, then I have to again do an indentation.
So here we go ahead and write.
Print this one print.
Write this one print.
Why it will not print because by default it is true.
This is going to print.
It is going to check this condition and it's going to come out of this particular condition.
Right.
And finally you'll be able to see that I'm printing one more thing over here saying that whether this
will print or not, you just think, okay, this will obviously print, right?
Because this is not getting executed.
We are not going inside this.
And finally, this print statement will execute.
Now, if I want to go outside the if clause, then this will be another print statement.
Right?
And I can basically say outside the if block.
So how many times print is going to, uh, just pause the video and understand how many times the print
statement is going to execute it, right?
Three times.
So if I go ahead and execute it, you'll be able to see correct indentation.
This will print outside the if block okay.
So this is basically going to get printed right.
So these are some of the amazing examples of indentation.
Okay.
Now uh, let me do one thing.
Let me show you one more thing.
Right.
Um, one is with respect to, uh, correct variable assignment and, uh, type inference.
But before I go ahead and explain you more about this, uh, this is some basics about syntax and semantics.
Uh, now, as we go ahead, uh, what all things we basically covered single line comments, multi line
comments, definition, syntax and semantics.
Basic syntax rules.
How do you define a variable?
What are scans are case sensitive and all everything I've actually covered over here.
Right.
This actually gives you an idea about uh, you know, the basic differences between syntax and semantics
also.
So I hope you like this particular video.
In the next video I am going to talk about, uh, variables, then data types, then operators.
Right.
So yes, this was it for my side.
I will see you all in the next video.
Thank you.