Python Forum
Need explanation of one line of code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need explanation of one line of code
#1
I am working through a tutorial trying to learn Python. I have come across a line of code I cannot decipher.

I have asked the developer of the code and their explanation ignored my question. My question seems too basic for them to address.

I have simplified the code to the following:

GPIO.output(22,((j == 1<<i) and GPIO.HIGH or GPIO.LOW))

The index i rotates through 0 to 3

The index j rotates through 0x01, 0x02, 0x03 and 0x04

I know the code sends a "High" or "Low" to GPIO pin 22.

I do not understand the code: (j == 1<<i)
it seems to be checking for equality between a hex number "j" and "1<<i"

Questions:
1. How does this work? In other words, what is it saying?

2. Once evaluated, how does it interact with the following "and" and "or" statement?

3.Is there a simpler way to write this line of code?
Reply


Messages In This Thread
Need explanation of one line of code - by Fliberty - Feb-17-2020, 03:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Explanation of code ejKDE 4 544 Feb-26-2024, 02:50 PM
Last Post: ejKDE
  Unable to understand the meaning of the line of code. jahuja73 0 408 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  A better explanation of the last post Led_Zeppelin 9 2,582 Sep-20-2022, 05:08 PM
Last Post: deanhystad
  Operator meaning explanation Sherine 3 2,151 Jul-31-2021, 11:05 AM
Last Post: Sherine
  how long can a line of code be? Skaperen 2 2,293 Jun-09-2021, 06:31 PM
Last Post: Skaperen
  Explanation of except ... as : Fernando_7obink 2 2,031 Feb-13-2021, 04:45 AM
Last Post: deanhystad
  .maketrans() - a piece of code which needs some explanation InputOutput007 5 3,108 Jan-28-2021, 05:05 PM
Last Post: buran
  .remove() from a list - request for explanation InputOutput007 3 2,331 Jan-28-2021, 04:21 PM
Last Post: InputOutput007
  I need a code line to spam a keyboard key | Image detection bot Aizou 2 3,233 Dec-06-2020, 10:10 PM
Last Post: Aizou
  Explanation of the left side of this statement please rascalsailor 3 2,607 Sep-09-2020, 02:02 PM
Last Post: rascalsailor

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020