Understanding slice notation

It’s pretty simple really: There is also the step value, which can be used with any of the above: The key point to remember is that the :stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements selected (if step is 1, the default). The other feature is that start or stop may be a negative number, which … Read more

Understanding the main method of python [duplicate]

The Python approach to “main” is almost unique to the language(*). The semantics are a bit subtle. The __name__ identifier is bound to the name of any module as it’s being imported. However, when a file is being executed then __name__ is set to “__main__” (the literal string: __main__). This is almost always used to separate the portion of code which should … Read more

how to sort pandas dataframe from one column

Use sort_values to sort the df by a specific column’s values: If you want to sort by two columns, pass a list of column labels to sort_values with the column labels ordered according to sort priority. If you use df.sort_values([‘2’, ‘0’]), the result would be sorted by column 2 then column 0. Granted, this does not really make sense for this example because … Read more

How to define a two-dimensional array?

You’re technically trying to index an uninitialized array. You have to first initialize the outer list with lists before adding items; Python calls this “list comprehension”. #You can now add items to the list: Note that the matrix is “y” address major, in other words, the “y index” comes before the “x index”. Although you … Read more

Iterating over dictionaries using ‘for’ loops

key is just a variable name. will simply loop over the keys in the dictionary, rather than the keys and values. To loop over both key and value you can use the following: For Python 3.x: For Python 2.x: To test for yourself, change the word key to poop. In Python 3.x, iteritems() was replaced with simply items(), which returns a set-like … Read more

Understanding slice notation

It’s pretty simple really: There is also the step value, which can be used with any of the above: The key point to remember is that the :stop value represents the first value that is not in the selected slice. So, the difference between stop and start is the number of elements selected (if step is 1, the default). The other feature is that start or stop may be a negative number, which … Read more