Learn Python For Beginners 2020 | Tutorial Part #3 |



Learn Python For Beginners 2020 | Tutorial Part #3 | Functions:  functions today we're going to be looking at you guessed it functions so let's get started hi, guys, i'm David and welcome back to our third lesson in pythons for beginners and today we're   going to be looking at functions, first of all, we're going to look at what function actually   is how functions work and then we're going to build a couple of functions and then to   finish it all we're going to build our first little program which is going to be a bitcoin   converter yes with bitcoin buzzing again this week i thought we might as well ride the wave   of the buzz and make a bitcoin for a converter so let's get over to my computer and get started 

 okay as always to get started what i'm going to do is start a new folder we're going to   start a new folder where we're going to open the folder and we're going to go lesson three select folder okay fantastic now we're going to add a file which is going to be you guessed   it main.py if you haven't actually followed along with the other lessons you'll find above   a playlist i put together of getting started on python, okay so we're in our main.dev here and the

last section

 first thing we're going to do is write a comment because as we learned in the last section we know   that it is important to comment so we're going to write learning functions with no variables okay okay to get started what we're going to do is we're going to look at how functions actually   work what they are and how it works a function is a group of reusable code most large programs   have hundreds even thousands of functions inside them which all built in bit further along into   classes but we'll look at that a bit later on so to optimize our code we're actually going   to use functions instead of rewriting the same blocks of code all over the page okay to define a   function what we're going to do is we're going to start with d e f and then we're going to give our   

function a name so it's going to be my underscore one then we're going to put two brackets here and   a dot very much like in the if statement so what we've done here is we've defined the function name   of my function and we've given it two brackets which means there are no variables we'll look at   variables in a minute now okay what we're going to do here we're just going to literally print higher there guys and we're going to do a second line and it's going to print i hope oh wow so you might remark that as in if statements after the function name defined you're actually   indented by four spaces or a tab anything that's indented below this function name  

 is actually part of the function block now if we go down two more lines and then write print i'm great thanks then we save okay now if i go to our terminal here and go new terminal   and go we are already in the right folder so we go python and then we called it main dot py   and then we run it's actually only going to run the i'm great thanks the reason is we actually   haven't actually called the function yet now as i said these two lines here are same part of   the same function as they're indented but this line here is not part of the function because   

it's no longer indented on this line so to call the function all we have to do is go back here   and we write my underscore function one with brackets okay so we'll save again   and we're going to run it again and as you can see everything's been written so hi guys i hope   you're well i'm fine i'm great thanks fantastic so that's what well now as i said functions are   a group of texts that are reusable so we could actually write this 150 times and it would write   the same word same code out 150 times let's just try three hey so if i now   press save and then i press run python.lane as you can see it's written out hi guys i hope   

you're well hi guys i hope you're well hi guys i hope you're well i'm great thanks so it's actually   it's actually reused the same block of code so you don't have to read it right here again   that's why functions are just so important to code you don't want to have to   type out the same block of code which is no good for your computer's memory and no good for you as   a developer imagine scrolling through hundreds of thousands of lines to find or replace a part   of code that you've got duplicated all over the place and with functions you're only going to   have to edit one area that could duplicate the results throughout the whole of your program   with only editing one part of the code okay now let's have a look at passing variables into this   piece of code okay okay to get started what i'm going to do is i'm going to remove these functions  

 here so that they don't show and i'm going to comment out this one here so it doesn't run   remember when we comment something out it is not run by the python script   okay so we're gonna now go and do the same thing you go diff my function and we're gonna do   two and then we're gonna have brackets double dot new line and that's gonna indent it by four spaces   okay so imagine that we actually want to pass through a name instead of saying hi there guys   i hope you're well we're going to say that hi hi there david i hope you're well so to do that   we're going to put name in between the brackets up here and then we're going to write print print and then we're going to write hi there space plus name okay so what's happening here is that we've   got the variable taken from the function and it's added it to our print statement print statement 

Learn Python For Beginners 2020 | Tutorial Part #3 | 

 will now say hi there and the person's name and then we're going to write print i really hope   you are well and then we're gonna add david again or fashion's name okay fantastic right now to run   this we're gonna go down we're going to backspace and then we're going to do a my function 2 to   make sure that we call the function call function2 now don't forget we have to pass through the name okay what's actually going to happen here as we have no name variable we actually have to   define our name variable before we actually call the function so we're going to go here and we're   

going to go david okay fantastic right let's have a look we're gonna clear here so that we don't get   confused and we're gonna run this script i have saved and off we go that's fantastic so it's   actually printed this above here which says i'm great thanks and it says hi there david so we've   passed through david here i hope you're really i hope you are really well david okay so what   if i change the name here and write mike hi there mike i hope you're really well mark okay so we can   like i said before we can actually copy and paste that several times and change the name to jenny   and i'm gonna also comment out this up here very quickly   

oh sorry jenny you have a capital e right jenny so let's run this again okay there you go hi there mike i hope you're really well mike or hi there jenny i hope you're   really well there jenny so as you can see passing variables through to a function is just as easy   that means in your head you can actually start thinking of all of the possibilities that you   could do i mean imagine mike's name or jenny's name was a user input that way that they could   impart their own name and you can do a welcome message and that's just a very simple program   but you've already built your first program okay let's go back and add some more variables   okay so what i was thinking maybe we could do is build a bitcoin converter complete coin as i said   

earlier was it's becoming a bit of a buzz topic so i thought well why not build a quick bitcoin for   the builder okay to do this what we're gonna have to do is we're gonna i'm gonna write this in the   comments is it's going to be a function that takes uh which is going to be called bit coin converter and that function is going to take a value   which is going to be the number of usd in and then it's going to take   the value it's going to take the value and divide it by the current value of bitcoin   

do you see okay so pretty simple really okay well i suppose the best thing we can do is get to it   so as before we're going to do dev and we're going to give our function a name so we're   going to call it bit coin underscore call without and then we're gonna open brackets and add a dot   now as before i said we're gonna need a value so we're gonna write and we're gonna write usd here   usd and then we're going to hit enter to start the block code on the block of code for the function   first thing we need to do is give a value to bitcoin so we're going to go b t c equals   and then we're going to put the current value of btc now i've worked out here if we go here one usd   

uh sorry one bitcoin is thirteen thousand seven hundred and well i should have bought some bitcoin   there's these moments where you see it thinking one afternoon buy some bitcoin   okay anyway and then the usd equals usd above so we're then we're going to take converted value underscore value to equals and then we're going to go us d divided by b d c   and what we're going to do with that is actually return it now i know we haven't actually looked   at this keyword before return what happens with return is it actually leaves the function   

goes back to your current code and returns and stocks stores the value into whatever we put   here i'll show you in a second okay so we're going to put returns and we have the value   converted value now what we're going to do here is we're going to do converted   and we're going to go here and we're going to go equals and we're going to copy this here the   name of the function we're going to paste it here the so the return value of the usd divided by btc  

 is actually stored in the converted thanks to the return key hit keyword here so we're going to have   to give a value to usd usd equals 20. okay so again we have the two returned values here so   first thing we're gonna do is print then we're gonna put a nice sentence here so we're gonna go   you can buy and then we're gonna print out print converted value i guess the converted value here   converted and then we're going to write print if you have okay and then we're gonna go print again   and then it's gonna be us usd okay so what's happening here actually we're gonna print out   four uh we're gonna use the print function four times so we're going to print out you can buy   and then it's going to revert with that and then it's going to return the converted amount here 

Python For Beginners

and then it's going we're going to have if you have and then the usd number right okay let's   save and run this there you go fantastic so you can buy if you have 20 usd you can buy 0.00 14   cents of a bitcoin okay i'm not sure you're going to go very far with 20 in bitcoin but   you never know there's plenty of people that spend a small amount of money on bitcoin eight million   okay back to the code now you can see how you can pass values through to a function and you re   reuse that code as many times as you like it might get a bit complicated but i thought let's try   adding some user input here be good if the 20 could actually be inputted inside the terminal   

after clicking so let's try that right first thing that i'm actually going to do is move this   btc value down to here the reason i'm doing this is to show you how you can send several   values through to the same function so i'm going to delete this up here   and very simply what i'm going to do here is add a comma and i'm going to put btc here so you can   pass as many so you can pass as many variables as you like through here just separating them by  

comma now after you've done this you have to put the btc up here as well so you're going to   do comma btc so that value will be the same as it once which is here okay fantastic so   now we're going to save and run that code so we clear and off we go and it works in the same way   right like i said maybe this 20 dollar here should be an input okay so the first thing   we're going to do is print out a message here so we're going to write print hi there how many um right instead of usd equals 20 what we're going to do here is add input if input is a   predefined function inside of python what happens is that you have you can make your pump functions  

but there's also functions like print and input that are actually already inside the python   library so input what actually happens is it waits until the user enters a value right okay so we're   going to open brackets then we're going to put quotation marks and we're going to write USD value and a double dot okay so now we can save python run hi there how many usd do you have so i'm going   to type 35 and it's not going to work because it's a string okay why is that happening then   okay we actually need to convert usc into it into an integer okay i knew this was going to get more   complicated than i thought so very quickly what we're going to do is take u s d equals us 

d and we're going to simply turn it into an i n t which is going to convert it into an integer an integer obviously is a number and not a string   because you can't multiply a string by an integer it has to be both they both have to be integers   okay now that should work we're going to clear and let's go again hi there how many usd value do you   have i'm going gonna be i have 500 and there we go fantastic so it's actually worked really well   we have you can buy and then it's got the value of 0.036   if you have 500 usd brilliant let's try this again pretty proud of this actually   uh i have 100 000 usd okay i can now buy a 7 bitcoins fantastic that's great congratulations   you can pat yourself on the back you just built your first python program with user input   

within three days we've gone from knowing zero python cloud into actually building our first   program tomorrow we're going to be looking into loops to see how you can loop through   information or numbers so i hope you enjoyed today if you did please give me a big thumbs   up and subscribe for further videos don't forget to put that little bell on so you'll be notified   for any future videos i do a video every day this week it's going to be completely python week   sometimes they're going to be videos on ethical hacking or and other times we're going to be   doing programming so i hope you enjoyed it as i said have a great day cheers bye