Move from Excel to Python with Pandas Transcripts
Chapter: Appendix: Python language concepts
Lecture: Concept: Creating functions

Login or purchase this course to watch this video and the rest of the course contents.
0:00 You just saw how to call functions. Now let's really quickly cover how to create functions.
0:06 Now, I should say right when we get started that there is a lot of flexibility, more than most languages in Python functions and methods,
0:13 and so we are just going to scratch the surface here, and not really get into all the details. So the keyword to define functions is def.
0:21 We always start with def and then some function name and regardless whether these are methods in classes or standalone functions,
0:27 def is the keyword and then we say the name, and then we have a variety of arguments or if we have no arguments, we can just leave this empty.
0:35 But here we have two positional required arguments, we could also make these optional by specifying default values,
0:42 we can create what are called named arguments where you have to say the name of the argument to pass the value instead of using the position.
0:49 We can also take additional extra arguments that the method was not necessarily designed for, but, like I said,
0:54 we are not going to dive too deeply into those, here is the basic way to define the method- def, name, parenthesis arguments and then colon
1:02 to define the block that is the method. Here we would probably do something like validate the arguments
1:08 like throw some kind of ValueError or something, if name is None or email is None, something like that.
1:13 Then we are going to do our actual logic of the function, create it using the database and here we are going to somehow get that information back
1:20 and to this db_user, maybe we want to tell whoever called create_user the id of the new user that was just created,
1:26 so we'll use a return value and we'll return the id that was the database generated id for when we create this user.


Talk Python's Mastodon Michael Kennedy's Mastodon