Python is Operator — Checking Identity

The Python is keyword tests if the left and right operands refer to the same object—in which case it returns True. It returns False if they are not the same object, even if the two objects are equal. For example, the expression [1, 2, 3] is [1, 2, 3] returns False because although both lists … Read more

Python Version Anzeigen

Als Guido van Rossum im Jahr 1991 die erste nutzbare Python Version 0.9.0 veröffentlichte konnte er nicht erwarten, dass er im Begriff war eine der einflussreichsten Programmiersprachen in der Welt zu erschaffen. Python hat eine goldene Zukunft: jede neue Python Version erweitert die vohergehende mit neuen Fähigkeiten und Funktionen. Um deine Python version anzuzeigen, führe … Read more

How to Serialize a Python Dict into a String and Back?

Problem Formulation Given a Python dictionary containing lists and other data structures. You want to store the dictionary in a file or send it over the network in a more efficient form. How to serialize a Python dictionary into a string, and then deserialize the string back to a dictionary data structure? Here’s a rough … Read more

How to End a Function Def in Python?

Problem Formulation Given a function definition in Python, starting with the keyword def: How to know when a “def” of a function ends? For example, in Java and C++, functions are enclosed with opening and closing parentheses {…}, so the ending of a function is not ambiguous. Ending a Function Syntactically In Python, whitespace indentation … Read more

How to Rename Column Names in Pandas?

Problem Formulation Given a Pandas DataFrame with column labels, and a list of new column names as strings. How to change the column names to replace the original ones? Here’s an example using the following DataFrame: You want to rename the column names [‘Col_A’, ‘Col_B’, ‘Col_C’] to [‘a’, ‘b’, ‘c’] so that the resulting DataFrame … Read more

NumPy Boolean Indexing

You can index specific values from a NumPy array using another NumPy array of Boolean values on one axis to specify the indices you want to access. For example, to access the second and third values of array a = np.array([4, 6, 8]), you can use the expression a[np.array([False, True, True])] using the Boolean array … Read more