Python Float Decimal to Octal

Problem Formulation: Given a float number. How to convert it to octal representation? Examples: Consider the following desired conversions from float decimal numbers to their converted float octal numbers. input: 3.14 output: 3.1075 input: 0.01 output: 0.005 input: 12.325 output: 14.246 You can play with some examples here: Solution: The following code function float_to_octal() takes … Read more

Python open() Function — An 80/20 Guide By Example

Python’s built-in open() function opens a file and returns a file object. The only non-optional argument is a filename as a string of the file to be opened. You can use the file object to access the file content. For example, file_obj.readlines() reads all lines of such a file object. Here’s a minimal example of … Read more

Python super() – A Simple Illustrated Guide

Python’s built-in super() method returns a temporary object of the superclass to help you access its methods. Its purpose is to avoid using the base class name explicitly. It also enables your class to inherit from multiple base classes. Visual Idea super() The idea is simple: use super() to call the methods defined in the … Read more

Python input() Function

Python’s built-in input() function reads a string from the standard input. The function blocks until such input becomes available and the user hits ENTER. You can add an optional prompt string as an argument to print a custom string to the standard output without a trailing newline character to tell the user that your program … Read more

Python memoryview() — Tame That Strange Beast!

The Python memoryview(arg) function returns a memoryview object of the given bytes or bytearray argument. This exposes the argument’s internal data (buffers) to help you access the data without intermediate copying. Syntax: memoryview(object) Arguments object Bytes or Bytearray object for which the memoryview should be returned Return Value memoryview Returns memoryview of the object. Python … Read more

Python hash() Function

Python’s built-in hash(object) function takes one object as an argument and returns its hash value. As the hash value is calculated based on the object’s data, two different but equal objects must have the same hash value. It doesn’t follow, though, that two objects with the same hash value are equal—they can have the same … Read more

Python hex() Function — Not a Magic Trick

Python’s built-in hex(integer) function takes one integer argument and returns a hexadecimal string with prefix “0x”. If you call hex(x) on a non-integer x, it must define the __index__() method that returns an integer associated to x. Otherwise, it’ll throw a TypeError: object cannot be interpreted as an integer. Argument integer An integer value or … Read more