How to Get and Format the Current Time in Python

Time Review Time as we know is represented by several components, some of which are numerical such as seconds, minutes and hours while others are strings such as timezone, AM and PM. Python provides an arsenal of utilities when dealing with time objects and strings. Therefore, it is relatively easy to get and format the […]

Read More

How to Sort Python Dictionaries by Key or Value

Note: If you want to sort a list, tuple or object in Python, checkout this article: How to Sort a List or Tuple in Python The dict (dictionary) class object in Python is a very versatile and useful container type, able to store a collection of values and retrieve them via keys. The values can be objects of any type […]

Read More

How to Traverse a Directory Tree in Python – Guide to os.walk

When you use a scripting language like Python, one thing you will find yourself doing over and over again is walking a directory tree, and processing files. While there are many ways to do this, Python offers a built-in function that makes this process a breeze. What is the os.walk() Function? The walk function is […]

Read More

How to Create a Thread in Python

Introduction to Python threads What are threads? Simply put, try to imagine them as running several programs concurrently, in a single process. When you create one or more threads in your program, they get executed simultaneously, independent of each other, and most importantly, they can share information among them without any extra difficulty. These features […]

Read More

How to Create a Python Package

Using packages is an essential part of Python programming. If packages didn’t exist, programmers would need to spend a lot of time rewriting code that’s been written before. Picture a scenario where you have to write a parser every time you want to use one – it would waste a lot of time and effort, […]

Read More

How to Install Virtualenv (Python)

Note: Edited comments about the –no-site-packages argument now being default, thanks to @dideler on Twitter. Python is a very powerful scripting language. The language has lots of Python packages you can install and use in your projects. Sometimes, you may have different projects that need different versions of the same package/module. For example, let’s say […]

Read More

Add Python to the PATH Environmental Variable (‘python’ is not recognized as an internal or external command)

Programming has become the most prominent technological course to be learned. Apart from good logical thinking skills, you need successfully installed software to excel in this field. Python is the most fundamental programming language to learn, but you may experience some errors while installing the Python setup. For Windows users, the most common error displayed […]

Read More

How to Run a Python Script via a File or the Shell

If you can’t execute or run a Python script, then programming is pointless. When you run a Python script, the interpreter converts a Python program into something that that the computer can understand. Executing a Python program can be done in two ways: calling the Python interpreter with a shebang line, and using the interactive […]

Read More

Check if a String is a Number in Python with str.isdigit()

Edit 1: This article has been revised to show the differences between str.isdigit() and the custom solution provided. Edit 2: The sample also supports Unicode! Often you will want to check if a string in Python is a number. This happens all the time, for example with user input, fetching data from a database (which […]

Read More