Skip to main content

Posts

Showing posts with the label The Crazy Programmer Python One Line for Loop Python One Line for Loop The Crazy Programmer

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 line