Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Naming Rules | Variables and Types
Introduction to Python

Naming Rules

As we discussed in the last chapter, variables are essential for holding values that we'll use later on. When naming variables in Python, there are some rules and best practices to keep in mind:

  • Variable names can include letters, numbers, or underscores:
    • myVariable1;
    • number_2;
    • data_frame3;
    • Python4Life;
    • _underscoreVar.
  • Variable names can't start with a number:
    • variable1 (Correct);
    • 1variable (Incorrect - starts with a number).
  • Variable names are case-sensitive:
    • MyVariable and myvariable are different variables;
    • Data and data are different variables;
    • Number1 and number1 are different variables.
  • Apart from the underscore _, special characters aren't allowed:
    • my_variable (Correct);
    • my-variable (Incorrect - contains a hyphen);
    • variable@name (Incorrect - contains an @);
    • data*star (Incorrect - contains an asterisk *).
  • Reserved words in Python can't be used as variable names:
    • forLoop (Correct - not a reserved word);
    • ifCondition (Correct - not a reserved word);
    • for (Incorrect - for is a reserved word);
    • while (Incorrect - while is a reserved word);
    • try (Incorrect - try is a reserved word).

Which of these would be valid variable names in Python?

Selecione algumas respostas corretas

Note

If you're uncertain about why a particular variable name is considered correct or incorrect, you can refer back to the above rules and examples for clarification.

Tudo estava claro?

Seção 2. Capítulo 2
course content

Conteúdo do Curso

Introduction to Python

Naming Rules

As we discussed in the last chapter, variables are essential for holding values that we'll use later on. When naming variables in Python, there are some rules and best practices to keep in mind:

  • Variable names can include letters, numbers, or underscores:
    • myVariable1;
    • number_2;
    • data_frame3;
    • Python4Life;
    • _underscoreVar.
  • Variable names can't start with a number:
    • variable1 (Correct);
    • 1variable (Incorrect - starts with a number).
  • Variable names are case-sensitive:
    • MyVariable and myvariable are different variables;
    • Data and data are different variables;
    • Number1 and number1 are different variables.
  • Apart from the underscore _, special characters aren't allowed:
    • my_variable (Correct);
    • my-variable (Incorrect - contains a hyphen);
    • variable@name (Incorrect - contains an @);
    • data*star (Incorrect - contains an asterisk *).
  • Reserved words in Python can't be used as variable names:
    • forLoop (Correct - not a reserved word);
    • ifCondition (Correct - not a reserved word);
    • for (Incorrect - for is a reserved word);
    • while (Incorrect - while is a reserved word);
    • try (Incorrect - try is a reserved word).

Which of these would be valid variable names in Python?

Selecione algumas respostas corretas

Note

If you're uncertain about why a particular variable name is considered correct or incorrect, you can refer back to the above rules and examples for clarification.

Tudo estava claro?

Seção 2. Capítulo 2
some-alt