The Underscore in Python [“_” vs “__”]

The single underscore in Python “_” is used to either make a variable different from a Python keyword such as in float_=8, or to indicate that it should be used in a private context such as in _var=8. The double underscore in Python “__” (called “dunder“) is used to make an instance attribute or method … Read more

Python Pass Statement

The pass statement does nothing when executed. You can use it as a placeholder for future code so that you can focus on the high-level structure first and implement the details later. For example, functions, loops, class definitions, or if statements require at least one statement in their indentation block. Think of the pass statement … Read more

How to Print a Percentage Value in Python?

To print a percentage value in Python, use the str.format() method or an f-string on the format language pattern “{:.0%}”. For example, the f-string f”{your_number:.0%}” will convert variable your_number to a percentage string with 0 digits precision. Simply run those three basic statements in your shell: your_number = 0.42 percentage = “{:.0%}”.format(your_number) print(percentage) As a … Read more

Iterators, Iterables and Itertools

Iterables and iterators are everywhere in Python. We usually aren’t aware of the iterators because the syntax of python hides them from us. Almost every time we manipulate a sequence type (strings, lists, tuples, sets, arrays, etc.), we’re using an iterator behind the scenes. An iterable represents a sequence of values each of which is … Read more