fb pixel

Introduction to Python

  • Profound Academy

    • Status
      • 1
        Input and Output
      • 2
        Variables and Integer Arithmetic
      • 3
        Conditional Operators
      • 4
        Advanced Conditions - Nesting
      • 5
        Types and Variables
      • 6
        Strings
      • 7
        Lists
      • 8
        For Loops
      • 9
        While Loops
      • 10
        continue, break, while...else
      • 11
        String and List methods
      • 12
        Nested Loops
      • 13
        List Comprehension
      • 14
        Tuples and Sets
      • 15
        dict
      • 16
        Functions
      • 17
        Functions 2
      • 18
        Lambda and higher-order functions
      • 19
        Files

  • 2D lists

    Python lists can contain different types of elements. They can have integers, strings, floating-point numbers etc. They can also contain other lists:
    l = [4, 5, 'hi', 3.14, ['how', 'are', 'you', '?'], 64]
    print(l[2])       # hi
    print(l[2][0])    # h
    inner = l[4]
    print(inner[1])   # are
    print(l[4][1])    # are
    This is an example of a list in python that contains another list in it. The operations with the nested list are exactly the same as the ones with the outer one. One can add elements with append, or remove them with pop, etc. Therefore the indexing would work like we are accessing a nested list from within a list.
    We can even keep a matrix of numbers this way:
    multiplication_table = [
    	[1, 2, 3, 4, 5, 6, 7, 8, 9],
    	[2, 4, 6, 8, 10, 12, 14, 16, 18],
    	[3, 6, 9, 12, 15, 18, 21, 24, 27],
    	[4, 8, 12, 16, 20, 24, 28, 32, 36],
    	[5, 10, 15, 20, 25, 30, 35, 40, 45],
    	[6, 12, 18, 24, 30, 36, 42, 48, 54],
    	[7, 14, 21, 28, 35, 42, 49, 56, 63],
    	[8, 16, 24, 32, 40, 48, 56, 64, 72],
    	[9, 18, 27, 36, 45, 54, 63, 72, 81]
    ]
    
    print(multiplication_table[2])
    # [3, 6, 9, 12, 15, 18, 21, 24, 27]
    notion image
     

    Challenge

    Given a square of size , you are asked to divide the square into 3 parts. The one above its diagonal, the one on the diagonal, and the one below it.
    The fields above should be painted with 0, the fields on the diagonal should be painted with 1, and the ones below should be in 2.
    The input of the program contains a single integer n - the size of the square.
    The output of the program should contain the painted square.
    Input
    Output
    4
    1 0 0 0 2 1 0 0 2 2 1 0 2 2 2 1
     
    To check your solution you need to sign in
    Sign in to continue