Python Tutorial for Beginners 8: Functions

Corey Schafer
17 May 201721:47

Summary

TLDRThis educational video script teaches the basics of functions in Python. It explains how functions are defined with the 'def' keyword, can have parameters, and may return values. The script covers the benefits of reusing code with functions, the importance of keeping code DRY (Don't Repeat Yourself), and the concept of function arguments and default values. It also touches on unpacking arguments using * and **, and ends with an example tying together the concepts using Python's standard library.

Takeaways

  • 📚 Functions in programming are blocks of code designed to perform a specific task.
  • 🔑 The 'def' keyword in Python is used to define a function, which groups together instructions.
  • 👤 Functions can have parameters, which are inputs that you can pass to the function when calling it.
  • 🔍 The 'pass' keyword can be used as a placeholder within a function when you don't want it to perform any action yet.
  • 💡 To execute a function in Python, you must include parentheses after the function name.
  • 🔄 Functions help to avoid code repetition by allowing you to reuse the same block of code multiple times.
  • 🔀 The concept of 'DRY' (Don't Repeat Yourself) is emphasized by using functions to keep code concise.
  • 🔔 Functions can return values, which means they can output data after performing an operation.
  • 🔀 Functions can be used to process data and then return the processed data to the caller.
  • 📝 Docstrings are used to document what a function does and are placed right after the function definition.

Q & A

  • What is the purpose of using the 'def' keyword in Python?

    -The 'def' keyword in Python is used to define a function, which is a block of organized, reusable code that is used to perform a single, related action.

  • How do you create a function in Python without any parameters?

    -You create a function without any parameters by using the 'def' keyword followed by the function name and empty parentheses: `def functionName():`.

  • What does the 'pass' keyword do in a Python function?

    -The 'pass' keyword in a Python function is a null operation that does nothing. It is used when a statement is required syntactically but no action is needed.

  • How do you execute a function in Python?

    -You execute a function in Python by calling its name followed by parentheses: `functionName()`.

  • What is the benefit of using functions to reuse code?

    -Using functions to reuse code allows you to make changes in one place instead of multiple places throughout your program, reducing the chance of errors and making your code more maintainable.

  • What does the acronym DRY stand for in programming?

    -DRY stands for 'Don't Repeat Yourself', a principle in software development aimed at reducing repetition of software patterns.

  • How can you make a function return a value in Python?

    -You can make a function return a value in Python by using the 'return' keyword followed by the value you want to return: `return someValue`.

  • What is the significance of the 'None' value when a function does not return anything?

    -If a Python function does not explicitly return a value, it defaults to returning 'None', which is a built-in constant that represents the absence of a value or a null value.

  • How do you pass arguments to a function in Python?

    -You pass arguments to a function in Python by specifying them within parentheses after the function name when calling the function: `functionName(arg1, arg2, ...)`.

  • What is the difference between positional and keyword arguments in Python functions?

    -Positional arguments are passed to a function in the order they are defined, without needing to specify the parameter name. Keyword arguments are passed by naming the parameter followed by the value.

  • How can you accept a variable number of arguments in a Python function?

    -You can accept a variable number of positional arguments using *args and a variable number of keyword arguments using **kwargs in the function definition.

  • What is the purpose of using * and ** in function calls in Python?

    -In function calls, * is used to unpack iterables into positional arguments, and ** is used to unpack dictionaries into keyword arguments.

Outlines

00:00

📚 Introduction to Functions

This paragraph introduces the concept of functions as a way to package instructions to perform a specific task. It explains how to create a function using the 'def' keyword and how to execute it with parentheses. The importance of functions is highlighted by showing how they can be used to avoid repeating code, using a 'hello' function as an example. The paragraph also discusses the use of the 'pass' keyword as a placeholder for future code and the significance of parentheses in executing functions. The concept of functions returning values is introduced, and the idea of reusability and DRY (Don't Repeat Yourself) programming is emphasized.

05:00

🔄 Functions and Reusability

The second paragraph delves deeper into the benefits of functions, particularly in reusing code. It demonstrates how changing text in multiple locations can be cumbersome without functions. The paragraph shows how to update a function to include a print statement and how running the function multiple times can replace repeated code. The concept of 'return' is introduced, explaining how functions can operate on data and pass results to other parts of a program. The idea of treating the return value as data is also discussed, along with an example of chaining methods to a function's return value.

10:01

📝 Passing Arguments to Functions

This paragraph explains how to pass arguments to functions, starting with defining parameters within the function's parentheses. It uses a 'greeting' parameter to customize the function's output. The paragraph differentiates between required and optional arguments, using a 'name' parameter with a default value as an example. The concept of function scope is briefly mentioned, emphasizing that variables within a function do not affect those outside. The importance of maintaining the order of required and optional arguments when calling functions is also highlighted.

15:01

🌟 Advanced Function Arguments

The fourth paragraph covers more advanced topics related to function arguments, such as using *args and **kwargs to accept a variable number of arguments. It demonstrates how these can be used to pass an arbitrary number of positional or keyword arguments to a function. The paragraph also explains how to unpack sequences or dictionaries when passing arguments to a function using the * and ** operators. An example using a 'student_info' function illustrates these concepts, showing how arguments are handled within the function and the importance of adhering to naming conventions for clarity.

20:01

🔍 Tying It All Together

The final paragraph ties together the concepts learned in the video series by examining code from the Python standard library. It discusses a 'month_days' list and a function 'is_leap' that determines if a year is a leap year. The paragraph explains the use of docstrings to document functions and the logic behind leap year calculation. It also presents a 'days_in_month' function that returns the number of days in a given month, considering leap years. The paragraph concludes with a walkthrough of how these functions operate together and how they determine their return values based on the arguments passed.

Mindmap

Keywords

💡Function

A function is a block of code designed to perform a specific task, packaged together for reusability. In the video, the instructor explains how functions are created using the `def` keyword and how they help avoid code repetition. The script uses a simple 'hello' function to demonstrate the importance of functions in programming.

💡Def

In Python, `def` is the keyword used to define a function. The instructor explains how functions are initialized by the `def` keyword followed by the function name and parameters in parentheses. This is essential for declaring new functions in Python.

💡Pass

The `pass` keyword in Python allows you to write a function with no implementation yet. The instructor demonstrates this by creating a placeholder function where the body is temporarily filled with `pass`, allowing the function to exist without throwing errors.

💡Return

The `return` keyword is used to exit a function and pass back a value to where the function was called. In the video, the instructor emphasizes the power of `return` in passing processed data back from a function. This allows functions to provide outputs that can be used in other parts of the code.

💡DRY (Don’t Repeat Yourself)

DRY is a principle in software development that encourages reducing code repetition. The video highlights the benefits of keeping code 'DRY' by using functions. Instead of duplicating similar code in multiple places, you can define a function once and call it wherever needed, improving maintainability.

💡Parameters

Parameters are variables listed inside the parentheses in a function definition. In the video, the instructor explains how functions can take parameters (inputs) to customize behavior. For example, the function `hello` takes a `greeting` parameter to personalize the greeting message.

💡Positional and Keyword Arguments

These are the two ways to pass arguments to a function. Positional arguments are passed by their order, while keyword arguments are passed with a key-value pair. The script discusses using both types of arguments, highlighting flexibility in defining function inputs.

💡Scope

Scope refers to the region of the code where a variable is accessible. In the video, the instructor explains how function parameters and variables inside the function are local in scope, meaning they don't affect variables outside the function unless explicitly returned or modified globally.

💡Star (*) and Double Star (**)

The `*args` and `**kwargs` allow passing a variable number of arguments to a function. The video demonstrates how these are useful for functions that accept multiple positional (`*args`) or keyword arguments (`**kwargs`), allowing for flexible inputs.

💡Standard Library

The Python Standard Library is a collection of modules and functions that come pre-installed with Python. Toward the end of the video, the instructor showcases examples from the standard library, showing how basic understanding of functions can allow you to work with more complex built-in Python tools.

Highlights

Introduction to functions in programming

Definition of a function using the 'def' keyword

Explanation of function parameters

Use of the 'pass' keyword in functions

Executing a function vs. referencing it

Adding code to a function to perform actions

Benefits of functions for code reusability

The DRY principle (Don't Repeat Yourself)

Returning values from functions

Understanding function inputs and outputs

Chaining function return values with methods

Passing arguments to functions

Creating parameters with default values

Required vs. optional arguments in functions

Using *args and **kwargs for arbitrary arguments

Unpacking sequences and dictionaries in function calls

Applying function concepts to Python's standard library

Example of determining leap years with functions

Example of calculating days in a month

Explanation of docstrings in functions

Walkthrough of function execution with arguments

Summary and conclusion of the tutorial

Transcripts

play00:00

hey there how's it going everybody in

play00:01

this video we'll be learning about

play00:03

functions now functions are basically

play00:05

some instructions packaged together that

play00:07

perform a specific task so let's create

play00:10

our first function and see why these are

play00:11

so beneficial Now to create a function

play00:14

we'll use the defa keyword which I

play00:16

believe stands for definition and let's

play00:18

just make a simple function here to get

play00:20

started I'll call this hello Funk now we

play00:23

have parentheses there because that is

play00:25

where our parameters will go when we add

play00:27

those in but we don't have any

play00:28

parameters just yet so that will be

play00:31

empty for now now it is possible to

play00:33

write a function and not have any code

play00:35

in it but we can't leave it completely

play00:37

blank uh but if we want to fill this

play00:40

function in later then we can use this

play00:43

pass keyword and basically that pass

play00:45

keyword is saying that we don't want to

play00:47

do anything with this for now but it

play00:48

won't throw any errors for leaving it

play00:50

blank so if we want to run our function

play00:54

then we can just say hello Funk and put

play00:57

in these parentheses and we need to add

play01:00

those parentheses after the function in

play01:01

order to execute it if we don't have

play01:04

those parentheses there then it'll be

play01:06

equal to the function itself um so let's

play01:09

actually see what that looks like so I'm

play01:11

going to print out that hello function

play01:14

without the parentheses in place uh

play01:16

which means that we're not executing the

play01:18

function so let me run that and we can

play01:20

see when we printed that out that it

play01:22

prints out that this is a function in a

play01:24

certain location in memory but it didn't

play01:27

execute the function so to execute it

play01:29

then we add in these parentheses so now

play01:32

if I run this then now it just gives us

play01:35

none because we're not doing anything

play01:37

with this function yet and it doesn't

play01:39

have a return value so let's go ahead

play01:41

and put some code into our function so

play01:44

first we'll just put in a print

play01:46

statement and we'll just print out some

play01:49

text that says hello function with an

play01:52

exclamation point and now that we're

play01:54

actually running that print statement

play01:55

from within the function we don't need

play01:57

to print out that executed function we

play02:01

can just execute that function and it

play02:03

should run that print statement so we'll

play02:05

run that so we can see that we executed

play02:07

our function here it came within our

play02:10

function and ran our print statement now

play02:12

one benefit of functions is that they

play02:14

allow us to reuse code without repeating

play02:16

ourselves so let's say for example that

play02:18

we had to print out some text in several

play02:20

locations throughout our program so it

play02:23

might look something like this so let me

play02:25

copy this and I'll comment out our uh

play02:27

function execution for now and I'm just

play02:29

going to paste this in about four times

play02:32

so now if we run this then as we expect

play02:35

it prints out our four messages now

play02:38

imagine our boss came to us and told us

play02:40

that uh the text was a little bit off

play02:42

and that we didn't want to have an

play02:43

exclamation point at the end of the

play02:45

string well the way that we have it now

play02:48

we'd have to come in here and change all

play02:50

of those manually so I'd come in and

play02:52

change all these man messages to have a

play02:55

period instead now that was only four

play02:57

changes to make there but in some instan

play03:00

that can be in hundreds of locations in

play03:02

multiple different files so that's the

play03:04

first benefit of functions it allows us

play03:07

to put code with a specific purpose into

play03:09

a single location so instead of printing

play03:12

those four statements uh what we can

play03:15

instead do is run our function four

play03:18

times so I will remove that and

play03:20

uncomment our function and we're going

play03:22

to execute this four different times so

play03:25

now if we run that then we can see that

play03:28

it ran our function four times and

play03:30

executed our print statement four

play03:31

different times but now if our boss came

play03:33

to us and asked us to remove that

play03:35

exclamation point then it doesn't matter

play03:37

if this is spread out over a 100

play03:39

different lines or 100 different

play03:41

locations we can just update it in this

play03:43

one spot so I can change this to a

play03:46

period and now if we run this then we

play03:48

can see that those changes are seen

play03:50

everywhere that the function is called

play03:52

now this is called keeping your code dry

play03:55

which stands for don't repeat yourself

play03:58

it's a common mistake for people new to

play04:00

programming to repeat the same things

play04:01

throughout their code when really they

play04:03

could either put their code into certain

play04:05

variables or functions so that it's in a

play04:08

single location so we saw earlier that

play04:11

since we aren't returning anything from

play04:13

our function uh it was actually equal to

play04:15

none so what does it mean for uh our

play04:19

function to return something now this is

play04:21

where functions become really powerful

play04:23

because it allows us to operate on some

play04:25

data and then pass the result to

play04:27

whatever called our function so instead

play04:29

of of printing this string hello

play04:31

function within here let's instead

play04:34

return this okay so what does this mean

play04:36

exactly this means that when we execute

play04:38

our function it's actually going to be

play04:40

equal to our return value so these

play04:43

executed functions here are actually

play04:45

equal to the string hello function so

play04:48

right now if we run this then it doesn't

play04:51

give us any results because it's just a

play04:53

string that we're not doing anything

play04:55

with but if instead we print this so me

play05:00

print that executed function and if we

play05:03

run that then we can see that it prints

play05:05

out our string so basically think of a

play05:08

function as a machine that takes input

play05:11

and produces a result when you execute a

play05:14

function you can think of it almost like

play05:16

a black box you don't need to know

play05:18

exactly how it's doing what it's doing

play05:20

you're mainly concerned about the input

play05:22

and the return value so in this simple

play05:24

example here we don't have any input and

play05:27

we can see that the return value is a

play05:29

string

play05:30

um now don't get me wrong it's useful to

play05:32

know what a function is doing but when

play05:34

you're first getting started don't get

play05:35

caught up on understanding every detail

play05:38

of what every function does just focus

play05:41

on the input and what's returned so for

play05:44

example when we call the Len function on

play05:47

a string so if I print out Len of this

play05:51

string test if I run this then as we saw

play05:55

in a previous video this just returns an

play05:57

integer that is the number of of

play05:59

characters in our string so we have no

play06:02

idea what the code that produces that

play06:04

result looks like but we do know that we

play06:07

passed in a string and that it returned

play06:09

this integer and we'll see why here in a

play06:11

bit why looking at functions in this way

play06:14

will help you become better when working

play06:16

with python because we can treat the

play06:18

return value just like the data type

play06:21

that it is and understanding this will

play06:23

allow you to chain together some

play06:24

functionality so we know our hello

play06:27

function returns a string so we can

play06:30

treat that executed function just like a

play06:32

string so if we remember back to our

play06:34

string Methods remember that we can

play06:36

uppercase a string with uper so really

play06:40

we can take this executed function and

play06:42

just chain uper onto the end of it so

play06:46

now if we run this now we can see that

play06:49

our executed function returned the

play06:51

string hello function and then we were

play06:53

able to use the string method upper on

play06:56

that returned value to uppercase the

play06:58

string um okay so now let's look at how

play07:01

we can pass arguments to our function

play07:04

and real quick I'm going to remove that

play07:07

uper method so to be able to pass

play07:09

arguments to our function we'll need to

play07:11

create some parameters here within our

play07:13

parentheses so let's say that we wanted

play07:16

to customize the greeting that our

play07:18

function returns so let's create a uh a

play07:21

parameter called greeting and now within

play07:24

our function we'll return a string where

play07:26

we use that greeting instead of of our

play07:30

uh hello text that we had before so now

play07:33

I'll just pass this in with a DOT format

play07:36

so now before we run this we have to

play07:38

pass in that greeting argument when we

play07:41

execute our function if we don't then

play07:43

we'll get an error so actually let's go

play07:45

ahead and run this and see this error so

play07:48

we can see that when we ran that it says

play07:50

that hello Funk is missing one required

play07:54

positional argument greeting so let's

play07:56

pass in that greeting argument to our

play07:58

hello function and to do that we can

play08:01

just pass it in directly here when we

play08:03

call our function so I'm just going to

play08:05

pass in uh high as our string so now if

play08:09

we run this then we can see that when we

play08:11

passed in that string High into our

play08:13

function that it set that greeing

play08:16

variable equal to the string high and

play08:19

then returned the string High function

play08:22

now this greeding variable doesn't

play08:24

affect any variables outside of the

play08:26

function its scope is only local to

play08:29

theun function which is nice because we

play08:31

don't have to worry about it affecting

play08:33

anything we don't want it to affect so

play08:36

and if you want to learn more about

play08:38

python scope then I do have a detailed

play08:40

video going in depth as to how that

play08:42

works exactly and I'll leave a link to

play08:44

that video in the description section

play08:46

below okay so right now this greeting

play08:48

parameter is a required argument and

play08:51

that is because it doesn't have a

play08:53

default value now if we had a default

play08:55

value then it would just fall back to

play08:57

the default value whenever we didn't

play08:59

pass that argument in so let's see an

play09:02

example of this so let's say that we

play09:05

also want to be able to pass a name to

play09:07

our hello function and it'll return a

play09:09

greeting and the name so we can add that

play09:13

to our parameters by putting in a comma

play09:16

here and saying that we also want to

play09:18

accept this name parameter but let's say

play09:21

that if no name is passed in then we

play09:23

want to have a default value of U so we

play09:26

can just say name is equal to U and now

play09:30

let's add that to our return string so

play09:33

I'll put in a comma space and then

play09:36

another placeholder and we'll pass in

play09:39

that name so what this is going to do is

play09:41

it'll return a greeting and a name

play09:43

separated by a comma and a space so if

play09:46

we run this then we can see that even

play09:49

though we didn't pass in a value for the

play09:51

name argument when we executed this

play09:53

function it didn't throw an error and

play09:55

instead Ed the default value that we

play09:58

specified as U but if we want to pass in

play10:01

a value then it will use that value

play10:03

instead so when we execute this function

play10:07

if I was to say name is equal to and

play10:10

we'll say Corey and run that then now we

play10:12

can see that printed out the greeting

play10:14

with the name that we passed in um now

play10:17

your required positional arguments have

play10:19

to come before your keyword arguments

play10:22

now if you try to create a function with

play10:24

those out of order then it's going to

play10:26

give you an error now this is a little

play10:28

more advanced topic topic that tripped a

play10:30

lot of people up but at some point

play10:32

you'll probably run across a function in

play10:34

Python uh that looks something like this

play10:37

so I'll say def student info and you

play10:41

might see something where you see this

play10:43

star args and star star quars and so let

play10:48

me just go ahead and within this new

play10:50

function here I will print out args and

play10:54

I'll also print out quars so let's not

play10:57

really worry about this function name

play10:58

for now it's the arguments that I want

play11:00

to focus on so seeing this star args and

play11:04

star star quars can seem confusing at

play11:06

first but basically all it's doing is

play11:09

allowing us to accept an arbitrary

play11:12

number of positional or keyword

play11:14

arguments so for example let's say that

play11:16

this student info function takes

play11:19

positional arguments that represent the

play11:21

classes that the student is taking plus

play11:24

the keyword arguments passed in will be

play11:26

random information about the student so

play11:29

so you can see in both of those examples

play11:31

we don't know how many of these

play11:33

positional or keyword arguments there

play11:35

will be and that's why we use star args

play11:38

and star star quars and the names don't

play11:41

have to be args and quars but that's a

play11:44

convention that you'll see a lot so it's

play11:46

always good to stick with convention so

play11:48

that people can understand your code so

play11:50

let's call this function with some

play11:52

random values so I'm going to say

play11:54

student info and first we want to pass

play11:57

in some positional arguments of the

play11:59

classes that they're taking so we'll say

play12:01

math and art and now for our keyword

play12:04

arguments we'll pass in some random

play12:06

information about the student so we'll

play12:09

say name is equal to John and age is

play12:12

equal to 22 so now if we run this then

play12:16

we can see that when we printed the args

play12:19

it's actually a tupal with all of our

play12:22

positional arguments and our quars are a

play12:26

dictionary with all of our keyword

play12:29

values so once you have that tupal and

play12:31

that dictionary then you'll be able to

play12:32

do whatever you want with that

play12:34

information now sometimes you might see

play12:36

a function call with arguments using the

play12:39

star or double star now when it's used

play12:42

in that context it will actually unpack

play12:44

a sequence or dictionary and pass those

play12:47

values into the function individually so

play12:50

to see what I mean let's make a list in

play12:52

a dictionary of everything that we just

play12:54

passed into our function and just to

play12:56

clear up some room here I'm going to go

play12:58

ahead and delete the hello function that

play13:00

we started off with so now I'm going to

play13:03

create a list called courses and I'm

play13:06

going to set this equal to math and art

play13:09

that we passed in before and instead of

play13:11

a tupal I'm going to make that a list so

play13:13

now for the student info I'm going to

play13:16

create a dictionary called info and set

play13:19

that equal to those values so now let me

play13:22

get rid of our positional and keyword

play13:24

arguments here so let's say that we

play13:26

wanted to pass all of these courses in

play13:29

as our positional arguments and the info

play13:32

dictionary as our keyword arguments so

play13:35

if we just pass these in as is and I

play13:37

passed in courses and info now if we run

play13:41

this then we can see that this might not

play13:44

be exactly what we thought instead of

play13:46

passing the values in individually and

play13:49

instead passed in the complete list and

play13:52

the complete dictionary as positional

play13:54

arguments so if we use the single star

play13:57

in front of our list and the double star

play14:00

in front of our dictionary then it will

play14:02

actually unpack these values and pass

play14:04

them in individually so basically it

play14:06

will be the equivalent to our previous

play14:09

execution uh where we pass them in

play14:11

individually so to see what I mean let's

play14:13

add a star in front of this courses to

play14:16

unpack those values and a star star in

play14:20

front of our dictionary to unpack those

play14:23

keyword values so now if we run this and

play14:26

we can see that we got what we had

play14:27

before um we can see that when our

play14:30

function prints args it's the values

play14:32

from our list that we unpacked and our

play14:35

quars is equal to the dictionary values

play14:37

that we unpacked now I know that's a

play14:39

little confusing especially to you know

play14:41

get the idea that whenever you're

play14:43

passing these in that it unpacks the

play14:45

values and within here it's for

play14:47

accepting an arbitrary number of

play14:49

positional or keyword values but uh it's

play14:52

a little more advanced of a topic and I

play14:55

know it's confusing but hopefully it

play14:57

makes some sense and you'll be able to

play14:58

better understand understand what's

play14:59

going on if you ever run into something

play15:01

like that okay so lastly I wanted to run

play15:04

through an example that ties together

play15:06

everything we've learned so far in this

play15:07

series of videos so I have some code

play15:10

here in my Snippets file that I'm going

play15:12

to grab real quick and paste into the

play15:15

file that we've been working with so now

play15:18

let me lower this output a little bit so

play15:20

that we can see everything here now

play15:22

these are actually a couple of functions

play15:23

that I grabbed in the python standard

play15:25

Library I modified them very slightly

play15:27

but it's basically the same and I wanted

play15:29

to show that even though we've only gone

play15:31

over the fundamentals we're already able

play15:33

to look at some code from within the

play15:35

standard Library itself and understand

play15:37

what's going on so at the top here we

play15:39

have a list called month days and this

play15:42

has the number of days in each month now

play15:45

the first index here is just a

play15:47

placeholder that's not going to get used

play15:49

um we're only going to be accessing

play15:51

indexes one through 12 since those are

play15:54

the months and then we have a function

play15:56

here called is Leap which determines if

play15:59

a year is a leap year it takes a single

play16:01

argument that is the year that it's

play16:03

checking and we can see that there's

play16:05

this string um after the function

play16:08

definition with three quotes and this is

play16:11

called a dock string and dock strings

play16:13

help document what a function or a class

play16:16

is supposed to do so it's a good

play16:18

practice anytime you write a function to

play16:20

write a doc string that goes along with

play16:21

it explaining what that function is

play16:23

supposed to do now this part here can

play16:26

seem a little intimidating but it's not

play16:28

important that you understand how a leap

play16:30

year is calculated there's not a lot of

play16:32

people who know that off the top of

play16:33

their head um but for various reasons

play16:35

this is how a leap year is calculated

play16:37

and it's not important but you could

play16:39

probably figure out what this uh

play16:41

conditional is doing so we're saying

play16:43

that if the year is divisible by four

play16:46

and uh it's not divisible by 100 or it's

play16:51

divisible by 400 so like I was saying

play16:53

there's a lot of different reasons why

play16:55

uh leap years are determined this way

play16:58

and if you don't know that that's

play16:59

completely fine but this function here

play17:01

is going to return true if a year is a

play17:03

leap year and false if it's a non-leap

play17:05

year and down here we have a days and

play17:08

month function that takes a year and a

play17:11

month as arguments and it'll return the

play17:13

number of days in that month so if we

play17:17

look at how this function works we can

play17:18

see that it first checks if a month is

play17:21

between one and 12 and if it's not then

play17:25

it returns that it's an invalid month

play17:27

and then it checks if the month that

play17:29

we're working with is the second month

play17:31

which would mean that it's February and

play17:33

is a leap year using our function up

play17:36

here at the top then it returns 29 if

play17:39

both of those are true and lastly if it

play17:42

makes it to the end without having

play17:43

returned anything yet then it will index

play17:46

into our month days and list up here at

play17:49

the top and return the value of our

play17:52

month so let's just run through this one

play17:54

time and see how these functions work so

play17:57

outside of both of the functions we're

play18:00

going to go ahead and first use this is

play18:03

Leap Year function so we'll say is Leap

play18:06

2017 so if we run this then it returns

play18:10

false so we ran this function is Leap

play18:13

passed in 2017 as our value and it went

play18:16

through this complicated conditional

play18:17

here and determined that that was false

play18:20

but if we type in 2020 here and run that

play18:24

then we can see that it returns true

play18:26

that 2020 is a leap year but now let's

play18:29

try our days and month function which is

play18:32

going to be a little bit longer of a

play18:34

walkthrough so we'll say days and month

play18:37

and we'll pass in a year so it takes a

play18:40

year first we'll pass in a year of 2017

play18:43

and we'll pass in a month of two which

play18:46

is February now since 2017 is not a leap

play18:49

year then this second month which is

play18:52

February should only have 28 days so if

play18:55

we run this then we can see that we got

play18:57

28 so let's walk through exactly what

play18:59

happened just so we're sure that we

play19:01

understand so we executed our days and

play19:04

month function with our arguments of

play19:06

2017 for the year and two for the month

play19:09

so it comes in uh to our days and month

play19:12

function and it sets this year variable

play19:15

equal to 2017 and this month variable

play19:18

equal to two so let's comment those here

play19:22

just to keep track of them through our

play19:24

walkthr so I'll put a comment for year

play19:27

as 2017 and a comment for month as two

play19:31

so first it checks if our month is not

play19:35

between 1 and 12 our month is two so it

play19:39

is in that range so it doesn't meet this

play19:41

conditional and since it doesn't meet

play19:43

that conditional then we just continue

play19:45

on so our next conditional asks if the

play19:48

month is equal to two and is a leap year

play19:51

so our month is equal to two but this is

play19:55

Leap function runs through its code with

play19:57

the year 200 17 and returns false so

play20:01

since is Leap is false and we're using

play20:04

an and operator then the whole

play20:06

conditional evaluates to false so we

play20:08

move on and lastly it accesses the month

play20:12

days list at this month index and

play20:16

remember that our month is equal to two

play20:19

so it's accessing the second index and

play20:22

if we look up here to our month day list

play20:25

and go to our second index so 0 one two

play20:29

then we can see that that's equal to 28

play20:32

so it should be returning 28 here and

play20:35

finally when we printed out that result

play20:37

28 is what we got as our result now I

play20:40

know that was kind of a long walkthrough

play20:42

but I thought it might be useful to see

play20:44

how these things actually work together

play20:46

and how do you go about determining what

play20:48

a function should return based on the

play20:50

arguments that you pass in okay so I

play20:52

think that is going to do it for this

play20:53

video I hope that now you have a clear

play20:55

understanding of how functions work how

play20:57

we return values and the different ways

play20:59

that we can pass arguments in the next

play21:02

video we'll be learning how to import

play21:04

modules and also learn about some of the

play21:06

useful modules that come in the standard

play21:08

library but if anyone has any questions

play21:10

about what we covered in this video then

play21:12

feel free to ask in the comment section

play21:14

below and I'll do my best to answer

play21:15

those and if you enjoy these tutorials

play21:17

and would like to support them then

play21:18

there are several ways you can do that

play21:20

the easiest way is to Simply like the

play21:21

video and give it a thumbs up and also

play21:23

it's a huge help to share these videos

play21:25

with anyone who you think would find

play21:26

them useful and if you have the means

play21:28

you can contribute through patreon and

play21:29

there's a link to that page in the

play21:30

description section below be sure to

play21:32

subscribe for future videos and thank

play21:34

you all for watching

Rate This

5.0 / 5 (0 votes)

Связанные теги
Python FunctionsCode ReusabilityFunction ParametersReturn ValuesScope ConceptLeetCode TutorialsCode OptimizationProgramming BasicsEducational ContentPython TutorialCode Walkthrough
Вам нужно краткое изложение на английском?