How to Convert a Tensor to a NumPy Array in TensorFlow?

There are two ways to convert a Tensor to a NumPy array: TensorFlow version 2.x — use tensor.numpy() TensorFlow version 1.x — use tensor.eval(session=tf.compat.v1.Session()) Let’s dive into these two methods in greater detail. Method 1: Explicit Tensor to NumPy Array Conversion in TensorFlow 2.x To convert a tensor t to a NumPy array in TensorFlow … Read more

Inheritance in Python

You have the eyes of your mother. One could say, you “inherited” the eyes of your mother. As you may have guessed, this article is about inheritance in Python. Inheritance is one of the most important features of object orientation. It’s a simple and intuitive concept but even advanced coders circumvent using inheritance because they … 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

How to Stop a While Loop in Python

Python provides three ways to stop a while loop: The while loop condition is checked once per iteration. If it evaluates to False, the program ends the loop and 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 … Read more

Python Set Methods [Video Guide]

The set data structure is one of the most underused primary data structures in Python. I’ve seen so many intermediate-level coders who use lists when all they need is a data structure that checks membership!! ? Understanding the basics differentiates the good from the great. In this tutorial, you’ll learn about Python’s basic set methods. First, … Read more

A Simple Example of Python Objects and Classes [+Video]

In Python, everything is an object. Even integers are objects — this is different from programming languages like Java where integers, floats, and Booleans are primitive data types. In this way, Python is built on a rigorously consistent object-oriented paradigm. Considering that objects are everywhere in Python, object-oriented programming is not well studied by Python … Read more

Python Set symmetric_difference()

Python’s S.symmetric_difference(T) method creates and returns a new set containing all elements that are in exactly one of the two sets S and T. Here’s a minimal example where we return a new set containing the elements 1 and 4 that are in exactly one of the two sets s and t. Here’s another visual … Read more

Static and Dynamic Attributes in Python – What’s the Difference?

Quick Answer: Static attributes are variables defined once for the class and shared by all instances. Dynamic attributes are variables defined for individual instances only. Static variables are used as a “fall-back” when there are no explicit dynamic attributes defined for the instances. When you try to “overwrite” a static attribute such as in x.attr … Read more

Python Set copy()

Python’s set.copy() method creates and returns a flat copy of this set. Here’s a minimal example where you copy a set with two integers and a string value: Syntax Let’s dive into the formal syntax of the set.copy() method. set.copy() Argument Data Type Explanation – – – The set.copy() method doesn’t take any argument. If … Read more

Python Set difference_update()

Python’s set.difference_update(*args) method removes all elements from this set that are members of any of the given set arguments. For example, s.difference_update({1, 2}) removes elements 1 and 2 from the set s. Its return value is None because it modifies the set it is called upon rather than creating a new set. Here’s a minimal … Read more