Python naming rules and Conventions. The best practices for naming variables and objects in Python

Publicado em: 22 Maio 2026
no canal de: LearnTechWithShahid
27
1

Naming in Python is governed by a combination of strict language rules (syntax) and community-agreed conventions (best practices). Following these ensures your code is readable, maintainable, and error-free.

1. Strict Syntax Rules (What is Acceptable)
If you violate these, Python will raise a SyntaxError and your code will not run.

Allowed Characters: Names can contain letters (a-z, A-Z), digits (0-9), and underscores (_).

Must Start With: A name must begin with a letter or an underscore. It cannot start with a number.

Case Sensitivity: Python is case-sensitive. my_variable, My_Variable, and MY_VARIABLE are three completely different identifiers.

Reserved Keywords: You cannot use Python's built-in keywords as names, as they have predefined roles in the language.

Python Keywords (Do not use these as names)
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.

While the syntax rules tell you what you can do, the PEP 8 style guide defines what you should do to make your code look professional and readable.

Variables, Functions, and Methods: Use snake_case (all lowercase, words separated by underscores).

Example: user_id, calculate_total_price.

Classes: Use PascalCase (capitalize the first letter of each word, no underscores).

Example: UserProfile, DataParser.

Constants: Use UPPER_SNAKE_CASE (all uppercase, words separated by underscores).

Example: MAX_RETRIES, PI.

Private/Internal Variables: Use a single underscore as a prefix.

Example: _internal_counter.

Descriptive Naming: Choose names that describe what the data represents. Avoid single-letter variables unless they are standard (like i or j for loop counters).
Bad: d = 86400

Good: seconds_in_day = 86400

Avoid Built-in Names: While you can name a variable list, str, or int, it is dangerous because you "shadow" the built-in functions, making them inaccessible later in your code.

Example: If you name a variable list, you can no longer call the actual list() constructor.

Readability: Python is designed to be readable. If you have to choose between a short, cryptic name (like x1) and a descriptive one (user_age), always choose the descriptive one.


Nesta página do site você pode assistir ao vídeo on-line Python naming rules and Conventions. The best practices for naming variables and objects in Python duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário LearnTechWithShahid 22 Maio 2026, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 27 vezes e gostou 1 espectadores. Boa visualização!