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

Python Presentation

Uploaded by

priyansharawat8
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Python Presentation

Uploaded by

priyansharawat8
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

NAME =

SECTION =
UNIVERSITY ROLL NO.=
CLASS ROLL NO.=
COURSE= B.TECH
BRANCH= C.S
CONTENT
What is programming language
What is program
Introduction to python
Python [ why python ?]
Basic pattern
Three major control constructs
Language terminology
Python assignment statement
Numeric data types
Integer operators
2
Identifiers
Keywords
Variables in python
Numerical input
Print statement
Dictionaries
Conditional branching
Looping with for
Modules
Lists
Error capture
Three kinds of errors
3
What Is a Programming Language?
A programming language is somewhat like a
natural language, but with a very limited set of
statements and strict syntax rules.

Has statements to implement sequential,


conditional and iterative processing - algorithms

Examples: FORTRAN, COBOL, Lisp, Basic, Pascal,


C, C++, Java, C#, Python, …
What Is a Program?
Usually, one or more algorithms written in a
programming language that can be translated to run
on a real machine

We sometimes call programs software


Introduction to Python
Python is a high-level programming language
Open source and community driven
“Batteries Included”
a standard distribution includes many modules
Dynamic typed
Source can be compiled or run just-in-time
Similar to perl, tcl, ruby

6
Why Python?
Unlike AML and Avenue, there is a considerable base
of developers already using the language
“Tried and true” language that has been in
development since 1991
Can interface with the Component Object Model
(COM) used by Windows
Can interface with Open Source GIS toolsets

7
Python
Python uses an interpreter. Not only can we write
complete programs, we can work with the interpreter
in a statement by statement mode enabling us to
experiment quite easily.

Python is especially good for our purposes in that it


does not have a lot of “overhead” before getting
started . It is easy to jump in and experiment with
Python in an interactive fashion.
The Basic Pattern
Most of our programs will use the basic pattern of

Get some user input

Perform some algorithm on the input

Provide results as output


Three major control constructs
of programming
(Execution flow of instructions)
Sequential: Simply do steps one after the other in
order they are listed.

Conditional: Decide which statement to do next


based on some true/false test.

Iterative: A set of statements is repeated over and


over until some condition is met.
Language terminology

Syntax: The formal rules for legal statements in the


language.

Semantics: The meaning of the statements - what


happens when the statement is executed.
Python Assignment Statement
Syntax: <variable> = <expression>
Note that variable is on left

Semantics:
Compute value of expression
Store this as new value of the variable

Example: Pay = PayRate * Hours

10 40 400
Payrate Hours Pay
Numeric Data Types
int
This type is for whole numbers, positive or
negative. Examples: 23, -1756

float
This type is for numbers with possible fraction
parts. Examples: 23.0, -14.561
Integer operators
The operations for integers are:
+ for addition
- for subtraction
* for multiplication
/ for integer division: The result of 14/5 is 2
% for remainder: The result of 14 % 5 is 4

*, /, % take precedence over +, -


x + y * z will do y*z first

Use parentheses to dictate order you want.


(x+y) * z will do x+y first.
Identifiers
Identifiers are names of various program elements
in the code that uniquely identify the elements.
They are the names of things like variables or
functions to be performed. They're specified by the
programmer and should have names that indicate
their purpose.

In Python, identifiers


Are made of letters, digits and underscores
Must begin with a letter or an underscore
Examples: temperature, myPayrate, score2
Keywords
Keywords are reserved words that have special
meaning in the Python language. Because they are
reserved, they can not be used as identifiers.
Examples of keywords are if, while, class, import.
Variables in Python
A variable has

 A name – identifier

 A data type - int, float, str, etc.

 Storage space sufficient for the type.


Numerical Input
To get numerical input from the user, we use an
assignment statement of the form

<variable> = input(<prompt>)

Here
<prompt> would be replaced by a prompt for the user inside
quotation marks
If there is no prompt, the parentheses are still needed

Semantics
The prompt will be displayed
User enters number
Value entered is stored as the value of the variable
Print Statement
For output we use statements of the form

print <expression>

Semantics
Value of expression is computed
This value is displayed

 Several expressions can be printed – separate them


by commas
Dictionaries
Dictionaries are sets of key & value pairs
Allows you to identify values by a descriptive name
instead of order in a list
Keys are unordered unless explicitly sorted
Keys are unique:
var[‘item’] = “apple”
var[‘item’] = “banana”
print var[‘item’] prints just banana

20
Conditional
if and else
Branching
if variable == condition:
#do something based on v == c
else:
#do something based on v != c
elif allows for additional branching
if condition:
elif another condition:

else: #none of the above

21
Looping with For
We could use a for loop to perform geoprocessing
tasks on each layer in a list
We could get a list of features in a feature class and
loop over each, checking attributes
Anything in a sequence or list can be used in a For
loop
Just be sure not to modify the list while looping

22
Modules
Modules are additional pieces of code that further
extend Python’s functionality
A module typically has a specific function
additional math functions, databases, network…
Python comes with many useful modules
arcgisscripting is the module we will use to load
ArcGIS toolbox functions into Python

23
Lists
Think of a list as a stack of cards, on which your
information is written
The information stays in the order you place it in
until you modify that order
Methods return a string or subset of the list or modify
the list to add or remove components
Written as var[index], index refers to order within set
(think card number, starting at 0)
You can step through lists as part of a loop

24
Error Capture
Check for type assignment errors, items not in a list,
etc.
Try & Except
try:
a block of code that might have an error
except:
code to execute if an error occurs in "try"
Allows for graceful failure
– important in ArcGIS

25
Three kinds of errors
Syntax error : Some statement in the program is not a
legal statement in the language.

Runtime error : An error occurs while the program is


executing, causing the program to terminate (divide by
zero, etc.)

Logic error : The program executes to completion, but


gives incorrect results.
27

You might also like