How to Print a Dictionary Without Brackets in Python?

Problem Formulation Given a dictionary of key value pairs in Python. If you print the dictionary to the shell using print({‘a’: 1, ‘b’: 2}), the output is enclosed in curly brackets (braces) like so: {‘a’: 1, ‘b’: 2}. But you want the dictionary without brackets like so: ‘a’: 1, ‘b’: 2. How to print the … Read more

Python Check Version of Package with pip

Problem Formulation Assuming you have the Python package manager pip installed in your operating system (Windows, Linux, macOS). How to check the version of a package with pip? Method 1: pip show To check which version of a given package is installed, use the pip show <your_package> command. For example, to check the version of … Read more

How to Generate Random Integers in Python?

The most idiomatic way to create a random integer in Python is the randint() function of the random module. Its two arguments start and end define the range of the generated integers. The return value is a random integer in the interval [start, end] including both interval boundaries. For example, randint(0, 9) returns an integer … Read more

How to Kill a While Loop with a Keystroke in Python?

To end a while loop prematurely in Python, press CTRL-C while your program is stuck in the loop. This will raise a KeyboardInterrupt error that terminates the whole program. To avoid termination, enclose the while loop in a try/except block and catch the KeyboardInterrupt. You can see the idea in the following code snippet: The … Read more