Skip to main content

Section A.1 Getting started with Python

We began with a neat practical algorithm. Unfortunately, even with a very good understanding of the problem and its solution a beginner is not ready to turn the complete algorithm into a working computer code, or program.

Subsection A.1.1 Getting IDLE

There are many, many different ways to get started as a Python programmer, but the first thing you need is to download Python itself. Download the latest version (3.10 as of this writing) from python.org. Once you have Python, the next tool you should have is an IDE (integrated development environment). The default Python IDE which comes bundled with Python is called IDLE.

Remark A.1.1. Other IDEs.

You do not have to use IDLE. Any text editor will work to edit your files (such as Notepad) and once you have the Python interpreter installed, you can run Python scripts. That being said, it’s often easiest to write code inside an IDE so that you can use some sort of debugging.
  • Visual Studio Code
     5 
    code.visualstudio.com
    , by Microsoft, with the Python extension
     6 
    marketplace.visualstudio.com/items?itemName=ms-python.python
    installed. VS Code is an extremely full-featured IDE, but is a fairly big program. This is the current IDE of choice of the author of this textbook since it can flexibly work in the typesetting language for mathematics as well, if the right extensions are enabled.
  • Atom
     7 
    atom.io
    , by Github, with the script package
     8 
    atom.io/packages/script
    installed. Atom is a very hackable text editor which can be treated as an IDE by adding enough packages.
  • Thonny
     9 
    thonny.org
    , originally by the University of Tartu and now maintained by the global community of users. Thonny actually comes bundled with its own installation of Python and so doesn’t require you to download from python.org at all.
    Thonny is actually designed for absolute beginners and is so user-friendly that it is pre-installed in the Raspbian operating system for RaspberryPi microcomputers, often used to teach programming to children. That being said it is still a functional Python IDE.
  • A final alternative, and a strong one, is to sign up for an account at cocalc.com and use CoCalc as your development environment. The major caveat here is that you must be online to access your files. This textbook was rewritten on CoCalc!
All that being said, the basic assumption of this text is that you are working in IDLE.

Subsection A.1.2 Interactive Mode

When you first run IDLE, you’re greeted with a box that looks like this:
Python 3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
Listing A.1.2. Starting IDLE
Different versions of IDLE may have some sort of dividing line where the prompt “>>>” is separated from the input, and the color scheme of the IDLE window can be configured by the user, so the window might look different depending upon who is using it.
When you type commands at the prompt and press enter (or return), they are executed.
>>> 2 + (3 / 5 ** 4)
2.0048
Listing A.1.3. The Python prompt for Interactive Mode
Interactive mode is of limited use, but is very good for doing quick calculations or checking to see if your syntax is correct.

Definition A.1.4. Syntax.

The syntax of a programming language is the set of rules which decide what combinations of symbols are meaningful in the language.
Python was designed to be as easy to write and read as possible, and to be very easy to go from an algorithm to a working program. Anything more complicated than a single line will be written in a module, which is a sequence of Python commands saved in a text file (usually with file extension .py). Python modules can be opened, edited, and executed all within IDLE.

Technology A.1.5. Quitting Python.

If you’re using Python via IDLE, just close all of your IDLE windows to quit the program. If not, you can use
>>> quit()

Subsection A.1.3 A first Python module

From within IDLE, do the following:
  1. Open a new file, either from the File menu or pressing Ctrl-N.
  2. Type print("Hello, world!") in the blank file.
  3. Save it somewhere memorable (like a Python folder on your desktop) as hello_world.py
  4. From the Run menu, select Run Module, or press F5 on your keyboard.
After a second, you should see some output in the IDLE Shell window, like so:
= RESTART: C:/Users/sgraves/OneDrive - The University of Texas at Tyler/Python/hello_world.py
Hello, world!
>>> 
Listing A.1.6. The output of running the “Hello, World!” module.
Congratulations! You can no longer say you have never written a computer program.
We will frequently be writing modules and listing code examples in the text, so they will appear like so:
print("Hello, world!")
Listing A.1.7. The “Hello, world!” program
All of your actual work in Python should be done in modules, which are also called scripts. It is convenient to have one folder where you keep all of your Python code, and even more convenient to name your Python modules sensibly so that they can be reused if ever you have the need.