+ 1

Do Letters and Special Characters Have Numerical Values Assigned to Them by Default?

I noticed while playing with the code for the "Try It Yourself" before the 4th question that "a" is less than "b" and "b" is less than "c", so it would follow that this continues through the rest of the alphabet. I then tried it with upper case letters and noticed that not only do they retain the same pattern, i.e. "A" is less than "B" and so on, but also that upper case letters are less than lower case letters, i.e. "A" is less than "a". The trend seems to continue with special characters as well. My question then is this: Are letters in Python assigned a numerical value by default? Are special characters also assigned numerical values? It seems a reasonable assumption that perhaps "a"-"z" = 1-26 and "A"-"Z" = 27-52 respectively, and that special characters would begin at 53. Or rather than numerical values were they simply given a hierarchy, so to speak, where one character is greater than another in the same way that a king is greater than an earl or a president is greater than a governor? What is the story here, and will this knowledge be relevant for coding later on or is it just an interesting quirk of Python? Edit: I've saved the code that I was playing with and made it public if anyone wants to take a look or play with it themselves.

9th Feb 2019, 11:22 PM
Zoltan
Zoltan - avatar
4 Respostas
+ 1
Thats not numerical value but it is ASCII code which is assigned to every number, character, special characters e.t.c. in computers by default. Not only in py, but in every language.
9th Feb 2019, 11:49 PM
Arushi Singhania
Arushi Singhania - avatar
0
Yes, there is. They have ASCII codes. "a" is 97. "A" is 65. In python, I think you can get them by using ord(c). To convert them back, there's chr(n).
9th Feb 2019, 11:48 PM
Jomari Pantorilla
Jomari Pantorilla - avatar
0
check the ASCII table to see the real values of every character. you can use this function in python to print the ASCII value of characters. ord('a' ) to get the value of a. use loop to iterate through the range from 0 to 127. letters do not start at the values you mentioned. I like your thinking tho :) try this in playground for c in range(0,128): print("character " + str(chr(c)) + " = "+ str(c))
9th Feb 2019, 11:49 PM
Bahhaⵣ
Bahhaⵣ - avatar
0
Characters are associated with numerical values in pretty much all programming languaged. Most languages use ASCII (Amercian Standard Code for Information Interchange) or unicode. Python uses ASCII. Computers use bits (0s and 1s) to represent information such as letters, larger numbers, multimedia, etc. https://en.m.wikipedia.org/wiki/ASCII
9th Feb 2019, 11:53 PM
naomi
naomi - avatar