Зміст курсу
Introduction to Python Video Course
Introduction to Python Video Course
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:
# 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)
Example of Incorrect Naming
In the following example, variable naming rules are not followed, resulting in errors and issues when running the code:
# 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.
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.
Завдання
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.
Завдання
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.
Все було зрозуміло?
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:
# 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)
Example of Incorrect Naming
In the following example, variable naming rules are not followed, resulting in errors and issues when running the code:
# 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.
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.
Завдання
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.
Завдання
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.
Все було зрозуміло?
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:
# 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)
Example of Incorrect Naming
In the following example, variable naming rules are not followed, resulting in errors and issues when running the code:
# 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.
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.
Завдання
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.
Завдання
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.
Все було зрозуміло?
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:
# 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)
Example of Incorrect Naming
In the following example, variable naming rules are not followed, resulting in errors and issues when running the code:
# 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.
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.
Завдання
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.