Create your own functions

Python Programming More of Python
8 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€64.90
List Price:  €92.72
You save:  €27.82
£55.48
List Price:  £79.26
You save:  £23.78
CA$94.84
List Price:  CA$135.49
You save:  CA$40.65
A$107.44
List Price:  A$153.49
You save:  A$46.05
S$94.44
List Price:  S$134.92
You save:  S$40.48
HK$547.73
List Price:  HK$782.50
You save:  HK$234.77
CHF 63.11
List Price:  CHF 90.16
You save:  CHF 27.05
NOK kr759.51
List Price:  NOK kr1,085.06
You save:  NOK kr325.55
DKK kr484.31
List Price:  DKK kr691.90
You save:  DKK kr207.59
NZ$117.18
List Price:  NZ$167.41
You save:  NZ$50.23
د.إ257.02
List Price:  د.إ367.19
You save:  د.إ110.16
৳7,686.18
List Price:  ৳10,980.73
You save:  ৳3,294.54
₹5,834.28
List Price:  ₹8,335.04
You save:  ₹2,500.76
RM330.66
List Price:  RM472.40
You save:  RM141.73
₦97,668.24
List Price:  ₦139,532.04
You save:  ₦41,863.80
₨19,468.63
List Price:  ₨27,813.53
You save:  ₨8,344.89
฿2,546.70
List Price:  ฿3,638.31
You save:  ฿1,091.60
₺2,265.91
List Price:  ₺3,237.16
You save:  ₺971.24
B$351.06
List Price:  B$501.53
You save:  B$150.47
R1,322.01
List Price:  R1,888.66
You save:  R566.65
Лв126.52
List Price:  Лв180.76
You save:  Лв54.23
₩94,315.14
List Price:  ₩134,741.70
You save:  ₩40,426.55
₪257.38
List Price:  ₪367.71
You save:  ₪110.32
₱3,931.96
List Price:  ₱5,617.33
You save:  ₱1,685.37
¥10,592.26
List Price:  ¥15,132.45
You save:  ¥4,540.19
MX$1,160.67
List Price:  MX$1,658.17
You save:  MX$497.50
QR255.40
List Price:  QR364.88
You save:  QR109.47
P964.60
List Price:  P1,378.06
You save:  P413.46
KSh9,279.27
List Price:  KSh13,256.67
You save:  KSh3,977.40
E£3,320.75
List Price:  E£4,744.14
You save:  E£1,423.38
ብር3,980.93
List Price:  ብር5,687.29
You save:  ብር1,706.36
Kz58,401.19
List Price:  Kz83,433.85
You save:  Kz25,032.66
CLP$68,800.17
List Price:  CLP$98,290.17
You save:  CLP$29,490
CN¥505.43
List Price:  CN¥722.08
You save:  CN¥216.64
RD$4,147
List Price:  RD$5,924.54
You save:  RD$1,777.54
DA9,435.57
List Price:  DA13,479.97
You save:  DA4,044.39
FJ$157.96
List Price:  FJ$225.66
You save:  FJ$67.70
Q546.07
List Price:  Q780.13
You save:  Q234.06
GY$14,663.88
List Price:  GY$20,949.30
You save:  GY$6,285.42
ISK kr9,758.70
List Price:  ISK kr13,941.60
You save:  ISK kr4,182.90
DH709.58
List Price:  DH1,013.74
You save:  DH304.15
L1,235.01
List Price:  L1,764.37
You save:  L529.36
ден3,991.30
List Price:  ден5,702.11
You save:  ден1,710.80
MOP$564.38
List Price:  MOP$806.29
You save:  MOP$241.91
N$1,336.41
List Price:  N$1,909.24
You save:  N$572.83
C$2,577.43
List Price:  C$3,682.20
You save:  C$1,104.77
रु9,342.84
List Price:  रु13,347.49
You save:  रु4,004.64
S/260.45
List Price:  S/372.08
You save:  S/111.63
K268
List Price:  K382.87
You save:  K114.87
SAR262.59
List Price:  SAR375.15
You save:  SAR112.55
ZK1,736.79
List Price:  ZK2,481.24
You save:  ZK744.44
L322.64
List Price:  L460.94
You save:  L138.29
Kč1,642.49
List Price:  Kč2,346.52
You save:  Kč704.02
Ft25,571.54
List Price:  Ft36,532.34
You save:  Ft10,960.80
SEK kr748.30
List Price:  SEK kr1,069.05
You save:  SEK kr320.74
ARS$60,059.22
List Price:  ARS$85,802.56
You save:  ARS$25,743.34
Bs483.92
List Price:  Bs691.35
You save:  Bs207.42
COP$270,819.36
List Price:  COP$386,901.39
You save:  COP$116,082.02
₡35,222.95
List Price:  ₡50,320.66
You save:  ₡15,097.70
L1,728.74
List Price:  L2,469.73
You save:  L740.99
₲517,758.37
List Price:  ₲739,686.52
You save:  ₲221,928.15
$U2,629.19
List Price:  $U3,756.16
You save:  $U1,126.96
zł279.29
List Price:  zł399
You save:  zł119.71
Already have an account? Log In

Transcript

So until now, what we have done is we have used function of different kinds. In this lesson, we'll learn how we can create custom functions. So functions in Python can be defined as a series of instruction that we do perform specific task. Now we use function when we want to simplify certain operations. Also functions allow us to shorten code and avoid repeating the same thing over and over again. Now let us see how to create your custom function.

So first, let us do a simple operation. That is I'll write x equal to 20 y equal to 10. And now I'll write x plus y. So this will give me the result 30. Now, so to perform the operation, we need to write these three lines of code. Now if we want to change this values, we need to run this again position or run this three lines of code several times.

Now if we know that we have to run this code several times, then what we can do is so we can use, we can simplify everything define a custom function to in order to create a function, we use a deaf option. So this will use like deaf, then what we have to do is we have to use an alias to the function, for example, or you use at x y. So this is my alias, and then I'll give the output so how I can give the output in this way. After defining the alias, I'll write in a first bracket I'll write x and then comma y. Now come to the next line. So before coming to the next line, we need to give here a colon.

Okay, so then come to the next slide and here you will define What operation you want to perform. So here I want to sum this two. So I'll write print x plus one. So it's done now, so run this through. Now here, x and y represent the parameter of the function. So this x and y are the parameters.

And now you know you print the result here. So now what we'll do is we'll use this custom function. So recall it with the name we assign. So what name we have assigned, we have assigned earliest name and x y. So we'll record it as an x y. Now, within this first bracket, give the value of the parameter.

So here I give 20 comma 10 now run it see it showed me 30. Now whenever you will change the value from here, so 30 comma 20, it will change your total output. So in this When we can create function, now, one of the most important thing that you must know before defining a function is the return function. So, let's see what return do. So I'll use the same thing. So define an x y and then x comma y.

Now here in place of print, I will write return return x plus y, then what it will do it will, it will return a value in the form of a variable that is it will store it will assign this value to something which is called a variable. So let's create a variable so run this. And then here. Let's create a variable in the name of s underscore one. And what s underscore one will store it will store the output of that file. So x y and then provide the value.

Now run it. And now print is one. So it has printed the value 30. So this perform the operation over here created a variable over here and then printed the variable 30 over here. Now to better understand the difference between return and print, let's use this print function in the Define statement. So define and x y x comma y, then write print.

Now write anything over here. So I'm write just some. And next, write return. Okay, no, Ronnie And here in the same way I write is to write add x y, and then write the values. Suppose 10 comma 30. Now run it, see it printed some now if you write the variable name that is a swan, what we have written over here, and then you will run the code or print is one, then you will get the sum of these two values.

So, what happened here is here, it didn't return the value, it just stored the value over here. And now whenever I'm calling this value, it is returning 14. Now, if you just write over here, and just the Alias Name, x y and now write 20, comma 30. Now what it will do is it will print both the thing that is really to return the value as well as It will print the thing that is written in the print statement. So, now, if I right here at x y and then provide three values over here this what it should return, can you tell me So, run it see it is giving you an error, because here I have defined only two parameters x and y and now here I am providing three values. So, it will show you an error now to how to overcome this error, how you can modify your code to overcome this error, so, that as many as number you provide, it will do or it will perform the operation as directed.

So, to overcome this error will assign or will recreate this function so, just write def Now, add x y and here write a comma star b. Now, this star b refers to that as many as numbers I provide give a colon come down and start a for loop. So for i in B now colon and here I will use a plus equal to I return a no return a should be outside follow. So, like this now run this code now provide the value to the parameter So, here I provide suppose one comma two comma three comma four comma five, okay, now run it see it has returned the result perfectly without an error and as many as number you provide here it will be perfect perform the operation as it is directed to do now, this is how the custom function work. So, we use function to repeat several operations suppose we need to perform a particular operation several times.

So we create function to reduce our time and also to simplify the operations. So, first TODAY WE HAVE DONE How to create a simple function using a print function then how to use customize your function using a return function. And then both return and print function and again, how to give any number of value to your function and return data output as required. So, I'll be ending my video over here. Keep practicing See you the next video. Thank you

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.