What is TEST-DRIVES: How to USE IPYTHON AND JUPYTER NOTEBOOKS

TEST-DRIVES: USING IPYTHON AND JUPYTER NOTEBOOKS

In this section, you’ll test­drive the IPython interpreter in two modes:

Before reading this section, follow the instructions in the Before You Begin section to install the Anaconda­ Python distribution, which contains the IPython interpreter.

  • In interactive mode, you’ll enter small bits of Python code called snippets and immediately see their results.
  • In script mode, you’ll execute code loaded from a file that has the .py extension (short for Python). Such files are called scripts or programs, and they’re generally longer than the code snippets you’ll use in interactive mode.

Then, you’ll learn how to use the browser­based environment known as the Jupyter Notebook for writing and executing Python code.

Jupyter supports many programming languages by installing their “kernels.” For more information see https://github.com/jupyter/jupyter/wiki/Jupyter­-kernels.

Using IPython Interactive Mode as a Calculator

Let’s use IPython interactive mode to evaluate simple arithmetic expressions.

Entering IPython in Interactive Mode

First, open a command­line window on your system:

  • On macOS, open a Terminal from the Applications folder’s Utilities subfolder.
  • On Windows, open the Anaconda Command Prompt from the start menu.
  • On Linux, open your system’s Terminal or shell (this varies by Linux distribution).

In the command­line window, type ipython, then press Enter (or Return). You’ll see text like the following, this varies by platform and by IPython version:

Click here to view code image

Python 3.7.0 | packaged by JuTT BaDshaH | (default, Dec 10, 2020, 13:11:52)

Type 'copyright', 'credits' or 'license' for more information

IPython 6.5.0 ­­ An enhanced Interactive Python. Type '?'

for help.

In [1]:

The text "In [1]:" is a prompt, indicating that IPython is waiting for your input. You can type ? for help or begin entering snippets, as you’ll do momentarily.

Evaluating Expressions

In interactive mode, you can evaluate expressions:

In [1]: 45 + 72

Out[1]: 117

In [2]:

After you type 45 + 72 and press Enter, IPython reads the snippet, evaluates it and prints its result in Out[1]. Then IPython displays the In [2] prompt to show that it’s waiting for you to enter your second snippet. For each new snippet, IPython adds 1 to the number in the square brackets. Each In [1] prompt in the article indicates that we’ve started a new interactive session. We generally do that for each new section of a chapter.

Let’s evaluate a more complex expression:

Click here to view code image

In [2]: 5 * (12.7 ­ 4) / 2

Out[2]: 21.75

Python uses the asterisk (*) for multiplication and the forward slash (/) for division. As in mathematics, parentheses force the evaluation order, so the parenthesized expression (12.7-4) evaluates first, giving 8.7. Next, 5 * 8.7 evaluates giving 43.5. Then, 43.5 / 2 evaluates, giving the result 21.75, which IPython displays in Out[2]. Whole numbers, like 5, 4 and 2, are called integers. Numbers with decimal points, like 12.7, 43.5 and 21.75, are called floating­point numbers.

Exiting Interactive Mode

To leave interactive mode, you can:

  • Type the exit command at the current In [] prompt and press Enter to exit immediately.
  • Type the key sequence <Ctrl> + d (or <control> + d). This displays the prompt "Do you really want to exit ([y]/n)?". The square brackets around y indicate that it’s the default response—pressing Enter submits the default response and exits.
  • Type <Ctrl> + d (or <control> + d) twice (macOS and Linux only).

Executing a Python Program Using the IPython Interpreter

In this section, you’ll execute a script named RollDieDynamic.py that you’ll write in Chapter 6. The .py extension indicates that the file contains Python source code. The script RollDieDynamic.py simulates rolling a six­sided die. It presents a colorful animated visualization that dynamically graphs the frequencies of each die face.

Changing to This Chapter’s Examples Folder

You’ll find the script in the article’s ch01 source­code folder. In the Before You Begin section you extracted the examples folder to your user account’s Documents folder. Each chapter has a folder containing that chapter’s source code. The folder is named ch##, where ## is a two­-digit chapter number from 01 to 17. First, open your system’s command­-line window. Next, use the cd (“change directory”) command to change to the ch01 folder:

  • On macOS/Linux, type cd ~/Documents/examples/ch01, then press Enter.
  • On Windows, type cd C:\Users\YourAccount\Documents\examples\ch01, then press Enter.

Executing the Script

To execute the script, type the following command at the command line, then press Enter:

ipython RollDieDynamic.py 6000 1

The script displays a window, showing the visualization. The numbers 6000 and 1 tell this script the number of times to roll dice and how many dice to roll each time. In this case, we’ll update the chart 6000 times for 1 die at a time.

For a six­sided die, the values 1 through 6 should each occur with “equal likelihood”—the probability of each is 1/6 or about 16.667%. If we roll a die 6000 times, we’d expect about 1000 of each face. Like coin tossing, die rolling is random, so there could be some faces with fewer than 1000, some with 1000 and some with more than 1000. We took the screen captures below during the script’s execution. This script uses randomly generated die values, so your results will differ. Experiment with the script by changing the value 1 to 100, 1000 and 10000. Notice that as the number of die rolls gets larger, the frequencies zero in on 16.667%. This is a phenomenon of the “Law of Large Numbers.”

BY JuTT BaDshaH

Creating Scripts

Typically, you create your Python source code in an editor that enables you to type text. Using the editor, you type a program, make any necessary corrections and save it to your computer.
Integrated development environments (IDEs) provide tools that support the entire software­-development process, such as editors, debuggers for locating logic errors that cause programs to execute incorrectly and more. Some popular Python IDEs include Spyder (which comes with Anaconda), PyCharm and Visual Studio Code.

Problems That May Occur at Execution Time

Programs often do not work on the first try. For example, an executing program might try to divide by zero (an illegal operation in Python). This would cause the program to display an error message. If this occurred in a script, you’d return to the editor, make the necessary corrections and re­execute the script to determine whether the corrections fixed the problem(s).
Errors such as division by zero occur as a program runs, so they’re called runtime errors or execution­time errors. Fatal runtime errors cause programs to terminate immediately without having successfully performed their jobs. Non­fatal runtime errors allow programs to run to completion, often producing incorrect results.

*

إرسال تعليق (0)
أحدث أقدم