Skip to main content
Combined multiple answers about the loop and the usage of j, k, l, m
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

Instead of

i = 0
while i < len(text):
    j = ord(text[i])
    ...
    ...
    ...
    i += 1

and using text[i] for accessing individual elements (which is NOT a Pythonic way).
Get rid of i and write simply

i = 0
whilefor ic <in len(text):
    j = ord(text[i]c)
    ...
    ...
    ...
    i += 1

    j = ord(text[i])
    k = bin(ord(text[i]))
    l = hex(ord(text[i]))
    m = oct(ord(text[i]))

and usingAfter performing the first of those commands is ord(text[i]) for accessing individual elements (which is NOT a Pythonic way).
Get rid ofalready in the variable ij and write simply, isn't it? So why not reuse it in the rest of them:

for c in text:
    j = ord(c)
    ...k = bin(j)
    ...l = hex(j)
    ..m = oct(j)

Instead of

print(text[i], " ", j, " ", k, "", l, "", m)

use the format() method and replacement fields {}:

print("{}   {}   {}  {}  {}".format(text[i], j, k, l, m))

Instead of

i = 0
while i < len(text):
    j = ord(text[i])
    ...
    ...
    ...
    i += 1

and using text[i] for accessing individual elements (which is NOT a Pythonic way).
Get rid of i and write simply

for c in text:
    j = ord(c)
    ...
    ...
    ...

Instead of

i = 0
while i < len(text):
    j = ord(text[i])
    ...
    ...
    ...
    i += 1

and using text[i] for accessing individual elements (which is NOT a Pythonic way).
Get rid of i and write simply

for c in text:
    j = ord(c)
    ...
    ...
    ...

    j = ord(text[i])
    k = bin(ord(text[i]))
    l = hex(ord(text[i]))
    m = oct(ord(text[i]))

After performing the first of those commands is ord(text[i]) already in the variable j, isn't it? So why not reuse it in the rest of them:

for c in text:
    j = ord(c)
    k = bin(j)
    l = hex(j)
    m = oct(j)

Instead of

print(text[i], " ", j, " ", k, "", l, "", m)

use the format() method and replacement fields {}:

print("{}   {}   {}  {}  {}".format(text[i], j, k, l, m))
added 4 characters in body
Source Link
MarianD
  • 2k
  • 1
  • 11
  • 20

Instead of

i = 0
while i < len(text):
    j = ord(text[i])
    ...
    ...
    ...
    i += 1

and using text[i] for accessing individual elements (which is NOT a Pythonic way) get.
Get rid of i and write simply

for c in text:
    j = ord(c)
    ...
    ...
    ...

Instead of

i = 0
while i < len(text):
    j = ord(text[i])
    ...
    ...
    ...
    i += 1

and using text[i] for accessing individual elements (which is NOT a Pythonic way) get rid of i and write simply

for c in text:
    j = ord(c)
    ...
    ...
    ...

Instead of

i = 0
while i < len(text):
    j = ord(text[i])
    ...
    ...
    ...
    i += 1

and using text[i] for accessing individual elements (which is NOT a Pythonic way).
Get rid of i and write simply

for c in text:
    j = ord(c)
    ...
    ...
    ...
Source Link
MarianD
  • 2k
  • 1
  • 11
  • 20

Instead of

i = 0
while i < len(text):
    j = ord(text[i])
    ...
    ...
    ...
    i += 1

and using text[i] for accessing individual elements (which is NOT a Pythonic way) get rid of i and write simply

for c in text:
    j = ord(c)
    ...
    ...
    ...