From a8bfdd25b5e3c95ecd8dab6286fcb9fe65065eda Mon Sep 17 00:00:00 2001 From: Real Python Bot <42617967+realpython-bot@users.noreply.github.com> Date: Tue, 1 Dec 2026 13:17:40 +0000 Subject: [PATCH] Sync python-variables with the updated tutorial Add config.py, switch matrix.py to the enumerate() loop, guard the module-level demo calls in scopes.py and employees.py so imports match the tutorial's REPL sessions, and add Setup/Usage to the README. Co-Authored-By: Claude Opus 5.5 --- python-variables/README.md | 19 +++++++++++++++++++ python-variables/config.py | 6 ++++++ python-variables/employees.py | 11 ++++++----- python-variables/matrix.py | 6 +++--- python-variables/scopes.py | 3 ++- 5 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 python-variables/config.py diff --git a/python-variables/README.md b/python-variables/README.md index 5e15a3aad6..51fc353f8b 100644 --- a/python-variables/README.md +++ b/python-variables/README.md @@ -1,3 +1,22 @@ # Variables in Python: Usage and Best Practices This folder provides the code examples for the Real Python tutorial [Variables in Python: Usage and Best Practices](https://realpython.com/python-variables/). + +## Setup + +The examples use only the standard library and were tested with Python 3.15. You don't need to install any third-party packages. + +## Usage + +Run any script directly, for example: + +```sh +$ python matrix.py +``` + +To follow the REPL sessions in the tutorial, start Python in this folder and import from the modules, for example: + +```pycon +>>> from scopes import outer_func +>>> outer_func() +``` diff --git a/python-variables/config.py b/python-variables/config.py new file mode 100644 index 0000000000..23eb743692 --- /dev/null +++ b/python-variables/config.py @@ -0,0 +1,6 @@ +settings = { + "background": "black", + "foreground": "white", + "logo": "logo.png", + "font": "Helvetica", +} diff --git a/python-variables/employees.py b/python-variables/employees.py index 2210b30ac1..8b415e4322 100644 --- a/python-variables/employees.py +++ b/python-variables/employees.py @@ -13,10 +13,11 @@ def display_profile(self): print(f"Salary: ${self.salary}") -jane = Employee("Jane Doe", "Software Engineer", 90000) -jane.display_profile() +if __name__ == "__main__": + jane = Employee("Jane Doe", "Software Engineer", 90000) + jane.display_profile() -john = Employee("John Doe", "Product Manager", 120000) -john.display_profile() + john = Employee("John Doe", "Product Manager", 120000) + john.display_profile() -print(f"Total employees: {Employee.count}") + print(f"Total employees: {Employee.count}") diff --git a/python-variables/matrix.py b/python-variables/matrix.py index 4ead972bd9..5e6e9e5b8c 100644 --- a/python-variables/matrix.py +++ b/python-variables/matrix.py @@ -4,6 +4,6 @@ [6, 4, 3], ] -for i in matrix: - for j in i: - print(j) +for i, row in enumerate(matrix): + for j, value in enumerate(row): + print(f"matrix[{i}][{j}] = {value}") diff --git a/python-variables/scopes.py b/python-variables/scopes.py index 309df9795b..f69bdfeb52 100644 --- a/python-variables/scopes.py +++ b/python-variables/scopes.py @@ -21,4 +21,5 @@ def inner_func(): inner_func() -outer_func() +if __name__ == "__main__": + outer_func()