How to Copy a File in Python with shutil

So you want to know how to copy a file in Python? Good! It’s very useful to learn and most complex applications that you may design will need at least some form of copying files. Copying a Single File in Python Alright, let’s get started. This first section will describe how to copy a single […]

Read More

How to Slice Lists/Arrays and Tuples in Python

So you’ve got an list, tuple or array and you want to get specific sets of sub-elements from it, without any long, drawn out for loops? Python has an amazing feature just for that called slicing. Slicing can not only be used for lists, tuples or arrays, but custom data structures as well, with the […]

Read More

How to Get a Sub-string From a String in Python – Slicing Strings

So how do you get a sub-string in Python? Well, Python has a handy dandy feature called “slicing” that can be used for getting sub-strings from strings. But first, we have to go over a couple of things to understand how this works. Slicing Python Objects Strings, in Python, are arrays of characters, except they […]

Read More

How to Use Python’s xrange and range

Python has two handy functions for creating lists, or a range of integers that assist in making for loops. These functions are xrange and range. But you probably already guessed that! The Difference Between xrange and range in Python Before we get started, let’s talk about what makes xrange and range different. For the most […]

Read More

How to Sort a List, Tuple or Object (with sorted) in Python

Sorting a list or tuple is easy in Python! Since a tuple is basically like an array that is not modifiable, we’ll treat it almost the same as a list. Sorting a Python List the Simple Way Okay, so if you only want to sort a list of numbers, Python has a built in function […]

Read More

How to Check if a List, Tuple or Dictionary is Empty in Python

The preferred way to check if any list, dictionary, set, string or tuple is empty in Python is to simply use an if statement to check it. For example, if we define a function as such:

It will magically detect if any built in structure is empty. So if we run this:

Then, […]

Read More

How to Implement an ‘enum’ in Python

What is enum and why we need it An enumerated type, a.k.a. enum, is a data type consisting of a set of named values called elements, members or enumerators of the type. These enumerated named values function as constants in the computing language. For example, a COLOR enum may include named values such as RED, […]

Read More

How to get an attribute from an object in Python

Once we know how to check if an object has an attribute in Python, the next step is to get that attribute. In Python, besides the normal dot-style attribute access, there’s a built-in function, getattr, which is also very useful for accessing an attribute. Python’s getattr The built-in function getattr(object, name[, default]) returns the value […]

Read More

How to check if an object has an attribute in Python

Since everything in Python is an object and objects have attributes (fields and methods), it’s natural to write programs that can inspect what kind of attributes an object has. For example, a Python program could open a socket on the server and it accepts Python scripts sent over the network from a client machine. Upon […]

Read More

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