Coding

Python Programming Tutorial #2 – Basic Operators and Input

  • 00:00:00 hey guys so welcome to the second video
  • 00:00:03 in the series um if you remember in the
  • 00:00:06 last video we talked about data types
  • 00:00:08 and variables we want briefly into some
  • 00:00:11 of the things that Python can do today
  • 00:00:13 I'm going to be talking about operators
  • 00:00:15 and we're going to be doing some print
  • 00:00:18 statements some input a little a few
  • 00:00:21 small console applications I'm just
  • 00:00:23 applying some of the knowledge that
  • 00:00:24 we've learned so pretty much the first
  • 00:00:27 thing I want to do is I want to go back
  • 00:00:28 and I want to view what we did in the
  • 00:00:29 last lesson very quickly so we talked
  • 00:00:31 about variables and data types so for
  • 00:00:32 example will be X variable we could set
  • 00:00:34 it to something equal to two like that a
  • 00:00:36 name we made equal to our name right so
  • 00:00:39 I make that equal to Tim so there we go
  • 00:00:42 we have the name right there equal to 10
  • 00:00:44 we'll start with that okay
  • 00:00:46 now if you remember the last one I
  • 00:00:48 didn't talk about this but I used
  • 00:00:49 something called print now the print
  • 00:00:51 statement pretty much takes an argument
  • 00:00:53 so inside these brackets the thing that
  • 00:00:57 is inside that brackets is called the
  • 00:00:58 argument so it takes a string argument
  • 00:01:02 usually so I will give it the name so
  • 00:01:05 now you'll see again we did this in a
  • 00:01:06 lasso I just want a review i'm clicking
  • 00:01:08 f5 simply to run the program when i go
  • 00:01:10 to the console it simply puts tim onto
  • 00:01:13 the console just like that right okay um
  • 00:01:17 very basic we already doing that so now
  • 00:01:19 let's say well we want to do something
  • 00:01:22 else so let's let's print here and we're
  • 00:01:25 just going to put a string and we're
  • 00:01:26 just going to type this in ourselves
  • 00:01:27 let's say hello comma what is your name
  • 00:01:34 that's going to print to the console now
  • 00:01:36 we want to get what the user's name is
  • 00:01:39 now there's a way to do this in Python
  • 00:01:41 it's very simple we're going to make a
  • 00:01:43 variable name it makes sense to put the
  • 00:01:45 variable name here do an equal sign
  • 00:01:48 because it's a variable right we're
  • 00:01:50 going to type the word input just like
  • 00:01:53 this now what's going to happen if I run
  • 00:01:56 the program here I'll show you is going
  • 00:01:59 to say hello what is your name and it
  • 00:02:01 can actually allow me now to type into
  • 00:02:03 the console which I couldn't do before
  • 00:02:05 now nothing happens after when I click
  • 00:02:07 enter because we don't have anything
  • 00:02:08 else after that
  • 00:02:09 but it's allowing me to type okay so now
  • 00:02:14 well we want to print out what the name
  • 00:02:15 is that was said so let's just simply
  • 00:02:17 type print and then name alright and we
  • 00:02:23 have hello what is your name I say oh
  • 00:02:24 well my name is Tim it says Tim all
  • 00:02:27 right now well we just printed out the
  • 00:02:31 name but maybe we want to go a little
  • 00:02:32 more advanced than that we want to say
  • 00:02:34 well hello Tim how are you doing Tim
  • 00:02:36 something like that right so I'm going
  • 00:02:38 to just put a comma here I'm going to
  • 00:02:41 separate these two things and I'm going
  • 00:02:43 to put in a string I'm just going to
  • 00:02:45 type it like the types let me say hello
  • 00:02:46 with a comma and then you see that I
  • 00:02:50 have this other comma here that is
  • 00:02:52 outside of the quotations meaning it's
  • 00:02:53 not a string it's actually just
  • 00:02:54 separating the two arguments in here and
  • 00:02:57 you'll notice what happens now when I
  • 00:02:59 print is it goes hello is your name's I
  • 00:03:02 say oh my name is Jim it says hello
  • 00:03:05 Tim right so let's pretty
  • 00:03:07 straightforward I hope that's easy to
  • 00:03:08 understand we're just using the input to
  • 00:03:10 get an input and then we're printing it
  • 00:03:12 back out to the screen okay so let's do
  • 00:03:15 let's go into the next part of this now
  • 00:03:16 we'll use that again later but let's
  • 00:03:18 talk about operators so operator that
  • 00:03:21 term may sound familiar to you in math
  • 00:03:24 we use things called operators so these
  • 00:03:27 four offers hopefully should look
  • 00:03:28 familiar to you this is a plus sign this
  • 00:03:31 is the addition operator we have the
  • 00:03:34 minus sign which is the subtraction
  • 00:03:35 operator the division sign which is the
  • 00:03:38 well division operator and then the
  • 00:03:40 multiplication sign which is again the
  • 00:03:42 multiplication operator so there's a few
  • 00:03:45 more operators I will talk about but
  • 00:03:47 these are the four basic ones in Python
  • 00:03:50 now you remember how I talked to you
  • 00:03:51 about data types this is where this
  • 00:03:53 comes in when we use these operators
  • 00:03:55 it's important that we use them on
  • 00:03:57 certain data types so for example in
  • 00:04:00 math I could do something like 3 plus 4
  • 00:04:03 right so what is 3 plus 4 equal well
  • 00:04:05 that is equal to 7 so the computer can
  • 00:04:08 actually do that math operation by using
  • 00:04:10 that operator to return this value so
  • 00:04:12 let's let's give an example here quickly
  • 00:04:14 so I have let's do num1 we're going to
  • 00:04:18 use variables here remember I can use a
  • 00:04:20 number in the variable as long as it's
  • 00:04:22 not at the beginning and I'm going to
  • 00:04:23 give it a value of 45 and then we'll do
  • 00:04:25 num2 and we're going to
  • 00:04:27 a value of three now if I want to print
  • 00:04:30 to the console let's say num1 plus num2
  • 00:04:35 well you can guess what that's going to
  • 00:04:37 give us it's going to give us 45 plus 3d
  • 00:04:41 I hope this is not air yeah so that gave
  • 00:04:45 us 48 right so oops didn't mean to make
  • 00:04:48 that fullscreen num1 plus num2 is 48 all
  • 00:04:52 right so now what about – let's try this
  • 00:04:56 42 basic we can do multiplication here
  • 00:05:00 with the multiplication sign there you
  • 00:05:04 go 135 and then we'll try again with the
  • 00:05:07 division sign just to show you all of
  • 00:05:09 them we get 15 point Oh like that okay
  • 00:05:12 so that's pretty straightforward on
  • 00:05:14 those four basic operators now in Python
  • 00:05:18 there's a few more operators that we
  • 00:05:20 want to talk about now for example in
  • 00:05:22 math we have something called exponents
  • 00:05:24 right so how do we do exponents in
  • 00:05:25 Python well it's actually 2 stars is how
  • 00:05:29 you do an exponent all right and now
  • 00:05:31 maybe there's something called integer
  • 00:05:33 division which I'll get into later but
  • 00:05:35 I'll just show you the operated for it
  • 00:05:36 right now it's two slashes that means
  • 00:05:39 I'll give you an example of math example
  • 00:05:41 here 64 divided by let's do 10 would
  • 00:05:47 give us usually in math a value of 6.4
  • 00:05:50 right that's if you use one division
  • 00:05:53 sign sorry what am i doing times I mean
  • 00:05:55 divided by will give us 64
  • 00:05:58 I've 6.4 but now if we do two division
  • 00:06:00 signs here like this it actually gives
  • 00:06:02 us a value of 6 that's because it just
  • 00:06:05 it doesn't worry about the remainder at
  • 00:06:07 all it just tells us how many times 10
  • 00:06:10 can go into 64 and that's all it can go
  • 00:06:14 in 6 times evenly so it gives us a whole
  • 00:06:16 number as our answer that's called
  • 00:06:18 integer division this double double
  • 00:06:21 slash like this okay and then we have
  • 00:06:24 another operator which is actually the
  • 00:06:27 modulus operator so this is the
  • 00:06:30 percentage sign and this gives us B
  • 00:06:32 remainder so here if I do five modulus
  • 00:06:37 two
  • 00:06:38 then the remainder of that is actually 1
  • 00:06:42 because 5 divided by 2 is 4 then with a
  • 00:06:45 remainder of 1 right so it's not going
  • 00:06:47 to give us the decimal point it's again
  • 00:06:48 just going to give us the remainder if I
  • 00:06:49 did 5 divided by 4 again the remainder
  • 00:06:52 is 1 5 divided by 3 the remainder would
  • 00:06:54 be 2 like that ok so that's what the
  • 00:06:56 modulus sign gives us so both modules
  • 00:06:58 here and then there's probably a few
  • 00:07:00 others that I'm forgetting about but we
  • 00:07:03 can go into those later
  • 00:07:05 so now same thing let's keep using our
  • 00:07:08 number of variables here and now let's
  • 00:07:09 introduce a third variable called num 3
  • 00:07:12 now I want to show you how we can use
  • 00:07:15 the variables so num 1 and let's see if
  • 00:07:17 you remember what this operator is so
  • 00:07:20 that's the exponent operator so that
  • 00:07:21 means num 1 which is going to be our
  • 00:07:22 base which is 45 raised to the power of
  • 00:07:26 4 because we're gonna use num2 and we
  • 00:07:28 can print num 3 press ok and you can see
  • 00:07:36 we get a pretty large number that's
  • 00:07:37 because of how exponents work and then
  • 00:07:40 same thing here if we want to do maybe
  • 00:07:43 integer division see what we're gonna
  • 00:07:45 get we had 11 write a whole number and
  • 00:07:49 then say we won't do modulus we get 1
  • 00:07:56 okay so now let's tie all these things
  • 00:07:58 together with the operators that we've
  • 00:08:00 used the variables the prints and the
  • 00:08:02 inputs so um let's start by just get
  • 00:08:06 doing with print statement saying pick a
  • 00:08:09 number
  • 00:08:12 okay pretty basic we're gonna pick a
  • 00:08:14 number and then we're going to take the
  • 00:08:16 inputs we're going to say number 1 again
  • 00:08:17 is equal to the input of that okay so we
  • 00:08:23 got the input a number 1 now let's say
  • 00:08:26 print pick another number
  • 00:08:34 all right so now we're going to pick
  • 00:08:36 another number so we're going to do num2
  • 00:08:37 is equal to input like that and now what
  • 00:08:41 we're going to do is we're gonna
  • 00:08:42 introduce our third variable so we'll
  • 00:08:43 call it sum because we're going to add
  • 00:08:45 these equal to num 1 plus not me – and
  • 00:08:49 now actually you can't use some because
  • 00:08:51 it's a reserved word so let's just do in
  • 00:08:53 all capital sum because remember how we
  • 00:08:55 talked to a variables capitals and
  • 00:08:57 lowercase they're different and then
  • 00:08:58 we're going to print to the console the
  • 00:09:02 sum like that okay so we run the program
  • 00:09:07 sets pick a number let's figure number
  • 00:09:09 let's say 4 and let's do 32 now what do
  • 00:09:12 you think it's going to be oh oops okay
  • 00:09:17 so yeah someone because num1 plus num2
  • 00:09:22 okay so this is why i talked about
  • 00:09:25 datatypes so I'm actually happy this
  • 00:09:27 happened it's because what actually
  • 00:09:28 happens when we get the input of
  • 00:09:30 something is it gives us the type of a
  • 00:09:32 string which in this case is not what we
  • 00:09:36 wanted to do so you saw there I'll run
  • 00:09:39 it again it's a good mistake this
  • 00:09:41 happened actually we have four and we
  • 00:09:43 have a three and it gave us 43 that's
  • 00:09:45 just because we added the string four to
  • 00:09:48 the string three so that simply gave us
  • 00:09:50 43 but now we know an actual math that 4
  • 00:09:52 plus 3 equals 7 so if we want to do the
  • 00:09:55 integers we have to actually convert
  • 00:09:57 these variables into integers so in
  • 00:10:00 order to do that remember I showed you
  • 00:10:01 the keyword int like this before we're
  • 00:10:05 just going to put brackets around our
  • 00:10:07 two variables here like this experience
  • 00:10:10 and now hopefully we should get the
  • 00:10:14 correct answer when we do 4 and we do 3
  • 00:10:16 and you can see we've got 7 so I didn't
  • 00:10:19 mean for that to happen originally but
  • 00:10:20 I'm happy it did because it shows us why
  • 00:10:23 data types are important so when we take
  • 00:10:25 the input of something we're typing it
  • 00:10:27 from the keyboard and that is actually a
  • 00:10:29 string so here we can print again don't
  • 00:10:33 worry about what I'm doing right now but
  • 00:10:34 I just want to show you the type which
  • 00:10:36 is going to give us if it's a string if
  • 00:10:37 it's an integer of num2 just to show you
  • 00:10:39 what the type actually is so we pick a
  • 00:10:43 number we pick 2 and we pick 3 it tells
  • 00:10:46 us that
  • 00:10:47 three is actually a string so that's why
  • 00:10:50 when we added originally three and two
  • 00:10:52 or two and three or whatever it was it
  • 00:10:54 gave us just them added together sub two
  • 00:10:57 and three rather than what it should be
  • 00:10:59 which is five okay
  • 00:11:01 now same thing here if we had a number
  • 00:11:04 and we wanted to convert it into a
  • 00:11:06 string all we would do is we'd type STR
  • 00:11:09 around the numbers so for example three
  • 00:11:11 and then that would give us three just
  • 00:11:15 like that so string so now we've kind of
  • 00:11:18 gone over conversion of variables
  • 00:11:21 conversion of numbers datatypes how to
  • 00:11:23 get input from the console using the
  • 00:11:25 input like this and some basic operators
  • 00:11:28 of Python so view this I hope you
  • 00:11:32 followed along and then in the next
  • 00:11:35 lesson move into some more advanced
  • 00:11:36 operators and we'll maybe start with
  • 00:11:39 conditions okay I hope you enjoyed
  • 00:11:41 please subscribe like the video and I'll
  • 00:11:44 see you in the next video