How to Sum the Digits of a Number in Python?

Problem Formulation Given an integer number. How to sum over all digits of this number to compute the crossfoot (cross total)? Consider the following examples: 12 –> 1+2 = 3 123 –> 1+2+3 = 3 244 –> 2+4+4 = 10 981223 –> 9+8+1+2+2+3 = 25 Method 1: sum() with Generator Expression The Python built-in sum(iterable) … Read more

Python Scrapy – Scraping Dynamic Website with API-Generated Content

Scrapy is an excellent tool for extracting data from static and dynamic websites. In this article, we are going to discuss the solution to the following problems: Extract all details of Offices from the website https:/directory.ntschools.net/#/offices Instead of using a whole scrapy framework, use a typical Python script for extracting the data. For each office, … Read more

How to Stop a For Loop in Python

Python provides three ways to stop a for loop: The for loop ends naturally when all elements have been iterated over. After that, Python proceeds with the first statement after the loop construct. The keyword break terminates a loop immediately. The program proceeds with the first statement after the loop construct. The keyword continue terminates … Read more

How to Convert a Float List to a String List in Python

The most Pythonic way to convert a list of floats fs to a list of strings is to use the one-liner fs = [str(x) for x in fs]. It iterates over all elements in the list fs using list comprehension and converts each list element x to a string value using the str(x) constructor. This … Read more

How to Suppress Warning Messages in Python

Problem Formulation and Solution Overview This article will show you how to suppress warning messages in Python scripts. Warning messages in Python are commonly used to notify the coder of a potential issue with their script. Warnings can inform the coder about any outdated or obsolete elements. An example would be if a plugin or … Read more

[List] How to Check Package Version in Python

If you’re short on time, the simple solution is to run the following command in your shell/terminal/CMD/PowerShell to check the version of library xxx: But many more interesting ways to check package versions may be useful for you! The following table refers to a list of articles to help you check the package/library/module version installed … Read more