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

Python Winter Training

This document provides an introduction to the Python programming language. It discusses key features of Python including being open source, having many standard modules, being dynamically typed and able to be compiled or run just-in-time. It also introduces Python interfaces like IDLE and shows basic examples of Python code. Finally, it discusses Python objects like strings, lists, tuples and dictionaries as well as concepts like conditionals, loops, modules and error handling.

Uploaded by

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

Python Winter Training

This document provides an introduction to the Python programming language. It discusses key features of Python including being open source, having many standard modules, being dynamically typed and able to be compiled or run just-in-time. It also introduces Python interfaces like IDLE and shows basic examples of Python code. Finally, it discusses Python objects like strings, lists, tuples and dictionaries as well as concepts like conditionals, loops, modules and error handling.

Uploaded by

Abhishek Giri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Presented By : Abhishek Giri

42796502818
ECE-6C

1
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

2
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

3
Why not Visual Basic?
 Visual Basic is still the method of configuring and
customizing ArcMap
 If you have a button on the toolbar, it’s VB
 Python scripts can be placed in ArcToolbox
 Python can be run from the command line without
ArcMap or ArcCatalog being open
 Using just the GIS Engine, lower overhead
 Rapid prototyping, ease of authoring, etc.

4
Python Interfaces
 IDLE – a cross-platform Python development
environment
 PythonWin – a Windows only interface to Python
 Python Shell – running 'python' from the Command
Line opens this interactive shell
 For the exercises, we'll use IDLE, but you can try them
all and pick a favorite

5
IDLE – Development Environment
 IDLE helps you program
in Python by:
 color-coding your
program code
 debugging
 auto-indent
 interactive shell

6
Example Python
 Hello World
print “hello world”
 Prints hello world to
standard out
 Open IDLE and try it out
yourself
 Follow along using IDLE

7
More than just printing
 Python is an object oriented language
 Practically everything can be treated as an object
 “hello world” is a string
 Strings, as objects, have methods that return the result
of a function on the string

8
String Methods
 Assign a string to a
variable
 In this case “hw”
 hw.title()
 hw.upper()
 hw.isdigit()
 hw.islower()

9
String Methods
 The string held in your variable remains the same
 The method returns an altered string
 Changing the variable requires reassignment
 hw = hw.upper()
 hw now equals “HELLO WORLD”

10
Other Python Objects
 Lists (mutable sets of strings)
 var = [] # create list
 var = [‘one’, 2, ‘three’, ‘banana’]
 Tuples (immutable sets)
 var = (‘one’, 2, ‘three’, ‘banana’)
 Dictionaries (associative arrays or ‘hashes’)
 var = {} # create dictionary
 var = {‘lat’: 40.20547, ‘lon’: -74.76322}
 var[‘lat’] = 40.2054
 Each has its own set of methods

11
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

12
List Methods
 Adding to the List
 var[n] = object
 replaces n with object

 var.append(object)
 adds object to the end of the list

 Removing from the List


 var[n] = []
 empties contents of card, but preserves order

 var.remove(n)
 removes card at n

 var.pop(n)
 removes n and returns its value

13
Tuples
 Like a list, tuples are iterable arrays of objects
 Tuples are immutable –
once created, unchangeable
 To add or remove items, you must redeclare
 Example uses of tuples
 County Names
 Land Use Codes
 Ordered set of functions

15
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

16
Indentation and Blocks
 Python uses whitespace and indents to denote blocks
of code
 Lines of code that begin a block end in a colon:
 Lines within the code block are indented at the same
level
 To end a code block, remove the indentation
 You'll want blocks of code that run only when certain
conditions are met

17
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

18
Looping with For
 For allows you to loop over a block of code a set
number of times
 For is great for manipulating lists:
a = ['cat', 'window', 'defenestrate']
for x in a:
print x, len(x)
Results:
cat 3
window 6
defenestrate 12

19
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

20
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

21
Modules
 Modules are accessed using import
 import sys, os # imports two modules
 Modules can have subsets of functions
 os.path is a subset within os
 Modules are then addressed by
modulename.function()
 sys.argv # list of arguments
 filename = os.path.splitext("points.txt")
 filename[1] # equals ".txt"

22
Files
 Files are manipulated by creating a file object
 f = open("points.txt", "r")
 The file object then has new methods
 print f.readline() # prints line from file
 Files can be accessed to read or write
 f = open("output.txt", "w")
 f.write("Important Output!")
 Files are iterable objects, like lists

23
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

24
Python
 Python is a real-world, production language that is freely available
for most computers.
http:www.python.org

 If you want a copy of Python to use with this course, go to

http://code.google.com/p/mediacomp-jes/ .

We are using JES (Jython Environment for Students) which has a


lot of special multimedia functionality.

 Note: Our textbook covers a limited amount of Python. There are


many excellent online tutorials. For example, see
http://en.wikibooks.org/wiki/Non-Programmer's_Tutorial_for_Python/Contents
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.
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.
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.
Sequential Operations
“Atomic”

 Input
 Computation
 Output
The Big Plan
 We want to get some experience of programming simple
algorithms in a real programming language. This gives us an
understanding of how software is written and allows us to
test our algorithms to see if they work.

 We’ll first work with programs where the variables have


numbers as values.

 Later we’ll work with programs dealing with pictures and


sound.

 In lab we’ll work with some simple statements and small


programs.
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


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.


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.
Integer Expressions
 Integer expressions are formed using

 Integer Constants

 Integer Variables

 Integer Operators

 Parentheses
Python Assignment Statements
 In Python, = is called the assignment operator and
an assignment statement has the form

<variable> = <expression>

 Here
 <variable> would be replaced by an actual variable
 <expression> would be replaced by an expression

 Python: age = 19


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
Python Session
What about floats?
 When computing with floats, / will indicate regular
division with fractional results.

 Constants will have a decimal point.

 14.0/5.0 will give 2.8 while 14/5 gives 2.


Comments
 Often we want to put some documentation in our
program. These are comments for explanation, but not
executed by the computer.

 If we have # anywhere on a line, everything following


this on the line is a comment – ignored
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
Example - Fahrenheit to Centigrade
 We want to convert a Fahrenheit temperature to
Centigrade.

 The formula is C = (F -32) x 5/9

 We use type float for the temperatures.


Python Session
What is Python Class And Object?
A class is a collection of objects, and an object is defined as an instance of class possessing attributes.
The object is an entity that has state and behavior. A class has all the similar attributes, like if we have a
class students, then it will only consist of students related data, such as subjects, names, attendance ratio,
etc.

Along with classes and objects, you will learn many new terminologies related to OOP in further tutorials.
Some of these terminologies are:

 Instances
 Constructor
 Methods
 Abstraction
 Inheritance

By using oop, we can divide our code into many sections known as classes. Each class holds a distinct
purpose or usage. For example, if we have created a class named "Books" then all the attributes it
possesses should be related to books such as the number of pages, publishing date or price, etc.
There is no limit to the number of classes we can create in a program. Also, one class can be easily
accessible by another, and we can also restrict the access of a class so other classes can not use its
functions. This concept comes in handy while working on bigger projects. All the employees are given
separate tasks to work on the classes they have been assigned. And after they are done with their
contribution, the classes can be combined as a whole to form a complete project. So, now you can
understand that to become a successful programmer, you must master the concept of OOP.
Object-oriented vs. Procedure-oriented
Programming
Index Object-oriented programming Procedure Oriented Programming
Object-oriented programming is the problem-solving It is Structure oriented. Procedural programming uses a
1 approach. The computation is done by using list of instructions. It performs the computation step by
objects. step.
When the project becomes lengthy, it is not easy to
2 OOP makes development and maintenance easier.
maintain the code.
OOP provides a proper way for data hiding. It is Procedural programming does not provide any proper
3 more secure than procedural programming. You way for data binding, so it is less secure. In Procedural
cannot access private data from anywhere. programming, we can access the private data.
4 Program is divided into objects The program is divided into functions.
In this tutorial, we have discussed the basics of object-oriented programming. In the next tutorial Creating Our
First Class in Python, we will start implementing the OOP concepts.
Project
Library management
# return book
# AbhishekGiri = Library(listofbooks, library_name)
#dictionary (books-nameofperson)

# create a main function and run an infinite while loop asking


# users for their input

class Library:
def __init__(self, list, name):
self.booksList = list
self.name = name
self.lendDict = {}

def displayBooks(self):
print(f"We have following books in our library: {self.name}")
for book in self.booksList:
print(book)

def lendBook(self, user, book):


if book not in self.lendDict.keys():
self.lendDict.update({book:user})
print("Lender-Book database has been updated. You can take the book now")
else:
print(f"Book is already being used by {self.lendDict[book]}")

def addBook(self, book):


self.booksList.append(book)
print("Book has been added to the book list")

def returnBook(self, book):


self.lendDict.pop(book)

if __name__ == '__main__':
Giri = Library(['Python', 'English ', 'Mathematics', 'C Basics', 'java'], "AbhishekGiri")

while(True):
print(f"Welcome to the {Giri.name} library. Enter your choice to continue")
print("1. Display Books")
print("2. Lend a Book")
print("3. Add a Book")
print("4. Return a Book")
user_choice = input()
if user_choice not in ['1','2','3','4']:
print("Please enter a valid option")
continue

else:
user_choice = int(user_choice)

if user_choice == 1:
Giri.displayBooks()
elif user_choice == 2:
book = input("Enter the name of the book you want to lend:")
user = input("Enter your name")
Giri.lendBook(user, book)

elif user_choice == 3:
book = input("Enter the name of the book you want to add:")
Giri.addBook(book)

elif user_choice == 4:
book = input("Enter the name of the book you want to return:")
Giri.returnBook(book)

else:
print("Not a valid option")

print("Press q to quit and c to continue")


user_choice2 = ""
while(user_choice2!="c" and user_choice2!="q"):
user_choice2 = input()
if user_choice2 == "q":
exit()

elif user_choice2 == "c":


continue
49
Thankyou

50

You might also like