0

So I'am reading a book named Tkinter-by-example. I am fairly new to programming in python. So while I'am typing a sample code in this book I noticed that there is an unfamiliar code to me.

colour_schemes = [{"bg": "lightgrey", "fg": "black"}, {"bg": "grey", "fg": "white"}]

_, task_style_choice = divmod(len(self.tasks), 2)

my_scheme_choice = colour_schemes[task_style_choice]

I understand how the code works and it runs but for some specific reason whenever I remove this part of the code "_," and i try entering a text this error shows.

TypeError: list indices must be integers or slices, not tuple

I'm using python version 3.6.3 and I can't seem to find any problems related to mine can someone explain to me what "_," this do to my code.

Thank you!

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
amtw123
  • 73
  • 7

1 Answers1

6

The underscore is a valid variable name and is commonly used to imply that the value will be unused. Only the second value of the pair returned by divmod will be used via the task_style_choice variable.

_, task_style_choice = divmod(len(self.tasks), 2)
jspcal
  • 50,847
  • 7
  • 72
  • 76