Python in GIS: A Beginner’s Guide

Getting Started with Python in GIS: A Beginner’s Guide

By Dr. Bratati Dey | Swastik Edustart – Institute of RS and GIS, Delhi

Geographic Information Systems (GIS) have transformed how we understand and interact with the world around us. But what happens when you combine GIS with one of the most versatile programming languages on the planet? You get a powerful toolkit for automating workflows, building custom tools, and solving real-world spatial problems. This post walks you through the fundamentals of Python in GIS — from understanding what GIS is, to writing your first lines of Python code.


What Is GIS — and Why Does It Matter?

GIS is a computer-based tool that analyses, stores, manipulates, and visualises geographic information on a map. At its core, GIS revolves around four steps:

  1. Create — build spatial datasets
  2. Manage — organise and maintain geographic data
  3. Analyse — derive insights from spatial relationships
  4. Display — communicate findings through maps and visualisations

The real power of GIS lies in the questions it can answer. How many wetlands exist in Delhi? What is the most suitable site for a new school in a given part of Dehradun? Which route gets you to Connaught Place in the shortest time? GIS stores information as thematic layers — each layer linked by geographic coordinates — and these layers can be queried and analysed with remarkable efficiency, saving both time and money.


Enter Python

So where does Python fit in? Python is a high-level, cross-platform, open-source programming language released under a GPL-compatible licence. Here are a few fun facts before we dive in:

  • It was invented in the Netherlands in the early 1990s by Guido van Rossum
  • It is maintained by the Python Software Foundation (PSF), a non-profit organisation
  • It is named after Monty Python’s Flying Circus — the British comedy show — not the snake

Why Python Is Loved by GIS Professionals

Python ticks nearly every box a GIS practitioner could ask for:

  • Free and open source — no licensing fees
  • Easy to learn and read — clean syntax with minimal clutter
  • Portable — runs on virtually any operating system
  • Interpreted — no compilation step needed
  • Versatile — supports both procedural and object-oriented programming
  • “Batteries included” — comes with a rich standard library out of the box

What Can You Do with Python in GIS?

  • Automate repetitive geoprocessing tasks
  • Build custom geoprocessing tools
  • Integrate GIS into web applications
  • Customise and extend desktop GIS applications
  • Create plugins
  • Boost your career — and potentially your salary

Setting Up: Downloading Python

Head to https://www.python.org/downloads/ to download the latest version of Python for your operating system. The installation is straightforward, and within minutes you’ll have a working Python environment.


Python Basics: What You Need to Know

Indentation & Code Blocks

Python uses indentation (not curly braces) to define code structure. The standard is 4 spaces per level, and every indented block follows a line ending in a colon (:).

python

if 5 > 2:
    print("Five is greater than two!")

Variables & Dynamic Typing

Python is dynamically typed — you don’t declare a variable’s type; Python figures it out at runtime.

python

x = 5        # integer
name = "Anita"  # string

Naming rules:

  • Must start with a letter or underscore
  • Can contain letters, numbers, and underscores
  • Case-sensitive: age and Age are different variables

Comments

Use # for single-line comments. For multi-line comments, developers commonly use triple quotes (''' or """).

python

# assigning variable
x = 5
print(x)

Input & Output

python

name = input("Enter your name: ")
print("Hello", name)

print() outputs to the console; input() captures user data as a string.

Conditional Statements

python

num = int(input("Enter a number: "))
if num % 2 == 0:
    print("Even Number")
else:
    print("Odd Number")

Loops

For loop — iterates over a sequence:

python

for i in range(1, 6):
    print(i)

While loop — runs as long as a condition is true:

python

count = 1
while count <= 5:
    print(count)
    count += 1

Python Data Types

TypeDescriptionExamples
intWhole numbers10, 25, -45
floatDecimal numbers23.0, -5.0
strText / alphanumeric"Anita", "Delhi"
boolLogical truth valueTrue, False

Every value in Python belongs to a type, and every type is implemented as a class. When you write x = 10, Python creates an object of the int class with a value of 10.

Object Identity

Every Python object has a unique id — its address in memory. You can inspect it using the built-in id() function.

Multiple Assignment

Python lets you assign multiple variables in a single statement:

python

a, b, c = 1, 2, 3

Python Keywords

Python 3.x has 33 reserved keywords — words like if, for, while, def, class, import, and so on. These have predefined meanings and cannot be used as variable names. You can view the full list in the Python shell using the help("keywords") command.


Hello World: Python vs Java

One of the most compelling arguments for Python’s simplicity is the “Hello, World!” comparison:

Java:

java

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}

Python:

python

print("Hello world!")

As the saying goes: “Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike.”


Basic Arithmetic — Your First Hands-On Exercise

Fire up the Python shell and try:

python

print(3 + 12)    # 15
print(12 - 3)    # 9
print(9 + 5 - 15 + 12)  # 11

Simple, clean, and immediate — that’s Python.


What’s Next?

This is just the beginning. In the sessions ahead, we’ll explore how Python integrates with GIS platforms, how to write scripts that automate spatial analysis, and how AI tools are beginning to reshape geographical studies.

Whether you’re a complete beginner or a GIS professional looking to level up, Python opens doors that manual workflows simply can’t. Start small, stay consistent, and the spatial world will open up to you one line of code at a time.


Dr. Bratati Dey teaches GIS and Remote Sensing at Swastik Edustart, New Delhi. Visit www.swastikedustart.com to learn more about their courses and training programmes.

Python GIS Code

layer = iface.addVectorLayer(
    "D:/qgis/data/boundary.shp",
    "Boundary",
    "ogr"
)
count = layer.featureCount()
print("Total Features:", count)

for feature in layer.getFeatures():
    print(feature["id"])   

layer.startEditing()
layer.dataProvider().addAttributes([QgsField("Area", QVariant.Double)])
layer.updateFields()
layer.commitChanges()
layer.startEditing()

for f in layer.getFeatures():
    area = f.geometry().area()
    f["Area"] = area
    layer.updateFeature(f)
layer.commitChanges()