1

My guess:

In Python:

// was used for floor division and they couldn't come up with any other alternative symbol for floor division so they couldn't use // for comments in Python.

# was an available character to be used in Python as there is no concept of pre-processing, and they made a choice to use # for comments.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
shar_m
  • 29
  • 1
  • 4
    Comments came *way* before floor division. – user2357112 Jul 04 '22 at 00:09
  • C got there first using `#` for the preprocessor directives, but most scripting languages use `#` for comments — shells, Perl, Python, Ruby, Tcl/Tk, PHP, you name it! And C only got `//` comments recently (C99); they appeared in C++ first. They're all different languages so different comment conventions are acceptable. (For fun, look up comments in Fortran, Cobol, Algol!) And Perl uses `//` for a totally different operation — neither comments nor truncating division. – Jonathan Leffler Jul 04 '22 at 00:17
  • 3
    Using `#` or `//` for comments predates both Python and C. There's no meaningful comparison to be made. The two languages just made different choices. Also, as a fun fact, Python using `#` for single line comments *came before* the introduction of `//` for single-line comments in standardized C. – Brian61354270 Jul 04 '22 at 00:18

3 Answers3

4

I'm afraid your assumption is false: the floor division operator // is quite recent in Python, whereas # comments are part of the original design.

The origin of # comments is older:

  • the early unix shells (the original sh command started in 1971, csh from 1978 and the Bourne shell 1979) introduced the use of # to start line comments, followed by all later unix shells.
  • Many script like programming languages already used # for comments: sed (1974), make (1976), awk (1977) ...
  • # was also used for comments in configuration files
  • later scripting languages followed the same convention: TCL (1988), Perl (1988), Python (1991), PHP (1994), Ruby (1995) and more recently Cobra, Seed7, Windows PowerShell, R, Maple, Elixir, Julia, Nim...

Regarding the C preprocessor, here is a quote from Dennis M. Ritchie's own memories of The Development of the C Language about the origin of the C preprocessor and the true meaning of the # character:

Many other changes occurred around 1972-3, but the most important was the introduction of the preprocessor, partly at the urging of Alan Snyder [Snyder 74], but also in recognition of the utility of the the file-inclusion mechanisms available in BCPL and PL/I. Its original version was exceedingly simple, and provided only included files and simple string replacements: #include and #define of parameterless macros. Soon thereafter, it was extended, mostly by Mike Lesk and then by John Reiser, to incorporate macros with arguments and conditional compilation. The preprocessor was originally considered an optional adjunct to the language itself. Indeed, for some years, it was not even invoked unless the source program contained a special signal at its beginning. This attitude persisted, and explains both the incomplete integration of the syntax of the preprocessor with the rest of the language and the imprecision of its description in early reference manuals.

# was used as a special character at the beginning of a C source file to determine if the preprocessor was to be invoked.

PL/I file include directives use %INCLUDE and BCPL uses GET "libhdr"

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • The earliest versions of Bourne shell used the `:` command for comments — and the comment material was normally placed inside single quotes and you had to be careful about embedded single quotes (`: 'It wasn't safe to use single abbreviations like this'`). AFAIK, the C shell introduced `#` comments, but they were swiftly adopted into Bourne shell too. – Jonathan Leffler Jul 04 '22 at 00:23
  • Also, note that many config files (e.g. `systemd`) use `#` for comments. They're easy to parse. – Craig Estey Jul 04 '22 at 00:52
2

C was by no means the only prior art available when Guido was choosing the details of Python language syntax. # is in fact a pretty common comment-introduction character, especially for scripting languages. Examples include the Bourne family of shells, the Csh family of shells, Perl, awk, and sed, all of which predate Python. I have always supposed that this aspect of Python's syntax was most influenced by this fairly large group of languages.

Whatever the influences were, they did not include consideration of a conflict with the use of // for floor division, as that operator was not introduced until much later.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
1

The use of // comments dates back to 1967 [or earlier].

I came across Keith Thompson's answer: With arrays, why is it the case that a[5] == 5[a]?

In it are links to language reference manuals for B and BCPL at Bell Labs:

  1. The B language manual from 1972 (precursor to C): User's Reference to B
  2. The BCPL language manual from 1967 (precursor to B): Martin Richards's BCPL Reference Manual, 1967
  3. From a link there, we have a link to a PDF file that is a transcription of MIT Project MAC Memorandum-M-352

From that memorandum (the BCPL manual), in section 2.1.2 (b):

2.1.2 Hardware Conventions and Preprocessor Rules

(a) If the implementation character set contains both capital and small letters then the following conventions hold:

(1) A name is either a single small letter or a sequence of letters and digits starting with a capital letter. The character immediately following a name may not be a letter or a digit.

(2) A sequence of two or more small letters which is not part of a NAME, SECTBRA, SECTKET or STRINGCONST is a reserved system word and may be used to represent a canonical symbol.
For example: let and logor could be used to represent LET and LOGOR but Let and Logor are names.

(b) User’s comment may be included in a program between a double slash '//' and the end of the line. Example:

let R[] be // This routine refills the vector Symb
§ for i = 1 to 200 do Readch [INPUT, lv Symb*[i]] §

Craig Estey
  • 30,627
  • 4
  • 24
  • 48