Quote of AWESOMENESS: “The function of leadership is to produce more leaders, not more followers” ~ Ralph Leader
Functions are so useful and cool at the same time and right now I’m going to teach just how to make one!
But, before we go there, what exactly is a function? It’s somewhat like a pre-made command that you get to customize yourself. For example, you wanted to make five circles, imagine you would have to make each circle repeating how big you wanted it to be, the color, locations, and others. But, if you had a function, you would make it so much simpler in just adding the code and copy-pasting the function.
Here’s the code for a function:
[remember: WHITESPACE is very important in Python]
# This hastag is a comment and won't affect the code # We start making function by defining it with the word def # The thing written inside the parenthesis is called the argument # Remember to put the colon then the indentation will be proper, too def list(alist): # Here, I'm setting the variable a to number 0 a = 0 # This is a for-loop and the i just stands for index for i in alist: if i > 0: a += 1 else: pass print(a) # Here is the actual function in action! # Test section mylist = [2,-4,5,-16,-20] mylist2 = [2,6,8,-4] mylist3 = [-5,-6,-7,-8,10] list(mylist) list(mylist2) list(mylist3)
This is just a simple example of what you can make of a function. Try it out!
Don’t forget to be awesome!
© Elizabeth Anne Villoria