Generating pattern similar to QR code using strings I wrote this class to generate a random "image," similar to a QR code, using strings. How can I make it better (I am a beginner in programming)?

import random

""" N is the length of squared field to be generated"""
class genImg():
  def __init__(self, N):
    self.N = N
    self.field = []

  def genField(self):
    for i in range(self.N):
      row = []
      for j in range(self.N):
        row.append("|-|")
      self.field.append(row[:])

  def getField(self):
    return self.field

  def fillField(self):
    field = self.getField()
    result = ""
    for i in field:
      row = ""
      for j in i:
        randNum = random.randint(1,100)
        if randNum % 2 == 0 or randNum % 3 == 0:
          j = '|0|'
        row += str(j) + " "
      result += row[:-1] + "\n"
    print(result)
You are doing unnecessary work by first looping over each row and each column to initialise the field to empty squares; and then looping over each row and each column to change the state based on a random condition. You can do it in one looping by assigning the "empty" or "filled" state based on said condition.

And while we're at it, you could simplify the condition as there is exactly 67 numbers that are either multiple of two or three between 1 and 100. So you could at least test if randNum &lt;= 67 which would yield the same probability. But this probability is pretty close to \$\frac{2}{3}\$ so we can come up with an other approach: use a tupple composed of two " filled" patterns and an "empty" one and use random.choice instead.

You could also benefit a lot from the str.join method instead of concatenating the chosen pattern and a separator to a temporary string. It will help improve your memory usage. Just put the selected patterns in a list and use the join method of your separator to create the whole string at once. Better yet, join over a list-comprehension or a generator expression.

Lastly, you don't need a class to solve this problem as you don't have any state. The various attributes are only there to store temporary variables. A simple function will do:

import random


def generate_image(size, *, filled='|0|', empty='|-|'):
    patterns = filled, filled, empty

    return '\n'.join(
            ' '.join(random.choice(patterns) for _ in range(size))
            for _ in range(size)
    )


if __name__ == '__main__':
    print(generate_image(20))