Move from Excel to Python with Pandas Transcripts
Chapter: Appendix: Python language concepts
Lecture: Concept: Creating functions
0:00 You just saw how to call functions.
0:02 Now let's really quickly cover how to create functions.
0:05 Now, I should say right when we get started that there is a lot of flexibility,
0:09 more than most languages in Python functions and methods,
0:12 and so we are just going to scratch the surface here,
0:14 and not really get into all the details.
0:17 So the keyword to define functions is def.
0:20 We always start with def and then some function name
0:23 and regardless whether these are methods in classes or standalone functions,
0:26 def is the keyword and then we say the name,
0:29 and then we have a variety of arguments or if we have no arguments,
0:32 we can just leave this empty.
0:34 But here we have two positional required arguments,
0:37 we could also make these optional by specifying default values,
0:41 we can create what are called named arguments
0:44 where you have to say the name of the argument to pass the value
0:46 instead of using the position.
0:48 We can also take additional extra arguments
0:50 that the method was not necessarily designed for, but, like I said,
0:53 we are not going to dive too deeply into those,
0:55 here is the basic way to define the method-
0:57 def, name, parenthesis arguments and then colon
1:01 to define the block that is the method.
1:03 Here we would probably do something like validate the arguments
1:07 like throw some kind of ValueError or something,
1:09 if name is None or email is None, something like that.
1:12 Then we are going to do our actual logic of the function,
1:15 create it using the database and here we are going to somehow get that information back
1:19 and to this db_user, maybe we want to tell whoever called
1:22 create_user the id of the new user that was just created,
1:25 so we'll use a return value and we'll return the id
1:28 that was the database generated id for when we create this user.