Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions python-variables/README.md
Original file line number Diff line number Diff line change
@@ -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()
```
6 changes: 6 additions & 0 deletions python-variables/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
settings = {
"background": "black",
"foreground": "white",
"logo": "logo.png",
"font": "Helvetica",
}
11 changes: 6 additions & 5 deletions python-variables/employees.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
6 changes: 3 additions & 3 deletions python-variables/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
3 changes: 2 additions & 1 deletion python-variables/scopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ def inner_func():
inner_func()


outer_func()
if __name__ == "__main__":
outer_func()
Loading