Python Crash Course #3 - Methods & Functions
Key Takeaways
The video covers the basics of Python, focusing on functions and methods, including their definitions, usage, and differences, as well as various tools and techniques for performing actions on data.
Full Transcript
welcome back to the next major part of the introduction to python in this bit we're going to cover functions methods and the return value to get started let's talk about functions and methods those two are very similar both of them are provide useful tools for example if you have words I.E a string a function or a method would allow you to capitalize every letter or get the number of letters if you have numbers both integers and and floats you could for example create an absolute number or select a larger of two numbers also what we have already seen input and print are both functions as a matter of fact let's talk about those two straight in the code editor here's what we finished on in the last part and I want to create a new python file this I do once again in the Explorer I want to right click new file I'm going to call this one functions uncore methods and don't forgetp that way we're getting a new python file and with that I can work in full screen so we don't have any distractions let's get started with functions those we have actually already seen for example print is a function where we can print a value also the input statement where you can ask for a value is also a function this means that you always start with the name of a function and then you have parentheses afterwards usually you are adding a value into these parentheses but that doesn't necessarily have to be the case to be a bit more explicit here a function always works in the same way we start with a function name then we have to call this function which we are doing with parentheses into these parenthesis we can add one or multiple arguments and then the function does a certain thing it could for example ask for user input or could print out a value or could do a host of other things after that in it can return a value although it doesn't necessarily have to for example input is returning a value to use a input but the print statement does not return a value and well that is a function there are quite a few inbuilt ones and later on we are also learning how to create our own custom functions and just to go over it really quickly again for the print function we are simply printing a value which we do with the word print then we are calling parentheses and then we're adding one or M multiple values same with input we have the word input then parentheses and then we're adding one value the major difference in terms of functionality of these two functions is that input returns a value to demonstrate that we can do something slightly weird the input method we can wrap inside of another function which is going to be once again the print statement what that is going to do if I run the code we get ask for a value and there I can write test and then on our next line this test is being printed right away what essentially happened here is that we start by running the input function this one is going to ask for a value and then return whatever the user has entered this return value is then what is being captured by the print function that we have just added which is then what we are seeing at the end via return values you can change together multiple functions easily however what you couldn't be doing is to wrap this print statement or rather this print statement in another print statement if I do that and print a print statement and run the code we are getting none because the print statement doesn't return a value so python telling us that we are well printing none which is what we have gotten from this line can be a bit confusing in the beginning but don't worry too much about it just be aware that some functions return a value While others don't and let me undo the second print statement because that one wouldn't make sense all right with that we have functions that's literally all they do besides that we have methods and those are working very similar compared to functions the only difference is that these are functions of data types meaning that they always have to be attached to a certain kind of value for example what we could be doing let's say I want to print a value that is in all lowercase letters on this I can call a method which I do with Dot and then the method name and in here vs code gives us all of the available methods strings do have quite a few the one method I want to use for now is called upper and this also has to be called like a function but it doesn't need any arguments if I now comment out the stuff we have done for the functions and run the code again we are getting value in all upper case letters that is because of this method it simply looks at the string it was attached to and then makes every letter uppercase we could also create an all uppercase value and then call the method lower on it and once again don't forget the parenthesis and what this one is doing is it turns every single letter into a lowercase one now both of these methods aren't that useful one that is a bit more interesting let me add add once again the word value and another method you could be using is called replace this one if you call it you have to add two arguments one is the letter that you want to replace and the second argument is the letter you want to replace it with for example for this value I want to look for the letter e and replace it with the string three if I now run the code we're getting value except the e at the end end has become a three and that is kind of all you have to know about them they work exactly like a function except now we are calling it with a DOT and then we have the function name and then parenthesis with arguments or without we usually also get a return value although not necessarily I suppose there's one thing you do want to be aware of if I want to print one to three so an integer and then call the upper method on it we would get an error because integers do not have this method it is only available for Strings no other data type has this kind of method as a matter of fact integers don't really have Methods at least none that are useful instead they have a couple of other functions that we haven't covered yet let me put it all the way at the bottom other functions or rather new functions for example what you could be doing is is ABS which stands for the absolute value which turns any number into a positive number meaning if I add -1 in there the result is going to be a one we are getting a one meaning we have turned a negative number into a positive one another function you could be using that works well with numbers is the max function this one always picks the larger number so if we have 10 and five it is going to return the 10 if I run the code we're getting 10 that works perfectly well the counterpart to the max function is the Min function which always picks the lower value if I add zero and one in there we should be getting a zero besides that there's also a function that works really well with strings it is called the Len function which is going to be much more useful later on but for now if you add a string in there with let's say test it is going to give you the number of characters inside of the string in this case four and later on when we learn about lists and dictionaries they are going to give you the items inside of this container so these would be a couple of really basic functions that you could be using inside of python there are loads more and if you are doing research online you can find plenty they are usually fairly straightforward to work with and quite accessible as a matter of fact let's do an exercise where you have to do some research I want you guys to create a Pythagoras theorem calculator this means that once you're running the code it should be asking the user for two numbers the width and the height of a triangle and the output then is going to be the length of the hypotenuse in case you have no idea what that means let me explain in fact let's do all of this in Google I want to look for Pythagoras Theorem all that Pythagoras Theorem is doing is we are looking at a triangle with one right side and we are providing the length of one side the length of the other side and the result is going to be this side the formula to get this side is this one we're getting the first value and squared the second value and squared and then on the resulting value we are taking the square root a really common operation and I'm pretty sure you are going to know it but I wanted to explain it just to make sure that is the kind of value I want you guys to calculate and there's going to be one difficulty and that is that the input function always returns a string even when a number was entered meaning you have to convert a string to an integer for that you would have to do some research see if you can figure it out also as a tip to simplify things a bit if you want to take the square root it is the same as doing the power of 1/2 pause the video now and see how far you get we are in the code and I want to comment out all the stuff I've done so far to have things a bit cleaner what I want to do is Pythagoras Theorem I want to get two variables for the input let's call it site a which I'm getting via input let's call this one the width of the triangle don't forget a space to make the thing look a bit cleaner and then we also want to get site B which we also get via input and this would be the height of the triangle and for now let me simply print side a and print side B if I run the code we are getting the width of the triangle let's say 12 and the height of the triangle I don't know four and we're getting 12 and four and those look look like numbers but they aren't to demonstrate what the problem here is let me try to add a value to whatever we get from site a and simply add a 10 to it if I now run the code again and add 10 and two we're getting an error that you can only concatenate string not into to a string concatenate means that you are joining different strings together which can only be done with two strings you couldn't do it with a string and an in integer I'll talk about this one later for now just be aware that you cannot create a sum of a string and an integer so the issue is that this input is always returning a string no matter what we do we never get a number as a consequence we have to convert this value from a string to an integer how could we do that once again for that let's use Google what you want to look for is python convert string to int as a result we would right away get one method or rather a function called int and if you scroll down you getting lots of websites that talk about this let's have a look at Geeks for geeks also pretty good website and in there if you scroll down you get use the in function and then probably a bit further down you get an example where you get actually two new functions type which gives you the type of the variable you pass into it and then the integer function int is the one we are looking for but let's play around with both actually back in our code I want to get rid of the plus 10 and instead wrap site a when we are printing it into another function called type if I now run the code again we once again are getting asked to provide two sides I'm going to go with 10 and 10 for both and now for the return value the first print is going to print the type of the variable with the user has entered which in this case is going to be S Str short for string and only for the second site so site B we getting the actual value once again understanding return value for this is really important for this type print we always start from the most inner part which in this case is side A this is simply a value that we have gotten from input this we are converting via the type function to the type that we have gotten so in this case string and after that we are using print to print the entire value which is this entire output bit all of these are connected via the return value which is super important to understand as a matter of fact we can add one more layer to it we want to convert site a to an integer and then we get the type and then we are printing all of that and here understanding the return value is fundamental let me run it actually once again again we have to put in two sides 10 and 10 and now we're getting class int short for integer and just to go over it really quickly once again hope it's not too boring we always start with the most inner part in this case side a then we are using the integer method to turn this string into an integer then the inth method is going to return that integer which we are capturing via the type function which is itself going to return a value which is the type of the variable and all of that we are capturing via the print function which is going to print the value and not return anything and with that we're getting the integer also what you could be doing is straight when we are doing the input you could wrap this input into an in function as well which I think is a much better way to approach all of this that way we have site a and site b as numbers right away all we have to figure out now is the hypotenuse and this I want to store in a separate value hypotenuse for this one we need a formula you can see it in the bottom right this is the formula we are trying to emulate inside of python now first of all we have to square both of the values A and B that one you could be doing in two ways you could either get site let's start with site a and then use two asteris with two that way you will get the power of two alternatively let's do this one for site B you could use the power function which is doing the same thing it wants to have two arguments number one in our case is the base value which is going to be site B the second value is going to be the power which in our case would be two and that is basically it both of these approaches are perfectly valid and you see them fairly often after that we have to do one more operation I want to wrap all of this inside of another set of parentheses and then get this square root of that now unfortunately at this point getting the square root directly we cannot do this we will cover later but what we can do is simply take the power of 1 12 which is the same as the square root and well with that we can print the output let's say the hypotenuse is and then we can add the hypotenuse let's try I can now run the code again and let's go with one and one and the hypotenuse is 1.0 which is incorrect also there's a typo should be hypotenuse to double check these values let's have a look at Google because in there you can add the two values one and one and the value should be roughly 1.41 which is not what we got so what went wrong could actually be a really good exercise for you try to understand what what is wrong with this formula there's only a very small change that you have to make and well the order of operation for this part went wrong this has to be in Brackets that way if I run it again I can add one and one and we get 1.41 with lots of other values the Google result was rounded which we can actually also do but first of all the reason why this one didn't work is that the formula that we want to have is a 2 + b 2 and we want to put all of this in Brackets and then take the power of 1/ half of all of that this is not what we did in this formula what we did was a^ 2 + b^ 2 to the power of 1 / 2 without the brackets on this part all that python sees is to the power of one and then we want to divide the entirety of the value by two so having the parenthesis for this part here to make sure that we are taking the entire value as the power value is important okay and just to finish off this video I want to round the value when we are printing the hypothenuse I want to round the value this one wants to have two arguments the first one is the value and then the number of values after the decimal point let's say in this case two if I now run this again I can once again enter one and one and we get 1.41 also I don't need to space after the hypothenuse is because python is doing this automatically so one last time one and one and we get the hypotenuse is 1.41 cool with that we have covered another important part and at this point you can take user input and convert it to another value which is a super useful functionality
Original Description
In this Python crash course tutorial series, you'll learn all the basics of Python from the ground up.
🚀🥷🏼Get access to the Python Crash Course on Net Ninja Pro:
https://netninja.dev/p/python-crash-course
🔥🥷🏼 Access Premium Courses with a Net Ninja Pro subscription:
https://netninja.dev/p/net-ninja-pro/
🔗🥷🏼 Check out Clear Code for more of his own tutorials:
https://www.youtube.com/c/ClearCode
🔗🥷🏼 VS Code - https://code.visualstudio.com/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: Prompt Craft
View skill →Related Reads
📰
📰
📰
📰
How to build a faceless YouTube channel with AI in 30 days
Dev.to AI
10 small AI wins that quietly give you your afternoon back
Dev.to AI
Stop Starting With AI Tools. Start With the Workflow.
Medium · AI
AI Can Deploy Your Infrastructure in Seconds. That’s Exactly What Worries Me.
Medium · AI
🎓
Tutor Explanation
DeepCamp AI