The Difference Between __str__ and __repr__

__str__ and __repr__ are used in very similar ways in Python, but they’re not interchangeable. __str__ is a built in function used to compute the informal string representations of an object, while __repr__ must be used to compute the official string representations of an object. The visible difference between the informal and official representations has […]

Read More

How to Check for Anagrams In Python

In Python, there’s a fairly straightforward way to create a method that can be used to check strings against each other to see if the two strings are anagrams. Check out the function below to see the method in action — essentially it works by using the “==” comparison operator to see if the strings on […]

Read More

Python Comparison Operators

In Python, comparison operators are used to compare the values on either side of the operators and then decide the relationship between them (sometimes they’re also referred to as relational operators). What follows is a complete list of the comparison operators in Python. Some of them might be pretty self-explanatory, while others you may have […]

Read More

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 More

Quick 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 More

Using Break and Continue Statements in Python

In Python, break statements are used to exit (or “break) a conditional loop that uses “for” or “while”. After the loop ends, the code will pick up from the line immediately following the break statement. Here’s an example: even_nums = (2, 4, 6) num_sum = 0 count = 0 for x in even_nums: num_sum = […]

Read More

Quick Tip: Using Sets in Python

In Python, sets are lists that don’t contain any duplicate entries. Using the set type is a quick and easy way to make sure that a list doesn’t contain any duplicates. Here’s an example of how you would use it to check a list for duplicates: a = set([“Pizza”, “Ice Cream”, “Donuts”, “Pizza”]) print a […]

Read More

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

How to Use the Enumerate() Function

Iterables are undoubtedly some of the most valuable objects in Python. They’re incredibly versatile, helping solve a large assortment of problems. Looping through iterables is an extremely common task, but keeping track of the current element’s index typically demands writing more code.   This is why the enumerate() function was built into Python all the way […]

Read More

The Basics: When to Use the del Statement

Using the del statement is relatively straightforward: it’s used to delete something. Often it’s used to remove an item from a list by referring to the item’s index rather than its value. For example, if you have the following list: list = [4, 8, 2, 3, 9, 7] And you want to remove the number […]

Read More