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

In Python, the enumerate() function is used to iterate through a list while keeping track of the list items’ indices. To see it in action, first you’ll need to put together a list: pets = (‘Dogs’, ‘Cats’, ‘Turtles’, ‘Rabbits’) Then you’ll need this line of code: for i, pet in enumerate(pets): print i, pet Your output should […]

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

Python’s string.replace() Method – Replacing Python Strings

Replacing Python Strings Often you’ll have a string (str object), where you will want to modify the contents by replacing one piece of text with another. In Python, everything is an object – including strings. This includes the str object. Luckily, Python’s string module comes with a replace() method. The replace() method is part of […]

Read More

Validate Python Function Parameter & Return Types with Decorators

Overview So I was playing around with Python decorators the other day (as you do). I always wondered if you could get Python to validate the function parameter types and/or the return type, much like static languages. Some people would say this is useful, whereas others would say it’s never necessary due to Python’s dynamic […]

Read More

Python’s range() Function Explained

What is Python’s range() Function? As an experienced Python developer, or even a beginner, you’ve likely heard of the Python range() function. But what does it do? In a nutshell, it generates a list of numbers, which is generally used to iterate over with for loops. There’s many use cases. Often you will want to […]

Read More

Python’s time.sleep() – Pause, Stop, Wait or Sleep your Python Code

Python’s time module has a handy function called sleep(). Essentially, as the name implies, it pauses your Python program. time.sleep() is the equivalent to the Bash shell’s sleep command. Almost all programming languages have this feature, and is used in many use-cases. Python’s time.sleep() Syntax This is the syntax of the time.sleep() function:

time.sleep() […]

Read More