The Python window in ArcGIS Desktop and Pro is a good place to start for GIS analysts without any Python experience. This blog posts describes how you can take advantage of this feature when first learning Python scripting.
Rather than starting with writing and running complex Python scripts right away, you might want to start easy. If you´re a GIS analyst and know your way around ArcMap or Pro using the available geospatial and data management tools, you´re already halfway: the only thing you need to focus on is how to convert a geospatial task or workflow into Python code.
One way to do this is by using the Python window, available in both Pro and ArcMap. In the latter application, the Python window can be activated in the Standard toolbar. Once opened, it shows two horizontal text areas: the upper one being an interactive interpreter (showing a primary prompt that looks like this: “>>>”) and the lower one offering a Help Section.
The interactive interpreter allows you to enter Python code and execute it right away by simply pushing ‘Enter’ once you´re done typing. The results of a geoprocessing tool are displayed in the map and results window, just as you would run it using ArcToolbox. This is certainly an advantage over Python IDE’s that require you to run your GIS after a script has finished running – running both at the same time results in a data lock, meaning your IDE cannot access your data.
One major advantage of the interpreter is that it can autocomplete your input, which means that you don´t have to spell out every geospatial tool or its parameters. Just typing in the first letters will cause the interpreter to offer a list of available options based on what you entered and select an option by using the tab key. Another advantage of the interpreter is that you can drag and drop input and output data files into your commands, resulting in data paths so arcpy knows where to look for data. This means you don´t have to look up and type long path names yourself.
The Help Section works dynamically: once you call a geoprocessing tool in the interpreter, it walks you through the parameters of the tool so you can configure its use step by step. The ArcGIS Desktop or Pro help section offers easy and complex code examples so you can see how to configure a tool the right way.
Even when you use a simple loop to carry out a task multiple times, a requisite is to maintain the right indentation level. As Python is very sensitive to this, it´s easy to make mistakes when you start learning Python. Luckily, the Python window comes to the rescue and automatically indents your code when you start defining a loop or decide to branch your code. The backspace key nullifies the automatic indentation.
Debugging forms a large part (if not the most part) of the time a programmer spends on code. The same goes for writing Python scripts. Even though the Python window helps you minimizing errors, it´s easy to get it wrong. Luckily, you can reproduce your code after running it simply by using the upper arrow button. You can enter more than one line of text at once by using Ctrl + Enter, which starts a new line in the interpreter without running the code you entered. Last but not least, you can use the Python window to test a standalone Python script by copy-pasting it into the interpreter line by line and see how it behaves.