Decoding data from a MIDI controller as eight buttons I thought about using loops but I don't know how I would do this. if I used for loops would it work the same way as the code down below?        

if data[-8]=="B" and data[-5]=="1" and data[-4]=="6":
    print("Button1")
if data[-8]=="B" and data[-5]=="1" and data[-4]=="7":
    print("Button2")
if data[-8]=="B" and data[-5]=="1" and data[-4]=="8":
    print("Button3")
if data[-8]=="B" and data[-5]=="1" and data[-4]=="9":
    print("Button4")
if data[-8]=="B" and data[-5]=="1" and data[-4]=="A":
    print("Button5")
if data[-8]=="B" and data[-5]=="1" and data[-4]=="B":
    print("Button6")
if data[-8]=="B" and data[-5]=="1" and data[-4]=="C":
    print("Button7")
if data[-8]=="B" and data[-5]=="1" and data[-4]=="D":
    print("Button8")


EDIT: When I press a button it prints out the number ex. If I press button one it prints button one. And it would work the same for other at and given time. It'll be on a while true loop.
Use a dict for lookups
When doing lookups, Python provides an associative array data structure called a dict.  Your stacked ifs can be cleanly written as a dict lookup like:
button_text = {
    (&quot;B&quot;, &quot;1&quot;, &quot;6&quot;): &quot;Button1&quot;,
    (&quot;B&quot;, &quot;1&quot;, &quot;7&quot;): &quot;Button2&quot;,
    (&quot;B&quot;, &quot;1&quot;, &quot;8&quot;): &quot;Button3&quot;,
    (&quot;B&quot;, &quot;1&quot;, &quot;9&quot;): &quot;Button4&quot;,
    (&quot;B&quot;, &quot;1&quot;, &quot;A&quot;): &quot;Button5&quot;,
    (&quot;B&quot;, &quot;1&quot;, &quot;B&quot;): &quot;Button6&quot;,
    (&quot;B&quot;, &quot;1&quot;, &quot;C&quot;): &quot;Button7&quot;,
    (&quot;B&quot;, &quot;1&quot;, &quot;D&quot;): &quot;Button8&quot;,
}

print(button_text[data[-8], data[-5], data[-4]])