E4 – Python programmingGeoinformatics: Geol 4123/6123
In all of the following questions assume arcpy is imported!
- Assuming arcpy is imported, print whether or not (Boolean yes or no) the streams.shp dataset exists in your C:/Data folderby calling the exists() function
- printarcpy.Exists. C:/Data/streams.shp”
- print arcpy.Exists (“C:/Data/streams.shp”)
- print arcpy.Exists = C:/Data/streams.shp”
- print arcbpy.Exists (C:/Data/streams.shp”) = 1
- Retrieve messages through the GetMessages function and print them:
- print arcpy.GetMessages
- Print arcpy.getMessages
- Print arcpy = GetMessages
- print arcpy.GetMessages()
- Assign the number of messages to the count variable from the GetMessageCount function
- Count = GetMessageCount
- count.GetMessageCount()
- count = arcpy.GetMessageCount()
- Count = Arcpy.GetMessageCount
- Describe the streams.shp shape file and store it in the desc variable
- desc.arcpy.Describe (“C:/Data/streams.shp”)
- desc. arcpy.Describe. “C:/Data/streams.shp”
- desc = arcpy.Describe (“C:/Data/streams.shp”)
- desc.arcpy.Describe = “C:/Data/streams.shp”
- Run the Clip tool if the shape type is polygon
- if type == “polygon”:
arcpy.Clip_analysis (infc, clipfc, outfc)
- if type = “polygon”:
arcpy.Clip_analysis (infc, clipfc, outfc)
- If type == “polygon”:
arcpy.Clip_analysis = (infc, clipfc, outfc)
- if type = “polygon”:
arcpy.Clip_analysis.(infc, clipfc, outfc)
- Get the list of feature classes and assign it to the fcList variable
- fcList = arcpy.ListFeatureClasses ()
- fcList == arcpy.ListFeatureClasses ()
- fcList = arcpy.ListFeatureClasses
- fcList = arcpy (ListFeatureClasses)
- Loop through the list of fields and print the field name followed by a blank and the string casted field length
- for field in fieldList
print field.name. + “ “ + str (field.length)
- for field fieldList:
print field.name. = + “ “ + str (field.length)
- for field in fieldList
print field.name. + “ “ + “strfield.length”
- for field in fieldList:
print field.name. + “ “ + str (field.length)
- Print the length of the feature classes
- print fcs = len (arcpy.ListFeatureClasses ())
- print len (fcs = arcpy.ListFeatureClasses ())
- print len (fcs == arcpy.ListFeatureClasses ())
- print len (fcs.arcpy.ListFeatureClasses ())
- Use a for loop to iterate over the rows in a table, and print the fourth row
- for row in cursor
Print row[3]
- for row in cursor:
print row[5]
- for row in cursor:
print row[3]
- for row in cursor:
print row(4)
- Insert five rows in a table using a loop. Assume x = 1
- while x<=5:
cursor.insertRow (“New Street”)
- While x<=5
cursor.insertRow (“New Street”)
- while x=5:
cursor.insertRow (“New Street”)
- While x<=5:
cursor.insertRow = “New Street”
- Loop through a table and delete the first row if its value is “Main ST”.
- for row in cursor
If row [0] == “Main ST”:
cursor.deleteRow ()
- for row in cursor:
if row [0] = “Main ST”:
cursor.deleteRow ()
- for row in cursor:
if row [0] == “Main ST”
cursor (deleteRow ())
- for row in cursor:
if row [0] == “Main ST”:
cursor.deleteRow ()
- Open the Sample.txt file and assign it to the variable f
- f == open (“C:/Data/Sample.txt”)
- f = open (“C:/Data/Sample.txt”)
- f = open.C:/Data/Sample.txt
- f = open (C:/Data/Sample.txt)
- Assign a point feature to the point variable
- point == arcpy.Point (x, y)
- point = arcpy.Point [x, y]
- point = arcpy.Point (x, y)
- point = arcpy (Point) (x, y)
- Assign a point geometry to a variable called pointGeometry. Assume point is a variable to a point object
- pointGeometry = arcpy.pointgeometry (point)
- pointGeometry = arcpy[PointGeometry (point)]
- pointGeometry == arcpy.PointGeometry (point)
- pointGeometry = arcpy.PointGeometry (point)
- Open a map document and assign it to a variable
- mapDoc = arcpy.mapping.MapDocument (C:/Data/GeoMap.mxd)
- mapDoc = arcpy.mapping.MapDocument (“C:/Data/GeoMap.mxd”)
- mapDoc == arcpy.mapping.MapDocument (“C:/Data/GeoMap.mxd”)
- mapDoc = arcpy.mapping(MapDocument (“C:/Data/GeoMap.mxd”))
- Assign a layer file to the lyr variable
- lyr = arcpy.mapping.Layer (“C:/Data/road.lyr”)
- lyr = arcpy.mapping.Layer “C:/Data/road.lyr”
- lyr = arcpy.mapping.Layer (C:/Data/road.lyr)
- lyr = arcpy.mapping(Layer (“C:/Data/road.lyr”))
- Print the layer name in a loop. Assume lyrList is a variable pointing to a list of layers
- for lyr in lyrList
print lyr.name
- for lyr in lyrList:
printlyr(name)
- for lyr in lyrList:
print lyr.name
- for lyr in lyrList
print (lyr =name)
- Turn on the labels for the layer
- lyr.showLabels == True
- lyr= showLabels (True)
- lyr.ShowLabels == True:
- lyr.showLabels = True
- If layer name is School
- if lyr.name = “School”:
- if lyr.name == “School”:
- if lyr.name = “School”
- if lyr.name( “School”):
- Print the first message
- arcpy.GetMessage = 0
- arcpy.GetMessage [0]
- arcpy.GetMessage ==0
- arcpy.GetMessage (0)
- Write code to ask the user to enter his/her last name:
- lastName == input("Please enter your last name >")
- lastName = input ("Please enter your last name >")
- lastName.input = ("Please enter your last name >")
- lastName = input (Please enter your last name >)
- Call a function called pow () by passing a number a to be powered with a second number b. Assign the result to a variable called result.
- Import.math> result== math.Pow (3, 2) > print (result)
- > Import math> result= math(pow (3, 2)) > print (result)
- > Import math> result= math.pow (3, 2) > print (result)
- > Import math> result== math(pow (3, 2)) > print (result)
- Python code to print an empty,vertical rectangle.
- > rec = (9*'_' + "\n|\t|\n\n|\t|\n\n|\t|\n" _ 9*'_')> print rec
- > rec = (9*'_' "\n|\t|\n\n|\t|\n\n|\t|\n" + 9*'_')> print rec
- > rec = (9*'_' + "\n|\t|\n\n|\t|\n\n|\t|\n" + 9*)> print rec
- > rec = (9*'_' + "\n|\t|\n\n|\t|\n\n|\t|\n" + 9*'_')> print rec
- Write Mississippi
- > print ("Mi"+2*'s'+'i'+2*'s'+'i'+2*'p'+'i')
- > print ('Mi' + 2*(2*'s')+'i'+2*'p'+'i')
- a and b
- > print (Mi + 2*(2*'s')+'i'+2*'p'+'i')
- Replace Bush with Obama
- > print (bush.replace ('Bu', 'Oba') + bush.replace ('sh', 'ma'))
- > print (bush.replace ('Bush', 'Oba') + bush.replace ('sh', 'ma'))
- > print (bush.replace ('Bush', 'Oba') + bush.replace ('Bush', 'ma'))
- > print (bush.replace ('Bush', 'Oba'),bush.replace ('Bush', 'ma'))
- Reverse the letter of the symmetric words:kayak, level, and radar, and print the reversed version:
- > words = ('k','a','y','a','k','l','e','v','e','l','r','a','d','a','r') > words.reverse() > print words
- > words = “'k','a','y','a','k','l','e','v','e','l','r','a','d','a','r'” >words.reverse() > print words
- > words = ['k','a','y','a','k','l','e','v','e','l','r','a','d','a','r'] > words.reverse() > print words
- > words == 'k','a','y','a','k','l','e','v','e','l','r','a','d','a','r' > words.reverse() > print words
- Define a tuple for the solar calendar seasons (Spring is first season) and print” Fall is season 3
- > (Spring, Summer, Fall, Winter) = (1, 2, 3, 4) > print ("Fall is season " + Fall)
- > (Spring, Summer, Fall, Winter) = (1, 2, 3, 4) > print "Fall is season " + Fall
- > (Spring, Summer, Fall, Winter) = [1, 2, 3, 4] > print ("Fall is season ") + Fall
- > (Spring, Summer, Fall, Winter) = (1, 2, 3, 4) > print ("Fall is season "), Fall
- Define a dictionary for the Geoinformatics and GIS courses, and print the dictionary into a set.
- coursePrefix = {'Geoinformatics', 'Geol 4123/6123', 'GIS', 'GEOG 4532/6532'}
> print copy.coursePrefix
- coursePrefix = {'Geoinformatics', 'Geol 4123/6123', 'GIS', 'GEOG 4532/6532'}
> print coursePrefix.copy ()
- coursePrefix = ('Geoinformatics', 'Geol 4123/6123', 'GIS', 'GEOG 4532/6532')
> print coursePrefix.copy ()
- coursePrefix = {'Geoinformatics', 'Geol 4123/6123' + 'GIS', 'GEOG 4532/6532'}
> print coursePrefix.copy {}
- When would the second print in the following code write?
year = 1990
> while year<2000 :
... print ("The year is in the 20th Century: "), year
... year +=2
... else :
... print ("The year is in the 20th Century: "), year
... year +=2
- When year is 2006
- When year is 2010
- When year is 2000
- When year is 2002
- Which lines of code finds the numbers that are divisible by 6?
- > numbers = [7, 15, 23, 36, 6]
> for i in numbers: if (i%6=0) :print ("The number is divisible by 6"), i - > numbers = [7, 15, 23, 36, 6]
> for i in numbers: if (i%6==0) : print ("The number is divisible by 6") + i - > numbers = [7, 15, 23, 36, 6]
> for i in numbers: if (i%6==0) : print ("The number is divisible by 6"), i - > numbers = [7, 15, 23, 36, 6]
> for x in numbers: if (i%6==0) : print ("The number is divisible by 6"), i