Intro
to Python
Python is a high-level programming language
High level languages use vocabulary, syntax, and semantics that are
similar to those in natural languages
As opposed to lower level languages like assembly or machine
The python designers have used a �multi-paradigm� approach that works
equally well with structured or object-oriented programming techniques
We will explore both types
They also have made �ease of use� a design priority
Unlike Java, python does not require you to �declare� the type of a
variable before you assign it a value
The context of a variable at run time determines its type
This can lead to unexpected behaviors for new programmers:
a = 5 / 3��� # a is assigned a value of 1
b = 5.0 / 3.0 # a
is assigned 1.666667
The types of a and b (int and float) are
determined at runtime
Creating and running (executing) python programs in
Idle
Idle is a relatively simple development environment
Editor, runtime environment
We will write our �standalone� python programs using Idle
We use Idle here because it has few installation restrictions for public
classrooms
You may prefer to work in other environments, like Sublime Text, later
on
We will use other techniques later on when we write python programs
(scripts) that link with the ArcPython environment in
ArcGIS
Writing Python code using the interpreter command
line
We will be using python version 2.7
This version works with ArcPython and is often
the version preferred by professionals in many fields
Later versions (like 3.5) have incompatibilities with 2.7
This is an easy way to try out programming techniques
Not a great way to write practical programs, however
Follow
the instructions here for an intro to writing and running python programs
with Idle
If you want to keep things as simple as possible, after starting the
Anaconda command prompt, type python
You will see something similar to the text below:
You can now enter python code at the >>> prompt
Try this:
>>> 5 / 3
1 should print in
the window
When you�re working at the command prompt, you can just type in
operations or variable names to see their values
If you get 1.6666667, you are probably running python 3.5
Now try:
>>> a = 5
/ 3
>>> a
And now:
>>> a =
5.0 / 3.0
>>> a
These are the basics of using the command line interpreter
General rules
You can�t print the value of a variable before you have assigned
something to it
Typing the up arrow key goes backwards in command history (try it)
Once you�ve found an older command, you can edit it and hit Enter
Although you can use the print command to print a value, typing the name
of a variable or constant will also print it
This only works with the command line interpreter; it won�t work in the
IDEs
Why use the command line interpreter?
No fuss, no muss: nothing fancy to get in between you and the
interpreter
Nice for trying out new commands, programming styles, etc
You can quit the python command line by typing ctrl-z or quit() at the command
line
Why not use command line python all the time? See the following�
Writing and running python code from a full IDE
Integrated Development
Environment
An IDE is an application that lets you edit and run (and possibly debug)
your code
You can use IDEs like Sublime Text to do this
Although Idle is simpler than Sublime Text, we will still be able to use
it as an editor and a runtime environment
Much easier than working with the command line for real projects
Basics of Idle
To start Idle from the Anaconda command prompt, type idle
You will see something like this:
The command prompt works mostly the same as the regular command prompt,
however:
The command history feature uses different keys by default
On Windows PCs, Alt-P brings up the previous command, Alt-N the next
On Mac OS X, fn-Option-P and fn-Option-N do the same things
You can change the key bindings from the Idle menu under Options�Configure IDLE�Keys (PC) or python�Preferences�Keys (Mac)
Check the documentation if you really want to do this�
File�New File creates a new
file
In this class, always save your file immediately with a .py extension, e.g. hello.py
If you don�t, then you won�t be able to run your code or see special
Python editing cues
Try this:
Create a new file (using File�New)
from the Idle menu
Save it somewhere as hello.py
Add this line to your file (with no indentations):
print 'hello'
On a Windows machine, press F5
On a Mac, press fn-F5
You should see hello in the command
line window (the console)
The console is the part of Idle that displays program output
By the way, python users generally refer to a single python file as a module
File�Open is the best way
to load an existing python module
Once you open a module, you can run it (if it�s error-free)
It�s possible to run a module from the Idle command line like this (a PC
example):
>>> execfile('c:\\temp\\test.py')
this is a test
>>>
But that�s just showing off�File�Open is generally more useful (no need
to worry about operating system fussiness)
Why 2 backslashes in the file path?
In most python implementations, a backslash character works as an
�escape�
The character following the backslash then has a special interpretation
For example, \t represents a tab
character
So python interprets the perfectly good path
C:\temp\test.py
As the bad path
C:\[tab character]emp[tab character]est.py
It turns out that�
\\ is a way of representing a backslash character
without the magic
Try using one backslash instead of 2 and see what kinds of errors you
get when you try to run the execfile command