Python Foreach: How to Program in Python

python foreachIn the world of programming, Python is quickly becoming a preferred choice amongst professional programmers for various applications. When it comes to programming though, Python is much more different from its counterparts than other languages, such as C-based languages. One of the key differences that Python has over other programs is the way you create loops.

For a C-based language it’s pretty simple to create a loop, specifically a foreach loop, but with Python you have to do things a bit different. It’s not incredibly complicated, and with a bit of time you can figure it out yourself, but if you have experience in other programming languages, you may find yourself having a bit of trouble getting used to the way that a foreach loop is written in Python.

Don’t worry if you have trouble learning the Python language. With a bit of practice, you can be programming in this language like a professional. Try your hand at it by checking out the Udemy course Python for Beginners.

What is a Foreach Loop?

Programming involves using, analyzing, and collecting data for various reasons. When you create a foreach loop, you’re applying something to a group of data and manipulating it. For example, you may want to add 1 to a list of numbers. Instead of you going through the list one by one and increasing the value, you would create a foreach loop to increase the value of each number in a list by one.

This can be taken a step further by adding a condition. Now instead of you creating a foreach loop that adds one to a set of numbers, you can create a foreach loop that adds one to a set of numbers each time a user presses a certain key. There is a great deal of versatility when it comes to creating a foreach loop, but first you have to learn how to make one.

Python doesn’t necessarily have foreach loops built into it like standard programming languages do, but instead you have to create something similar using standard for loops.

If this all seems to be a bit complicated for you, you can start off with something a bit more basic. Try the Udemy course Python for Rookies, which explores some of the simpler concepts that Python has to offer. 

Making Your First Foreach Loop in Python 

If you’ve ever used a standard foreach loop, a Python loop may seem a little strange. As mentioned before, Python foreach loops aren’t the built in ones you’ve seen before, but instead you create something similar to a foreach loop by using the built-in methods range and xrange.

therange = range(10)

print therange

[0,1 2, 3, 4, 5, 6, 7, 8, 9]

type(r)

<type ‘list’> 

The two examples above demonstrate what an xrange and a range are. The first set of code is for the range. The way you call a range is applying the upper limit as a single value, which we’ve done with the number 10. We then created a list of integers going through the numbers 0 to 9 inclusively. You can create various range methods to include different sets of data. For example, if you wanted to create a range method to go through a list of only even numbers, instead of writing range(10) you can write range(0, 100, 2), which would only count the even numbers from 0 to 100.

A short example of using a range and for loop to create a sample of a foreach loop would be something like what is shown below.

For x in range(20):

Another short example would be using a foreach loop to type out letters in a name. Try this line of code and see how easy it is for Python to print out each letter.

myName = “Alex”

for everyLetter in myName:

print(everyLetter)

When you compile the code, you will see that Python will print out each letter of the name on a different line, without you having to write anything extra. In fact, the main reason why Python doesn’t have a foreach loop is because it is so easy to go through data without one. 

As you can see, using a foreach loop in Python is actually a lot simpler than you may think. Check out the Udemy courses Python for Web Programming to not only learn about Python and how it can be used for web design, but also how it is compared to other languages, such as Python vs. Perl and Python vs. Ruby. 

Learning More about Python 

Now that you’ve experienced a small sample of Python programming, you should explore it more and learn more about it. Check out the Udemy course Python, The Next Level, which teaches you about some of the more difficult intermediate things you can do with Python and how you can use this knowledge to create new things.