Python: Built-in Function range, ord, chr

# Help on built-in function range in module __builtin__:

range(...)
    range(stop) -> list of integers
    range(start, stop[, step]) -> list of integers

    Return a list containing an arithmetic progression of integers.
    range(i, j) returns [i, i+1, i+2, ..., j-1]; start (!) defaults to 0.
    When step is given, it specifies the increment (or decrement).
    For example, range(4) returns [0, 1, 2, 3].  The end point is omitted!
    These are exactly the valid indices for a list of 4 elements.

Examples

$ python2

>>> range(-1)
[]

>>> range(0)
[]

>>> range(1)
[0]

>>> range(2)
[0, 1]

>>> range(3)
[0, 1, 2]

>>> range(4)
[0, 1, 2, 3]

>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> len(range(32))
32

>>> range(1, 1, -1)
[]

>>> range(1, 0, -1)
[1]

>>> range(1, -1, -1)
[1, 0]

>>> range(1, -2, -1)
[1, 0, -1]

>>> range(1, -3, -1)
[1, 0, -1, -2]

>>> range(1, -4, -1)
[1, 0, -1, -2, -3]

In Reverse Order

$ python2

>>> range(16)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

>>> range(0, 16, 1)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]


>>> range(16)[::-1]
[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

>>> range(15, -1, -1)
[15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
$ python3

>>> range(-1)
range(0, -1)

>>> list(range(-1))
[]

>>> range(0)
range(0, 0)

>>> list(range(0))
[]

>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> list(range(0, 10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> list(range(0, 10, 1))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> list(range(10, 0, -1))
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

>>> range(10)[::-1]
range(9, -1, -1)

>>> list(range(9, -1, -1))
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

>>> list(range(0, 10, -1))
[]

>>> l = ['a', 'b', 'c']
>>> for i in range(len(l)):
...     print(l[i], end=" ")
...     if i == len(l) - 1:
...         print()
...
a b c


>>> for i in range(97, 97+26):
...     print(chr(i), end=" ")
...     if i == 97+25:
...         print()
...
a b c d e f g h i j k l m n o p q r s t u v w x y z


# https://en.wikipedia.org/wiki/Burmese_alphabet
>>> for i in range(34):
...     ka = ord('က')
...     print(chr(ka+i), end=" ")
...     if i == 33:
...         print()
...
က ခ ဂ ဃ င စ ဆ ဇ ဈ ဉ ည ဋ ဌ ဍ ဎ ဏ တ ထ ဒ ဓ န ပ ဖ ဗ ဘ မ ယ ရ လ ဝ သ ဟ ဠ အ


# Adding odd number below 10
>>> s = ''
>>> sum = 0
>>> for i in range(1, 10, 2):
...     sum = sum + i
...     s = s + '+' + str(i) if i > 1 else str(i)
...     print(f'{s} = {sum}')
...
1 = 1
1+3 = 4
1+3+5 = 9
1+3+5+7 = 16
1+3+5+7+9 = 25
# Help on built-in function ord in module builtins:

ord(c, /)
    Return the Unicode code point for a one-character string.

# Help on built-in function chr in module builtins:

chr(i, /)
    Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.
>>> ord('a')
97

>>> ord('က')
4096

>>> ord(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ord() expected string of length 1, but int found

>>> ord('ab')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 2 found

>>> ord('ကို')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 5 found

>>> len('ကို')
3

Looks like a bug in error reporting of ord where the length of the string 'ကို' is reported as 5 instead of 3.

>>> chr(97)
'a'

>>> chr(4096)
'က'

>>> chr('a')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required (got type str)

Leave a Comment

Your email address will not be published. Required fields are marked *