Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 03:16 18 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Rudimentary Counting

Author Message
hitsware2

Guru

Joined: 03/08/2019
Location: United States
Posts: 735
Posted: 07:02pm 27 Mar 2021
Copy link to clipboard 
Print this post

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: Australia
Posts: 2828
Posted: 10:14pm 27 Mar 2021
Copy link to clipboard 
Print this post

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: Croatia
Posts: 59
Posted: 12:27am 28 Mar 2021
Copy link to clipboard 
Print this post

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 States
Posts: 735
Posted: 03:25pm 28 Mar 2021
Copy link to clipboard 
Print this post

  Rado said  range in Python is a built-in with three arguments, two optional, one required:

Thank You !
Would You elaborate somewhat as to what constitutes a " built in " ?
my site
 
Rado
Regular Member

Joined: 27/11/2020
Location: Croatia
Posts: 59
Posted: 10:43pm 28 Mar 2021
Copy link to clipboard 
Print this post

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
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025