Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Variable Naming Rules | Variables and Types
Introduction to Python Video Course
course content

Course Content

Introduction to Python Video Course

Introduction to Python Video Course

1. Getting Started
2. Variables and Types
3. Conditional Statements
4. Other Data Types
5. Loops
6. Functions

Variable Naming Rules

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

1. 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).

2. 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).

3. Be Aware of Case Sensitivity

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

4. 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

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.

Task

Now, it's your turn to apply these rules. Create and print variables for a grocery item — "Bread". Make sure your variable names reflect their purpose clearly.

Replace ___ placeholders with descriptive variable names to accurately represent details for the grocery item "Bread", where the price per loaf is $4.52, and there are 230 units in stock.

After naming your variables, add them to the corresponding print statements.

Task

Now, it's your turn to apply these rules. Create and print variables for a grocery item — "Bread". Make sure your variable names reflect their purpose clearly.

Replace ___ placeholders with descriptive variable names to accurately represent details for the grocery item "Bread", where the price per loaf is $4.52, and there are 230 units in stock.

After naming your variables, add them to the corresponding print statements.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 2. Chapter 3
toggle bottom row

Variable Naming Rules

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

1. 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).

2. 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).

3. Be Aware of Case Sensitivity

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

4. 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

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.

Task

Now, it's your turn to apply these rules. Create and print variables for a grocery item — "Bread". Make sure your variable names reflect their purpose clearly.

Replace ___ placeholders with descriptive variable names to accurately represent details for the grocery item "Bread", where the price per loaf is $4.52, and there are 230 units in stock.

After naming your variables, add them to the corresponding print statements.

Task

Now, it's your turn to apply these rules. Create and print variables for a grocery item — "Bread". Make sure your variable names reflect their purpose clearly.

Replace ___ placeholders with descriptive variable names to accurately represent details for the grocery item "Bread", where the price per loaf is $4.52, and there are 230 units in stock.

After naming your variables, add them to the corresponding print statements.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

Section 2. Chapter 3
toggle bottom row

Variable Naming Rules

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

1. 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).

2. 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).

3. Be Aware of Case Sensitivity

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

4. 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

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.

Task

Now, it's your turn to apply these rules. Create and print variables for a grocery item — "Bread". Make sure your variable names reflect their purpose clearly.

Replace ___ placeholders with descriptive variable names to accurately represent details for the grocery item "Bread", where the price per loaf is $4.52, and there are 230 units in stock.

After naming your variables, add them to the corresponding print statements.

Task

Now, it's your turn to apply these rules. Create and print variables for a grocery item — "Bread". Make sure your variable names reflect their purpose clearly.

Replace ___ placeholders with descriptive variable names to accurately represent details for the grocery item "Bread", where the price per loaf is $4.52, and there are 230 units in stock.

After naming your variables, add them to the corresponding print statements.

Switch to desktop for real-world practiceContinue from where you are using one of the options below

Everything was clear?

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

1. 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).

2. 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).

3. Be Aware of Case Sensitivity

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

4. 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

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.

Task

Now, it's your turn to apply these rules. Create and print variables for a grocery item — "Bread". Make sure your variable names reflect their purpose clearly.

Replace ___ placeholders with descriptive variable names to accurately represent details for the grocery item "Bread", where the price per loaf is $4.52, and there are 230 units in stock.

After naming your variables, add them to the corresponding print statements.

Switch to desktop for real-world practiceContinue from where you are using one of the options below
Section 2. Chapter 3
Switch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt