dir() versus __dir__() – What’s the Difference?

Problem Formulation What’s the difference between the built-in dir() function and __dir__ dunder method in Python? Quick Answer Python’s built-in function dir(object) returns a list of the object’s attribute names and method names. The dir() function is a wrapper around the __dir__() method because it internally calls the object’s dunder method object.__dir__(). But the two … Read more

How to Restart a Loop in Python?

Problem Formulation Given a Python loop and a logical condition. How to restart the loop if the condition is met? Solution 1: Reset While Loop The while loop checks a condition in order to determine whether the loop body should be executed or not. You can reset the condition to the initial value in order … Read more

How to Convert a String to a Dictionary in Python?

Problem Formulation Given a string representation of a dictionary. How to convert it to a dictionary? Input: ‘{“1”: “hi”, “2”: “alice”, “3”: “python”}’ Output: {‘a’: 1, ‘b’: 2, ‘c’: 3} Method 1: eval() The built-in eval() function takes a string argument, parses it as if it was a code expression, and evaluates the expression. If … Read more

How to Convert a Unicode String to a Dictionary in Python?

Problem Formulation Given a Unicode string representation of a dictionary. How to convert it to a dictionary? Input: u”{‘a’: 1, ‘b’: 2, ‘c’: 3}” Output: {‘a’: 1, ‘b’: 2, ‘c’: 3} Note: The u’string’ representation represents a Unicode string that was introduced in Python 3. This is redundant as all strings in Python 3 are … Read more

Python List of Dunder Methods

When searching for a list of dunder methods, I only found a couple of resources all over the web—each covering only a fraction of dunder methods. So, here’s my collection of 127 dunder methods (also called “magic methods”) from those sources, sorted alphabetically: __abs__ __add__ __aenter__ __aexit__ __aiter__ __and__ __anext__ __await__ __bool__ __bytes__ __call__ __ceil__ … Read more

Python Version Backward Compatibility

The Python language does not generally provide backward compatibility. The ability to break inefficiencies and fix wrong design choices are major reasons why Python has remained lean and efficient over the last decades. However, the PEP 387 standard discusses that incompatibility issues should be well thought out. Here’s the basic policy when backward incompatibilities may … Read more

Python Version AWS Lambda

The following table provides an overview of the Python versions supported by AWS SDK for Python: Name Operating system Python 3.9 Amazon Linux 2 Python 3.8 Amazon Linux 2 Python 3.7 Amazon Linux Python 3.6 Amazon Linux Python 2.7 Amazon Linux Boto3 is the Software Development Kit (SDK) for Python from Amazon Web Services (AWS). … Read more

The Complete Python Library Guide

This article is about a topic that is far more important and even more fundamental than any specific Python tutorial: Libraries. What Are the Top 10 Python Libraries? The following list reflects the most important Python libraries based on my experience: Pandas. “Excel for coders”. NumPy. Fundamental to many libraries in the data science and … Read more

How to Install Jupyter Notebook on PyCharm

1. Create a new Python project and install the jupyter package using the command pip install jupyter in the “Terminal” view. 2. Open or create a Jupyter notebook that is a file with the suffix .ipynb. 3. Add cells and execute them directly in PyCharm.

How to Check the Python Version at Runtime?

Method 1: sys.version To check your version at runtime in your code, import the sys module and print the sys.version attribute to your Python shell: Method 2: sys.version_info If you need an easy-to-process output for the major, minor, and micro versions, use the sys.version_info attribute. For example, Python version 3.9.5 has major version 3, minor … Read more

How to Install Kite on PyCharm?

Kite is an intelligent code-autocompletion tool that helps you write source code faster and more efficiently. This short tutorial will show you how to install it in PyCharm. Open your PyCharm IDE with the latest PyCharm version. Go to File > Settings > Plugin. Search for “Kite” in the Marketplace tab. And click “Install”. Restart … Read more