2. Python language semantics
Oct 8, 2023
Semantics and syntax are essentially the grammar rules that a programming language uses to make sure that information is communicated without misinterpretation. In coding, this is things like spelling, spacing, semicolons, brackets etc. This video covers a few of the key differences in python and introduces a few other programming basics.
All right, in this video we're going to talk about something called language semantics.
Think of this sort of like grammar, right?
The grammar rules that we have to follow in the English language when we write and speak
help us make sure that communication of information
passes seamlessly from one person to another.
And the same thing has to happen with programming.
You have to have certain semantics or rules for typing up the language
that everyone agrees to or you get miscommunications, right?
So if you've done this in other languages,
Python's going to be a little bit different, right?
For example, maybe you've done some Arduino projects.
Hopefully you have.
Arduino's a wonderful tool for doing really rapid prototyping
and building things really quickly and easily.
And if you've done that, you've noticed, right, when you do this,
you have to start out by before you ever dive into your code
where you actually make things happen, you have to declare all your variables.
All the variables that you're going to use, you have to declare them up front.
We have to say what type it is, in this case, int.
That corresponds to a certain type of integer.
You have to put this semicolon at the end of each statement,
otherwise it'll give you an error, right?
So there's these specific rules.
And then you have to do this, like you have to set up,
you have to declare which pins on your Arduino board are going to be active.
So you have to say this one's going to be an output,
this one's going to be an input, all this stuff.
And then when you get into the code, there's other complications, right?
There's something called an if statement, for example.
An if statement says, if a condition is met, then do this.
And you could say, if it's not met, then do something else, right?
So in this case, they say, if buttonState, that's a variable,
is equal to high, right?
That's some value that the variable can be.
Then they put this curly bracket, right?
That curly bracket right there, and that goes all the way down to here.
And inside these curly brackets, right,
inside that region is the stuff that happens if that condition is met.
So one of the first differences you'll notice with Python
is that we don't have to rely on these curly brackets.
Instead, what we use is indentations, right?
So, for example, take a look here.
Here, they use white space, they call it.
They use spaces. The default is four spaces, right?
That's one tab.
That's used to denote blocks of code.
So if we come over here, I'm going to close our Arduino.
If we come over to Spyder,
I could write a block of code the same way.
I could say that you've got your variable, let's just call it x,
and we've got our variable y, right?
And y is going to be equal to 8 or 9, right?
Now, here we're going to say, if y is greater than x,
and I'm going to do colon,
that says that it's starting our if statement, right?
Now we're going to say, I just hit Enter,
and notice what it already did,
it already put those four white spaces over here for me.
But now we can say what should happen.
So we're going to print, right?
Now, print doesn't have to be capitalized, right?
This is a special word.
If I capitalize print, it won't work, right?
It's not going to be the same way.
So we need to do lowercase print.
Now it shows up in orange, so you know it's a special word, right?
Now we're going to have it print something.
We'll say, like, y is greater than x, right?
That's the text that will be printed out.
So let's go ahead and run this.
We're going to hit F5, right?
And when it runs, sure enough, the output over here
is a y is greater than x.
And if we look at our Variable Explorer over here,
we see that x is equal to a value of 5,
y is equal to a value of 9.
Everything's great, right?
When we're done with this, if we wanted other things to happen,
we could have other things happen.
We could say that z is now equal to, you know, x plus y, right?
You could have that happen.
Now, if we're done with our if statement,
and we want to keep on coding,
but not inside of that if conditional statement,
now notice that the white space is gone.
So if I have, like, my dog,
and I want to name my dog something, like Scout,
it's not inside this.
I haven't tabbed over,
so it's not part of that statement that was above, okay?
So what else can we say about these?
You don't need a semicolon to finish your statement,
but you can use semicolons
if you want to put multiple on the same line.
Like, here they named three students.
Student 1 is named Tom, then he's a semicolon,
and then student 2 is Nick, student 3 is Chris.
And because they put these all in the same line,
they did semicolons to separate them.
But if you notice in our code,
we didn't use semicolons
when we declared these different variables, all right?
You don't have to in Python, which is nice, okay?
What else?
You can comment out sections of your code
by putting this hash, right, or the pound sign,
in front of that line, okay?
Or you can highlight a section of your code
and hit Ctrl-1,
and it will toggle that whole section on and off
in terms of being a comment.
So, for example,
if you decide that you don't like this if statement, right,
I can just highlight it and then hit Ctrl-1,
and see what it did?
It put pound signs in front of them,
and the text turned gray.
And if we run it now, let's run our code again.
This time when we run it,
it didn't output any text, right?
It didn't tell us that y is greater than x
because it's been commented out.
Basically, the compiler, or the interpreter,
when it comes across those lines,
it just skips them.
It ignores them, right?
And if you decide that you want those back again,
you highlight them and then Ctrl-1.
So, that's just some handy stuff
that Python makes available for us.
Or on any individual line, right,
you can manually type in that pound sign
and it'll get rid of it, right?
Here's something that's cool.
You can have functions in this language.
And so, for example,
maybe you don't want to type out the mathematical expression
to solve for the quadratic formula every single time.
What if you just did it once
and then you could call that every time
and just send it the values of a, b, and c,
and it will send back the values of your roots, right?
That would be an example of a function.
Well, to do that in Python,
and we'll learn about these later,
you just have to build your function
and then you send it with round brackets
the values that you're going to send to your function, right?
Here's another thing.
When you name your variables in Python,
what it does is it creates an object
to represent that data, right?
So, for example, so my dog's name is Scout.
If we say that your dog, right,
a new variable is equal to dog,
then it's going to name it the same thing as Scout, okay?
In other words, naming a variable
creates a reference to the object being named.
It does not create or recreate that object, right?
So, for example, when I took my restaurant
was equal to Red Iguana over here
and I say that name equals restaurant in the next line,
the data under Red Iguana didn't necessarily get copied.
We just have the variables restaurant and name
both pointing at the same data, right?
So more on that later when we talk about
the importance of copying data over
if you don't want to destroy the original, right?
Now, variables can be different types
and they can change different types, right?
In our Arduino, remember when we declared like a port,
it was an integer,
and then if we wanted to use that port as a string later on,
that was going to give us a problem.
You couldn't use it as a text, right?
But that's not a problem in Python, right?
If I currently say that restaurant equals Red Iguana,
that's a string of text,
and then I say that name is equal to restaurant,
and then I say that restaurant is equal to 500,
I've changed type from a string of text
to now a numeric value.
No big deal.
That's not a problem in Python, okay?
All right.
And then over here, let's talk about this.
Variables have lots of attributes and methods.
We will talk about more of these later.
Think of attributes like things that you could use
to describe the type of data that's there.
For example, if you have a list of data,
if your variable is a list of data,
then that data would be iterable.
You could iterate through it.
So that would be an attribute, right?
If you had a single value,
you can't iterate through a single value.
It's just a single value, right?
But a list you could, so that's an attribute.
And then a method,
think of methods as things that you can do
to modify your data that's stored in that variable.
For example, let's come back over here,
and we'll go to,
so in my variable for dog,
it is a string of text,
and so because it's a string of text,
it will have specific attributes associated with strings.
If I hit dog, and then I hit tab,
it's going to bring up lots of options
for things that are methods that I can apply to it.
For example, I could do capitalize, right?
And what it's going to do
is it's going to capitalize the word
if it wasn't capitalized.
Or I could do upper.
All right, let's try upper.
What does upper do?
So we're going to make a new variable,
new dog, right,
is equal to dog, our previous variable,
with this method applied to it, upper.
So let's see what happens when we run it.
When we run that new dog, see what it did?
The previous dog was named Scout,
and the S was capitalized because that's her name,
but this time it capitalized everything.
So the upper function must automatically go through
and change all your variables from lowercase to uppercase, right?
So that's an example of a method.
Those become really useful
because a lot of these things would be painstaking
to type out by hand to make those changes manually,
but we have methods that make it really easy
for them to do for us, okay?
So those are methods.
There's something called duck typing,
which is kind of interesting.
And when you're working with different variables
of different types, like maybe one's a list,
and one's a number, and one's a, you know, whatever,
different types,
duck typing comes from the phrase,
if it walks like a duck, if it quacks like a duck,
then it's a duck.
You don't have to necessarily know exactly what type of variable it is,
as long as if they share a certain attribute,
then you can make them do something together,
like if they're iterable, for example.
More on that later.
Then the last thing I want to say before we move on from this video
is that you can do something cool called importing modules in Python.
So what's a module?
Over here, we've been typing this in the temporary.py,
temp.py file.
So if I was to save this,
let's say that I built, like, a really cool function in here
for the quadratic function,
and then every time I'm going to do math,
I want to remember to build that function again,
what's great is I can import this whole temp.py, right?
So in your code, here's an example of it.
Here I did an example of a function that takes,
I called it student name,
and it's inside the module name underscore joiner.py, right?
And what it does is if you give it a first and a last name,
it will put them together with a space between, right?
Now, that's a simple one,
but you can imagine more complicated functions
that you wouldn't want to recreate every time.
Now, in another module, I can import name underscore joiner,
which, again, that was the name of this previous module,
and now I have this function available,
student underscore name.
Here it is.
So I can say some new name.
I'm going to call the name joiner module, right?
And then .student name, that's the function,
and I can send it the two names,
and it will put them together to give the full name.
So that's a trivial example,
but those become really useful later on.
And then lastly, I just put a link here
to some really useful,
it's a reference cheat sheet,
so if you need to remember the syntax
for how you multiply something
or how you comment something out or whatever,
this has a really good cheat sheet
for lots of the Python 3 commands
that you're going to run into.
So with that said,
I think we're now ready to move from semantics
to now our very first Python section,
which is going to be talking about data types.
We'll see that in the next video.

