Python Case-Sensitivity Rules
π·οΈ Python Basics: Syntax, Variables, and Types / Basic Syntax Rules
Python is a case-sensitive programming language, which means it treats uppercase and lowercase letters as completely different characters. This is one of the first rules new engineers encounter when writing Python code, and understanding it helps avoid frustrating errors.
βοΈ What Does Case-Sensitivity Mean?
Case-sensitivity means that Python distinguishes between identifiers based on their letter casing. For example, the variable name myVar is different from myvar, MyVar, or MYVAR. Each of these would be treated as separate, independent variables in your program.
π΅οΈ Key Rules to Remember
- Variable names are case-sensitive: username and UserName are two different variables
- Function names follow the same rule: calculate_total() is not the same as Calculate_Total()
- Keywords in Python are always lowercase: True, False, and None are the only exceptions (they start with capital letters)
- Built-in functions like print(), len(), and type() must be written in lowercase
- Module names are typically lowercase, but once imported, you must use the exact casing
π Case-Sensitivity Comparison Table
| Expression | Valid? | Explanation |
|---|---|---|
| myVar = 10 | β | Standard lowercase variable |
| myvar = 20 | β | Different variable from myVar |
| MyVar = 30 | β | Third distinct variable |
| print("Hello") | β | Correct casing for built-in function |
| Print("Hello") | β | Python will raise a NameError |
| True | β | Python keyword with capital T |
| true | β | Not recognized as a keyword |
| len("test") | β | Correct lowercase function name |
| Len("test") | β | Python will raise a NameError |
π οΈ Common Mistakes and How to Avoid Them
- Inconsistent variable naming: If you define a variable as userName but later try to access it as username, Python will throw a NameError
- Mixing up built-in functions: Writing Print() instead of print() is one of the most common beginner mistakes
- Confusing keywords: Remember that None, True, and False are the only keywords with capital letters
- Import errors: When importing modules, the casing must match exactly. import OS will fail if the module is named os
π‘ Best Practices for Engineers
- Choose a naming convention and stick with it consistently throughout your project
- Use lowercase with underscores for variable names (snake_case) like user_name or total_count
- Use CamelCase for class names like UserProfile or DatabaseConnection
- Always double-check the casing of built-in functions and keywords
- Use an IDE or code editor with syntax highlighting to catch casing errors early
π§ͺ Quick Self-Check
Ask yourself these questions when debugging case-sensitivity issues:
- Did I define the variable with the exact same casing I'm using to access it?
- Am I using print() (lowercase) or Print() (capital P)?
- Is the keyword True written with a capital T?
- Does the module name match the casing in my import statement?
π Final Thought
Case-sensitivity is a fundamental rule in Python that becomes second nature with practice. The key is consistencyβchoose a naming style, stick with it, and always verify your casing when encountering unexpected errors. Remember, Python treats data, Data, and DATA as three completely different things, so precision matters.
Python treats uppercase and lowercase letters as different characters β Name, name, and NAME are three separate identifiers.
π€ Example 1: Variable names are case-sensitive
This shows that two variables with the same letters but different case are completely different.
name = "Alice"
Name = "Bob"
print(name)
print(Name)
π€ Output: Alice
π€ Output: Bob
π€ Example 2: Function names are case-sensitive
This shows that calling a function with wrong case causes an error.
def greet():
return "Hello"
print(greet())
print(Greet())
π€ Output: Hello
π€ Output: NameError: name 'Greet' is not defined
π€ Example 3: Keywords are always lowercase
This shows that Python keywords like True, False, and None must be written exactly as shown.
is_active = True
print(is_active)
print(TRUE)
π€ Output: True
π€ Output: NameError: name 'TRUE' is not defined
π€ Example 4: String values are case-sensitive
This shows that Python treats the same word in different case as different strings.
status = "active"
if status == "Active":
print("Match found")
else:
print("No match")
π€ Output: No match
π€ Example 5: Class and method names are case-sensitive
This shows that class names and their methods must be called with exact case.
class Car:
def start(self):
return "Engine running"
my_car = Car()
print(my_car.start())
print(my_car.Start())
π€ Output: Engine running
π€ Output: AttributeError: 'Car' object has no attribute 'Start'
π Comparison Table: Case-Sensitivity Rules
| Element Type | Correct Case Example | Incorrect Case Example | Behavior |
|---|---|---|---|
| Variable | user_id |
User_Id |
Creates a new variable |
| Function | calculate() |
Calculate() |
Raises NameError |
| Keyword | True |
TRUE |
Raises NameError |
| String value | "active" |
"Active" |
Different string, no match |
| Method | .start() |
.Start() |
Raises AttributeError |
Python is a case-sensitive programming language, which means it treats uppercase and lowercase letters as completely different characters. This is one of the first rules new engineers encounter when writing Python code, and understanding it helps avoid frustrating errors.
βοΈ What Does Case-Sensitivity Mean?
Case-sensitivity means that Python distinguishes between identifiers based on their letter casing. For example, the variable name myVar is different from myvar, MyVar, or MYVAR. Each of these would be treated as separate, independent variables in your program.
π΅οΈ Key Rules to Remember
- Variable names are case-sensitive: username and UserName are two different variables
- Function names follow the same rule: calculate_total() is not the same as Calculate_Total()
- Keywords in Python are always lowercase: True, False, and None are the only exceptions (they start with capital letters)
- Built-in functions like print(), len(), and type() must be written in lowercase
- Module names are typically lowercase, but once imported, you must use the exact casing
π Case-Sensitivity Comparison Table
| Expression | Valid? | Explanation |
|---|---|---|
| myVar = 10 | β | Standard lowercase variable |
| myvar = 20 | β | Different variable from myVar |
| MyVar = 30 | β | Third distinct variable |
| print("Hello") | β | Correct casing for built-in function |
| Print("Hello") | β | Python will raise a NameError |
| True | β | Python keyword with capital T |
| true | β | Not recognized as a keyword |
| len("test") | β | Correct lowercase function name |
| Len("test") | β | Python will raise a NameError |
π οΈ Common Mistakes and How to Avoid Them
- Inconsistent variable naming: If you define a variable as userName but later try to access it as username, Python will throw a NameError
- Mixing up built-in functions: Writing Print() instead of print() is one of the most common beginner mistakes
- Confusing keywords: Remember that None, True, and False are the only keywords with capital letters
- Import errors: When importing modules, the casing must match exactly. import OS will fail if the module is named os
π‘ Best Practices for Engineers
- Choose a naming convention and stick with it consistently throughout your project
- Use lowercase with underscores for variable names (snake_case) like user_name or total_count
- Use CamelCase for class names like UserProfile or DatabaseConnection
- Always double-check the casing of built-in functions and keywords
- Use an IDE or code editor with syntax highlighting to catch casing errors early
π§ͺ Quick Self-Check
Ask yourself these questions when debugging case-sensitivity issues:
- Did I define the variable with the exact same casing I'm using to access it?
- Am I using print() (lowercase) or Print() (capital P)?
- Is the keyword True written with a capital T?
- Does the module name match the casing in my import statement?
π Final Thought
Case-sensitivity is a fundamental rule in Python that becomes second nature with practice. The key is consistencyβchoose a naming style, stick with it, and always verify your casing when encountering unexpected errors. Remember, Python treats data, Data, and DATA as three completely different things, so precision matters.
Interactive Views
You are currently in π All-in-One mode. Use the tabs at the top to switch to π Theory Only or π» Code Only views.
Python treats uppercase and lowercase letters as different characters β Name, name, and NAME are three separate identifiers.
π€ Example 1: Variable names are case-sensitive
This shows that two variables with the same letters but different case are completely different.
name = "Alice"
Name = "Bob"
print(name)
print(Name)
π€ Output: Alice
π€ Output: Bob
π€ Example 2: Function names are case-sensitive
This shows that calling a function with wrong case causes an error.
def greet():
return "Hello"
print(greet())
print(Greet())
π€ Output: Hello
π€ Output: NameError: name 'Greet' is not defined
π€ Example 3: Keywords are always lowercase
This shows that Python keywords like True, False, and None must be written exactly as shown.
is_active = True
print(is_active)
print(TRUE)
π€ Output: True
π€ Output: NameError: name 'TRUE' is not defined
π€ Example 4: String values are case-sensitive
This shows that Python treats the same word in different case as different strings.
status = "active"
if status == "Active":
print("Match found")
else:
print("No match")
π€ Output: No match
π€ Example 5: Class and method names are case-sensitive
This shows that class names and their methods must be called with exact case.
class Car:
def start(self):
return "Engine running"
my_car = Car()
print(my_car.start())
print(my_car.Start())
π€ Output: Engine running
π€ Output: AttributeError: 'Car' object has no attribute 'Start'
π Comparison Table: Case-Sensitivity Rules
| Element Type | Correct Case Example | Incorrect Case Example | Behavior |
|---|---|---|---|
| Variable | user_id |
User_Id |
Creates a new variable |
| Function | calculate() |
Calculate() |
Raises NameError |
| Keyword | True |
TRUE |
Raises NameError |
| String value | "active" |
"Active" |
Different string, no match |
| Method | .start() |
.Start() |
Raises AttributeError |