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...

Olive and NTT DATA Join Forces to Accelerate the Global Development and Deployment of AI Solutions

U.S.A., March 14, 2021 — Olive , the automation company creating the Internet of Healthcare, today announced an alliance with NTT DATA , a global digital business and IT services leader. The collaboration will fast track the creation of new healthcare solutions to transform the health experience for humans — both in the traditional healthcare setting and at home. As a member of Olive’s Deploy, Develop and Distribute Partnership Programs , NTT DATA is leveraging Olive’s open platform to innovate, build and distribute solutions to Olive’s customers, which include some of the country’s largest health providers. Olive and NTT DATA will co-develop new Loops — applications that work on Olive’s platform to provide humans real-time intelligence — and new machine learning and robotic process automation (RPA) models. NTT DATA and Olive will devote an early focus to enabling efficiencies in supply chain and IT, with other disciplines to follow. “This is an exciting period of growth at Olive, so...