Lecture Note
University
University of California San DiegoCourse
DSC 10 | Principles of Data SciencePages
2
Academic year
2023
anon
Views
19
Understanding Loops in Python: Loops are a key topic to grasp if you're just starting off with Python programming. Byautomating repetitive processes, loops help you save time and effort. In this post, we'llexplore the fundamentals of Python loops and the range function's applications to both forand while loops. You'll be confident in your ability to create loop-using Python programs bythe conclusion of this article. The Python For Loop One of the most popular programming loops is the for loop, and Python is no exception.Although Python's for loop syntax is quite similar to those of other programming languageslike C++ and Java, there are a few significant distinctions that should not be overlooked. One distinction is that Python does not use brackets; instead, it uses indentation. Although atfirst this might seem unusual, it gradually becomes second nature. The usage of the rangefunction in the for loop, which returns values between the start and stop arguments, is anotherdistinction. The loop will begin at the value supplied because the start argument is inclusive.On the other hand, the end parameter is exclusive, therefore the loop will finish at a value thatis one less than the one supplied. The step argument, which comes last, defines how much theloop should lengthen with each iteration. Let's look at a straightforward Python for loop example: scss for i in the range (0, 10, 2) print (i) This code will output all even numbers between 0 and 8. (0, 2, 4, 6, and 8). The loop onlyoutputs the even numbers since the range function starts at zero, climbs by two, and endsbefore hitting ten. The Function of Range When using Python loops, the range function is a helpful tool. It enables us to set the start,stop, and step parameters for our loop, as we saw in the previous example. However, it's alsocrucial to keep in mind that the end parameter is exclusive, which means the loop will finishat a number that is one lower than the one supplied. Let's see another another instance of the range function being used in a for loop: scss in the range (2, 12, 3) for i print(i)
These values will be printed by this code: 2, 5, 8, and 11. The range function begins at two,grows by three, and terminates at twelve. It's important to note that substituting 13 or 14 for12 would have the same effect. The Loop While While loops are an additional kind of loop that can be used in Python when the increment ismore complicated or when the loop condition is not known in advance. The while loop hasthe typical elements you might anticipate, such as setup, a condition to test, and updating avariable. Here is an illustration of a Python while loop: css i = 2 as long as i 12: print(i) i += 3 The values 2, 5, 8, and 11 will be printed as a consequence of this code, producing the sameoutcome as the previous example. The setup for the while loop is i = 2 ," the test condition is i < 12 ," and the updating of i is i += 3 ." The loop will keep running as long as the condition is still true.
Loops in Python: Automate Your Way to Efficiency
Please or to post comments