Python iter() — A Simple Illustrated Guide with Video

Python’s built-in iter() function returns an iterator for the given object. For example, iter([1, 2, 3]) creates an iterator for the list [1, 2, 3]. You can then iterate over all elements in the iterator, one element at a time, in a for or while loop such as: for x in iter([1, 2, 3]). Basic … 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

Python oct() Function

Python’s built-in oct(integer) function takes one integer argument and returns an octal string with prefix “0o”. If you call oct(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

Python property() — What You Always Wanted to Know But Never Dared to Ask

Object-orientation is great way to encapsulate data in your application. This minimizes complexity and adheres to good software engineering principles. However, attributes in Python can be easily accessed from the outside—they’re not really encapsulated. That’s one of the reason the property() built-in function exists: it allows you to truly encapsulate data with the means of … Read more

Python id() Function

Python’s built-in id(object) function takes a Python object as an input and returns the identity of an object that is a static, unique integer. The identity is static, it never changes throughout the program’s execution, and unique, no other object has the same identity. It is implemented in cPython by returning the address of the … Read more

Python vars() Function

Python’s built-in vars() function returns the __dict__ attribute of an object—a dictionary containing the object’s changeable attributes. Without argument, it returns the local symbol table similar to locals(). Python’s built-in vars() function returns a dictionary of name: value mappings of all the names defined in the local scope or the scope of the optional object … Read more