Python How To’s
A collection of small, useful, how to’s for Python.
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 MoreHow 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. In this article, we will guide you on how to effectively utilize Python’s xrange and range functions while also exploring the best laptops for working from home. But you probably already […]
Read MoreHow 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 MoreHow 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: [python] def is_empty(any_structure): if any_structure: print(‘Structure is not empty.’) return False else: print(‘Structure is empty.’) return True [/python] It […]
Read MoreHow 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 MoreHow 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 MoreHow 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 MoreHow 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 MoreHow 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 MoreHow 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