TutorMe homepage

SIGN IN

Start Free Trial

Albert H.

Computer Engineering Undergraduate w/Professional Experience

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.

Answer:

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.

Answer:

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.

Answer:

Great question! First, before we dive into the details of what a pointer is, let's talk about a quick scenario. Imagine that you are buying something off of Amazon. You found your item and go to the checkout page. Here, you entered everything but the shipping information. They ask for your "address"... instead of you having to go there, they have the awesome capability of sending the package directly to you. That said, instead of sending it to you by bringing your room to Amazon, they simply just ask for an address. Convenient, right? Pointers are exactly that in C: they are addresses. Specifically, they are addresses to memory locations on the system. Say that you wanted to share a variable with another function within your code, and wanted to allow easy updating of said variable without doing anything too complicated. Instead of trying to send and change the value, you can simply send the function a pointer that "references" your variable, and then the function can "dereference" that pointer to see what's inside and change it. In the real world, this is a very fast and practical way of sending and retrieving data within your program. These pointers can be extended to various other C concept to build much more complicated applications, like games and word processors, while still maintaining quick speed and lower complexity.

Send a message explaining your

needs and Albert will reply soon.

needs and Albert will reply soon.

Contact Albert

Ready now? Request a lesson.

Start Session

FAQs

What is a lesson?

A lesson is virtual lesson space on our platform where you and a tutor can communicate.
You'll have the option to communicate using video/audio as well as text chat.
You can also upload documents, edit papers in real time and use our cutting-edge virtual whiteboard.

How do I begin a lesson?

If the tutor is currently online, you can click the "Start Session" button above.
If they are offline, you can always send them a message to schedule a lesson.

Who are TutorMe tutors?

Many of our tutors are current college students or recent graduates of top-tier universities
like MIT, Harvard and USC.
TutorMe has thousands of top-quality tutors available to work with you.

Made in California

© 2018 TutorMe.com, Inc.