Difference Between {} and [] in Python

Difference Between {} and [] Python

When I build a small reporting script, I often need two very different things: a sequence of report rows and a quick way to look up details by an ID. Python uses [] for one of those jobs and {} for the other, so choosing the wrong brackets can cause confusing errors later. The difference between {} and [] in Python looks small … Read more >>

Difference Between List and Dictionary in Python

Difference between list and dictionary in Python: Command Prompt timing of a membership check in a list vs a dict

Say you run a small store in Columbus, Ohio. Today’s register log has four scans, and your catalog prices three SKUs at $24.99, $89.50 and $12.75. That’s the difference between list and dictionary in Python in one picture. The scans belong in a list, an ordered sequence you read by position. The catalog belongs in … Read more >>

Python Naming Conventions for Variables

Python naming conventions for variables: Command Prompt output of a script that checks nine sample variable names

Python naming conventions for variables come down to one PEP 8 rule: write variable names in lowercase snake_case, like sales_tax or order_total. Constants get UPPER_CASE, and classes get CapWords. On top of that style rule, Python itself rejects some names outright, such as ones that start with a digit or match a keyword. In this … Read more >>

and, or, not in Python: Logical Operators Explained

and and or in Python: Command Prompt output of a logical operators truth table

In Python, and is True only when both sides are true, or is True when at least one side is, and not flips a value. They’re spelled as plain English words, not && or ||: Every snippet below ran on Python 3.12.5; you can see the actual Windows Command Prompt output in each screenshot. I’ll … Read more >>

TypeScript Keywords List: Reserved, Strict and Contextual

TypeScript keywords list: Command Prompt output counting reserved, strict-mode and contextual keywords

The TypeScript keywords list has 84 words in three groups: 36 reserved words you can never use as names, 9 strict-mode reserved words, and 39 contextual keywords like type and keyof that are only special in certain spots. Here’s the rule in one line: I pulled the three groups straight from the compiler’s own token … Read more >>

How to Split a String by New Line in TypeScript

Split string by new line in TypeScript: Command Prompt output of splitting lines and removing empty ones

To split a string by new line in TypeScript, call split(/\r?\n/). The regular expression matches both Unix line breaks (\n) and Windows line breaks (\r\n), and you get back a string[] with one entry per line: Nothing here is made up: tsc compiled each file in strict mode (TypeScript 7.0.2 and Node.js v22.22.2), Node ran … Read more >>

How to Add Tuples to Lists in Python

Add a tuple to a list in Python: Command Prompt output of append() adding a (name, price) tuple to a cart

To add a tuple to a list in Python, call list.append(t). It puts the whole tuple at the end as one item. If you want the tuple’s elements added one by one instead, use list.extend(t): Every snippet below ran in a Windows Command Prompt on Python 3.12.5, and the outputs are copied from those runs. … Read more >>

How to Split a Python List Into Evenly Sized Chunks

Split a Python list into evenly sized chunks: Command Prompt output of 10 orders in batches of 3

To split a Python list into evenly sized chunks, slice it in steps of the chunk size with a list comprehension. Each slice items[i:i + n] becomes one chunk, and the last chunk just holds whatever’s left over: I ran every example in a Windows Command Prompt on Python 3.12.5 with NumPy 2.5.3, and the … Read more >>

How to Find the Largest Number in a List Using Python

Find the largest number in a list using Python: Command Prompt output of max() on a list of 2026 sales

To find the largest number in a list using Python, pass the list to the built-in max() function. max([4, 17, 9]) returns 17. It checks each item once, so it’s the fastest option: I ran each snippet here on Python 3.12.5 in a Windows Command Prompt and pasted the printed results as they appeared. In … Read more >>