Basic Data Types

Lists

if __name__ == '__main__':
    N = int(input())

    LIST = list()

    for _ in range(N):
        instruction = input().strip().split(' ')

        if instruction[0] == "insert":
            LIST.insert(int(instruction[1]), int(instruction[2]))

        elif instruction[0] == "remove":
            LIST.remove(int(instruction[1]))

        elif instruction[0] == "append":
            LIST.append(int(instruction[1]))

        elif instruction[0] == "sort":
            LIST.sort()

        elif instruction[0] == "pop":
            LIST.pop()

        elif instruction[0] == "reverse":
            LIST.reverse()

        elif instruction[0] == "print":
            print(LIST)

Find the Runner-Up Score!

if __name__ == '__main__':
    n = int(input())
    arr = map(int, input().split())

    print(sorted(set(list(arr)))[-2])

Finding the Percentage

if __name__ == '__main__':
    n = int(input())
    student_marks = {}
    for _ in range(n):
        name, *line = input().split()
        scores = list(map(float, line))
        student_marks[name] = scores
    query_name = input()
    
    print("%.2f" % (sum(student_marks[query_name]) / len(student_marks[query_name])))

Nested Lists

if __name__ == '__main__':
    markSheet = []
    scores = []

    for i in range(0, int(input())):
        name = input()
        score = float(input())
        markSheet.append([name, score])
        scores.append(score)

    sec_low_grade = sorted(list(set(scores)))[1]

    for NAME, SCORE in sorted(markSheet):
        if SCORE == sec_low_grade:
            print(NAME)

List Comprehensions

if __name__ == '__main__':
    x = int(input())
    y = int(input())
    z = int(input())
    n = int(input())
    
    print(sorted([[i, j, k] for k in range(0, z + 1) for j in range(0, y + 1) for i in range(0, x + 1) if i + j + k != n]))

Tuples

# Compile with Pypy3

if __name__ == '__main__':
    n = int(input())
    print(hash(tuple(map(int, input().split()))))

Last updated