How to Open a File in Python (Modes, Encoding, Paths)

How to open a file in Python: Command Prompt output of open() in x, a, r and w modes

You open a file in Python with the built-in open() function: pass it the file path, a mode such as “r” or “w”, and an encoding. Wrap it in a with block so Python closes the file for you, even when an error happens: My test machine for this page runs Python 3.12.5 on Windows … Read more >>

How to Add Days to a Date in TypeScript (setDate and DST)

TypeScript add days to date: Command Prompt comparing setDate with adding 86400000 ms across daylight saving time

Copy the Date, then call setDate(getDate() + days) on the copy: that’s how you add days to a date in TypeScript. JavaScript rolls the month and year over for you, and negative numbers go back in time: Every result here came from TypeScript 7.0.2 and Node.js v22.22.2 in strict mode, with the time zone set … Read more >>

How to Check Array Equality in TypeScript (5 Ways)

TypeScript array equality: Command Prompt output of a deepEqual check on two arrays of objects

To check array equality in TypeScript, compare the lengths and then every element by index. === on two arrays only tells you whether they’re the same object, so build a small typescript array equality helper: Strict mode was on for all of these snippets, with TypeScript 7.0.2 and Node.js v22.22.2 doing the work, so what … Read more >>

Python Import from Another Directory (5 Ways That Work)

Python import from another directory: ModuleNotFoundError fixed by running the app with python -m

For a Python import from another directory, the cleanest fix is to make the folder a package and run your script as a module from the project root with python -m. Python then finds sibling folders automatically: I built that small project for real and ran every command with Python 3.12.5 in Command Prompt, errors … Read more >>

os.path.join() in Python: Join Paths the Right Way

os path join in Python: Command Prompt output of os.path.join with absolute paths and drive letters

os.path.join() in Python combines folder and file names into one path, using the right separator for your operating system: a backslash on Windows and a forward slash on macOS and Linux. My machine runs Python 3.12.5 on Windows, so the screenshots show backslashes; I also use posixpath to show what macOS and Linux would print. … Read more >>

How to Concatenate Arrays in TypeScript Using concat()

Concatenate arrays in TypeScript: Command Prompt output of joining two order arrays with concat()

concat() is the built-in way to concatenate arrays in TypeScript: a.concat(b) returns a new array with every item of a followed by every item of b, and neither original changes. The spread syntax […a, …b] does the same job: Every snippet was compiled and run with TypeScript 7.0.2 and Node.js v22.22.2 in strict mode, so … Read more >>

How to Find the Length of an Array in TypeScript

TypeScript array length: Command Prompt output of counting orders with filter().length

To find the length of an array in TypeScript, read its length property: arr.length returns the number of elements as a number, and an empty array gives 0. There’s no size() or count() method on arrays: I ran every snippet with TypeScript 7.0.2 and Node.js v22.22.2 in strict mode, so the output you see is … Read more >>

Check if a TypeScript Array Contains a Specific Value

Check if a TypeScript array contains a specific value: Command Prompt output of includes() on an array of cities

To check if a TypeScript array contains a specific value, call includes(): cities.includes(“Denver”) returns true or false. That works for strings, numbers and booleans. For an array of objects it will not, because includes() compares by reference, so you test a property with some() instead: orders.some(o => o.id === “ORD-1001”). This guide covers both, plus … Read more >>

How to Get Distinct Values from an Array in TypeScript

TypeScript distinct values: Command Prompt output of new Set removing repeated state codes from an array

To get distinct values from an array in TypeScript, pass the array to a Set and spread it back: […new Set(arr)]. A Set keeps each value once, in first-seen order, and TypeScript keeps the element type, so a string[] stays a string[]: I compiled these snippets with TypeScript 7.0.2 running on Node.js v22.22.2. One of … Read more >>

How to Find Prime Numbers in a Range Using Python

Find prime numbers in a range using Python: Command Prompt output listing the prime years from 2000 to 2030

The simplest way to find prime numbers in a range using Python is a loop over every number from low to high that keeps the ones that have no divisor between 2 and their square root. One list comprehension does it: I ran every listing below with Python 3.12.5 and SymPy 1.14.0 and pasted the … Read more >>