.index() vs. where()
.index() is the builtin function for finding the index of something in a list or tuple. This does not work with NumPy arrays! NP arrays rely on where() to return an array of indices that match the condition.
—————————
for-else statement
‘else’ can be used after a for-loop. But here, the ‘else’ statement only executes if the for-loop completes all iterations. Think of it as using the for-loop to search for something, or expecting a break from the loop. If nothing is resolved or no break occurs then the else statement can be used for notification. The else can be used after a try-except in a similar way; executing only when nothing breaks down, throwing an exception.
—————————
how to prevent a new-line character in a print() statement
Use a comma at the end. You can have consecutive print() statements print to the same line simply by adding a comma at the end of each line.
—————————
Use a dictionary for formatting a long output string
%20s will format a string in the list after a text. But when using this formatting method, the order must match in the supplied list. %(keyword)20s or %(keyword)10f with a supplied dictionary can avoid the ordering issue and having to have the same variable or value in a list multiple times.
—————————
use an asterisk * in front of a functions input argument to accept an infinite number of args.
def MyFunction( *args ): This will accept any number of arguments and create a list to use within the function.
—————————
ternary operation or inline if (iif)
In Java you can use a iif like this ( x == 2 ? y = 4 : y = 0 ).
( condition ? if true do this : if false do this)
I found that Python also has a version of this but ordered differently:
(do this) if (condition) else (do this if false)
Not the same order as java but still pretty straight forward.