Python Forum
Zen of Python violated by Python
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Zen of Python violated by Python
#11
Some Older buildings in US don't have a 13th floor!
Reply
#12
Now I have to look up Cryptonomicon!
I am trying to help you, really, even if it doesn't always seem that way
Reply
#13
Why Python uses 0-based indexing
So slice notation had a big part of why Python is 0-based indexing.
GVR Wrote:Especially the invariant that when two slices are adjacent,
the first slice's end index is the second slice's start index is just too beautiful to ignore.
For example, suppose you split a string into three parts at indices i and j -- the parts would be a[:i], a[i:j], and a[j:].
If test it out,it do make sense.
>>> s = 'hello'
>>> s[:1]
'h'
>>> s[1:4]
'ell'
>>> s[4:]
'o'
The month problem with the often forgotten second parameter to enumerate('...', 1).
>>> months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
>>> list(enumerate(months, 1))
[(1, 'Jan'),
 (2, 'Feb'),
 (3, 'Mar'),
 (4, 'Apr'),
 (5, 'May'),
 (6, 'Jun'),
 (7, 'Jul'),
 (8, 'Aug'),
 (9, 'Sep'),
 (10, 'Oct'),
 (11, 'Nov'),
 (12, 'Dec')]

# Dicionary
>>> month = dict(list(enumerate(months, 1)))
>>> month
{1: 'Jan',
 2: 'Feb',
 3: 'Mar',
 4: 'Apr',
 5: 'May',
 6: 'Jun',
 7: 'Jul',
 8: 'Aug',
 9: 'Sep',
 10: 'Oct',
 11: 'Nov',
 12: 'Dec'}

>>> month[2]
'Feb'

# The other way araound
>>> month = {v: k for k, v in month.items()}
>>> month
{'Jan': 1,
 'Feb': 2,
 'Mar': 3,
 'Apr': 4,
 'May': 5,
 'Jun': 6,
 'Jul': 7,
 'Aug': 8,
 'Sep': 9,
 'Oct': 10,
 'Nov': 11,
 'Dec': 12}

>>> month['Feb']
2
Reply
#14
C also uses 0 indexing. I always thought it made sense.
Some of my earliest micro code (rockwell 6502, Intel 8080, etc) was done with Dartmouth basic.
I remember often thinking that it would have been nice of indexes began with 0, but with this language, they began with 1.
Reply
#15
(Jul-04-2018, 04:34 PM)Larz60+ Wrote: Some Older buildings in US don't have a 13th floor!

However, snippsat has the 13th post in this thread, which is 1-indexed. Cool
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#16
(Jul-04-2018, 10:36 AM)DeaD_EyE Wrote: How does this look with 1-based indexing?

you are hurting my brain.

i've worked in assembler/machine code on at least a dozen different CPU architectures. every one of them, with no exceptions, was 0-based.

(Jul-04-2018, 03:44 PM)gruntfutuk Wrote: Whenever I visit USA and get in a lift (elevator) I'm confused because they count the ground floor as floor 1. In the UK, that's ground (0) and the floor above is floor 1, first floor.
you will be doubly confused if you visit the towers dormitories at West Virginia University. one of the towers has an actual 0 floor.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#17
(Jul-04-2018, 10:36 AM)DeaD_EyE Wrote: How does this look with 1-based indexing?

Here's how it would work in SAS with a string (character) variable.

text = 'ONE-BASED INDEXING';
first_two = substr(text, 1, 2);
next_four = substr(text, 3, 4);
The parameters are the string, the starting index, and the length of the sub-string. Arrays in SAS, which are totally different from lists in Python, are not referenced with slices. They are only referenced one item at a time, or all of them at once (but all at once only works in limited situations).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#18
the big irony ... years numbers are 1-based. yet, a few years ago, around the turn of the century, so many people actually thought years were 0-based and said 2000 was the new century. but 2001 was. there never was a year 0. the 1st century began with year 1 and ended at the end of year 100. year 101 was the start of the 2nd century which ended at the end of year 200. year 1901 was the start of the 20th century which ended at the end of the year 2000. year 2001 was the start of the 21st century which will end at the end of year 2100. got it?

years, months, days of the year, and days of the month are all 1-based. hours of the 24 hou systems, minutes, and seconds are all 0-based. hours of the 12 hour system are proxy 0-based with 12 substituting/proxying for 0.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#19
(Jul-04-2018, 03:44 PM)gruntfutuk Wrote: Whenever I visit USA and get in a lift (elevator) I'm confused because they count the ground floor as floor 1. In the UK, that's ground (0) and the floor above is floor 1, first floor.

Same in China :-D
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

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