Home Python Getting started with Python IDLE

Getting started with Python IDLE

Python is well-known programming and scripting language that is widely used. Python scripts and apps can be developed using a variety of tools and technologies.

What is Python IDLE and how does it work?

The acronym IDLE stands for Integrated Development and Learning Environment. IDLE is a primary IDE (Integrated Development Environment) that does not have all of the functionality of a typical IDE. However, IDLE comes with enough capabilities to get you started learning and developing Python. The Python Programming Language community built and developed Python IDLE. So Python IDLE was first released with Python 1.5, which was quite some time ago. It is provided as an optional tool that is not installed by default with Python but may be installed with a few simple steps.

Find out what Python is, what IDLE (Integrated Development and Learning Environment) is, how to install it, and how to use its different features to write Python programs are all covered in this article.

The origins of the term IDLE are similar to that of Python. Guido Van Rossum named Python after the British comedy group Monty Python, whereas IDLE was chosen to honor Eric Idle, a founding member of Monty Python. Since the 01.5.2b1 release, IDLE includes the Python language’s default implementation. Many Linux, Windows, and Mac distributions include it as an optional element of the Python package.

IDLE, as illustrated above, is a very simple and sophisticated IDE designed particularly for novices, and it is highly regarded and recommended for instructional reasons because of its simplicity.

IDLE Features

Python IDLE is a primary and straightforward IDE with less functionality than other Python IDEs. Python IDLE has the following capabilities to make Python programming and testing easier. It has several essential features that we’ll examine deeply and see examples of later in this guide.

  • Operating systems such as Windows, Linux, Ubuntu, and macOS are supported
  • The GUI was created and coded in Python using the Tkinter module
  • The interactive Python shell works in the same way as the Python command-line shell
  • The Python syntax is used to colorize the code
  • Multiple windows can be opened and used
  • Smart indent is a feature that allows you to automatically indent Python code sections, auto-completes keywords, variables, and modules, among other things
  • In a single or several windows and files, search and replace features are available
  • Use breakpoints and moving through the script to find faults and exceptions
  • Syntax highlighting in Python shell
  • Text editor with many windows
  • Autocompletion of code
  • Ingenious indenting
  • Debugging is made easier using program animation and stepping, allowing one line of code to run at a time
  • Breakpoints that don’t go away
  • Finally, there’s the visibility of the call stack

How do I get IDLE to work?

Installing IDLE from the official Python website, as illustrated below, is one option.

This website contains the most recent Python versions for several operating systems, including Windows, Linux, and Mac. It also includes Python docker images, which you may use immediately if you have Docker installed on your system.

Let’s get Python up and running on the Windows operating system.

Installing Python on the Windows Operating System

The Python installation executable comes with IDLE for Windows, which you can download from the Python website. To install Python IDLE on Windows, select the tcl/tk and IDLE features during Python installation. For the installation of the Python IDLE, other features are optional.

Since Python2 is no longer being developed, we’ll install the most recent Python version, 3.8.10

Depending on whether you have a 32-bit or 64-bit operating system, download and run the Windows x86 executable installer file. A window will appear once you run the file, as shown below.

Ensure that the install launcher is selected for all users and that Python3.8.10 is added to the path. Finally, follow the recommended installation procedure.

When the installation is finished, go to the start menu and type idle in the search box. The IDLE can be accessed from the Start menu or program files by typing its name in the box. The IDLE software should now be visible on your computer.

Python IDLE installation on Ubuntu, Debian, and Mint

IDLE is available as a package in deb-based distributions like Ubuntu, Debian, and Mint, among others. Python must be installed to install and execute IDLE; however, it will be installed automatically if Python is not installed during the IDLE installation. Python is divided into two major versions: Python2 and Python3. For these versions, two IDLE installation packages are available.

IDLE for Python 3 can be installed using the idle-python3.8 package, as shown below. However, keep in mind that the package name may change if the Python version is updated to 3.9.

tuts@codeunderscored:~$ sudo apt-get install idle3
Python IDLE installation on Ubuntu
Python IDLE installation on Ubuntu

Using the package idle-python2.7, you can install IDLE for Python2 as follows.

tuts@codeunderscored:~$ sudo apt-get update

tuts@codeunderscored:~$ sudo apt-get install idle-python2.7

From the application menu or the command line, Python IDLE can be launched. The IDLE can be found in the Development Tools section of the XFCE desktop’s application menu.

Use the idle command to start IDLE from the Linux bash or command line, as shown in the example. As a result, IDLE will be launched in Python 3.

$ idle
IDEL Shell 3.8.10
IDEL Shell 3.8.10

Use the idle-python2.7 command from the Linux bash or command line if you have installed Python2 IDLE.

$ idle-python2.7

The Python shell appears on the main screen when IDLE is launched. Also, by default, the IDLE provides information such as the Python interpreter, the build date, the build tool, and the help information.

Download and install Python IDLE for MacOS

Similar to Windows, Python IDLE can be installed on macOS. The IDLE will be included in the Python installation as a feature that can be easily installed. Python3.8 MacOS 64-bit installer in pkg format can be downloaded from the following link- https://www.python.org/downloads/release/python-385/. They are compatible with OS X 10.9 and later. If you need to install an older version of Python, check out Python3.7, Python3.6, and so forth.

Printing Information about the IDLE Version

about idle macOS

The Help->About IDLE menu can be used to print the IDLE version and related information. This section provides information on IDLE installation, Python interpreter version, email for support, Tk version, copyright information tec.

IDLE with Anaconda

Anaconda can also be used to run IDLE. If you already have Anaconda installed, enter the anaconda prompt from the start menu and type idle in the terminal, as seen below.

You will need to get Anaconda from this website because IDLE comes packed with it. Then install Anaconda on your system.

Now that you’ve installed the IDLE software on your systems, let’s look at its features.

How to use IDLE Software Features

Remember that using the IDLE shell to write many lines of code with functions/classes is not recommended. In such circumstances, you can go to IDLE’s File menu and select New File.

The Format, Edit, and Options menus all have options for customizing IDLE.

Go to the Options menu and select Customize idle from the drop-down menu to change the font size.

As illustrated below, you can select any font face, font size, make the type bold, and even adjust the indentation width you prefer.

fonts configurations

This function is provided by IDLE so that everyone can work with a font size that they are comfortable with. However, because your monitor screen size is often modest, you’ll need an IDE that allows you to increase the font size. That’s when IDLE comes in to save the day and give your eyes a break.

Not only can you change the font size and style, but you can also change the indentation, which is set to four spaces by default according to the PEP-8 coding requirements.

IDLE Interactive Shell allows you to run Python scripts

This Python print() function will be run in the IDLE interactive shell. The python shell, similar to an MS-DOS or Linux Bash shell, is available from the IDLE main screen. However, if you want to develop applications, you should use the Python files described below.

print("Hello Code Underscored")
Hello Code Underscored
Hello Code Underscored

We can see that the script will output to the currently active interactive shell, as you can see. To test simple scripts or code, this is extremely helpful.

In IDLE, write your first program

You’ll create a tiny program that imports the NumPy library, defines two arrays, and adds them together. Then, finally, run the code and examine the results in the IDLE shell.

Make sure the NumPy library is installed on your system before attempting to import it. If not, execute pip install NumPy at the command prompt.

import numpy as np

first_array = [14,7]
second_array =[10,90]

arr = np.array([first_array, second_array])
print(np.sum(arr))

IDLE program to sum array elements
IDLE program to sum array elements

Visibility of the Call Stack

You’ll notice a dialog window on the screen for both the print and np.sum() functions while creating the preceding code. The parameters provided to the np.sum() function will be listed in IDLE. IDLE’s call stack is displayed verbosely, which is one of the more essential and less noticeable features.

Go to the Run menu, select the run module option, or press F5 to execute the code above. It should launch the IDLE shell, along with the program’s output.

Python IDLE Customization

There are numerous ways to give Python IDLE a visual style that you prefer. The colors in the Python logo are used to create the default look and feel. If you don’t like the way something looks, you almost always have the option of changing it.

Select Options Configure IDLE from the menu bar to open the customization window. Apply to see a preview of the outcome of a change you want to make. When you’re finished customizing Python IDLE, click OK to save your changes. If you do not want to save your changes, press the Cancel button.

Python IDLE allows you to customize the following five areas:

  • Fonts/Tabs
  • Highlights
  • Keys
  • General
  • Extensions

IDLE Menu Editor

IDLE Menu
IDLE Menu

IDLE’s edit menu contains various general functionality, such as: replace, move to the line, show surrounding parenthesis, undo, redo, find, locate in files, undo, and redo.

The display completions function, which allows you to autocomplete your code, is one feature that sets it apart. Assume you’re working on a large, complex project that requires a significant amount of code. You might find yourself typing the code for an extended period.

IDLE’s display complete function helps you save time by attempting to finish the code for you. In addition, the Python IDLE can autocomplete only functions and classes. You can use the autocompletion option by using the tab key or ctrl+space.

You’ve previously seen the Fonts/Tabs functionality, so let’s have a look at some more choices in configure_idle. The latter can be found in the options menu.

Formatting the Menu

Like the other menus, Format has a few essential features such as indent region, comment out a region, etc.

Let’s have a look at the indented area. In Python, indentation is used to separate a block of code. So, for example, if you wrote a code but forgot to indent it, the indent area feature could come to your rescue.

indentation width

As illustrated above, the indent region tab adds four spaces to the selected lines of code:

read_forwards = input("Enter the sequence:") # it will read a string standard input
read_backwards = read_forwards[::-1] # store the reverse copy of forward in backward
if read_forwards == read_backwards: # check if foward is equal to backward
  print("The sequence is a palindrome")
else:
  print("It is not a palindrome sequence")

Now we’ll save and run the code we just wrote.

The program expected input from the user in all four tests, as evidenced by the output, and the if-else conditions were executed based on the input.

Fonts/Tabs

The first tab lets you customize the font color, font size, and font style. Depending on your operating system, you can change the font to almost any style you want. The font settings window appears as follows:

fonts-configurations
fonts-configurations

You can choose your preferred font by scrolling through the window. (we recommend using a fixed-width font such as Courier New.) Choose a font size that is large enough for you to see clearly. Toggle whether or not all text appears in bold by clicking the checkbox next to Bold.

This window also allows you to change the number of spaces used for each indentation level. It will be set to the PEP 8 standard of four spaces by default. You can adjust this to make the width of your code more or less spread out.

Highlights

Not only can you alter the color of the code syntax here, but you can also change the font color. You can also set idle to work in a dark mode or theme, which is an excellent tool to have when you need to code for more extended periods and need some eye relief.

Highlights
Highlights

It allows you to visually differentiate between the various Python constructs and the data used in your code.

Python IDLE allows you to customize the look of your Python code completely. It comes with three different highlight themes pre-installed:

  • IDLE Day
  • IDLE Night
  • IDLE New

In this window, you can choose from the pre-installed themes or create your custom theme:

Idle is set to work in classic mode by default, but it can be changed to dark mode.

Keyboard shortcuts

This feature allows you to bind different keypresses to different actions. These are essential elements for increasing your overall efficiency when using an IDE. You have the option of creating your keyboard shortcuts or using the defaults.

As shown in the diagram below, the keys are represented in the Action – Key format, which shows you the action executed when a given key or keys are pressed. Though rarely utilized by developers, the keys feature in IDLE makes your life easier, especially when you need to type a lot of code quickly.

Keyboard Shortcuts
Keyboard Shortcuts

Extensions

The fifth tab of the customization window allows you to add Python IDLE extensions. Extensions enable you to add new and exciting features to the editor and interpreter windows. You can get them from the internet and install them in Python IDLE.

Select Options Configure IDLE -> Extensions to see what extensions are installed. There are numerous extensions available on the internet about which you can learn more. Choose your favorites and add them to Python IDLE!

IDLE extensions
IDLE extensions

General

The fourth tab of the customization window is where you can make minor, general changes. The general settings tab appears as follows:

General IDLE settings
General IDLE settings

You can change the size of the window and whether the shell or the file editor opens first when you launch Python IDLE from this menu. However, most of the settings in this window aren’t particularly exciting to change, so you probably won’t need to tinker with them all that much.

Interacting with Python

A “shell” is a type of computer program that allows you to interact with another program or operating system. IDLE allows you to launch a “Python shell,” where you can type commands for Python to execute (or “interpret”). In this section, we’ll use the Python shell to perform some interactive calculations.

At the ‘>>>’ prompt, you can type Python code directly into this shell. It will be executed whenever you enter a complete code fragment. For example, consider the following:

print ("visit www.codeunderscored.com")

and pressing ENTER, the following will be displayed:

visit www.codeunderscored.com
visit www.codeunderscored.com

Do you see what we mean? The bottoms of hanging letters such as ‘g’ or ‘y’ and underscores are not visible in IDLE on some operating systems. If this is the case, go to Options -> Configure IDLE and change the default font size to 9 or 11. This will solve the issue! Next, try typing an underscore (_) instead.

Python includes operators for addition (+), subtraction (-), multiplication (), division (/), modulo ( % ), and power (*). It means you can put them to use right away. You can use a square root in your calculation by either raising something to the power of 0.5 or importing the math module. As a result, IDLE can also function as a calculator. Here are calculation examples:

>>> 6+10
>>> 5**8
>>> math.log(25, 2)
>>> math.cos( 0 )
>>> import math
>>> math.sqrt(16)
Calculations in IDLE
Calculations in IDLE

It is important to note that you only need to run the import command once after starting IDLE. After that, however, you will need to rerun it if you restart the shell because restarting resets everything back to how it was when you first opened IDLE. So don’t worry too much about it for the time being; we’ll go over it more thoroughly!

Creating Scripts in Python

For these first sections, we’ve been using Python interactively. Interactive shell sessions help run multiple commands in a row. However, if you need to chain together a sequence of several commands, as in the previous exercise, it is far more efficient to save them to a file and run the file with IDLE. A script is a collection of Python commands stored in a file. Like a movie script for an actor, the Python script tells the Python interpreter what to do and in what order to do it.

First, we’ll discuss comments, which are lines of text that the Python interpreter ignores but are included to help explain your code to others and remind yourself what your code is supposed to do. Good code necessitates good comments. In addition, Python comments begin with a # sign. The interpreter ignores any text that comes after the # sign. For instance, if you copy and paste the following command into the Python shell, the text following this print statement will not be printed.

print( "This statement is printable.")  # However, this comment is not printed.
comments are not printed
comments are not printed

Note how the color highlighting in the code block causes the comments to appear greyed out. It is the type of convention used by the wiki software we’re using; On the contrary, some people find the comments challenging to read.

Creating a Python script

A new script file can be created by selecting File->New File from the menu bar. To create a new script file directly, we can use the CTRL+N keyboard shortcut.

Next, we’ll see a simple text editor with more menu options. To create a script or code, we need to add it to the file in the following format:

print("code to print velocity at Underscored")

#Initialize velocity 
v = 24.0
#initialize gravity
g = 6.0
# show the height formula
h = v**2/(2*g)
# print the result
print("If v = ", v, " and g = ", g, " then h = ", h,".")

Using the File->Save menu or the CTRL+S keyboard shortcut, we can create a new file. We’ll be asked for the filename we’d like to save, and the default file extension is *.py.

Execute Python Script File

We can execute a Python file by selecting Run->Run Module from the IDLE menu. You can also use the F5 keyboard shortcut. Running a python script file will launch a new interactive shell and execute the contents of the Python script file.

When the script is run, you will see a screen similar to this, with the executed script’s full path and name printed to the interactive shell and the script’s outputs printed to the screen.

run python script in IDLE
run python script in IDLE

Launch the Python script file

IDLE can open a Python Script file by selecting File->Open from the menu. You can also open the file menu by pressing CTRL+O on your keyboard.

In the IDLE, we can navigate to the Python file we want to open and select it. This open file menu will only display Python files that have been configured in the right bottom corner. Python files can have extensions such as.py, py2, or.py3.

The selected python file will be opened in the current editor after clicking the Open button.

Conclusion

You’ve made it to the end of this tutorial, which is a huge accomplishment!

In this tutorial, you’ve learned about Python IDLE, how to install it, and some of its capabilities. It will be much easier for you to begin using IDLE now that you are familiar with its features and possibilities.

In addition, IDLE is the most excellent programming environment for those just starting to learn Python and want to get their hands dirty by coding in it.

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More