DEV Community

Chase
Chase

Posted on

Why Python Is Easier (Loops edition)

Introduction
Python is a much simpler syntax language than all other programming languages, this I know you have heard before, probably many times throughout your search on which language you should learn first. One of the most common answers to that question is python and that comes with good reason. Let's talk about why python is so great compared to other languages.

Unlike languages like Javascript or React that can’t use Object-Oriented Programming (OOP), Python can! Which lets you create and store data inside of classes that can be assigned to a piece of data. Not only does the language have that advantage over the common frontend languages but Python’s ability to handle backend programming as well makes it a well diverse language; but Python’s syntax within itself allows you to create mini short cuts throughout your code that other languages don’t have. The first one being List Comprehension and Dictionary Comprehension. Let’s talk a bit about these and why they are so useful in python when creating for loops.

Let's talk about it
When you want to take a for loop that doesn't have very complex features going on then list comprehensions is the best thing for you! Let's take a look at an example

For loop:

days = ["mon", "tues", "wed", "thur", "fri"]

for day in days:
    print(day)
Enter fullscreen mode Exit fullscreen mode

List comprehension:

days = ["mon", "tues", "wed", "thur", "fri"]

[print(day) for day in days]
Enter fullscreen mode Exit fullscreen mode

This is only a small and simple example of list comprehensions.

Pros and Cons

Cons

  1. The biggest and most obvious con is that with anything too complex list comprehension probably won't be the best option.

  2. Another con is that the readability of your code can be a bit more difficult as having it as just one line of code instead of being spread out over multiple lines as in a regular for loop.

Pros

  1. Time, the time it takes to process the one line of code over a for loop in much better.

  2. Easier to write, it is much easier to write just one line of code compared to multiple and that making it easier to find where in your code you could've went wrong in your loop.

Lets break it down further
the average template for list comprehension is this

[(output of loop) for (item) in (list)]

but did you know that you could add conditions to a list comprehension? Well now you do! and that looks something like this...

[(output of loop) for (item) in (list) if (condition)]

This is best use if you are needing to filter through the list that you are given in need for a smaller more accurate result.

In all, the use of List comprehension is something to be used in certain situations to make your coding project or learning easier to understand and easier to manage.

Now just like list comprehension you can also do the same thing with dictionaries! Although it looks just a bit different because as you know dictionaries have a key and a value.

Let's Take A Look​​
For starters lets look at an example:

  keys = ['a','b','c','d','e']
  values = [1,2,3,4,5]  

  dictionary = { k:v for (k,v) in zip(keys, values)} 
Enter fullscreen mode Exit fullscreen mode

As you can see this makes iterating over a dictionary much simpler and in turn faster. This is why Python can be such a strong and diverse language, with simple syntax that it offers people who want to start learning programming it makes it welcoming and not as scary as other languages such as, Java, C or C++.

In Conclusion, if you are wanting to get into python programming and feel as if you're going to struggle, don't worry everyone feels that way. Python's syntax is here to make sure beginners feel welcome no matter your programming back ground. List and Dictionary comprehension isn't the only shortcuts that python has to offer! So, go out and try to discover your own little tips and tricks within the language and let others know what you've found to be helpful. You never know, something that you learn on your own might just be the answer someone else is looking for in their code.

Thanks for taking time to read my blog about python loop syntax and happy coding!

Top comments (0)