Hello everybody and welcome back to this tutorial. And what we're going to do or
start right now is we're going to start with the basics of Python programming,
such as variables, integers, strings, math operators, comparison
operators, boolean values, and so on and so on. Some of the basic stuff that you
need to know before you actually start coding real programs. Of course, you will
need to be familiar with the variables themselves. You need to know what an integer
is, you need to know what a string is. You need to know how to do simple calculator
operation, such as addition, multiplication, division, exponent, and so
on and so on. We're going to take a look at all of
that in this lecture right here, or basically we're going to take a look at
the math operators in this lecture. And later on we will see more about the
variables themselves. So, first of all, open up your IDLE, which I already have
open right here. You can enlarge it if you want to. I have this print statement
from the previous lecture. Let me just go right here, okay, I will just reopen it so
we have a new and a clean version open. So IDLE 64-bit, here it is. Okay, I will
enlarge this, and we can see that we have a simple
three arrows right here, indicating that we are going to type a
command right here. So, first of all, you can take a look at the IDLE as a big
calculator. So, for example, if you just type here 2, it will print 2. But if you
type 2 + 2 it will print 4. So it basically performs simple addition
with the specified numbers. Of course that can be any other number, such as 2
+ 4 is 6. You can also try to subtract the numbers, so 2 - 3 will
be -1. And we can see that the Python interpreter already knows all of this
basic stuff. So, therefore, we can conclude that
the actual addition, and the actual subtraction, as we can see right here, is
performed same as in the real world. So we can set up variable and set that
variable to be equal to 2 + 2, and then that variable will have a
value of 4. But there are some different things that we also need to consider in
the math operators. So, we saw the addition and the subtraction, the
multiplication is done with this star right here. So if you just type 2*5
you can see it will print out 10. So the multiplication is actually used with
this star sign right here. Now the 2 star signs, what the star signs will do
is, basically, that is called an exponent. So if you just type here 3
to the 3rd, we will get a result of 27. But in order for Python to understand what a
3 to the 3rd degree means, basically, we need to type something like this. So,
3 and then add two **, and then 3 right here. What this will do is it will
perform 3*3 *3, which will give us 27. So, basically, if you just type
here 3**4, which will perform 3*3 *3 *3, so it will
times 3 4 times, and we will get 81. So, that is the exponent. Now let us take a
look at division, since that is also something important. There are different
types of divisions, basically, based on the actual variable types, such as the
float and integer. The integer variable type will be the entire number. So, for
example, if 3 is an integer, but 3.0 is a float value. Because
we have the actual digit behind the actual dot. So it is a decimal number, it
is not an entire whole number. So, once again, the integer is the whole number,
the float value is not. So, let's see how we can actually get
both the integer and both the float value out of a simple division. So if you
just type right here 22 and you divide it by 7 right here, you can see we get
this number right here, which strangely looks something like pi. But, okay, it
starts the same, so 3.14, which I didn't even want that to be, but here it is.
Something funny happened. And you can consider this to be an actual
decimal number. So, if this is not an integer, this is rather a float value. Now
right here in order to get an integer value, of course, you will need to divide the
number with the actual dividable number. So 20/5 is 4.
Now you can see we still get 4.0, that is because you're using a single slash
right here. So, this single slash will always give you, whether the number is
whole or like this 4 right here, or whether it is not like this 3.14 right
here, it will always give you a decimal number as a result. So if you even type 1
divided by one, so 1/1, you will get 1.0
as a result. In order to get an actual integer value without any decimal
behind it, you can just use double slash. So 1// and then
1, and you can see this will only print 1. So make sure to remember that one
slash will give you a decimal value, while two slashes will give you an
integer value. So, it will not only give you an integer value once you perform
the division of the actual dividable numbers. If you, for example, go with the
same number right here, which is 22 divided by 7, you will get 3 as a result.
Oops...I used 22 divided with a single slash, so we need to use double slash
with 7. You can see it will circle the number to the closest
whole number, which will be 3. Of course if the result was something like 3.6
it will circle it to be 4. So let me just try it right here. If you just go
with 7, okay so 25 /7. Okay, so if you just now divide 25 and then two slashes
with 7, okay, so it will always, oh yeah, it will
actually always circle it to the lower number. It doesn't work like in math. So,
for example, if you had 27/7, you can see that the 27 divided by 7 is
3.85. Which if you actually encountered this in math you would circle
it to the whole number, which is the closest, which would be 4. But Python 3
will always circle it to the lower number, which in our case will be 3. So
if you just use 27//7, you can see we still get 3 as an answer.
Now another thing that we need to take a look at in Python 3 is the module, which
is the remaining of the actual division. Now the module is referred to in Python
3 with the %, so this sign is referred to as a module in Python 3.
And what this module will do is it will perform a simple division. So, for example,
if you just use 22, or let me just divide it's normally, so 22/8, you can
see the result of 22 divided by 8 is 2.75. Which means the 8 goes in to 22
2.75 times. But what this will do is, what the module will do, is it
will see how many times does 8 go in to 22. And we can simply perform it in our
mind. We know that 8 goes in 22 two times, which will give 16, and the remaining will
be 6. So 16 + 6 equals 22. So 22%8 will
give 6 as a result, because 22 = 2*8 + 6. So that is the basic
understanding of the actual math operators, those are all that there are.
Now we are going to look at different operators later on such as the
comparison operators, the smaller the larger number, and so on and so on, but
for now on this will be enough. One more thing that you need to know is
which of these actually comes first. Now also something to take a look at is the
actual privileges of these different math operators. So we know
that in math the actual division has more privileges than the actual
subtraction and addition. But in Python 3 the actual biggest privileges
have the brackets. So, for example, as in math if you specify open and closed
brackets, you can see right here I will try something, and you type, for example,
(5+5)*2, the result by math right here should be
20. Since 5+5 is something that we do first which is 10, and then we
times it with 2 which is 20. And if you just press here enter, the result in
Python 3 will be the same. So we know that the actual brackets have the
biggest privileges in Python 3 as well. But right after the brackets if you, for
example, wanted to perform something that didn't have brackets, the privilege
comparison would be something like this. The biggest privileges would have the
exponent, which is these two stars right here. Then after the exponent goes the actual
multiplication. After the multiplication goes the division with a single slash.
We know that division with a single slash will give us a decimal value. After the
division with a single slash comes the division with two slashes, which will
give us the integer value, whether that number is divisible by that actual
number or not. Right after the double slashes go the actual module, or the
percentage sign. And right after it goes the plus and minus, which are the least
privileged math operators in Python, as well as in math as well. So that is
something also that you should take a look at. So, for example, let us give it a
try. Let us try to divide and also multiply numbers at
the same time. If you go 2*2/...
let's do 2. Okay, so we know that
in math, for example, this would be equal to, or not in math in Python 3 we
know now that this will be equal to actually first executing the
multiplication and then the division. So this should be equal to 2 if you press
here enter, and it is. Now this is not looked at as the two times and then one,
since two divided by two is one, this has the most privilege. This star
sign has the most privilege, the multiplication sign has most privilege.
So let us try in reverse, so 2/2*2. You can see the result is the
same. So the order of the signs doesn't really matter. So that is all that I wanted to
show you. I hope you enjoyed this tutorial, and we will continue coding, and we will
continue learning, in the next tutorial. Hope I see you there and take care. Bye!