Python print() Function: Syntax and 5 Examples

Python print() function printing text, numbers and variables in one call

If you want to show values on the screen in Python, the Python print function is what you’re looking for. It takes one or more objects, turns each into a string, adds a space between them and ends the line. You’ll rarely need more, but four keyword arguments let you change the separator, the line … Read more >>

Finite State Machine in Python (4 Examples)

Finite state machine in Python: Command Prompt output of an order moving from pending to delivered

A finite state machine in Python is a set of states, plus rules saying which event moves you from one state to the next. The simplest version is an Enum for the states and a dictionary of transitions: Each of these state machines ran on Python 3.12.5, using an online store’s order workflow as the … Read more >>

How to Convert a Pandas DataFrame to a TensorFlow Dataset or Tensor

Convert a pandas DataFrame to a TensorFlow Dataset: Command Prompt output of a dict of columns sliced into a tf.data.Dataset

To turn a pandas DataFrame into a TensorFlow Dataset, pass its columns and the label Series to tf.data.Dataset.from_tensor_slices(). You can then train a model on it in Python. My first try, tf.convert_to_tensor(df), failed with a ValueError: the file mixes text and numbers, and a tensor holds one dtype. A dict of columns doesn’t have that … Read more >>

TypeScript Sort Array Descending: Numbers, Copies and Objects

TypeScript sort array descending: VS Code running sort((a, b) => b - a) on sales numbers

If you need the biggest numbers first in a TypeScript array, sort the array in descending order using a compare function: sales.sort((a, b) => b – a). Without it, sort() compares numbers as text, and 15000 lands before 2300. In this tutorial, you’ll learn to sort a number array in descending order using TypeScript, keep … Read more >>

TypeScript to Lowercase: Convert a String with toLowerCase()

TypeScript to lowercase: VS Code running toLowerCase() on a signup email

To convert a string to lowercase in TypeScript, call toLowerCase(): it returns a new lowercase string and leaves the original alone. It’s handy for email addresses, which people type in every style. In this tutorial, I’ll cover how to convert a string to lowercase in TypeScript, then compare and search without case, handle undefined, fix … Read more >>

What Is IDLE in Python? (Shell, Editor and Tips)

What is IDLE in Python: the real IDLE Shell window running Python commands

What is IDLE in Python? It’s the Integrated Development and Learning Environment, the simple editor and interactive shell that ships with Python itself. It’s written in Python with tkinter, so it runs the same on Windows, macOS and Linux: All the screenshots here are from the real IDLE that came with Python 3.12.5 on Windows … Read more >>

Singleton Design Pattern in Python (4 Ways)

Singleton design pattern in Python: Command Prompt output of a metaclass returning the same Database object

The singleton design pattern in Python makes sure a class only ever has one instance, and every call hands back that same object. The quickest version overrides __new__ to store and reuse the instance: Each implementation below ran on Python 3.12.5, and I checked the “same object” claim with is every time. We’ll start with … Read more >>

TypeScript Check Empty Array: Null, Undefined and Not Empty

TypeScript check empty array: VS Code running a cart length === 0 check

To check for an empty array in TypeScript, use cart.length === 0, and use !cart?.length when the cart can also be null or undefined. In this tutorial, I’ll cover the ways to check if array is empty in TypeScript, how to check that it’s not empty, and why if (cart) never works. Every output below … Read more >>

TypeScript Empty Array: Declare, Initialize and Type It

TypeScript empty array: VS Code running typed empty arrays declared four ways

To create a TypeScript empty array, give the variable an element type and assign []. Skip the type and strict mode infers any[] or never[], which bites you later: You get a real array with length 0 that only accepts strings. In this tutorial, I’ll cover every way to set up a TypeScript empty array, … Read more >>

How to Flatten an Array of Arrays in TypeScript

TypeScript flatten array of arrays: VS Code running a recursive generic deepFlatten function

If your data comes as a TypeScript array of arrays, such as one inner array of totals per day, you can flatten it into one list. A nested loop works, but flat() does it in one call. The real trouble came later: a lib error and a loosely typed flat(Infinity) result. Quick answer: call flat() … Read more >>