In Python, functions are defined using the 'def' keyword followed by the function name, input parameters, and a colon, with the function body indented and ending with a 'return' statement; lambda functions provide a streamlined alternative for defining simple functions using the syntax 'lambda parameters: expression', which is equivalent to a regular function definition but more concise.
Python Functions and Lambda Functions: A Math-Based Introduction
Added:hello everybody this is going to be a video sequence on python functions this is going to be a great tool that we're going to use over and over again so let's jump in now if you're watching this video you are likely in a math class which means you actually already probably have a good sense of what a function is in a mathematical sense a function in a mathematical sense is a tool where we throw something in do an operation to it and get something out so you throw one number in do some stuff to it out pops a number or more generally you throw in a thing do something to that thing out pops whatever something else in computer programming a function is very much the same idea you throw in a collection of things you apply a bunch of code to those things and out pop some nice result so we're going to do this in a mathematical sense in this particular video but then in subsequent videos we're going to look at slightly more complicated functions that you wouldn't think of as a mathematical function but are definitely programming functions so here's our first one we're going to define the function this cubic guy x to the third plus some stuff and evaluate it at some points so here's how a python function looks i start with the word def for define f of x colon so i would read this as define the function f i'm giving f the name as a function that accepts x as an input so i'm defining x the name of the input and now just like all things python after the colon i'm going to press enter and then i've got a tab here that says what i'm going to do inside that function so so far i've said f is the name x is the name of the thing coming in and now i'm going to return x to the third plus three times x to the second minus five times x plus one so this is just like the idea you learned when you first learned about mathematical functions you throw a thing in that thing's name is x into the thing named f this thing's named f and you return some collection of operations on that thing so i can try it out if i do f of 2.3 just like i would write it on paper ctrl enter there's f of 2.3 of course i can make this look nicer by putting some text in there so i know what oops oh f of 2.3 oh goodness gracious because that's supposed to be the print command that's what i was intending to do sorry so print f of 2.3 is equal to f of 2.3 and it does it for you well what if i wanted to evaluate this function at several places now there's more efficient ways to do this and what i'm about to show you um but that's good that's for a future video for right now i'm just going to mash the keyboard and say let's say i wanted to evaluate this function at those x values whatever for some reason i wanted to do that well i can write a loop so i could say 4x in my x values and then i can print i'll make this nicely formatted for x equals x f of x equals f of x now let's think about what this is going to do for little x in that list so it's going to do them one at a time for x is 1 it's going to come down here and say the word for x equals 1 comma space f of x equals and then it will go to this function and evaluate f of 1.
then once it's done one it's going to do 5 then it'll do 67 then it'll do 4 and so on ctrl enter so i can evaluate the function in many different ways and so if i had lots of different function values i wanted out of this function i can just have the program go do it unless relatively convenient okay this form of definition is a python function there's another one so i'm going to squeeze in here a text box a lambda function streamlines the way that you write the definition so we had our function here let me copy this guy there's our function okay we want that function okay so i can do it as f equals now notice this is different than def of f of x f equals lambda x x to the third plus three times x to the second minus five times x plus one okay lambda is it's a little bit of an oddity in python it's just saying okay don't don't get hung up on the word lambda itself it's saying f is a function of x colon and then return this stuff so lambda tells tells python that we are defining a function and we're doing so in kind of a streamlined way so i can still come through and find f of 2.3 if that's what i wanted to do or f of 784 if that's what i wanted to do doesn't matter this is just a streamlined way to define a function which is pretty slick okay so let's do another one oh and by the way i should have said this you should have a collab document open if you haven't yet put me on pause go do it go define these this function in two different ways and play with it make sure that it'll actually work in fact a little exercise for you define this function as a lambda function and make sure that you can run this loop on that same function it should work just fine once you've done that let's go down to this one to find the function g of n is n squared plus n plus 41 then evaluate it for every integer up to n equals 41.
put me on pause and go do this okay now let's go do it i assume that you've got it done you can now see my solution all right so i'm going to say def g of n and i'm going to return n to the second plus n plus 41.
so now i want this for every integer up to and including 41. so for n in range 42 print g of n okay so it gave me let's see here every integer oh so it started at zero yes here we go right so there it is okay now i could have see if i comment those oops that's not what i wanted to do sorry if i comment those out i guess say g is lambda n oops i actually messed that up i am sorry let me go back it's good to see when people are making typos here that's to the second there we go though the reason i kind of paused earlier because i expected different answers and i didn't get them and i didn't see why i forgot to square that number i just multiplied it by 2.
okay so there's all my numbers so now let's define that as a lambda function and make sure it does the same thing so g is lambda n n to the second plus oh my goodness n to the second plus n plus 41.
that gives me the same thing so this is as close as we're going to get in python to saying g is a function of n and returns n to the second plus n plus 41.
okay that's the thing that we would want to write on paper right that's what we want right but this right here is not good programming practice until you know what n is so telling python that you have a variable sitting there this is what a lambda function or a python definition does okay so one more example and then um i don't know why that did that one more example and then i'll pause this video we'll go on to more complicated examples in the next video let's define this function it's got two inputs this time so um capital a and x so i'm going to define f as a function of capital a and x and it's going to return to me oops a times ooh sign sign is under the math library i'm going to need to import math right because i've got a sine function here and now i can evaluate f of see i put the a first so i want one comma math dot pi over three there's one of them f of let's print that guy two comma math dot pi over three you know i'm getting tired of the math dot let's do this from math import sign from math import pi so check this out now i can come in here and do sign and pi without having to worry about calling them from the math library okay and i can very easily evaluate this function at different amplitudes at pi over three okay i'm gonna stop there the next video's got some slightly more interesting function examples
Up Next

Understanding Functors in Scala: A Practical Guide for Functional Programming
@rockthejvm
13.7K views•2021-01-05

BitTorrent Protocol Explained: Piece Selection & Peer Choking
@StevenGordonAU
481 views•2013-02-22

HTTP Requests Explained: GET, POST, PUT, DELETE
@codecademy
103.1K views•2021-10-07

Enigma Machine Mechanics: WWII Encryption Explained
@JaredOwen
13.2M views•2021-12-11
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Computer Science


































