Fundamentals 5 min read

Master Python Variables: Definitions, Naming Rules, and Best Practices

This article explains what variables are, why they’re essential in programming, how to define them in Python, the components of a variable, naming conventions, common pitfalls, and demonstrates both camelCase and snake_case styles with code examples, plus a brief note on constants.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Python Variables: Definitions, Naming Rules, and Best Practices

Variables represent quantities that can change; they record the state of real-world objects so a computer can recognize them.

1. What is a variable?

A variable is a quantity that can vary. It records a state of the real world, enabling the computer to recognize objects similarly to how humans do.

Quantity: records real-world state for computer recognition.

Change: real-world states can change over time.

2. Why do we need variables?

Variables are needed to describe the ever‑changing states in both the real world and program execution, such as a game character’s level increasing from 0 to 30.

3. Defining variables in Python

In Python, variables are created by assigning a value to a name.

name = 'nick'
age = 19
gender = 'male'
height = 180
weight = 140

4. Components of a variable

A Python variable consists of three parts:

Variable name – used to reference the stored value.

Assignment operator – the equals sign (=).

Variable value – the data representing a state.

name  # error, no value assigned
age = 18
height = 185
print(age)   # 18
print(height)   # 185

5. Naming conventions

Good variable names should be descriptive, use only letters, numbers, and underscores, start with a letter or underscore, and avoid Python keywords.

and, as, assert, break, class, continue, def, del, elif, else, except,
exec, finally, for, from, global, if, import, in, is, lambda, not,
or, pass, print, raise, return, try, while, with, yield

6. Variable naming styles

Two common styles are camelCase and snake_case, with snake_case recommended.

# camelCase
AgeOfNick = 19
print(AgeOfNick)

# snake_case
age_of_nick = 19
print(age_of_nick)

7. Constants

Constants are values that should not change. Python has no dedicated syntax, so the convention is to use all‑uppercase names. They can still be reassigned, but it is discouraged.

AGE_OF_NICK = 19
print(AGE_OF_NICK)   # 19
AGE_OF_NICK = AGE_OF_NICK + 1
print(AGE_OF_NICK)   # 20
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythonnaming conventionsprogramming fundamentalsVariables
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.