Python Forum
Please help in understanding this piece of code
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please help in understanding this piece of code
#5
(May-10-2018, 10:46 AM)ThiefOfTime Wrote: you are building an array out of a list which is created using a list comprehension.
this specific list comprehension picks each number in [2, 4, 6], one at a time and using the range function. the range(lower_bound, upper_bound) function itself returns a list containing all numbers from lower_bound to upper_bound - 1. So your list will contain 3 sublists. for the first one range will be called with 2, since it is the first one in [2, 4, 6]. So range(2, 5) == [2, 3, 4]. The second list is made from i = 4, so that range(4, 7) will return [4, 5, 6]. And finally using i = 6 will result in range(6, 9) == [6, 7, 8]. so that your complete list will look like this: [[2, 3, 4], [4, 5, 6], [6, 7, 8]]. And out of that you are creating an array :)

Thanks you very much, You explained the concept in an excellent way.
Reply


Messages In This Thread
RE: Please help in understanding this piece of code - by tinkalgo - May-10-2018, 11:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with understanding the following code ClimbAddict 3 2,421 Oct-13-2019, 10:14 AM
Last Post: ClimbAddict

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020