Lesson goal: Dividing by Zero

Previous: Programming as a calculator | Home | Next: Testing divisibility

You probably heard some strange words by your math teacher that you're not supposed to divide by zero. Also, there was probably some controversy in your class over what $\frac{0}{0}$ is equal to. Is this $1$? $0$? (It is neither.)

In this lesson, we'll experiment a little bit with these two oddities. By the way, to fully understand why you cannot divide by zero, you have to pay close attention in calculus class when you get to college. Even then, it can be a tricky concept.

In this lesson, we'll see what the computer says about dividing by zero and trying to compute $0/0$. Simply try two statements here. First, try having the computer divide by zero by enclosing some division by zero calculation in a print statement, perhaps print(4/0). What does the computer show? Next, try displaying the result of $\frac{0}{0}$.

Now you try. Use the print statement to see what the computer outputs for some number divided by $0$, and $\frac{0}{0}$.

Type your code here:

See your results here:

When the computer displays "nan," it means "not a number." When it displays "inf," it means "infinity." Dismiss.
Show a friend, family member, or teacher what you've done!

Here is a share link to your code:

Does your code work? Want to run it on your iPhone?

Here's your code:

  1. Use [Control]-[C] (Windows) or [⌘]-[C] (MacOS) to copy your code.

  2. Paste it using [Control]-[V] (Windows) or [⌘]-[V] (MacOS) into this page

  3. Then click the "Use on iPhone" button that you'll see.