[파이썬] 변수 명명 시 조건문 고려

When naming variables in Python, it is important to consider certain conditional statements. Choosing meaningful and descriptive variable names can make your code more readable and maintainable. Here are some guidelines to keep in mind when naming variables with conditional statements in Python.

1. Use descriptive names

Choose variable names that accurately describe their purpose and usage. This will make your code more understandable for yourself and others who read it. Avoid generic names like temp or val which can be ambiguous.

# Bad example - unclear variable name
val = 5
if val > 10:
    print("Value is greater than 10")

# Good example - descriptive variable name
age = 21
if age > 18:
    print("You are an adult")

2. Follow PEP 8 naming conventions

Follow the Python Enhancement Proposal (PEP) 8 guidelines for variable naming. Use lowercase letters for variable names and separate words with underscores (snake_case). This convention improves code readability and consistency.

# Bad example - variable name not following PEP 8
dayOfMonth = 10

# Good example - variable name following PEP 8
day_of_month = 10

3. Avoid reserved keywords

Avoid using reserved keywords as variable names. These are words that have special meanings in the Python language and cannot be used as variable names.

# Bad example - using a reserved keyword as a variable name
class = "Math"

# Good example - using a meaningful variable name
subject = "Math"

4. Be consistent with your naming style

Maintain consistency in the naming style throughout your codebase. If you use a specific naming convention for variables with conditional statements, stick to it. Consistency helps in understanding and navigating your code.

# Inconsistent example - using different naming styles
isTrue = True
if isTrue:
    print("The condition is true")

is_false = False
if not is_false:
    print("The condition is false")

# Consistent example - using consistent naming style
is_true = True
if is_true:
    print("The condition is true")

is_false = False
if not is_false:
    print("The condition is false")

By following these guidelines, you can write more readable and maintainable code by considering conditional statements when naming variables in Python.