0% found this document useful (0 votes)
4 views

Python Basics

The document is a comprehensive guide on Python programming specifically tailored for geospatial data analysis, authored by MSc. Ir. Aphrodis Nzabandora. It covers Python fundamentals, installation, syntax, variable management, and the application of Python libraries in spatial data analysis. The document aims to enhance Python skills and knowledge through interactive learning and hands-on examples.

Uploaded by

globallandmark11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Basics

The document is a comprehensive guide on Python programming specifically tailored for geospatial data analysis, authored by MSc. Ir. Aphrodis Nzabandora. It covers Python fundamentals, installation, syntax, variable management, and the application of Python libraries in spatial data analysis. The document aims to enhance Python skills and knowledge through interactive learning and hands-on examples.

Uploaded by

globallandmark11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

PYTHON PROGRAMMING FOR

GEOSPATIAL DATA

By MSc. Ir. Aphrodis Nzabandora


[email protected]
Mobile:0788302072
MSc. Ir. Aphrodis Nzabandora
Academia, Industry & Personal
• Adventist University of Central Africa, Kigali,
Rwanda, Bachelor of Science in Information
Management, September, 2011
• Amity University, UTTAR PRADESH India, Post
Graduate Diploma in Information Technology,
March, 2013
MSc. Ir. Aphrodis Nzabandora
Academia, Industry & Personal
• Kigali Independent University, Kigali, Rwanda,
Master of Science in Internet Systems,
December, 2014.
• ITC-University of Twente, The Netherlands, Master
of Science in Geo Information Science and
Earth Observation for Geo-Informatics, March
11th, 2016
• Theology from Alliance Evangelical of Rwanda
MSc. Ir. Aphrodis Nzabandora
Academia, Industry & Personal
• School Teacher : CAG
• IT Professional and database Administrator, ASYV
• Software Development Engineer, RNRA,
• GIS Developer Specialist, RLMUA,
• Database and Application Specialist, RLMUA
• Part-Time assistant Lecturer at UTB, UoK, UR,
INES Ruhengeli
MSc. Ir. Aphrodis Nzabandora
Academia, Industry & Personal
• 5 years experience as Software
Development Engineer
• 2 years experience as Database
Administration
• 5 Years in Utility Spatial Planning
• Few years in teaching experience
Logistics & Goals

• Interactive & enjoyable Classroom


• Call me as Aphrodis– no formalities - Treat me like a
friend & co-worker!
• Show & play with hands-on examples
• Enhance your Python skills and knowledge
• I do not know everything, but I do have resources to get
answers
• Questions?
Allocation of study and teaching
hours
ASSESSMENT PATTERN
Learning Outcomes
Learning Outcomes
Apply Python Programming to
Geospatial data
Contents

• Apply Python Fundamentals


• Apply Python data structure
• Connect Python to the Geospatial database.
• Apply Python libraries in spatial data analysis
• Apply Python to remote sensing data
Why python in geospatial analysis

• Key capabilities of python in geospatial data analysis


is based on Powerful tools for loading, manipulating,
and visualizing spatial datasets.
What is a python?

• Python is an interpreted, object-oriented, high-level


programming language with dynamic semantics.
• Its high-level built in data structures, combined with
dynamic typing and dynamic binding, make it very
attractive for Rapid Application Development, as
well as for use as a scripting or to connect existing
components together
Python Fundamentals
Environment Setup
• First step in the journey of learning
Python is to install it on your machine.
• Python is available on a wide variety
of platforms
• Download python on this link if you
are running windows OS.
https://www.python.org/downloads/
release/python-3124/
• You need to download only the binary
code applicable for your platform and
install Python
Python Fundamentals
Python Basics

• Open the Window Command Prompt terminal and run


Python to check the success of installation.
• Python's standard library has an executable module
called IDLE – short for Integrated Development and
Learning Environment.
IDLE contains Python shell (interactive interpreter) and a
customizable multi-window text editor with features such as
syntax highlighting, smart indent, auto completion etc.
• It is cross-platform so works the same on Windows, MacOS
and Linux.
Python Fundamentals
Environment Setup
• Here are the steps to install Python on Windows machine:
1. Open a Web browser and go
to https://www.python.org/downloads/.
2. Follow the link for the Windows installer python-XYZ.msi file where
XYZ is the version you need to install.
3. To use this installer python-XYZ.msi, the Windows system must
support Microsoft Installer 2.0. Save the installer file to your local
machine and then run it to find out if your machine supports MSI.
4. Run the downloaded file. This brings up the Python install wizard,
which is really easy to use. Just accept the default settings, wait until
the install is finished, and you are done.
Python Fundamentals
Environment Setup
• You can run Python from a Graphical User Interface
(GUI) environment as well, if you have a GUI
application on your system that supports Python.
Python Fundamentals
Python -Syntax
• Python is known for its clean and readable syntax.
• The Python syntax defines a set of rules that are
used to create a Python Program.
• The Python Programming Language Syntax has
many similarities to Perl, C, and Java Programming
Languages. However, there are some definite
differences between the languages.
Python Fundamentals
Python -First Python Program
• We can invoke a Python interpreter from command
line by typing python at the command prompt as
following
• >>> print ("Hello, World!")
Python Fundamentals
Python -Script Mode Programming
• We can invoke the Python interpreter with a script
parameter which begins the execution of the script
and continues until the script is finished.
• When the script is finished, the interpreter is no
longer active.
• Let us write a simple Python program in a script
which is simple text file.
• Python files have extension .py
Python Fundamentals
Python -Identifier

• A Python identifier is a name used to identify


a variable, function, class, module or other object.
• An identifier starts with a letter A to Z or a to z or an
underscore (_) followed by zero or more letters,
underscores and digits (0 to 9).
• Python does not allow punctuation characters such as @,
$, and % within identifiers.
• Python is a case sensitive programming language.
Thus, Manpower and manpower are two different identifiers
in Python.
Python Fundamentals
Python -Identifier
• Here are naming conventions for Python identifiers

• Python Class names start • Starting an identifier with two


with an uppercase letter. All leading underscores indicates a
other identifiers start with a strongly private identifier.
lowercase letter. • If the identifier also ends with two
• Starting an identifier with a trailing underscores, the identifier is
single leading underscore a language-defined special name.
indicates that the identifier
is private identifier.
Python Fundamentals
Python –Reserved Words
• The following list
shows the Python
keywords.
• These are
reserved words
and you cannot
use them as
constant or
variable or any
other identifier
names.
• All the Python
keywords contain
lowercase letters
only
Python Fundamentals
Python –Lines and Indentation

• Python programming provides


no braces to indicate blocks of
code for class and function
definitions or flow control.
• Blocks of code are denoted
by line indentation, which is
rigidly enforced.
• The number of spaces in the
indentation is variable, but all
statements within the block
must be indented the same
amount.
Python Fundamentals
Python –Multi Line Statements

• Statements in Python total = item_one + \


typically end with a new item_two + \
line. item_three
• Python does, however,
allow the use of the line
continuation character (\)
to denote that the line
should continue.
Python Fundamentals
Python –Multi Line Statements

• Statements contained within the [], {}, or ()


brackets do not need to use the line
continuation character.
• For example following statement works well in
Python.
days = ['Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday']
Python Fundamentals
Python –Quotations

• Python accepts single ('), word = 'word'


double (") and triple (''' or """) print (word)
quotes to denote string sentence = "This is a
literals, as long as the same sentence."
type of quote starts and ends print (sentence)
paragraph = """This is
the string. a paragraph. It is
• The triple quotes are used to made up of multiple
lines and
span the string across multiple sentences."""
lines. For example, all the print (paragraph)
following are legal
Python Fundamentals
Python –Comments

• A comment is a programmer-readable explanation or


annotation in the Python source code.
• They are added with the purpose of making the source
code easier for humans to understand, and are ignored
by Python interpreter.
• Python supports single-line (or end-of-line) and multi-
line (block) comments.
Python Fundamentals
Python –Comments

• A hash sign (#) that is not inside a string literal begins a


comment. All characters after the # and up to the end
of the physical line are part of the comment and the
Python interpreter ignores them

This produces the following result:


Python Fundamentals
Python –Comments

• You can type a comment on the same line after a


statement or expression
• name = "Madisetti" # This is again comment
• You can comment multiple lines as follows
o # This is a comment.
o # This is a comment, too.
o # This is a comment, too.
o # I said that already.
Python Fundamentals
Python –Comments

• Following triple-quoted string is also ignored by Python


interpreter and can be used as a multiline comments:
''' This is a multiline comment. '''
Python Fundamentals
Using Blank Lines in Python
Programs
• A line containing only whitespace, possibly with a
comment, is known as a blank line and Python totally
ignores it.
• raw_input("\n\nPress the enter key to exit.")
• Here, "\n\n" is used to create two new lines before
displaying the actual line. Once the user presses the key,
the program ends
Multiple Statements on a Single Line

• The semicolon ( ; ) allows multiple statements on the


single line given that neither statement starts a new code
block. Here is a sample snip using the semicolon.
import sys; x = 'foo'; sys.stdout.write(x + '\n')
Python Fundamentals
Python –Variables
• Python variables are the reserved memory locations used to
store values with in a Python Program.
• This means that when you create a variable you reserve some
space in the memory.
• Based on the data type of a variable, Python
interpreter allocates memory and decides what can be stored
in the reserved memory.
• By assigning different data types to Python variables, you can
store integers, decimals or characters in these variables.
Python Fundamentals
Python –Memory Address
• Data items belonging to
different data types are
stored in computer's
memory.
• Computer's memory
locations are having a
number or address,
internally represented in
binary form
Python Fundamentals
Python –Creating Variable
• Python variables do not need explicit declaration to
reserve memory space or you can say to create a
variable.
• A Python variable is created automatically when you
assign a value to it.
• The equal sign (=) is used to assign values to variables
Python Fundamentals
Python –Creating Variable
• Examples:
This example creates different types (an integer, a float,
and a string) of variables.
counter = 100 # Creates an integer variable
miles = 1000.0 # Creates a floating point variable
name = "Zara Ali" # Creates a string variable
Python Fundamentals
Python –Printing variables
• Once we create a Python variable and assign a value to
it, we can print it using print() function.
Codes Output
Python Fundamentals
Python –Deleting variables
• You can delete the reference to a number object by
using the del statement.
The syntax of the del statement is:
del var1[,var2[,var3[....,varN]]]]
You can delete a single object or multiple objects by using
the del statement. For example:
del var
del var_a, var_b
Python Fundamentals
Python –Deleting variables
• Example:
Codes Output
Python Fundamentals
Python –Getting Type of a Variable

• You can get the data type of a Python variable using the
python built-in function type() as follows:
Python Fundamentals
Python –Casting a Variable

• You can specify the data type of a variable with the help
of casting as follows:
Python Fundamentals
Python –Variable Multiple
Assignment
• Python allows to initialize Instead of separate
more than one variables assignments, you can do it in a
in a single statement. In single assignment statement as
the following case, three follows
variables have same value. >>> a=b=c=10
>>> print (a,b,c)
• >>> a=10 10 10 10
• >>> b=10
• >>> c=10
Python Fundamentals
Taking input in Python

• Developers often have a need to interact with users, either to


get data or to provide some sort of result.
• Most programs today use a dialog box as a way of asking the
user to provide some type of input.
• While Python provides us with two inbuilt functions to read
the input from the keyboard:
Python Fundamentals
Taking input in Python

• Developers often have a need to interact with users, either to


get data or to provide some sort of result.
• Most programs today use a dialog box as a way of asking the
user to provide some type of input.
• While Python provides us with two inbuilt functions to read
the input from the keyboard:
Python Fundamentals
Taking input in Python

• input (): This function first takes the input from the user
and converts it into a string. The type of the returned
object always will be <class ‘str’>.
• It does not evaluate the expression it just returns the
complete statement as String.
• Syntax:
• Example:
Python Fundamentals
How the input function works in
Python
• When input() function executes program flow will be stopped
until the user has given input.
• The text or message displayed on the output screen to ask a
user to enter an input value is optional i.e. the prompt, which
will be printed on the screen is optional.
• Whatever you enter as input, the input function converts it into
a string. if you enter an integer value still input() function
converts it into a string. You need to explicitly convert it into an
integer in your code using typecasting .
Python Fundamentals
How the input function works in
Python
• raw_input(): This function works in older version (like Python
2.x). This function takes exactly what is typed from the
keyboard, converts it to string, and then returns it to the
variable in which we want to store it.
QUESTIONS?

You might also like