Skip to content

Instantly share code, notes, and snippets.

@knu2xs
Created May 2, 2014 20:36
Show Gist options
  • Select an option

  • Save knu2xs/9599316d6c8f9889ea3b to your computer and use it in GitHub Desktop.

Select an option

Save knu2xs/9599316d6c8f9889ea3b to your computer and use it in GitHub Desktop.
Python basics
#### Python has many built-in functions
# len() - Returns the length
fc = "Railroads.shp"
print len(fc)
fields = ["OID", "Shape", "Name"]
print len(fields)
#max() - Returns the maximum value
##xExtent = (6260474.996464, 6338807.996464)
##print max(xExtent)
# round() - Rounds a number
##yCoord = 1811884.623964
##print round(yCoord)
# dir() - Returns the attributes of a given object
# i.e., the list of built-in functions or the functions within a module
##print dir(__builtins__)
##help() - returns the help for a given object
import arcpy
print help(arcpy.Buffer_analysis)
# str() - converts a value to a string
##gdbPath = "C:\\SouthAfrica.gdb"
##fc = "Roads"
##fullPath = gdbPath + "\\" + fc + str(1)
##print fullPath
## No declaration keyword
## No type assignment
## Variables are case sensitive, can hold numbers, and expressions
distance = 500
Distance = 600
new_distance = 500 + 100
print new_distance
## Variables can hold strings.
## Must surround string value in either double (") or single (') quotes
folder = "C:/Student"
inputFC = r"C:\Student\DEMOS\PYTH\Pensacola.gdb\Parks"
folder = r"C:\Student Data\GIS"
folder = "C:/Student/data"
folder = "C:\\Student\\data"
##Can embed one string in another. This is quite common for queries
whereClause = "\"STREET_NAME\" = 'Catalina'"
#Strings can be combined together
gdbPath = "C:\\SouthAfrica.gdb"
fc = "Roads"
date = 2013
fullPath1 = gdbPath + "\\" + fc + str(date)
fullpath2 = "%s\\%s%i" %(gdbPath, fc, date)
print fullPath1
print fullpath2
## Strings are indexed; indexing starts at 0
##fc = "Street.shp"
##newFC = fc[:-4]
##print newFC
##fc1 = fc[0:6]
##print fc1
## Variables can hold lists
## Lists are created by assigning a variable to a series of values
## separated by commas and embedded in brackets
##myList = [1, 2, 3]
##fcList = ["Roads", "Streets", "Parcels", "Zipcodes"]
## Lists are indexed; indexing starts at 0
##fc1 = fcList[1]
##fc2 = fcList[0:2]
##fc3 = fcList[0:-1]
##fc4 = fcList[2:]
#print fc1
#print new
#print fc2
#print fc3
#print fc4
##fcList.insert(0, "Railroads")
##print fcList
## Variables can hold dictionaries
## Dictionaries are created by assigning a variable to a series of
## "key: value" pairs separated by commas and embedded in curly braces
##myDictionary = {"food":["pancakes", "butter"], "quantity": 4, "place":"Cracker Barrel"}
## Instead of indexing, values are accessed or fetched by using the key
#value1 = myDictionary["food"]
#value2 = myDictionary["quantity"]
#value3 = myDictionary["place"]
#print value1
#print value2
#print value3
#print value4
# Variables can hold tuples
# Tuples are created by assigning a variable to a series of values
# separated by commas and embedded in parentheses
# Tuples are very similar to lists except tuples CANNOT be changed once created
##myTuple = (1, 2, 3)
##del myTuple
#Tuples are indexed; indexing starts at 0
##tuple1 = myTuple[0]
##tuple2 = myTuple[2]
##print tuple1
##print tuple2
## To gain access to a function that is not built-in, you must import
## the associated module
#import math
#print dir(math)
#print help(math.sqrt)
#print math.sqrt(64)
#print math.pow(10,2)
####import os.path
####print os.path.basename("C:\\Student\\Streets.shp")
####print os.path.dirname("C:\\Student\\Streets.shp")
####
import time
print time.strftime("%c")
print time.strftime("%A %B %d, %Y")
##Decision making syntax: if...elif...else
##Rules:
##Colons used at end of each condition
##Indentation defines what executes for each condition
##Two equal signs for conditions, one for assignment
#x = 3
#if x == 1:
# print "x is 1"
#elif x == 2:
# print "x is 2"
#else:
# print "x is not 1 or 2"
#x = 1 #assignment
#y = 8 + 2 #assignment
#if x == 6: #testing a condition, not assigning a value
##Looping syntax
##Python has 3 types of loops: the while loop, the list loop, the counter loop
##Rules:
##Colons used at the end of each statement
##Indentation defines what executes for the loop
##while loop
#x = 5
#while x < 10:
# print x
# x = x + 1
##list loop
#x = [1, 2, 3]
#for num in x:
# print num
##counted loop
#for num in range(1, 5):
# print num
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment