[Solved] Shortening the following if statement?


As was mentioned by cco:

if data.char in "123456789"
    data.board[row][col] = int(data.char)

The in operator evaluates to true if it finds a variable in the specified sequence and false otherwise. It can be used for lists, strings, tuples and dictionaries. However the in will only check the keys of the dictionary – not the values.

If you wanted ints for example, you could declare a list of numbers and use that instead.

if num in [1,2,3,4,5,6,7,8,9]:
    do something

or alternatively:

if num in range(1,9):
    do something

1

solved Shortening the following if statement?