Welcome to yet another How to Python article. Today, we’ll be looking at looping over dictionaries which appears to be a hot topic—at least by an organic standpoint. As it turns out, there are few ways to get it done. First, we could loop over the keys directly: for key in dictionary. Alternatively, we might only need to loop over the ...
Read More »Home »
How to Split a String by Whitespace in Python: Brute Force and split()
Once again, I’m back with another look at some ways to solve a common Python problem. This time, we’ll be looking at how to split a string by whitespace (and other separators) in Python. If you’re in a rush, here’s the key takeaway. You could write your own whitespace splitting function, but they’re often slow and lack robustness. Instead, you ...
Read More »How to Convert a String to Lowercase in Python
As this series grows, I’ve started poking at seemingly simple problems to expose their complexity. This time, I thought it would be interesting to look at how to convert a string to lowercase. As it turns out, there’s a really straightforward solution (lower()), but I think it’s worth looking at a few homemade solutions first. For instance, we could try ...
Read More »How to Round a Number in Python: Truncation, Arithmetic, and More
Remember learning to round in grade school? Me too! The only problem is I don’t use the idea very often. As a result, I don’t always remember how to round a number in programming contexts like Python. Luckily, I’ve pieced together a little article for myself. Hopefully, you get some value out of it as well. As it turns out, ...
Read More »How to Create a List in Python: Loops, Comprehensions, and More
If you’re working with Python for the first time, you might be interested in learning how to store data. For example, you might want to collect a list of exam scores, so you can average them. If so, the list data structure gets the job done. But, how do you create a list in Python? That’s the topic of today’s ...
Read More »How to Compute Absolute Value in Python: Control Flow, Abs(), and More
As Python is increasingly used for computation, it becomes more important for math related resources to be made available. To aid in that effort, I figured I’d share a few ways to compute absolute value in Python. As it turns out, there are a few ways to compute absolute value in Python. First, there’s the abs() function which comes built ...
Read More »How to Obfuscate Code in Python: A Thought Experiment
As with most articles in this series, I was doing some browsing on Google, and I found that some folks had an interest in learning how to obfuscate code in Python. Naturally, I thought that would be a fun topic. By no means am I an expert, but I’m familiar with the idea. As a result, treat this like a ...
Read More »How to Increment a Number in Python: Operators, Functions, and More
Every once in awhile, I like to revisit Python fundamentals to see if I can learn anything new about the language. This time around I thought it would be fun to look at a few different ways to increment a number in Python. As it turns out, there two straightforward ways to increment a number in Python. First, we could ...
Read More »How to Brute Force Sort a List in Python: Bubble, Insertion, and Selection
Earlier in this series, I wrote a couple of articles on how to sort different types of lists in Python. For instance, I wrote one article on how to sort a list of strings. Then, later I wrote an article on how to sort a list of dictionaries. In both of those article, I used a few elegant solutions that ...
Read More »