Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Variable Naming Rules | Section
Python Basics for Data Analysis

bookVariable Naming Rules

Pyyhkäise näyttääksesi valikon

Fantastic progress! Now, let's dive into something foundational yet crucial — naming your variables. Like every item in your grocery store has a specific place and label, each variable in Python must be named thoughtfully. Good naming helps you and others understand what your code does at a glance.

Watch as Alex explains the variable naming rules and how these work in practice:

Rules for Naming Variables

Always Start with a Letter or an Underscore

Variable names must start with a letter or an underscore, like item_name or _price. Don't start a variable name with a number (e.g., 2item is invalid).

Use Only Letters, Numbers, and Underscores

Variable names should only contain letters, numbers, and underscores. For example, item_name1 is fine, but avoid using special characters like dashes (item-name is invalid).

Be Aware of Case Sensitivity

Python treats uppercase and lowercase letters as different. So, item and Item are two separate variables in Python.

Don't Use Python's Reserved Words

Avoid using Python's built-in keywords (like print, if, and type) as variable names, as these are already used for special purposes in the language.

Example of Proper Variable Naming

In the following example, all variable naming rules are followed, ensuring that the variables are named correctly and the code will run smoothly:

123456
# Correct variable names item_name = "Apple" _item_price = 0.99 item1_quantity = 10 storeName = "Green Valley Groceries" print(item_name, _item_price, item1_quantity, storeName)
copy

Example of Incorrect Naming

In the following example, variable naming rules are not followed, resulting in errors and issues when running the code:

123456
# Incorrect variable names 1item = "Banana" # Variables cannot start with a number. Instead try `item_one`, or `oneItem` item-name = "Orange" # Variables cannot contain a dash (-), use underscores (_) instead. For example `item_name` is a valid variable print = 5.0 # You cannot use reserved keywords as a variable # But you can use these words in combination with others to name a variable # For example, `print_quantity = 5.0` is valid.
copy
Note
Note

You can attempt to correct the variable names above to ensure the code runs without errors.

Properly named variables enhance code readability and maintainability. Following Python's naming conventions is crucial to avoid syntax errors and other potential issues.

question mark

Which of the following are valid Python variable names according to the rules discussed in this chapter?

Select all correct answers

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 6

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Osio 1. Luku 6
some-alt