Skip to main content

Python One Line for Loop Gorakh Gupta The Crazy Programmer

In our daily life, we often write some big programs and some small level programs to solve the different problems. Whenever we try to solve a complex level problem, then we have to write so many lines of code.

In that code, we often use different types of loops like for, while, or any conditional statements like if, if-else or if-elif-else.

Sometimes we have to write the whole loop to process a single statement in the loop or a minimum of two or three statements. When we write this in our code it generally takes approx 4 to 5 lines to complete a general for loop in which we have to perform a single operation.

Case 1:

For example, I am showing you the code where we are trying to print whole numbers upto 5.

for i in range(5):
    print(i)

Output:

PS C : \ Users \ ASUS \ Desktop \ TheCrazyProgrammer Work > python -u " c : \ Users \ ASUS \ Desktop \ TheCrazyProgrammer Work \ test.py "
0
1
2
3
4

As we can see in the loop we are only printing the i value, it takes 2 lines to complete the loop and execute it.

If we want to process 2 or 3 statements in a loop, python allows us to do it in a single line only.

It is also called single liners in python. We can use a single line for loop to write the same program in less numbers of spaces. Although sometimes it increases the readability but if you are experienced with it then you will find it useful.

Now let’s see the one liner for the above example.

for i in range(5): print(i)

In the above example, we can see that we have written the same code in a single line only.

These single liners can be useful to quickly write a task in which we simply want to perform one or two operations.

Case 2:

In the above examples, we have seen how to write one liner for loop in which we have to perform a single operation. Now we will see how to write one liner for loop in which we have to perform some complex operations.

Let’s see the example where we are going to use a for loop and we will be using some if else conditions in that loop.

First, we will see how to write it in a traditional way, after that we will see how to write it in one liner way.

for i in range(10):
    if (i < 5):
        j = i ** 2
    else:
        j = 0
    print(j)

Output:

PS C : \ Users \ ASUS \ Desktop \ TheCrazyProgrammer Work > python -u " c : \ Users \ ASUS \ Desktop \ TheCrazyProgrammer Work \ test.py "
0
1
4
9
16
0
0
0
0
0

In this example, we are trying to print the square value of i when it is less than 5.

If it is greater than 5 then we simply print 0. As we can see in the example to write code for this problem, we use 6 lines to complete it. But using one liner we can complete it in a single line only.

for i in range(10): print(i**2 if i < 5 else 0)

We will get the same output in both of the cases. In one case we have written the code in 6 lines and in the second case we have written the code in 1 line only.

Although for a beginner I will recommend going with the traditional option only because as a beginner there will be some difficulty in understanding one liner.

The post Python One Line for Loop appeared first on The Crazy Programmer.



from The Crazy Programmer https://ift.tt/kdRIbJG

Comments

Popular posts from this blog

Difference between Web Designer and Web Developer Neeraj Mishra The Crazy Programmer

Have you ever wondered about the distinctions between web developers’ and web designers’ duties and obligations? You’re not alone! Many people have trouble distinguishing between these two. Although they collaborate to publish new websites on the internet, web developers and web designers play very different roles. To put these job possibilities into perspective, consider the construction of a house. To create a vision for the house, including the visual components, the space planning and layout, the materials, and the overall appearance and sense of the space, you need an architect. That said, to translate an idea into a building, you need construction professionals to take those architectural drawings and put them into practice. Image Source In a similar vein, web development and design work together to create websites. Let’s examine the major responsibilities and distinctions between web developers and web designers. Let’s get going, shall we? What Does a Web Designer Do?

A guide to data integration tools

CData Software is a leader in data access and connectivity solutions. It specializes in the development of data drivers and data access technologies for real-time access to online or on-premise applications, databases and web APIs. The company is focused on bringing data connectivity capabilities natively into tools organizations already use. It also features ETL/ELT solutions, enterprise connectors, and data visualization. Matillion ’s data transformation software empowers customers to extract data from a wide number of sources, load it into their chosen cloud data warehouse (CDW) and transform that data from its siloed source state, into analytics-ready insights – prepared for advanced analytics, machine learning, and artificial intelligence use cases. Only Matillion is purpose-built for Snowflake, Amazon Redshift, Google BigQuery, and Microsoft Azure, enabling businesses to achieve new levels of simplicity, speed, scale, and savings. Trusted by companies of all sizes to meet

2022: The year of hybrid work

Remote work was once considered a luxury to many, but in 2020, it became a necessity for a large portion of the workforce, as the scary and unknown COVID-19 virus sickened and even took the lives of so many people around the world.  Some workers were able to thrive in a remote setting, while others felt isolated and struggled to keep up a balance between their work and home lives. Last year saw the availability of life-saving vaccines, so companies were able to start having the conversation about what to do next. Should they keep everyone remote? Should they go back to working in the office full time? Or should they do something in between? Enter hybrid work, which offers a mix of the two. A Fall 2021 study conducted by Google revealed that over 75% of survey respondents expect hybrid work to become a standard practice within their organization within the next three years.  Thus, two years after the world abruptly shifted to widespread adoption of remote work, we are declaring 20