Python for GIS
Prepared By: Asst. Prof Ahmad Shekib IQBAL
Department: GIS & Remote Sensing
Faculty: Engineering Geomatics
Year: 2025
Variable
• Variables are a basic building block of computer programming. A
programming variable, is a name that gets assigned a value.
• A variable can have a short name (like x and y) or a more descriptive
name (age, carname, total_volume).
Conditions for Python variables
• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and underscores (A-z,
0-9, and _ )
• Variable names are case-sensitive (age, Age and AGE are three different variables)
Abcd = 2 _=8
_abcd = 4 A=5
Abc4d = 5 a=7
Data Types
All Python objects having a data type. Built-in Python data types,
such as integers, floating point values, and strings are the building
blocks for GIS scripts.
Built-in Data Types
In programming, data type is an important concept. Variables can
store data of different types, and different types can do different
things.
Built-in Data Types
Data Type Script Name of the Types
Text type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
Examples and Setting of Data Type:
Data Type Script Name of the Types
x = "Hello World" str
x = 20 int
x = 20.5 float
x = 1j complex
x = ["apple", "banana", "cherry"] list
x = ("apple", "banana", "cherry") tuple
x = range(6) range
x = {"name" : "John", "age" : 36} dict
x = {"apple", "banana", "cherry"} set
x = frozenset({"apple", "banana", "cherry"}) frozenset
x = True bool
x = b"Hello" bytes
x = bytearray(5) bytearray
x = memoryview(bytes(5)) memoryview
Numbers
• Python has four numeric data types: int, long, float, and complex.
• Float values have a decimal point; Integer values don’t. In the following
example, x is an integer and y is a float:
• >>> x = 2
• >>> y = 2.0
• >>> type(x)
• <type 'int'>
• >>> type(y)
• <type 'float'>
Numerical Operators
Operation Operator Example Result
Addition + 7+2 9
Subtraction - 7-2 5
Multiplication * 7*2 14
Division / 7/2 3
Exponentiation ** 7**2 49
Modulus division % 7%2 1
What is string?
• A string literal is a set of characters surrounded by quotation marks.
A variable assigned a string literal value is called a string variable.
String Operations
GIS Python programming requires frequent string manipulation. To deal with
file names, field names, and so forth, you’ll need to be familiar with finding
the length of a string, indexing into a string, concatenating, slicing, and
checking for a substring in a string.
Find the Length of Strings
• The built-in len function finds the length of a string literal:
>>> len('trees.shp')
9
• Or the length of the value held by a string variable:
>>> data = 'trees.shp'
>>> len(data)
9
Indexing into strings
• Each character in a string has a numbered position called an index. The
numbering starts with zero, in other words Python uses zero - based indexing.
From left to right, the indices are 0, 1, 2, and so forth.
>>> fieldName = 'COVER'
>>> fieldName[0]
'C'
Slice strings
• To get more than one character, use slicing instead of indexing. Slicing
gets a substring (a slice) of a string. Slicing uses square brackets with a
colon inside.
>>> fieldName = 'COVER’ >>> fieldName[:3] >>> inputData = 'trees.shp'
>>> baseName = inputData[:-4] #
'COV'
>>> fieldName[1:3] Remove the file extension.
>>> fieldName[1:] >>> baseName
'OV' 'OVER' 'trees'
Concatenate strings
• Concatenation glues together a pair of strings. You use the same sign
for addition, but it acts differently for strings.
>>> 5 + 6 # adding two numbers together >>> rasterName = 'NorthEast'
11 >>> route = 'ATrain'
>>> '5' + '6' # concatenating two strings >>> output = rasterName + route
'56' >>> output
'NorthEastATrain'br />
Check for Substring Membership
• The in keyword enables you to check if a string contains a substring. Python
documentation refers to this as checking for ‘membership’ in a string.
• Suppose you want to check if a file is a buffer output or not, and you have
named each buffer output file so that the name contains the string buff.
>>> a = ‘hello world' >>> substring = 'buff'
>>> print(‘world’ in a) >>> substring in outputData
True True
>>> print(‘universe’ in a) >>> substring in inputData
False False
Sequence operations on strings.
Operation Sample code Result
Length len(exampleString) 8
Indexing exampleString[2] 'z'
Slicing exampleString[:-4] 'tuzi'
Concatenation exampleString + exampleString 'tuzigoottuzigoot'
Membership 'ample' in exampleString False