<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Python If Statements in Field Calculator in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70804#M5791</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="color: black;"&gt;Great. It works for me. Very much appreciated&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: black;"&gt;["Extreme", "High", "Moderate", "Low", "Very Low"][!Value!-1]&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: black;"&gt;&lt;IMG alt="" class="jive-emoji image-1 jive-image j-img-original" src="https://community.esri.com/legacyfs/online/488308_Clip_438.jpg" /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Apr 2020 12:13:55 GMT</pubDate>
    <dc:creator>JamalNUMAN</dc:creator>
    <dc:date>2020-04-12T12:13:55Z</dc:date>
    <item>
      <title>Python If Statements in Field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70793#M5780</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am trying to convert a nested IF statement from Excel into Python via the Field Calculator in ArcMap. The data is wind data which has two fields: U and V; and the calculation is to be entered in the DIR field. These are vectors can be applied with triganometry to create wind direction in degrees. In Excel, the DIR field has the following formula:&lt;/P&gt;&lt;P&gt;=IF(D2&amp;gt;0,((180/PI())*ATAN(C2/D2)+180),IF(AND(C2&amp;lt;0,D2&amp;lt;0),((180/PI())*ATAN(C2/D2)),IF(AND(C2&amp;gt;0,D2&amp;lt;0),((180/PI())*ATAN(C2/D2)+360),IF(AND(D2=0,C2&amp;gt;0),270,IF(AND(D2=0,C2&amp;lt;0),90,0)))))&lt;/P&gt;&lt;P&gt;...where column C is the U field and column D is the V field.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've been playing all day with this and so far, I have the following:&lt;/P&gt;&lt;P&gt;codeblock:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def winddir(DIR):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if !V! &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; !DIR! = ((180/math.pi)*math.atan(!U!/!V!)+180)
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif !U! &amp;lt; 0 and !V! &amp;lt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; !DIR! = ((180/math.pi)*math.atan(!U!/!V!))
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif !U! &amp;gt; 0 and V &amp;lt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; !DIR! = ((180/math.pi)*math.atan(!U!/!V!)+360)
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif !V! == 0 and !U! &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; !DIR! = 270
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif !V! == 0 and !U! &amp;lt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; !DIR! = 90&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DIR =&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;winddir(DIR)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I keep getting the generic error:&lt;/P&gt;&lt;P&gt;ERROR 000989: Python syntax error: Parsing error SyntaxError: invalid syntax (line 2)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm really pulling my hair out with this as I've tried tweaking the syntax but I cannot resolve it. Any assistance would be greatly appreciated.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;DK&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:43:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70793#M5780</guid>
      <dc:creator>DarrochKaye</dc:creator>
      <dc:date>2021-12-10T22:43:17Z</dc:date>
    </item>
    <item>
      <title>Re: Python If Statements in Field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70794#M5781</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import math
def winddir(DIR, V, U):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if V &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return (180/math.pi) * math.atan(U/V) + 180
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif U &amp;lt; 0 and V &amp;lt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return (180/math.pi) * math.atan(U/V)
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif U &amp;gt; 0 and V &amp;lt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return (180/math.pi) * math.atan(U/V) + 360
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif V == 0 and U &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 270
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif V == 0 and U &amp;lt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 90
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
winddir(!DIR!, !V!, !U!)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="http://desktop.arcgis.com/en/arcmap/latest/manage-data/tables/calculate-field-examples.htm#ESRI_SECTION1_81CD39A7B56F4BC5874E41C759F51C9F" title="http://desktop.arcgis.com/en/arcmap/latest/manage-data/tables/calculate-field-examples.htm#ESRI_SECTION1_81CD39A7B56F4BC5874E41C759F51C9F" rel="nofollow noopener noreferrer" target="_blank"&gt;Calculate Field examples—Help | ArcGIS for Desktop&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:43:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70794#M5781</guid>
      <dc:creator>FreddieGibson</dc:creator>
      <dc:date>2021-12-10T22:43:20Z</dc:date>
    </item>
    <item>
      <title>Re: Python If Statements in Field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70795#M5782</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import math
k = (180/math.pi)

def winddir(v,u):
&amp;nbsp;&amp;nbsp;&amp;nbsp; v = float(v) 
&amp;nbsp;&amp;nbsp;&amp;nbsp; u = float(u)
&amp;nbsp;&amp;nbsp;&amp;nbsp; if v &amp;gt; 0:&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result = (180/math.pi)*math.atan(u/v)+180)&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif u &amp;lt; 0 and v &amp;lt; 0:&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result = ((180/math.pi)*math.atan(u/v))&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif u &amp;gt; 0 and v &amp;lt; 0:&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result = ((180/math.pi)*math.atan(u/v)+360)&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif v == 0 and u &amp;gt; 0:&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result = 270&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; elif v == 0 and u &amp;lt; 0: 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; result = 90&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; else: result = -1
&amp;nbsp;&amp;nbsp;&amp;nbsp; return result

#-------------------&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; windir(!V!,!U!)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Line 21 belongs to the expression-line, L1- 18&amp;nbsp; is part of the codeblock&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So the expression in L21 calls the &lt;EM&gt;windir&lt;/EM&gt; function (L4-18) for every feature in the table with the current values of the fields &lt;EM&gt;V&lt;/EM&gt; and &lt;EM&gt;U&lt;/EM&gt;.&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Dir&lt;/EM&gt; is the targetfield for the result of the function. For the function itself it's not nessesary.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Line 1&amp;amp;2 will be evaluated only one time at the start of calculation.&lt;/P&gt;&lt;P&gt;The following lines will be evaluated as a part of the &lt;EM&gt;windir&lt;/EM&gt;-function every feature/record&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;L4: The function to calculate the wind direction needs only two parameters: The values of the fields &lt;EM&gt;U&lt;/EM&gt; and &lt;EM&gt;V&lt;/EM&gt;.&lt;/P&gt;&lt;P&gt;L5&amp;amp;6: You have to make shure, that the variables have the correct datatype.&lt;/P&gt;&lt;P&gt;L8,10,12,14&amp;amp;17: The variable is defined by the calculation and at ...&lt;/P&gt;&lt;P&gt;L18 at least by the word &lt;SPAN style="font-family: courier new,courier;"&gt;return&lt;/SPAN&gt; the function has the instruction to&amp;nbsp; "give the value back to the one who calls you up"&lt;/P&gt;&lt;P&gt;It's more "pythonical" to do it like Freddie, but for beginners/examples&amp;nbsp; my contruction is easyer to understand.&lt;/P&gt;&lt;P&gt;L17: Every time you&amp;nbsp; use an if statemant, IMO you have to use &lt;EM&gt;else&lt;/EM&gt; at the end to capture all other oportunities.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:43:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70795#M5782</guid>
      <dc:creator>LotharUlferts</dc:creator>
      <dc:date>2021-12-10T22:43:22Z</dc:date>
    </item>
    <item>
      <title>Re: Python If Statements in Field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70796#M5783</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Uups, because you calculate only with integer it's better to use int() then float().&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 11 Mar 2016 20:43:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70796#M5783</guid>
      <dc:creator>LotharUlferts</dc:creator>
      <dc:date>2016-03-11T20:43:37Z</dc:date>
    </item>
    <item>
      <title>Re: Python If Statements in Field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70797#M5784</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks to both for the feedback. Can I ask why the fields are only referenced in the expression line and not in the codeblock? I'm currently teaching myself Python so I haven't covered all the theory yet!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Also, as I have ~50 shapefiles to apply this code to, I've run Freddie's code in Field Calculator which worked well (I've added some extra elif's at the end to cover all possibilities. However, when I copy the successful code as a Python snippet from the Results window, amend the shapefile name and run again, I get a similar syntax issue as before. The code I'm trying to run from the Python snippet is below:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code _jivemacro_uid_14579469712228975 jive_text_macro" data-renderedposition="138_8_1332_64" jivemacro_uid="_14579469712228975"&gt;&lt;P&gt;arcpy.CalculateField_management("New Group Layer/09-03-2016 02_00_00","DIR","winddir(!DIR!, !V!, !U!)","PYTHON_9.3","import math&amp;nbsp; /ndef winddir(DIR, V, U):&amp;nbsp; /n&amp;nbsp;&amp;nbsp;&amp;nbsp; if V &amp;gt; 0:&amp;nbsp; /n&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return (180/math.pi) * math.atan(U/V) + 180&amp;nbsp; /n&amp;nbsp;&amp;nbsp;&amp;nbsp; elif U &amp;lt; 0 and V &amp;lt; 0:&amp;nbsp; /n&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return (180/math.pi) * math.atan(U/V)&amp;nbsp; /n&amp;nbsp;&amp;nbsp;&amp;nbsp; elif U &amp;gt; 0 and V &amp;lt; 0:&amp;nbsp; /n&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return (180/math.pi) * math.atan(U/V) + 360&amp;nbsp; /n&amp;nbsp;&amp;nbsp;&amp;nbsp; elif V == 0 and U &amp;gt; 0:&amp;nbsp; /n&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 270&amp;nbsp; /n&amp;nbsp;&amp;nbsp;&amp;nbsp; elif V == 0 and U &amp;lt; 0:&amp;nbsp; /n&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 90/n&amp;nbsp;&amp;nbsp;&amp;nbsp; elif U == 0 and V &amp;gt; 0:&amp;nbsp; /n&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 180&amp;nbsp; /n&amp;nbsp;&amp;nbsp;&amp;nbsp; elif U == 0 and V &amp;lt; 0:&amp;nbsp; /n&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0 /n&amp;nbsp;&amp;nbsp;&amp;nbsp; elif U == 0 and V == 0:&amp;nbsp; /n&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0")&lt;/P&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've only changed one digit in the file name from the Python snippet (01_00_00 to 02_00_00) in the above code and I'm getting the following error:&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;Runtime error&amp;nbsp; Traceback (most recent call last):&amp;nbsp;&amp;nbsp; File "&amp;lt;string&amp;gt;", line 1, in &amp;lt;module&amp;gt;&amp;nbsp;&amp;nbsp; File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\management.py", line 3354, in CalculateField&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; raise e ExecuteError: ERROR 000989: Python syntax error: Parsing error SyntaxError: invalid syntax (line 1) &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #303030;"&gt;Thanks again for your assistance.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #303030;"&gt;DK&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 14 Mar 2016 09:19:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70797#M5784</guid>
      <dc:creator>DarrochKaye</dc:creator>
      <dc:date>2016-03-14T09:19:36Z</dc:date>
    </item>
    <item>
      <title>Re: Python If Statements in Field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70798#M5785</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Because &lt;EM&gt;DIR&lt;/EM&gt; doesnt appear in the &lt;EM&gt;winddir&lt;/EM&gt; function , I removed the parameter in the &lt;EM&gt;def&lt;/EM&gt;-statement. &lt;/P&gt;&lt;P&gt;I guess you're working in the python-window, so please try this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;arcpy.env.workspace= "A:\\test" # Change to the data source directory
tables = arcpy.ListFeatureClasses('*') #all shapes inside the directory
#ALTERNATIVE if tables wanted
# tables = arcpy.ListFiles('*.dbf') #all dBase-files inside the directory&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
len(tables) # just to controll, should be ~50

for table in tables: #loop through all files
&amp;nbsp;&amp;nbsp;&amp;nbsp; #
&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.UpdateCursor(table,["DIR","U","V"]) as cursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in cursor: #loop through all rows
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; u,v = row[-2:]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row[0] = winddir(u,v) #new DIR-value associated
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor.updateRow(row) #rewrite the row in the table&lt;/PRE&gt;&lt;P&gt;You can replace line 11&amp;amp;12&amp;nbsp; by this expression:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;row[0] = winddir(*row[-2:])&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 22:43:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70798#M5785</guid>
      <dc:creator>LotharUlferts</dc:creator>
      <dc:date>2021-12-10T22:43:25Z</dc:date>
    </item>
    <item>
      <title>Re: Python If Statements in Field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70799#M5786</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;SPAN style="color: #3d3d3d; font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif;"&gt;Thanks to both for the feedback. Can I ask why the fields are only referenced in the expression line and not in the codeblock? I'm currently teaching myself Python so I haven't covered all the theory yet!&lt;/SPAN&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;The reason for this is that the Calculate Field expression is interpreted row by row as you go through the table - the field expression !FIELDNAME! are converted to a value, say, 1024.4, and then that expression is executed.&amp;nbsp;&amp;nbsp; A similar thing is done with the VBScript parser with a different syntax [FIELDNAME]. (I recommend sticking with the Python parser.)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The code in the function is not involved in this field name to value substitution process which is why you use real variables in the code block (passed to the function as arguments).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;There are nice examples in the help that make more sense if you know this. There's a help article on Calculate Field examples that I refer to again and again! I recommend bookmarking it!&lt;/P&gt;&lt;P&gt;&lt;A href="http://desktop.arcgis.com/en/arcmap/10.3/manage-data/tables/calculate-field-examples.htm" title="http://desktop.arcgis.com/en/arcmap/10.3/manage-data/tables/calculate-field-examples.htm"&gt;Calculate Field examples—Help | ArcGIS for Desktop&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Calculate Field is much easier to use in ModelBuilder and from tool dialogs.&amp;nbsp; Cursors are often a better approach in scripting and at the python command line.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 04 May 2016 04:44:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70799#M5786</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2016-05-04T04:44:26Z</dc:date>
    </item>
    <item>
      <title>Re: Python If Statements in Field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70800#M5787</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="color: black;"&gt;As the “VB” is no longer available within the “field calculator” tool, I couldn’t figure out how to write a code for the if statement in Python for the 5 cases shown in the screenshot below&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: black;"&gt;&lt;IMG alt="" class="jive-emoji image-1 jive-image j-img-original" src="https://community.esri.com/legacyfs/online/487512_Clip_305.jpg" /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: black;"&gt;&lt;IMG alt="" class="jive-emoji jive-image image-2 j-img-original" src="https://community.esri.com/legacyfs/online/487513_Clip_306.jpg" /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 04 Apr 2020 20:08:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70800#M5787</guid>
      <dc:creator>JamalNUMAN</dc:creator>
      <dc:date>2020-04-04T20:08:51Z</dc:date>
    </item>
    <item>
      <title>Re: Python If Statements in Field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70801#M5788</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Jamal,&lt;/P&gt;&lt;P&gt;If they are that sequential (1-5) you can do this one line Python expression:&lt;/P&gt;&lt;PRE class="language-python line-numbers"&gt;&lt;CODE&gt;&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;&lt;SPAN class="string token"&gt;"Extreme"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"High"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"Moderate"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"Low"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;,&lt;/SPAN&gt; &lt;SPAN class="string token"&gt;"Very Low"&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;[&lt;/SPAN&gt;!FIELD!&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;‍ &lt;SPAN class="operator token"&gt;-&lt;/SPAN&gt; &lt;SPAN class="number token"&gt;1&lt;/SPAN&gt;&lt;SPAN class="punctuation token"&gt;]&lt;/SPAN&gt;  &lt;SPAN class="comment token"&gt;# corrected&lt;/SPAN&gt;&lt;SPAN class="line-numbers-rows"&gt;&lt;SPAN&gt;‍&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Apr 2020 23:36:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70801#M5788</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2020-04-07T23:36:43Z</dc:date>
    </item>
    <item>
      <title>Re: Python If Statements in Field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70802#M5789</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="color: black;"&gt;Many thanks Curtis for the help.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: black;"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: black;"&gt;The code ends up with an error. What could be the issue here?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: black;"&gt;&lt;IMG alt="" class="jive-emoji image-1 jive-image j-img-original" src="https://community.esri.com/legacyfs/online/487958_Clip_368.jpg" /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 Apr 2020 19:11:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70802#M5789</guid>
      <dc:creator>JamalNUMAN</dc:creator>
      <dc:date>2020-04-08T19:11:22Z</dc:date>
    </item>
    <item>
      <title>Re: Python If Statements in Field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70803#M5790</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;OOPS in Python it's zero based indexes&amp;nbsp; ([1,2,3][0] == 1) so use [!Value! - 1]&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will edit my post to be correct.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 Apr 2020 19:15:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70803#M5790</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2020-04-08T19:15:19Z</dc:date>
    </item>
    <item>
      <title>Re: Python If Statements in Field Calculator</title>
      <link>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70804#M5791</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="color: black;"&gt;Great. It works for me. Very much appreciated&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: black;"&gt;["Extreme", "High", "Moderate", "Low", "Very Low"][!Value!-1]&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: black;"&gt;&lt;IMG alt="" class="jive-emoji image-1 jive-image j-img-original" src="https://community.esri.com/legacyfs/online/488308_Clip_438.jpg" /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Apr 2020 12:13:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-if-statements-in-field-calculator/m-p/70804#M5791</guid>
      <dc:creator>JamalNUMAN</dc:creator>
      <dc:date>2020-04-12T12:13:55Z</dc:date>
    </item>
  </channel>
</rss>

