<?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 Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-if-statements/m-p/1181956#M64745</link>
    <description>&lt;P&gt;if/elif/else are good constructs and I'll just add if you have a lot of values the if/elifs can get long and get hard to follow.&amp;nbsp; To get around this, you can use a dictionary lookup.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def f(y, p2020, p2021):
  fieldDict = {2020: p2020, 2021: p2021}
  # gets the key (y) or returns 0 if there is no paring.
  return fieldDict.get(y, 0)

PASER_CLR = 
f(!RATINGYEAR!, !PASER2020!, !PASER2021!)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 10 Jun 2022 16:56:39 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2022-06-10T16:56:39Z</dc:date>
    <item>
      <title>Python If Statements</title>
      <link>https://community.esri.com/t5/python-questions/python-if-statements/m-p/1181917#M64742</link>
      <description>&lt;P&gt;I am trying to do a simple if statement in ArcMap to calculate a new field. It takes the year that an attribute was rated from the "[RatingYear]" field and depending on that value, chooses the corresponding "[PASER&amp;lt;year&amp;gt;]" field value.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to learn more about Python coding, but can't get this to translate to Python. Is it possible to set a variable to a field value in Python?&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/43231i4BC3A6D479112FE8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description>
      <pubDate>Fri, 10 Jun 2022 15:46:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-if-statements/m-p/1181917#M64742</guid>
      <dc:creator>nbreese9</dc:creator>
      <dc:date>2022-06-10T15:46:26Z</dc:date>
    </item>
    <item>
      <title>Re: Python If Statements</title>
      <link>https://community.esri.com/t5/python-questions/python-if-statements/m-p/1181932#M64743</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Codeblock:&lt;/STRONG&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def reclass(RY,P20,P21):
    if (RY == 2020):
        return P20
    elif (RY == 2021):
        return P21
    else:
        return 0&lt;/LI-CODE&gt;&lt;P&gt;&lt;STRONG&gt;Expression:&lt;/STRONG&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;reclass(!RATINGYEAR!,!PASER2020!,!PASER2021!)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jun 2022 16:13:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-if-statements/m-p/1181932#M64743</guid>
      <dc:creator>JayantaPoddar</dc:creator>
      <dc:date>2022-06-10T16:13:27Z</dc:date>
    </item>
    <item>
      <title>Re: Python If Statements</title>
      <link>https://community.esri.com/t5/python-questions/python-if-statements/m-p/1181939#M64744</link>
      <description>&lt;P&gt;The syntax for fields is different, it uses ! instead of []. I find that it's better to call a function and pass the values into it so that I don't have to deal with the weird syntax in my own code, here is a snapshot for an example.&lt;/P&gt;&lt;P&gt;I called the variables var1 and var2 in the function and call the function in the expression below.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 579px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/43236i5E75A69338922B70/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Translating your code would look something like this&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def f(y, p2020, p2021):
  if y == 2020:
    return p2020
  elif y == 2021:
    return p2021
  return 0

PASER_CLR = 
f(!RATINGYEAR!, !PASER2020!, !PASER2021!)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It keeps what I am doing separate from the dataset then too in my mind it's less confusing.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jun 2022 16:25:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-if-statements/m-p/1181939#M64744</guid>
      <dc:creator>Brian_Wilson</dc:creator>
      <dc:date>2022-06-10T16:25:00Z</dc:date>
    </item>
    <item>
      <title>Re: Python If Statements</title>
      <link>https://community.esri.com/t5/python-questions/python-if-statements/m-p/1181956#M64745</link>
      <description>&lt;P&gt;if/elif/else are good constructs and I'll just add if you have a lot of values the if/elifs can get long and get hard to follow.&amp;nbsp; To get around this, you can use a dictionary lookup.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def f(y, p2020, p2021):
  fieldDict = {2020: p2020, 2021: p2021}
  # gets the key (y) or returns 0 if there is no paring.
  return fieldDict.get(y, 0)

PASER_CLR = 
f(!RATINGYEAR!, !PASER2020!, !PASER2021!)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Jun 2022 16:56:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-if-statements/m-p/1181956#M64745</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-06-10T16:56:39Z</dc:date>
    </item>
  </channel>
</rss>

