Additional Variable Types
In Review
Introduction
The four types of number supported by Python are: plain integers, long integers, floating point numbers, and complex numbers. Python also includes types for strings, dates, and other types. You can also create your own "data types" by defining "classes" which we'll talk more about later. The sections below cover the types you'll probably use the most in GIS programming.
1. Constants
Constants are values that never change while your program is running. Python does not support constants as other languages do. There is a standard convention that constants will be all capitol letters while variables are all small or mixed case. I recommend defining your constants with all caps and then just remember not to set them to new values.
As an example, if you wanted to define a set of constants to represent types of plants you could:
PLANT_FORB=1 PLANT_SHRUB=2 PLANT_TREE=3 PLANT_GRASS=4
2. Dates
In science we rarely have to do anything with dates other than maintaining them with our data. Dates are not actually a built-in type in Python, you'll need to import the "datetime" package. These really are a date with a time attached so you can use them for timing within a day but we'll just use them for days for now.
You can create a "datetime" object by importing the datetime package and then calling the "today()" function:
import datetime TheDate=datetime.date.today() print(TheDate)
You can also create a datetime with any date you desire by calling the "date()" function. Then, you can access the individual elements of the date (year, month, and day) individually.
TheDate=datetime.date(2011,12,31) print(TheDate) print("The year="+format(TheDate.year)) print("The month="+format(TheDate.month)) print("The day of the month="+format(TheDate.day))
3. Variable Naming
You get to select the names of your variables as long as they follow the following rules:
- Variables can only contain characters from the alphabet, digits 0 to 9, and the "underscore" ("_") character.
- The first character must be from the alphabet.
- Variables must not be another reserved word in Python such as "print" or "for".
There are three different ways to name variables, functions, and classes in programming languages:
- All small case with underscores between words (variable_name)
- The first letter of each word capitalized without spaces, also known as "CamelCase" (VariableName)
- CamelCase with the first letter small (variableName)
I prefer CamelCase because it is the most like a proper name in english and easier to type than adding underscores between each word. What you use is going to be up to you and those you work with. I do recommend adopting a "code standard" within in any group you work with and use that! Working together with common standards is always between than having to read code that is in a format you are not used to.
i,j,k, and l are commonly used for general loop counters. x,y, and z are often used for coordinates in space. Most of the rest of the time, you will want to make your variables have names that are easy for you to know what they mean. Names like "RowIndex", "NumberOfFish", and "DBH" are good names. Using letters and numbers such as "a1", "g2", is not a good practice because you will forget what they mean as your programs grow in size an others will not be able to understand your code.
Additional Resources
Python Documentation: Datetime Functions
Python Documentation: Complex Number Functions