Skip to main content

Chapter 1: Getting started with Python Language

🖥️ Section 1.6: The Python Interactive Shell (IDLE)

IDLE is the Integrated Development and Learning Environment that comes with Python. It is a basic editor and interpreter that provides a simple way to write and run Python code.

IDLE is the Integrated Development and Learning Environment that comes with Python. It is a basic editor and interpreter that provides a simple way to write and run Python code.

Starting IDLE: To open IDLE, simply type idle in your command line (on Windows, Linux, or macOS). This command opens the IDLE shell, a place where you can type and execute Python code interactively.

IDLE Interface: When you open IDLE, you will see a window with a prompt where you can type Python code. The top menu offers options like File, Edit, Shell, and Help, which you can use to manage files, edit code, control the shell environment, and access help documents.

>>> print("Hello, world!")
Hello, world!

In this example, >>> is the Python prompt indicating that IDLE is ready to accept Python code. The print function outputs a string to the shell, which appears on the next line.

Basic Operations in IDLE

Writing and Running Code: You can type Python code directly at the Python prompt in the IDLE shell and press Enter to execute it. For longer pieces of code, it’s better to use the editor window.

  • Open a New File: Go to File > New File to open a new editing window. Here, you can write scripts that contain multiple lines of code.
  • Run a Script: After writing your script in the new file, save it with a .py extension. Then, run the script by selecting Run > Run Module from the menu or by pressing F5.
# Example of a simple script
number = 5
factorial = 1
for i in range(1, number + 1):
    factorial *= i
print(f"The factorial of {number} is {factorial}")

This script calculates the factorial of a number. The for loop iterates from 1 to the number (inclusive), multiplying the factorial variable by each i in each iteration. Finally, it prints the factorial of the number.

Using the IDLE Shell for Quick Tests and Calculations: The IDLE shell can be used for quick mathematical calculations or to test small code snippets.

>>> 8 * 3.57  # Multiplication
28.56
>>> 10 / 4    # Division, Python 3 performs true division by default
2.5

In these examples, you can perform arithmetic operations directly in the shell and see the results immediately.

Accessing Python Documentation: Within IDLE, you can access help for any Python function by typing help() and passing the function or module as an argument.

>>> help(print)

This command will show the documentation for the print function, explaining how to use it and detailing its parameters.

IDLE is a versatile tool for beginners and those who need a quick way to write and test Python code without setting up a full development environment. It's especially useful for learning, experimentation, and small projects.

For more comprehensive guides and tutorials on using IDLE, consider visiting: