|
Forum Index : Microcontroller and PC projects : Rudimentary Counting
| Author | Message | ||||
| hitsware2 Guru Joined: 03/08/2019 Location: United StatesPosts: 735 |
Sequence of 4 : With Basic : FOR x = 0 TO 3 Ruby : For x IN 0..3 Python : FOR x IN RANGE( 0 , 4 ) What ' s the basic differance with how Python works that demands the extra numeral ? my site |
||||
| phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 2828 |
This is just a guess. The first two languages indicate the first and last numbers of the count, but in Python perhaps the second number is the total number of runs through the loop. |
||||
| Rado Regular Member Joined: 27/11/2020 Location: CroatiaPosts: 59 |
range in Python is a built-in with three arguments, two optional, one required: range (x) - will count from 0 to x-1 (a range of x numbers) - will internally expand to (0,x) range (x,y) - will count from x to y-1 range (x,y,z) - will count from x to y-1, using step z Therefore, you can say in Python for x in range (4), and it will count 0, 1, 2 and 3. That 4 is the stop range, meaning that it will count up to that number, but stop before reaching it. This makes sense given the option base is zero, so range (5) will produce a range of five numbers: 0-4. This makes Python syntax-consistent for other things, as it goes into coder's motor memory that things start with zero and finish with max-1. Could it be different? I guess it could, but this is the way that language works. People who find this counterintuitive can easily write their own range function that conforms to whatever they want. Also, please note that Ruby can follow the same syntax: for x in 0...4 (note three dots) will produce 0,1,2,3 - a nice way to accommodate to both range expectations and coder's motor memory. |
||||
| hitsware2 Guru Joined: 03/08/2019 Location: United StatesPosts: 735 |
Thank You ! Would You elaborate somewhat as to what constitutes a " built in " ? my site |
||||
| Rado Regular Member Joined: 27/11/2020 Location: CroatiaPosts: 59 |
A built-in as an integral part of language (think BASIC commands) and always available, not an external dependency or part of an external module. Here are built-ins for Python3, totally uncalled for: abs delattr hash memoryview set all dict help min setattr any dir hex next slice ascii divmod id object sorted bin enumerate input oct staticmethod bool eval int open str breakpoint exec isinstance ord sum bytearray filter issubclass pow super bytes float iter print tuple callable format len property type chr frozenset list range vars classmethod getattr locals repr zip compile globals map reversed __import__ complex hasattr max round |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |