Python Tutorial 2 – Basic Calculator [w/ subs]



00:00:00 – hello guys and welcome to my second
00:00:02 – python tutorial today we're going to be
00:00:05 – making a calculator and it really
00:00:08 – expands on the thing some of the things
00:00:11 – that we learned in in my first video so
00:00:13 – if you haven't watched that i recommend
00:00:16 – strongly recommend that you do just to
00:00:18 – keep up the speed if you're whether
00:00:20 – you're a brand new programmer or I mean
00:00:24 – if you know a little bit about Python
00:00:26 – you'll probably be good but anyways i'm
00:00:29 – just going to jump into it so what we're
00:00:32 – going to do is we're going to define
00:00:33 – some functions and where they're
00:00:36 – actually going to have parameters to
00:00:38 – these functions so I'm going to teach
00:00:40 – you guys how to use functions with
00:00:42 – parameters we're going to do we're going
00:00:45 – to define our add function and it's
00:00:48 – going to take the parameters X & Y and
00:00:51 – what these are you can just think of
00:00:53 – these as placeholders for this program
00:00:55 – they're really just they're really just
00:00:59 – meant so that you can have something
00:01:01 – underneath that they're meant to hold
00:01:02 – the place of X&Y so like for example i
00:01:06 – know that was kind of a poor explanation
00:01:08 – but when we call ad we can pass the
00:01:11 – numbers one and three and it should
00:01:15 – print for okay i don't know if that
00:01:19 – makes a little bit more sense but one
00:01:22 – and three it will it will go down to
00:01:24 – here and it'll plug this one where the X
00:01:27 – is because that's holding the X place
00:01:29 – right here and then why is going to be 3
00:01:33 – because it's holding where y is in that
00:01:35 – function so at these X&Y in this case
00:01:38 – are just placeholders well you're
00:01:40 – defining the function
00:01:42 – alright we're going to print explore the
00:01:45 – value of x + y we're going to print new
00:01:47 – line after all of them just to make it
00:01:50 – look nice
00:01:51 – I've gone and set these function up with
00:01:53 – subtract multiply divide so that we have
00:01:58 – are four main operators so jumping into
00:02:02 – main we have 2 i've defined main
00:02:06 – function this one doesn't take any
00:02:07 – parameters we're going to get everything
00:02:09 – we need well with in Maine
00:02:12 – the first thing is a new one if you if
00:02:15 – you are following this series running
00:02:18 – equals true so what this is is there's a
00:02:24 – data type called boolean type that out
00:02:27 – real quick so you can see it's a boolean
00:02:32 – and billions are true or false and they
00:02:37 – help you manipulate the program in the
00:02:41 – way that you want
00:02:42 – it's a way to run things if this
00:02:45 – variable is this kind of think of like a
00:02:47 – toggling like it's either one or zero on
00:02:51 – or off true or false
00:02:52 – that's really about how that's really
00:02:54 – about how true and false are used so
00:03:00 – we've created a variable called running
00:03:02 – and we set it equal to true so the next
00:03:06 – part maybe a little complicated maybe a
00:03:08 – little advanced for some people just
00:03:10 – starting out but try to bear with me one
00:03:14 – thing I want to get into before we
00:03:15 – continue is 11 equal sign is what I call
00:03:22 – it's called identity it sets something
00:03:26 – equal to something else so running
00:03:28 – equals true for the rest of this program
00:03:31 – while we say running or when we say
00:03:33 – running that is true the program season
00:03:36 – is true this next line we say while
00:03:40 – running 2 equals and then true that says
00:03:45 – while running is equal to true because
00:03:49 – the value of running can change to false
00:03:53 – so we're going to loop through all of
00:03:57 – this while running is equal to true so 2
00:04:02 – equals will test a variable like test
00:04:06 – the value of variable protest the
00:04:08 – condition 1 equals is for setting the
00:04:12 – value of something so we're going to
00:04:16 – take this next part here
00:04:18 – ok sorry i'll go over a while loop a
00:04:20 – little bit while just means continue to
00:04:24 – run this while this condition
00:04:26 – isn't that as long as this condition is
00:04:28 – met just keep running keep running keep
00:04:30 – running
00:04:30 – i use running as a variable this could
00:04:34 – be whatever you want i just use running
00:04:36 – because it's really easy to modify and
00:04:38 – it just makes a lot of sense when you
00:04:40 – look at it for me at least
00:04:44 – ok so the first line have you been
00:04:47 – watching the tutorial this will probably
00:04:49 – look familiar from the last one we
00:04:52 – create a variable called a and we set
00:04:55 – that equal to the value of the user
00:04:59 – input converted to a float so we use raw
00:05:03 – input i'll show you what that looks like
00:05:06 – let's say a equals raw input enter first
00:05:17 – number and raw input kind of works like
00:05:20 – the print function where whatever is
00:05:23 – inside the parentheses is going to get
00:05:25 – displayed to the screen that way you can
00:05:27 – give the user some direction but now
00:05:32 – that we have gotten if you think about
00:05:34 – it like this ryan put gets a string and
00:05:36 – saves and were saved that string to a so
00:05:39 – what's the what are we missing we need
00:05:41 – this to be a mathematical data type so
00:05:45 – we're going to convert it to a float
00:05:47 – once again just like the last video so
00:05:50 – that we can get the that we can perform
00:05:53 – mathematical operations with it then
00:05:56 – we're going to do the same thing with be
00:05:57 – right underneath it and i used a and B
00:06:00 – instead of X&Y because i want to show
00:06:03 – you how passing values two functions
00:06:07 – work but I'll get to that in just a
00:06:10 – moment
00:06:11 – print selected operation so here's where
00:06:15 – it comes into a kind of sort of flow of
00:06:21 – our program we have to give the user
00:06:23 – options here so i'll say selection I'm
00:06:26 – created a variable called selection and
00:06:29 – I set that equal to the integer not the
00:06:32 – float remember the float is a number
00:06:36 – with a zero or with I'm not was here
00:06:39 – with a decimal
00:06:40 – going on it and an integer is a whole
00:06:42 – number so we're saying for the user
00:06:46 – we're going to tell them to pick one for
00:06:48 – ad and then this right here this forward
00:06:51 – flesh and that's a new line the special
00:06:53 – special character for Python you can do
00:06:57 – you actually use this within a string
00:06:59 – and it will print the new line and you
00:07:02 – actually get to see what that looks like
00:07:03 – here when we were on it and we do add a
00:07:06 – new line to to subtract then followed by
00:07:09 – a new line 32 x 949 new line 0 to quit
00:07:17 – and then a new line so what this is
00:07:19 – doing is saying alright display
00:07:24 – instruction to the user get that input
00:07:28 – and then convert whatever they input
00:07:29 – into a whole number and we set that all
00:07:34 – of that equal to the value of the
00:07:35 – variable selection the next thing we're
00:07:39 – going to get into is if statements or RS
00:07:42 – statements is this statements one of the
00:07:44 – other if the variable selection is equal
00:07:50 – to when i when i read or when I think of
00:07:54 – two equal signs i always say is equal to
00:07:57 – I think it just kind of its really
00:07:59 – solidified that I'm too weary to equal
00:08:02 – sign is testing variable I can't tell
00:08:04 – you how many times that I've missed
00:08:06 – typed that like when learning and just
00:08:08 – made mistakes that way so i would
00:08:10 – recommend thinking of running equals
00:08:13 – true or and then if selection is equal
00:08:17 – to 1
00:08:18 – I don't know if that if you kind of hear
00:08:20 – the difference your kind of feel out the
00:08:21 – difference like I do but that's how it
00:08:23 – made sense to me
00:08:25 – alright this is where were it's if a
00:08:29 – condition is met which is selection
00:08:33 – equal to 1 our selection is equal to one
00:08:37 – then do this and this is not like a
00:08:40 – while loop this is only going to run one
00:08:42 – time if only run things one time
00:08:45 – and we're going to go to the next 10
00:08:50 – actually you know what i'll go over this
00:08:52 – so remember up here we created an add
00:08:55 – function and x and y where x and y is
00:08:59 – just a place holder right here we're
00:09:01 – going to do is we're going to call the
00:09:03 – our add function and then we're going to
00:09:06 – pass the values of a and B into it in
00:09:10 – the place of x and y so if the selection
00:09:14 – is equal to one we're going to add a and
00:09:17 – B so it's going to take a and B it's
00:09:19 – okay so here's the values of a and B
00:09:23 – it's going to print the value of a and B
00:09:25 – a plus B and then it's going to print a
00:09:28 – new line and that's what our add
00:09:29 – function is going to do he list is just
00:09:33 – a fancy if statement truthfully he lifts
00:09:37 – means else if if this condition is met
00:09:40 – it's not going to bother going through
00:09:41 – the rest of these because it knows that
00:09:43 – we'd doesn't have to or we've told it
00:09:46 – that it doesn't have to
00:09:47 – that's what else if we have a long block
00:09:50 – like a bunch of things to do for the
00:09:52 – program you can just use else if
00:09:53 – statements and the program on Python
00:09:57 – will know not to do all of them we're
00:10:00 – not to test to see if all of them are
00:10:02 – able to be run so for number two since
00:10:08 – and I'm just following up here what I
00:10:10 – did one is add to subtract so else if
00:10:14 – selection is equal to 2 and we can run
00:10:17 – our subtract function and we're passing
00:10:19 – our a and B values to the X&Y positions
00:10:23 – are doing a minus B in selection is
00:10:28 – three we run our multiply function if
00:10:31 – it's for run are divided function and
00:10:34 – then else if selection is equal to 0
00:10:37 – then running equals false that is equal
00:10:42 – to false it is false running becomes
00:10:45 – false so running equals false or true up
00:10:50 – here right and we say while running is
00:10:52 – true
00:10:53 – while running becomes false so it's
00:10:55 – actually going to break this while loop
00:10:58 – so none of this
00:10:59 – will get executed anymore nothing will
00:11:01 – happen and then it will move on and so
00:11:04 – it was initially start the program we
00:11:07 – call our main function so it starts all
00:11:10 – of this and then because there's a while
00:11:13 – loop with on our main function it's
00:11:15 – going to keep running all of this over
00:11:17 – and over and over before doing this
00:11:19 – before doing our quit function which
00:11:21 – quits python but when we select zero
00:11:26 – down here running becomes false it
00:11:30 – breaks this loop and moves on to the
00:11:33 – next thing so it's going to run our
00:11:36 – quick function and actually quit but
00:11:38 – enough of me talking i'm going to get
00:11:40 – into the program itself and ok so the
00:11:49 – first thing it's asking us to do is
00:11:51 – enter the first number and this is
00:11:53 – within our main function this is our a
00:11:56 – equals float raw input enter first
00:11:59 – number so this is says hey enter the
00:12:02 – first number for us and then we'll
00:12:04 – decide what to do with it so that has
00:12:07 – become a float and has been saved to the
00:12:10 – value a we do this one to five and then
00:12:15 – enter this is then say this has been
00:12:18 – converted to a float and then save to
00:12:20 – the ballot value of B that North select
00:12:23 – an operation this is where our selection
00:12:27 – equals integer raw input 1 add new line
00:12:32 – as you can kind of see how that those
00:12:34 – new lines panned out and then it gives
00:12:37 – us an option to quit
00:12:39 – let's add these to forget that it says
00:12:43 – 10 plus 5 is 15.0 which it is but then
00:12:49 – you'll notice down here it's doing it
00:12:53 – again and that's because we use our
00:12:55 – while that's while that's because
00:12:57 – running is still true
00:12:59 – so it's going to loop through getting
00:13:01 – the input for two different numbers so
00:13:05 – that we can run this again I should
00:13:07 – probably
00:13:08 – 20 and 5 15 again subtract or add 15 i
00:13:15 – just realized we got 15 for both of
00:13:17 – these but 252 and let's divide them
00:13:22 – could not convert string to float
00:13:29 – I think I type something in here let's
00:13:33 – try running this again maybe I made a
00:13:38 – yeah okay I must type like a space or
00:13:44 – something in there because it it was
00:13:48 – saying that we could it could not
00:13:49 – convert a string to float and so I must
00:13:52 – have typed the space in there or
00:13:54 – something because it was trying to take
00:13:56 – a non-numerical character and turn it
00:13:59 – into a float and that that doesn't fly
00:14:02 – in Python but let's make sure our quit
00:14:06 – function works when we hit 0 it's going
00:14:09 – to make running equal to false it's
00:14:12 – going to break out of that while loop
00:14:14 – and then it's going to run the quick
00:14:16 – function right after it and look at that
00:14:20 – it it quit so that's it i hope you guys
00:14:27 – enjoyed the tutorial hopefully you've
00:14:30 – got that she'll after it was after it
00:14:33 – was run a second time and be really
00:14:36 – upset if i have to do all of this again
00:14:38 – of this long time so run through the
00:14:45 – program one more time into the first
00:14:47 – number get the input for a enter the
00:14:50 – second number gets the input for be
00:14:52 – selecting operation and that's the
00:14:55 – selection equals integer Ryan put select
00:14:59 – operation on that if we do divide task
00:15:04 – and then let's just run a quit and then
00:15:10 – I don't think you can see this prompt
00:15:13 – but it asks you to ask me to kill it
00:15:15 – right now do I want to kill it
00:15:17 – yes and that's it


Video Url:
http://youtu.be/aGALk2DngGo

Comments

comments