0
0
Pythonprogramming~5 mins

Naming rules and conventions in Python

Choose your learning style9 modes available
Introduction

Good names help you and others understand your code easily. Following rules avoids errors and confusion.

When creating variables to store information like age or name.
When defining functions to perform tasks like calculate or print.
When naming classes to represent things like Car or Person.
When writing code that others will read or maintain.
When you want your code to be clear and easy to fix later.
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
Variable names use lowercase letters and underscores to separate words.
Python
age = 25
user_name = "Alice"
Function names follow the same style as variables: lowercase with underscores.
Python
def calculate_sum():
    pass
Class names use CapitalizedWords style (called PascalCase).
Python
class Car:
    pass
Names starting with underscore mean 'private' or for internal use.
Python
_hidden_value = 10
Sample 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}")
OutputSuccess
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.