As one of the world‘s most popular programming languages used by millions of developers across the globe, Python stands out for its easy-to-read syntax, vast standard library, and huge collection of open source packages.

Python continues to grow rapidly year after year. The latest major version, Python 3, introduces a number of language enhancements over its predecessor Python 2, while maintaining near full compatibility.

For those looking to learn Python programming on an Ubuntu desktop, Python 3 comes readily available in the Ubuntu repositories. The IDLE integrated development environment also installs with a single command, offering new programmers an excellent tool for getting started.

In this comprehensive, step-by-step guide, you‘ll learn:

  • How to install the latest Python 3 release on Ubuntu 18.04 LTS
  • How to customize and use the beginner-friendly IDLE editor
  • Best practices for structuring Python code and setting up development environments

So if you‘re ready to install Python 3 and write your first program on Ubuntu, let‘s get started!

Step 1 – Update Package Repository

Keeping the local package repository up-to-date ensures that all of the latest available Python packages will be accessible for installation.

Open a new terminal window or session and run the following apt command:

sudo apt update

Enter your account password when prompted.

After a few seconds, apt will refresh the package index with the latest upgrades and additions from all configured software channels.

Terminal window showing apt package repository update in progress on Ubuntu 18.04

With the repositories refreshed, Python 3 and IDLE can now be installed from the Ubuntu package servers.

Step 2 – Install Python 3.6 and Python 3.7

Ubuntu 18.04 ships with Python 3.6 available in the default distribution. However, newer patch versions get frequently added to the repositories.

Python 3.7 introduces a range of new major features like:

  • Data Classes
  • Decimal Literal Support
  • New dict merge operators
  • Async generators and more

So installing Python 3.7 ensures access to all the latest language improvements.

First, use apt to install Python 3.6:

sudo apt install python3.6

Accept any prompts to proceed with the installation.

Repeat this for Python 3.7 by running:

sudo apt install python3.7

Terminal showing Python 3.7 installing on Ubuntu with apt

With multiple versions of Python 3 now setup, verify they are available by printing the version string:

For Python 3.6

python3.6 --version

For Python 3.7

python3.7 --version

This will output details on the active Python interpreter, confirm the installation succeeded correctly.

Step 3 – Install the IDLE Development Environment

IDLE provides a user-friendly development environment for writing Python programs. It‘s actively developed and shipped as part of the standard Python distribution.

The key features include:

  • Intuitive GUI with menu shortcuts
  • Python shell prompt for testing code snippets
  • File browser for opening existing scripts
  • Debugging tools for identifying issues
  • Support for rudimentary auto-indenting and text highlighting

IDLE makes it easy for novice programmers to start coding Python without needing to use the command line exclusively.

Install idle3 for the latest Python 3 release with:

sudo apt install idle3

Then for Python 3.6 specifically:

sudo apt install idle-python3.6

Do the same for Python 3.7:

sudo apt install idle-python3.7 

With all the packages installed, IDLE can be accessed from the main menu.

IDLE menu shortcut on Ubuntu desktop

The Python shell should load up:

IDLE Python shell window

From here, access the full graphical editor by going to File > New File to start programming.

The empty code editor window enables creating a new Python script:

IDLE Python code editor interface

With IDLE fully loaded, you can now take advantage of all it‘s features for coding Python on Ubuntu!

Step 4 – Save and Run Your First Program

Let‘s verify everything is correctly setup by walking through a "Hello World" example.

In the IDLE editor, write the following code:

print("Hello World!") 

Save the script with File > Save As and name the file hello.py.

This naming convention of using the .py file extension is standard practice for Python programs.

To execute the program, select Run > Run Module or press F5.

The output from print("Hello World!") should display back in the Python shell:

Hello world program output in IDLE shell

With a working first program tested, you can now start building more elaborate Python scripts leveraging all the strengths IDLE provides!

Customizing IDLE‘s Look and Feel

While usable defaults are set, IDLE does provide a number of customization options including:

  • Changing font sizes and styles
  • Adjusting color themes
  • Setting indentation widths
  • Configuring keybindings

Access the settings via Options > Configure IDLE:

IDLE configuration interface

Try the dark theme for lower contrast:

Dark theme with green text on black background

Or increase font sizes for improved readability:

IDLE using 28 point Ubuntu font

Tweak these and other preferences until you arrive at an editing environment that‘s comfortable and effective.

Following Best Practices

Now that Python 3 and IDLE are setup, what are some best practices to follow when writing scripts?

Here is a short list of high-level guidelines and techniques:

Modular Programming

  • Break code into logical chunks using functions and modules
  • Encapsulate pieces of functionality into separate *.py files
  • Structure program by domain-specific modules

Virtual Environments

  • Avoid version conflicts by isolating app dependencies
  • Create virtualenvs to install PyPI modules safely
  • Activate venvs on a per-project basis

Effective Commenting

  • Document modules, functions, classes and global variables
  • Use inline comments to explain complex logic flow
  • Include lots of docstrings in functions

Debugging

  • Trace runtime errors back to origin through IDLE tools
  • Set breakpoints and step through code execution
  • Watch variables and analyze stack state when issues occur

Mastering these areas will ensure you write maintainable, trustworthy Python code that withstands the test of time regardless of how large your programs grow.

Next Steps on Your Programming Journey

With Python 3 installed and IDLE providing a solid foundation, you‘re ready to continue your programming education by:

  • Reading beginner-focused Python tutorials using Google, YouTube, Udemy etc.
  • Working through interactive courses that offer coding exercises and examples
  • Building real projects that solve useful problems and help others
  • Joining Python meetup groups and tech communities both online and locally
  • Experimenting on hobbyist single-board computers like Raspberry Pi

Regardless if you‘re looking to pickup Python skills for a future career or out of personal interest in software development, set tangible goals for what you want to achieve in fixed timelines.

Stay committed to consistent learning, ask questions frequently, and remain excited about the incredible things you‘ll build.

Now go unleash your potential by writing awesome Python apps!

Similar Posts