Each lesson in this course includes some simple practice exercises with Python. These are not submitted or graded, but they are highly recommended if you are new to programming or if the project initially looks challenging. Lessons 1 and 2 contain shorter exercises, while Lessons 3 and 4 contain longer, more holistic exercises. Each practice exercise has an accompanying solution that you should carefully study. If you want to use the USA.gdb referenced in some of the solutions you can find it here.
Remember to choose File > New in PythonWin to create a new script (or click the empty page icon). You can name the scripts something like Practice1, Practice2, etc. To execute a script in PythonWin, click the "running man" icon.
- Say hello
Create a string variable called x and assign it the value "Hello". Display the contents of the x variable in the Interactive Window.
Practice 1 Solution
- Concatenate two strings
Create a string variable called first and assign to it your first name. Likewise, create a string variable called last and assign to it your last name. Concatenate (merge) the two strings together, making sure to also include a space between them.
Practice 2 Solution
- Pass a value to a script as a parameter
Example 1.6.4 shows the use of the arcpy.GetParameterAsText() method. This method is typically used in conjunction with an ArcGIS script tool that has been designed to prompt the user to enter the required parameters. However, you may have noticed that the little dialog that appears after clicking the Run button in PythonWin also includes a place to supply arguments.
For this exercise, write a script that accepts a single string value using the GetParameterAsText method. The value entered should be a name and that name should be concatenated with the literal string "Hi, " and displayed in the Interactive Window. Test the script from within PythonWin, entering a name (in quotes) in the Arguments text box after clicking the Run button.
Practice 3 Solution
- Report the geometry type of a feature class
Example 1.6.2 demonstrates the use of the Describe method to report the spatial reference of a feature class. The Describe method returns an object that has a number of properties that can vary depending on what type of object you Described. A feature class has a spatialReference property by virtue of the fact that it is a type of Dataset. (Rasters are another type of Dataset and also have a spatialReference property.)
The Describe method's page in the Help lists the types of objects that the method can be used on. Clicking the Dataset link pulls up a list of the properties available when you Describe a dataset, spatialReference being just one of several.
For this exercise, use the Describe method again; this time, to determine the type of geometry (point, polyline or polygon) stored in a feature class. I won't tell you the name of the property that returns this information. But I will give you the hint that feature classes have this mystery property not because they're a type of Dataset as with the spatialReference property, but because they're objects of the type FeatureClass.
Practice 4 Solution