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

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

Time a Python Function

In one of the previous articles (Measure Time in Python – time.time() vs time.clock()), we learned how to use the module timeit to benchmark a program. Since the program we timed in that article includes only raw statements instead of functions, we’re going to explore how to actually time a function in Python. Time a […]

Read More

Hashing Files with Python

Remember that a hash is a function that takes a variable length sequence of bytes and converts it to a fixed length sequence. Calculating a hash for a file is always useful when you need to check if two files are identical, or to make sure that the contents of a file were not changed, […]

Read More

Hashing Strings with Python

A hash function is a function that takes input of a variable length sequence of bytes and converts it to a fixed length sequence. It is a one way function. This means if f is the hashing function, calculating f(x) is pretty fast and simple, but trying to obtain x again will take years. The value returned […]

Read More

How to Recursively Copy a Folder (Directory) in Python

Ever tried to copy a directory/folder in Python? Ever failed? No matter. Try again! If you haven’t already read it, look at the article How to Copy a File in Python with shutil for an explanation of how to copy files with shutil. Recursively Copying a Directory/Folder of Files in Python In the article that […]

Read More

How to Rename (Move) a File in Python

Renaming (in Python it’s known as moving) a file in Python is really simple, and can be done in very few lines thanks to a handy module called shutil. shutil has a function called move that does precisely what the function name implies. It moves files or directories from one location to another. Here’s a […]

Read More

How to Slice Custom Objects/Classes in Python

For an introduction to slicing, please view the article How to Slice Lists/Arrays and Tuples in Python. Slicing Your Own Python Objects Alright, that’s all cool, now we know how to slice. But how do we slice our own objects? If I implement an object that has a list, or custom data structure, how do […]

Read More

How to See if a String Contains Another String in Python

Ever wanted to see if a string contains a string in Python? Thought it was going to be complicated like C? Think again! Python implements this feature in a very easy to read and easy to implement fashion. There are two ways of doing it, and some will like one way better than the other, […]

Read More