[Python] Why constants in Python can be changed?

Discussão em 'Python' iniciado por Stack, Janeiro 26, 2025.

  1. Stack

    Stack Membro Participativo

    A constant is a variable whose value stays the same throughout the life of a program. Python doesn’t have built-in constant types, but Python programmers use all capital letters to indicate a variable should be treated as a constant and never be changed.

    Here is an example:

    num1 = 20
    NUM = 40
    print(NUM)

    if num1 > 10:
    NUM = NUM - 5
    print(NUM)

    Output:


    40
    35


    I expected the constant NUM to not be changed, i.e. still be 40 and not 35, or get an error.

    Continue reading...

Compartilhe esta Página