Notice the 5 index number is not included since Python slicing only returns characters up to, but not including, the second offsetĮither of the offsets can be left off. The last character returned is the 4 index, which corresponds to the d character. The first character, which occupies the 0 position, is F. In our example, the returned variable will contain the Flood characters. This can be particularly confusing for new Python programmers and is a common source of errors. Python slicing returns the characters beginning with the first offset up to, but not including, the second offset. Indexing always creates a new variable to hold the character: In this case, the last character in a string is stored at the -1 index. Negative offsets can be used to search backwards from the end of a string. Python is a zero-based language, meaning the first item in a list is 0. For example, you could obtain the first string character in the fc variable by using the fc syntax.
In Python, this is referred to as indexing in the case of accessing an individual character, and slicing in the case of accessing a series of characters.Ĭharacters in a string are obtained by providing the numeric offset contained within square brackets after a string. Some collections, such as a dictionary, do not maintain a set order. What does this mean? It simply means that we can access individual characters or a series of characters from the string and that the order of the characters will remain the same until we change them. I have briefly mentioned that strings are an ordered collection of characters.
This will be useful to you (or another programmer) when the time comes to update your scripts: You should also strive to include comments throughout your script to describe important sections of your script.
In the next screenshot, the commented lines of code are displayed with a single pound sign that prefixes the line of code. They are simply used to document your code. The Python interpreter does not execute these lines of code. These lines of code begin with a single pound sign ( #) or a double pound sign ( #), and are followed by whatever text you need to document the code. Comments are lines of code that you add to your script that serve as a documentation of what functionality the script provides. This documentation is accomplished in Python through the use of comments. This introductory documentation will help you and other programmers in the future to quickly scan the details and purpose of a script. It is a commonly accepted practice that the beginning of each script should serve as documentation, detailing the script name, author, and a general description of the processing provided by the script. Python scripts should follow a common structure. By the end of this chapter, you will have learned the following:
Finally, you'll learn how to access modules that provide additional functionality to the Python language.
In addition to this, we'll cover statements, including decision support and looping structures to make decisions in your code, and/or looping through a code block multiple times along with the with statement, which is used extensively with the cursor objects from the ArcPy data access module that are used to insert, search, and update data. We'll introduce you to these complex data structures, which you'll use extensively when you write geoprocessing scripts with ArcGIS. Classes and objects are a fundamental concept in object-oriented programming and in the Python language. Next, we'll look at the various built-in data types that Python offers, such as strings, numbers, lists, and dictionaries. From there, we'll delve into language features, such as adding comments to your code, creating and assigning data to variables, and built-in variable typing with Python, which makes coding with Python easy and compact. Initially, we'll cover how to create new Python scripts and edit existing scripts. In this chapter, we'll cover many of the basic language constructs found in Python. Python supports many of the programming constructs found in other languages. Chapter 1. Fundamentals of the Python Language for ArcGIS