Python Tips and Tricks
Be the best Python programmer you can be. Your guide to writing cleaner, quicker and neater Python code. Plus some handy Python tricks!
How to Build Strings Using .format()
Often developers try to build strings in Python by concatenating a bunch of strings together to make one long string, which definitely works but isn’t exactly ideal. Python’s .format() method allows you to perform essentially the same effect as concatenating strings (adding a bunch of strings together) but does so in a much more efficient […]
Read MoreQuick Tip: The Difference Between a List and an Array in Python
Arrays and lists are both used in Python to store data, but they don’t serve exactly the same purposes. They both can be used to store any data type (real numbers, strings, etc), and they both can be indexed and iterated through, but the similarities between the two don’t go much further. The main difference between […]
Read MoreThe 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 MoreEncoding 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 MoreWhat 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 MoreDifference 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 MoreHow 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 MoreAdd, 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 MoreOne 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 MoreMeasure 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