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
course content

Course Content

Introduction to Python

Introduction to Python

1. First Acquaintance
2. Variables and Types
3. Conditional Statements
4. Other Data Types
5. Loops
6. Functions

book
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?

Which of these would be valid variable names in Python?

Select a few correct answers

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.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 2
We're sorry to hear that something went wrong. What happened?
some-alt