TutorMe homepage
Subjects
PRICING
COURSES
Start Free Trial
Albert H.
Tutor Satisfaction Guarantee
Python Programming
TutorMe
Question:

In Python, there are these @names that sometimes shows up above functions. What are these, and what do they do?

Albert H.

These @names are called decorators. As the name and context may imply, these are meant to decorate functions in Python. Functions are interesting in Python. Functions themselves can receive arguments and return a value. However, they can also be treated as an object - you can make a function an argument, you can return a function, and you can modify its properties as an object! Functions can also be defined within a function as well. Knowing all of this, let's demo a Python decorator: -------- def this_is_a_decorator(function_arg): def decorator_wrapper(): print("This happens before we execute function_arg!") function_arg() print("This happens after we execute function_arg!") return decorator_wrapper def cool_function(): print("Yay! This functions prints.") decorated_cool_function = this_is_a_decorator(cool_function) decorated_cool_function() # Prints: # This happens before we execute function_arg! # Yay! This functions prints. # This happens after we execute function_arg! -------- See what happened? We surrounded our original function with decorator_wrapper, and executed it to achieve our result. This is what we call function decoration. One more thing - what about the @name? Let's make this work! Instead of manually wrapping the function by hand, we can do this: -------- def this_is_a_decorator(function_arg): def decorator_wrapper(): print("This happens before we execute function_arg!") function_arg() print("This happens after we execute function_arg!") return decorator_wrapper @this_is_a_decorator def cool_function(): print("Yay! This functions prints.") cool_function() # Prints: # This happens before we execute function_arg! # Yay! This functions prints. # This happens after we execute function_arg! -------- The @name is simply a shortcut, where name is the decorator function's name. These decorators can be really useful - here's a practical example: -------- def odds_evens_decorator(function_arg): def decorator_wrapper(num): odd_even_str = "Odd Number: " if (num % 2 == 0): odd_even_str="Even Number: " print(odd_even_str) function_arg(num) return decorator_wrapper @odds_evens_decorator def print_num(num): print(num) print_num(3) print_num(4) # Prints: # Odd Number: # 3 # Even Number: # 4 -------- Here, we can print whether the number is even or odd before print_num() actually gets executed. Simply put, if you want to execute something before or after a certain function runs, Python decorators are for you!

Calculus
TutorMe
Question:

In calculus, there's something called the "chain rule". What is it, and how do I use it?

Albert H.

The chain rule is a rule that describes how you can take the derivative of a particular kind of function - a function that has another function inside of it. What does that mean? Let's look at an example! Let's try to take the derivative of this function: $$\frac{\mathrm{d} y}{\mathrm{d} x}=\frac{\mathrm{d}}{\mathrm{d} x}(2x+5)^9$$ Before we start, let's take a quick look at the power rule. The power rule, or $$\frac{\mathrm{d}}{\mathrm{d} x}x^a=ax^{a-1}$$, says that you can take the derivative of a function like $$x^8$$ by taking the $$8$$ and copying it over to the left side, and then decreasing the power by one, like this: $$8x^{8-1}=8x^7$$. Using the power rule, $$a=8$$. Now, let's look at this function. It's tempting to try and use power rule in $$(2x+5)^9$$, but it's not directly possible since we need a pure $$x$$ to be raised to the power, not $$2x+5$$. What can we do to solve this? Well, let's use the chain rule! The chain rule is: $$\frac{\mathrm{d} y}{\mathrm{d} x}=\frac{\mathrm{d} y}{\mathrm{d} u}\frac{\mathrm{d} u}{\mathrm{d} x}$$. What does it mean? It means that in order to take the derivative of a function, we can take the derivative of the main function by a new variable/function $$u$$, and multiply that by the derivative of variable/function $$u$$ against $$x$$. In essence, this rule allows you to pull a few strings to make derivation work. Let's do it! First, let's do a trick: we want to aim for $$x^9$$, right? Our goal for chain rule is to get the function to something easy that we can take the derivative of. Let's create that new variable, $$u$$, and set it to this: $$u=2x+5$$. This is the function that is "inside" our original function! Once we are done, let's replace the $$2x+5$$ with a $$u$$ in the original: $$y=(2x+5)^9=u^9$$. From the chain rule, let's take our first derivative - here, we can use the power rule: $$\frac{\mathrm{d} y}{\mathrm{d} u}=\frac{\mathrm{d}}{\mathrm{d} u}u^9=9u^8$$ Now, let's do the second part of the chain rule - the function/variable u with respect to x: $$\frac{\mathrm{d} u}{\mathrm{d} x}=\frac{\mathrm{d}}{\mathrm{d} x}(2x+5)=2$$ Combining the two parts together, we have: $$\frac{\mathrm{d} y}{\mathrm{d} x}=\frac{\mathrm{d} y}{\mathrm{d} u}\frac{\mathrm{d} u}{\mathrm{d} x}=(9u^8)*2=18u^8$$. However, we still have a $$u$$! To fix that, we need to substitute our $$2x+5$$ value back into $$u$$. Therefore, the final answer is: $$18u^8=18(2x+5)^8$$.

C Programming
TutorMe
Question:

Within C, there is a concept called "pointers". What are "pointers", and how are they useful in the real world?

Albert H.

Send a message explaining your
needs and Albert will reply soon.
Contact Albert