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

Cursusinhoud

Introduction to Python (copy)

Introduction to Python (copy)

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

book
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

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

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.

Taak

Swipe to start coding

Create and print variables that describe a grocery item — "Bread". Define variables for its name, price per loaf, and stock quantity, using clear and descriptive names.

Code Instructions

  • Create a variable item_name and assign it the string "Bread".
  • Create a variable item_price and assign it the float 4.52 (price per loaf).
  • Create a variable items_in_stock and assign it the integer 230 (quantity available).
  • Use these variables in print() statements to display the item’s details.

Output Requirements

Print the item information in the following format:

  • Item: <item_name>
  • Price per loaf: $ <item_price>
  • Quantity in stock: <items_in_stock>

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 3
toggle bottom row

book
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

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

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.

Taak

Swipe to start coding

Create and print variables that describe a grocery item — "Bread". Define variables for its name, price per loaf, and stock quantity, using clear and descriptive names.

Code Instructions

  • Create a variable item_name and assign it the string "Bread".
  • Create a variable item_price and assign it the float 4.52 (price per loaf).
  • Create a variable items_in_stock and assign it the integer 230 (quantity available).
  • Use these variables in print() statements to display the item’s details.

Output Requirements

Print the item information in the following format:

  • Item: <item_name>
  • Price per loaf: $ <item_price>
  • Quantity in stock: <items_in_stock>

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 3
Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?
some-alt