🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!
🖥️ Function RANGE in Python (FOR Loops)
Awesome! You are now able to write your own iterations – then repeat a block of code, until you have reached the last item of a list. But, in most cases, you would also like to execute a group of statements for a fixed number of times. Right?
Fortunately, Python provides a very simple way to repeat an action, for a specific number of times: the built-in function range() in Python, which generates a sequence of numbers, from 0 up to – but not including – a specified number.
○ Function RANGE in Python (Default)
A range function in Python has three parameters, which are: starting, ending and step size parameters. You can define each one of them as range(start, stop, step_size).
This function does not store all the values in memory, it would be inefficient. Instead, it remembers the start, stop, and step_size values, then generates the next number on the go.
Let’s take a look at how the range() function can be used, with for loop in Python.
for i in range(7):
print(i)
In the above example, the sequence starts from 0 and ends at 6 because the ending parameter is 7 and is NOT included. The default step is 1, if not provided. Therefore, the for loop execution jumps 1 step for each new iteration, and will result in printing out all the numbers from 0 through 6 – included.
○ Starting Parameter (Default: 0)
As you may have noticed, the range function starts from 0, by default. But, you can start the range at another number, by specifying the start parameter. This will result in printing out numbers from 2 through 6, included.
for i in range(2, 7):
print(i)
○ Negative numbers
You can even generate a range of negative numbers, as well. This example will result in printing out numbers from -5 through -1, included – since the ending parameter is never included.
for i in range(-5, 0):
print(i)
○ Step Size Parameter (Increment)
Finally but not least, the range increments by 1 – by default. But, you can specify a different increment, by specifying a “step size” parameter. In the below example, the sequence starts from 2 and ends at 6, because the ending parameter is 7 – and the step is 2. Therefore, the for execution jumps 2 steps, after each iteration, and will result in printing out the numbers 2, 4 and 6.
for i in range(2, 7, 2):
print(i)
Let’s play this video, stick around and watch until the end of this video! 👍🏻
– Digital Academy™ 🎓
***
☞ WATCH NEXT:
○ Data Types in Python – https://youtu.be/cweUByxBWiU
○ Operators in Python – https://youtu.be/-wDaVLkKOiU
○ IF Statements in Python – https://youtu.be/CC5seZ6OBJ4
○ FOR Loops in Python – https://youtu.be/JgH-D5DSTho
☞ WATCH MORE:
○ HOW TO Learn Python? Python Tutorial for Beginners [FULL Course] https://youtu.be/9hvnSZPMtuw
📖 Blog: http://digital.academy.free.fr/blog/
📖 Complete Python Development Course for Beginners [PLAYLIST]: http://digital.academy.free.fr/playlist/python-development-for-beginners
🛒 Shopping and Discounts: http://digital.academy.free.fr/store/
#Python #Tutorial #Beginners #Shorts
***
♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.
***
♡ FOLLOW US ♡
✧ http://digital.academy.free.fr/
✧ https://twitter.com/DigitalAcademyy
✧ https://www.facebook.com/digitalacademyfr
✧ https://www.instagram.com/digital_academy_fr/
✧ https://www.youtube.com/c/DigitalAcademyOnline
♡ SUPPORT US ♡
✧ http://digital.academy.free.fr/join
✧ http://digital.academy.free.fr/donate
✧ http://digital.academy.free.fr/subscribe
✧ https://www.patreon.com/digital_academy
✧ https://www.buymeacoffee.com/digital_academy
***