The Basics: Concatenating Strings

Concatenating strings basically just means to add a number of strings together to form one longer string. It can be done easily in Python using the ‘+’ symbol. Let’s say you had three strings: “I am”, “Learning”, “Python” To concatenate these, simply add them together using the ‘+’ symbol, like this: >>> print “I am” + “Learning” […]

Read More

Encoding and Decoding Strings (in Python 3.x)

In our other article, Encoding and Decoding Strings (in Python 2.x), we looked at how Python 2.x works with string encoding. Here we will look at encoding and decoding strings in Python 3.x, and how it is different. Encoding/Decoding Strings in Python 3.x vs Python 2.x Many things in Python 2.x did not change very […]

Read More

What is the difference between __str__ and __repr__ in Python

Purpose of __str__ and __repr__ in Python Before we dive into the discussion, let’s check out the official documentation of Python about these two functions: object.__repr__(self): called by the repr() built-in function and by string conversions (reverse quotes) to compute the “official” string representation of an object. object.__str__(self): called by the str() build-in function and […]

Read More

Difference between @staticmethod and @classmethod in Python

Class vs Static Methods in Python In this article I’ll try to explain what are staticmethod and classmethod, and what the difference is between them. staticmethod and classmethod both use decorators for defining a method as a staticmethod or classmethod. Please take a look at the article Python Decorators Overview for a basic understanding of […]

Read More

How Metaclasses Work Technically in Python 2 and 3

A metaclass is a class/object which defines a type/class of other classes. In Python a metaclass can be a class, function or any object that supports calling an interface. This is because to create a class object; its metaclass is called with the class name, base classes and attributes (methods). When no metaclass is defined […]

Read More

Add, Remove, and Search Packages in Python with pip

Using pip to Manage Python Packages Like many useful programming ecosystems, Python provides a powerful and easy-to-use package management system called pip. It is written to replace an older tool called easy_install. From a high-level point of view, pip has the following advantages over easy_install: All packages are downloaded before installation to prevent partial (thus […]

Read More

One line if statement in Python (ternary conditional operator)

In the real world, there are specific classifications and conditions on every action that occurs around us. A twelve-year-old person is a kid, whereas a thirteen-year-old person is a teenager. If the weather is pleasant, you can make plans for an outing. But if it isn’t, you will have to cancel your plans. These conditions […]

Read More

Measure Time in Python – time.time() vs time.clock()

A prerequisite before we dive into the difference of measuring time in Python is to understand various types of time in the computing world. The first type of time is called CPU or execution time, which measures how much time a CPU spent on executing a program. The second type of time is called wall-clock […]

Read More

Select a random item from a list/tuple/data stucture in Python

One of the most common tasks that requires random action is selecting one item from a group, be it a character from a string, unicode, or buffer, a byte from a bytearray, or an item from a list, tuple, set, or xrange. It’s also common to want a sample of more than one item. Don’t […]

Read More

How to Check if a File Exists in a Directory with Python

With Python there are several methods which can be used to check if a file exists, in a certain directory. When checking if a file exists, often it is performed right before accessing (reading and/or writing) a file. Below we will go through each method of checking if a file exists (and whether it is […]

Read More