Module 0.10.0 - Functional Programming

  • a method of programming using funcitons

  • function, a statement that [typically] takes an input, uses that input to perform an operation, and will return some output.

print("Hello, World")
>>> Hello, World

functions are typically structured as a statement, followed by some parenthesis "()"

0.10.1 - Defining and Calling Functions

  • in python, and most programming languages, we can make our own functions

  • in order to do this, we must define them

  • "def" statement, defines function

# defines function
def my_func():
	print("Hello, World!")
  • now we that we have a function defined, we can call it

  • functions are called by placing the name of the function, followed by parenthesis "()"

my_func()
>>> "Hello, World!"
  • functions can be called multiple times

0.10.2 - Working With Arguments

  • functions can take argument, allows for ease of use and optimal code

  • argument, data that is inputted into a function [of some sort]

    • arguments [can] serve as varibles, only available in the functions scope

  • multiple arguments can be used within a function.

  • each argument is seperated by a comma.

0.10.3 - Returning Data

  • functions can return data that can be assigned and used in operations

  • "return" statement, returns data that can be used in operetions

    • the function will stop once the "return" statement is used

  • the function can now be used as an object, argument, or value; because it can be assigned a value

  • the 'double' function above is a pure function, this is because it returns a value that only depends on the arguments attached

    • pure functions are more ideal in the programming world

  • an impure function depends on an external value or datatpype

0.10.4 - Higher-Order Functions

a type of function that utilizes another function as an argument

  • this allows us to make our functions versatile, and add more functionality

  • we can use this same example to output different types of text.

0.10.5 - Special Arguments

  • python has special arguments we can use in our functions

  • defined arguments, assinging default values to arguments

    • in the event that they are not defined, avoids error

  • defining arguments can be done with the assigment operator "="

  • "*" args argument, allows you to pass an undefined amount of arguments to a function, and assigns them to a tuple labled after your args variable name

    • the default is "args"

  • "**" kwargs argument, passes defined arguments into a dictionary labled after your kwarg variable name

    • the default is "kwargs"

  • dictionary key = argument name, dictionary value = argument value

0.10.6 - __main__

  • allows your code to be both imported into other projects and scripts, or can run as is

  • the conditional if __name__ == "__main__:" detects if the code is being ran or imported, respectively

    • the attached expression will run if the code is being ran

  • let's make a file called 'script.py' and run it

  • script.py syntax

  • we can now effeciently import other dictionaries it into another file

  • calculator.py syntax

0.10.7 - Mapping and Filtering

  • "map" function, use each item in a list as an argument to a function

    • the first argument is the function

    • the second argument is the iterable

  • "filter" function, iterates through a list, only returns values that follow a certain argument

    • the first argument is the function

    • the second argument is the iterable

0.10.8 - Lambda

  • a less powerful function that works in one line

    • can take in many arguments but only returns one expression

  • lambda's can take more than one argument

  • lambda's aren't always assigned to varaibles

  • they can be used as arguments for functions