• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Justin Joyce

Practical tips and tutorials about software development.

  • Standing Invitation
  • Featured Posts
  • Latest
  • About

Python

Reverse a string in Python

Posted Feb 8, 2024 — Updated Apr 6, 2024

The simplest way is to use Python double colon slicing with a negative step value: You can also use the builtin reversed method, which will “Return a reverse iterator over the values of the given sequence” according to its official help(). Note that this returns an iterator, not a string, so you’ll have to do […]

Filed Under: Python

Replace a string in Python

Posted Oct 29, 2023 — Updated Jan 10, 2024

To replace a string or substring in Python you have two main options: Python’s String Replace Method The string.replace method works roughly how you would expect it to: By default, string.replace will replace all occurrences of the string you want to replace. However, it accepts an optional integer third argument specifying how many times to […]

Filed Under: Python, Regex

JSON in Python

Posted Aug 13, 2023 — Updated Jan 10, 2024

Working with JSON in Python relies on Python’s builtin json module. After you import json, here are the json module methods: Notice that the methods ending in “s” deal directly with strings, whereas the others deal with files. To disambiguate them, I call them “load string” and “dump string” (in my own head, at least). […]

Filed Under: Python

Python sets

Posted Jul 30, 2023 — Updated Jan 10, 2024

Sets are one of Python’s built-in types, and they’re very useful for deduplicating and comparing collections of data. Sets have tons of useful built-in functionality, and this post covers a lot. Here are some jump links to make life easier: Creating a set There are a few options: Check if a set contains a member […]

Filed Under: Python

Python try except

Posted Jun 1, 2023 — Updated Feb 18, 2024

Try and except are the building blocks of exception handling in Python. You’ll also sometimes see finally and else. Here’s the summary: Try and Except A simple try-except block looks like this: You’ll usually see try and except by themselves; finally is used less often, and else even less. Here’s a (slightly) more realistic example: […]

Filed Under: Python

Python double slash operator

Posted May 10, 2023 — Updated Feb 6, 2024

Python’s double slash (//) operator performs floor division. What exactly is floor division? Floor division is a normal division operation except that it returns the largest possible integer. This integer is either less than or equal to the normal division result. – Educative.io In code, it looks like this: Some languages perform floor division by […]

Filed Under: Python

Python for loops

Posted Mar 6, 2023 — Updated Jan 10, 2024

For loops in Python are one of many features that make Python so popular; they’re as close to plain English as you can get when writing software. They generally look like this: What is an iterable? According to the Python docs an iterable is: An object capable of returning its members one at a time. […]

Filed Under: Python

Python List Comprehensions

Posted Feb 28, 2023 — Updated Jan 10, 2024

List comprehensions provide you with a “concise way to create lists”, according to the official docs, but they do a lot more than that. They’re a great feature of Python; let’s do a few examples to illustrate why. List comprehension as a map function The example above is the same as using a map function […]

Filed Under: Python

Writing CSVs in Python

Posted Feb 13, 2023 — Updated Jan 10, 2024

To write CSVs in Python, you’ll want the builtin csv module. More specifically, I usually use csv.DictWriter. Python DictWriter Python’s csv module has both a writer and a DictWriter class, but I’m virtually always working with dictionaries, so I always use DictWriter. It’s pretty straightforward. You grab your data, open a file in a context […]

Filed Under: Python

Python “is” operator vs double equals “==”

Posted Feb 11, 2023 — Updated Jan 10, 2024

Python’s is operator compares object identity, while == compares object values. Python “is” operator In Python, is compares identity. In other words, it checks if two objects are the same object. It does not care if they have equal values, it cares if they have the same id in memory. This is why you often […]

Filed Under: Python

  • Page 1
  • Page 2
  • Go to Next Page »

Primary Sidebar

Recent Posts

  • Every Built-In Vim Color Scheme (with screenshots)
  • Reverse a string in Python
  • Meeting Cost Calculator
  • Vim find and replace
  • What makes an effective development team

Categories

  • Arrays (5)
  • Command Line (9)
  • Dates (3)
  • Featured (7)
  • Git (7)
  • Golang (5)
  • Javascript (8)
  • Productivity (8)
  • Projects (4)
  • Python (15)
  • Regex (2)
  • Ruby (3)
  • Shell (2)
  • Thoughts (2)
  • Tips (11)
  • Tools (3)
  • Tutorials (1)
  • Vim (4)

Archives

  • July 2024 (1)
  • February 2024 (1)
  • January 2024 (1)
  • December 2023 (1)
  • November 2023 (1)
  • October 2023 (4)
  • September 2023 (1)
  • August 2023 (2)
  • July 2023 (5)
  • June 2023 (3)
  • May 2023 (6)
  • April 2023 (5)
  • March 2023 (5)
  • February 2023 (10)
  • January 2023 (6)
  • December 2022 (7)

Copyright © 2025 · Contact me at justin [at] {this domain}

  • Privacy Policy