<–Previous – Conditionals & Loops & Indentation
Methods & Functions
You’re almost done guys, one more lesson to go! Remember if you have any questions about anything that we’ve covered up until now, feel free to comment in any of the posts.
Functions in python are very powerful and allow you to deliver quality code that’s easy to use and is modular. When a function belongs to an object it is called a method. We won’t dwell on that for too long, but just know that I will use the two terms in this tutorial. Code that is modular is easy to use and move around. You can also re-use it and and call it from anywhere in your code. Writing modular and re-usable chunks of code also makes your code less redundant. Theres nothing worse than trying to read through lines and lines of code trying to figure something out, and seeing the same lines of code repeat themselves over and over again. Not fun. Redundancy reduces your code quality, and the maintainability of your code. I can’t stress that enough. Remember, it’s not enough to just know how to write code, but it’s important to know how to write good code.
We will begin our dive into function using a very simple approach and slowly build on the concept.
A function definition starts with the keyword def. Following the def keyword, you specify a function name. Finally, you include function parameters and a colon. Feel free to follow along with you favorite text editor and the command line. Consider this example:
def buildHouse():
print("house built")
In this example, def tells python that you’re about to define a new method. buildHouse is the function name. There are no function input parameters, because nothing is placed inside of the () parenthesis. The colon at the end means, as you learned in the previous lesson, that the lines beneath it need to be indented. Indentation when it comes to methods is important because that’s how you tell python that the function definition is over. Every line of code that is indented one level after that colon belongs to that function and will be executed. In this simple example, all we are doing in this function is printing a string to the console. It’s important to note that if you execute this code as is, nothing will happen. You have to call the function! Try adding this to the end of your python script:
buildHouse()
You should now see this:
house built
Congrats! You’ve written your first python function! Now you can call buildHouse() anywhere in your code and can execute that same block of code as many times as you want.
Start Your Own Work From Home Business – Click Here to Learn How
Here’s an example of a function that has input parameters:
def buildHouse(materials, housesCount):
print("We will build " + str(housesCount) + " houses today." )
print("we will use these materials: ")
print(materials)
buildHouse("wood, brick, cement, nails, paint, drywall", 5)
You will see this output:
We will build 5 houses today.
we will use these materials:
wood, brick, cement, nails, paint, drywall
In this example, materials is the first input parameter, and is given the value “wood, brick, cement, nails, paint, drywall”. housesCount is the second input parameter and is given the integer value 5. Feel free to play around with this function and try different values for these two input parameters.
Lambdas
Python is well known for its ability to get your ideas up and running quickly without much overhead, which is why a lot of people choose it over other programming languages. Lambdas are one of those things about python that give it this fame. In a Lambda, you can essentially define a function that performs a simple function and call it the same way you would any other function. Lambdas are sometimes used to create many different functions quickly without having to define them formally with the def keyword. Here’s an example:
area = lambda l, w : l * w
print(area(10,20))
Can you guess what this lambda does? Yupp, you guessed it, it’s a rectangular area calculator. It multiplies length(l) times width and returns the area(l * w). In this case, it prints 200 to the console because it multiplies 10 times 20. The syntax for Lambda is as follows: <lambdaName> = lambda <input params>: <lambda logic>. lambda here is a keyword, lambdaName can be anything you want it to be, the colon : is required, and lambda logic is all the stuff you want your lambda to do. Just like methods, you can now call your lambda from anywhere in your code as many times as you’d like.
Tutoring Jobs – Work Online From Home As A Tutor
A note from the author
Well we have reached the end my friends. The end of this beginners tutorial that is. I really hope that these short lessons in python have given you the help you need to get started on your journey in python. If you’ve made it this far, I’d like to extend my gratitude to you by offering you a free e-book I’ve written titled, “Work From Home as a Software Developer – Getting Started.” If you’re interested in receiving this free gift, please follow this link.
My Freelance Paycheck – Start Your Career as a Freelancer Today
2 Comments