Python Variables: Basics, Naming Rules, and Avoiding Keywords

In programming, a “variable” is like a “named container” used to temporarily store data such as numbers or strings. In Python, using variables is very simple.

This article explains the basic usage of variables in Python and the naming rules to avoid errors.


目次

Basic Usage of Variables (Assignment)

To use a variable, you store data in it using the assignment operator (=). This is called “assignment”.

# Assign the string "Tanaka" to the variable 'user_name'
user_name = "Tanaka"

# Assign the number 35 to the variable 'user_age'
user_age = 35

# Output the values stored in the variables
print(user_name)
print(user_age)

# You can overwrite the value of a variable
user_age = 36
print(f"One year later: {user_age}")

Output:

Tanaka
35
One year later: 36

Naming Rules

Developers can name variables freely, but there are rules (naming conventions) that must be followed in Python.

1. Valid Names (Usable Characters)

You can use a combination of the following characters for variable names:

  • Lowercase letters (a-z)
  • Uppercase letters (A-Z)
  • Numbers (0-9)
  • Underscore (_)

Also, Python is case-sensitive. item and Item are treated as different variables.

# Valid Examples
item_price = 1500
_internal_flag = True
user_id_001 = "A-001"

2. Invalid Names (Syntax Error)

If you try to use a name that violates the following rules, a SyntaxError will occur, and the program will not run.

A. Cannot start with a number You cannot use a number at the beginning of a variable name.

# SyntaxError: invalid syntax
# 1st_place = "Gold" 

B. Cannot use Reserved Words (Keywords) Words that Python uses for its grammar (like if, for, def, while, True, False) cannot be used as variable names. These are called “reserved words” or “keywords”.

# SyntaxError: invalid syntax
# if = 10
# for = "loop"

C. Cannot use Symbols (except Underscore) You cannot use hyphens (-), spaces, @, $, etc.

# SyntaxError
# item-code = "X100" # Hyphen is interpreted as subtraction
# item code = "X100" # Space is not allowed

Not Recommended Names (Conflict with Built-ins)

There are names that do not cause a syntax error but should be avoided. These are the names of Python’s standard “built-in functions” and “built-in types”.

Examples: print, list, str, int, sum, max, etc.

If you use these as variable names, the original function is overwritten by your variable, causing unexpected errors.

# Not Recommended Example
# Create a variable named 'list'
list = [10, 20, 30]

print(f"List content: {list}")

# The original list() function (type conversion) stops working
# numbers = list("123") 
# TypeError: 'list' object is not callable

In the example above, creating a variable named list makes it impossible to call the list() function later.

How to Avoid Conflicts

If you really want to use a built-in function name as part of your variable, use the following methods:

  1. Add an underscore (_) at the end This is a common convention in the Python community.Pythonlist_ = [10, 20, 30] # Does not conflict with list()
  2. Give a more specific name (Recommended) This is the best method. item_list or user_list is much easier to read than list_.Python# Specific and clear name (Recommended) item_list = [10, 20, 30] total_value = sum(item_list) # The original sum() works fine

Summary

Variables are a basic feature for handling data in Python.

  • Use = to assign data.
  • Follow naming rules (do not start with a number, do not use keywords).
  • Avoid using built-in function names like list or str.
  • Using specific names like user_list or total_value makes your code easier to read and maintain.
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次