Which Variable Is Illegal in Python? Understanding Python's Naming Conventions
If you're diving into the world of Python programming, you'll quickly encounter the concept of variables. Variables are like containers that hold data. You give them names so you can refer to them later in your code. But not all names are allowed. Python has a set of rules for naming variables, and breaking these rules can lead to errors, preventing your program from running. So, what exactly makes a variable "illegal" in Python?
In Python, there isn't a single "illegal variable" in the sense of a pre-defined variable that you can't use. Instead, what makes a variable name invalid are violations of Python's naming conventions. These conventions are crucial for ensuring your code is readable and functional. Let's break down what constitutes a valid and invalid variable name.
What Makes a Variable Name Valid in Python?
For a variable name to be considered valid in Python, it must adhere to the following rules:
- Must start with a letter or the underscore character (_): The very first character of your variable name can be any letter from 'a' to 'z' (both lowercase and uppercase) or an underscore.
- Can contain letters, numbers, and underscores: After the first character, your variable name can include any combination of letters (a-z, A-Z), numbers (0-9), and underscores.
- Are case-sensitive: This is a very important point!
myVariableis different frommyvariableandMYVARIABLE. Python treats them as distinct variables.
What Makes a Variable Name Invalid (or "Illegal") in Python?
Now, let's look at what will cause an error when you try to name a variable:
- Cannot start with a number: You cannot begin a variable name with a digit (0-9). For example,
1st_placeis an invalid variable name. - Cannot contain special characters (except underscore): Variable names cannot include characters like !, @, #, $, %, ^, &, *, (, ), -, +, =, {, }, [, ], |, \, :, ;, ", ', <, >, ,, ., ?. For instance,
my-variableoryour_name!are not allowed. - Cannot be a Python reserved keyword: Python has a set of words that have special meaning to the language. These are called reserved keywords, and you absolutely cannot use them as variable names.
Python Reserved Keywords
These keywords are fundamental to Python's syntax and structure. If you try to use one of them as a variable name, Python will raise a SyntaxError.
Here's a list of common Python reserved keywords:
False, None, True, and, as, assert, async, await, break, class, continue, def, del, elif, else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield.
Important Note: While you *can* technically use keywords that are not part of the main list (like `print` or `str` in some older versions or specific contexts), it's an extremely bad practice and strongly discouraged. Always stick to the reserved keyword list to avoid confusion and potential issues.
Examples of Invalid Variable Names
To further illustrate, here are some examples of variable names that are considered "illegal" or invalid in Python:
2cool(Starts with a number)my-variable(Contains a hyphen)print(Is a reserved keyword)for_loop(foris a reserved keyword)variable with spaces(Contains spaces)@home(Starts with a special character)
Examples of Valid Variable Names
And here are some examples of valid variable names:
my_variableuser_input_private_var(The underscore at the beginning often signifies a variable intended for internal use within a module or class)counter1userName(Camel case, common in some programming styles)MAX_SIZE(All uppercase with underscores is common for constants)
When you encounter a SyntaxError related to variable naming in Python, it almost always means you've violated one of these fundamental rules. By paying close attention to these conventions, you'll be well on your way to writing clean, error-free Python code.
Understanding these naming rules is a foundational step for anyone learning Python. It ensures your programs are not just functional but also maintainable and easy for others (and your future self!) to understand.
Frequently Asked Questions (FAQ)
How do I know if a word is a reserved keyword in Python?
The easiest way is to refer to the official list of Python keywords. You can also try to use a word as a variable name in a Python interpreter; if it's a keyword, you'll immediately get a SyntaxError.
Why can't variable names start with a number?
This rule helps Python distinguish between variable names and numerical literals. If variable names could start with numbers, it would be ambiguous whether `123` represented a variable or the number one hundred twenty-three.
What's the deal with underscores in variable names?
Underscores are allowed in Python variable names and are often used to separate words in a variable name (like first_name) for readability. A leading underscore (_variable) can also be used as a convention to indicate that a variable or function is intended for internal use and not part of the public interface.
Can I use the same variable name multiple times?
Yes, but it's generally not a good idea unless you intend to reassign the variable to a new value. When you assign a new value to an existing variable name, the old value is lost, and the variable now holds the new information. For example, x = 5 then x = 10 means x now holds the value 10.

