Good names help you and others understand your code easily. Following rules avoids errors and confusion.
Naming rules and conventions in Python
Introduction
Syntax
Python
variable_name = value def function_name(): class ClassName:
Names can include letters, numbers, and underscores (_), but cannot start with a number.
Python is case-sensitive: age and Age are different names.
Examples
Python
age = 25 user_name = "Alice"
Python
def calculate_sum(): pass
Python
class Car: pass
Python
_hidden_value = 10Sample Program
This program shows two variables with different naming styles and prints their values.
Python
user_age = 30 UserName = "Bob" print(f"User age is {user_age}") print(f"User name is {UserName}")
Important Notes
Do not use Python reserved words like if, for, or class as names.
Use meaningful names that describe what the variable or function does.
Stick to one style for consistency to make your code easier to read.
Summary
Names must start with a letter or underscore, not a number.
Use lowercase with underscores for variables and functions.
Use CapitalizedWords for class names.
Practice
1. Which of the following is a valid variable name in Python?
easy
Solution
Step 1: Check the first character of each name
Python variable names must start with a letter or underscore, not a number or special character.Step 2: Identify invalid characters
Names cannot contain spaces or hyphens. Only letters, digits, and underscores are allowed.Final Answer:
my_variable -> Option CQuick Check:
Valid variable name = my_variable [OK]
Hint: Variable names start with letter/underscore, no spaces or hyphens [OK]
Common Mistakes:
- Starting variable name with a number
- Using hyphens instead of underscores
- Including spaces in variable names
2. Which of these is the correct way to name a class in Python?
easy
Solution
Step 1: Recall class naming convention
Class names in Python use CapitalizedWords style (PascalCase), starting with uppercase letters.Step 2: Compare options to convention
Only 'MyClass' follows CapitalizedWords style correctly.Final Answer:
MyClass -> Option AQuick Check:
Class name style = CapitalizedWords [OK]
Hint: Class names use CapitalizedWords (PascalCase) [OK]
Common Mistakes:
- Using lowercase or underscores for class names
- Starting class names with lowercase letters
- Mixing camelCase with underscores
3. What will be the output of this code?
_count = 5 Count = 10 print(_count + Count)
medium
Solution
Step 1: Understand variable names and case sensitivity
Python variable names are case-sensitive, so '_count' and 'Count' are different variables.Step 2: Calculate the sum of the two variables
_count = 5 and Count = 10, so 5 + 10 = 15.Final Answer:
15 -> Option BQuick Check:
5 + 10 = 15 [OK]
Hint: Variable names are case-sensitive; add values accordingly [OK]
Common Mistakes:
- Ignoring case sensitivity and treating variables as same
- Expecting concatenation instead of addition
- Assuming syntax or name errors
4. Find the error in this variable name:
user-name = 'Alice'
medium
Solution
Step 1: Identify invalid characters in variable names
Hyphens are not allowed in Python variable names because they are interpreted as minus signs.Step 2: Confirm other options are incorrect
Variable names can start with letters, contain underscores, and be lowercase.Final Answer:
Variable names cannot contain hyphens -> Option AQuick Check:
Hyphens not allowed in names [OK]
Hint: No hyphens in variable names; use underscores instead [OK]
Common Mistakes:
- Using hyphens instead of underscores
- Thinking variable names can't start with letters
- Confusing underscores with invalid characters
5. You want to create a dictionary where keys are variable names and values are their types. Which naming style is best for the keys?
variables = { 'UserName': str, 'user_age': int, '2ndUser': str } Which keys follow Python naming rules and conventions?hard
Solution
Step 1: Check each key for naming rules
'UserName' starts with a letter and uses CapitalizedWords (good for class names). 'user_age' uses lowercase with underscore (good for variables). '2ndUser' starts with a number, which is invalid.Step 2: Identify keys following naming conventions
'UserName' and 'user_age' follow naming rules; '2ndUser' does not.Final Answer:
'UserName' and 'user_age' only -> Option DQuick Check:
Keys must start with letter; numbers not allowed [OK]
Hint: Keys must start with letter; numbers not allowed [OK]
Common Mistakes:
- Allowing keys starting with numbers
- Ignoring naming conventions for readability
- Assuming all strings are valid variable names
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
