How to Use the Python Main Function with Arguments

Python main function with arguments: Command Prompt runs of an argparse tip calculator with different options

To use the Python main function with arguments, define main() with a parameter for the arguments, then call it under if __name__ == “__main__”: and pass it sys.argv[1:], the words after the script name. Its return value goes to sys.exit() as the exit code: I ran each script here on Python 3.12.5 in a Windows … Read more >>

How to Access Variables Outside a Function in Python

Python access variables outside a function: Command Prompt output of a function returning subtotal, tax and total

To access variables outside a function in Python, return the value from the function and store the result in a variable where you called it. A local variable disappears when the function ends, but the value you return doesn’t: I tested each snippet on Python 3.12.5 in the Command Prompt, so the outputs below are … Read more >>

How to Set Global Variables in Python Functions

Python set global variable in function: Command Prompt output of a global order counter and revenue total

To set global variables in Python functions, write global name at the top of the function, then assign to name. Without that line, any assignment inside the function creates a new local variable and the global stays the same: Every example below ran on Python 3.12.5 from the Command Prompt, and the outputs are copied … Read more >>

Best Python Libraries for Web Scraping (With Examples)

Python libraries for web scraping: Requests and Beautiful Soup scraping quotes in Command Prompt

The best Python libraries for web scraping are Requests and Beautiful Soup for simple pages, lxml for speed, Scrapy for large crawls, and Selenium or Playwright for sites that build their content with JavaScript. Most projects start with the first two: My examples scrape quotes.toscrape.com and books.toscrape.com, two sandbox sites built for practice, using Python … Read more >>

How to Split a String with Multiple Separators in TypeScript

Split a string with multiple separators in TypeScript: Command Prompt output of split with a character class

You split a string with multiple separators in TypeScript by passing a regular expression to split(). Put single-character separators in a character class like /[,;|]/, and you’ll get back a string[] with one piece per value: All examples were compiled in strict mode with TypeScript 7.0.2 and Node.js v22.22.2, and I pasted the console output … Read more >>

How to Convert a TypeScript Enum to a Number (With Examples)

Convert TypeScript enum to number: Command Prompt output of a numeric enum read as a number

The simplest way to convert TypeScript enum to number values is to read the member itself: a numeric enum member already is a number at runtime, so const n: number = OrderStatus.Shipped works with no cast. String enums are different, and need a lookup map: Nothing below is hand-typed output: I ran each file through … Read more >>

Python with open() as file: Read, Write and Append Safely

Python with open as file: Command Prompt output showing the file closed after the with block

with open(“file.txt”) as file: is how Python opens a file safely. The with block gives you the file object as file, and Python closes it automatically when the block ends, even if an error happens inside: You’ll see real output from Python 3.12.5 on Windows 11 in every Command Prompt screenshot below. I’ll cover reading … Read more >>

How to Subtract Two Numbers in Python

Subtract two numbers in Python: Command Prompt output of the minus operator on prices and integers

To subtract two numbers in Python, put the minus operator between them: a – b. It works the same way for integers and floats, and the result is negative when the second number is bigger: Every output below comes from Python 3.12.5 in a real Command Prompt, using US prices in dollars and 2026 dates. … Read more >>

How to Concatenate Multiple Lists in Python

Concatenate multiple lists in Python: Command Prompt output of three regional order lists joined with +

Joining three or more lists takes one expression. To concatenate multiple lists in Python, chain them with + or unpack them into a new list; for a list of lists, use itertools.chain.from_iterable(): Every output below is a real run on Python 3.12.5 in Command Prompt, with US order totals in dollars and 2026 dates. Concatenate … Read more >>