From d716ae61941fea8d7201e291c97a3487ff15e9ae Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Wed, 2 Oct 2024 15:01:53 +0200 Subject: [PATCH] Sample code for the article on variables --- python-variables/README.md | 3 +++ python-variables/colors.py | 18 ++++++++++++++++++ python-variables/employees.py | 22 ++++++++++++++++++++++ python-variables/matrix.py | 9 +++++++++ python-variables/scopes.py | 24 ++++++++++++++++++++++++ python-variables/timeout.py | 10 ++++++++++ 6 files changed, 86 insertions(+) create mode 100644 python-variables/README.md create mode 100644 python-variables/colors.py create mode 100644 python-variables/employees.py create mode 100644 python-variables/matrix.py create mode 100644 python-variables/scopes.py create mode 100644 python-variables/timeout.py diff --git a/python-variables/README.md b/python-variables/README.md new file mode 100644 index 0000000000..5e15a3aad6 --- /dev/null +++ b/python-variables/README.md @@ -0,0 +1,3 @@ +# 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/). diff --git a/python-variables/colors.py b/python-variables/colors.py new file mode 100644 index 0000000000..547f1dddaf --- /dev/null +++ b/python-variables/colors.py @@ -0,0 +1,18 @@ +colors: dict[str, str] = { + "red": "#FF0000", + "green": "#00FF00", + "blue": "#0000FF", + "yellow": "#FFFF00", + "black": "#000000", + "white": "#FFFFFF", +} + + +# colors: dict[str, tuple[int, int, int]] = { +# "Red": (255, 0, 0), +# "Green": (0, 255, 0), +# "Blue": (0, 0, 255), +# "Yellow": (255, 255, 0), +# "Black": (0, 0, 0), +# "White": (255, 255, 255), +# } diff --git a/python-variables/employees.py b/python-variables/employees.py new file mode 100644 index 0000000000..fa77066161 --- /dev/null +++ b/python-variables/employees.py @@ -0,0 +1,22 @@ +class Employee: + count = 0 + + def __init__(self, name, position, salary): + self.name = name + self.position = position + self.salary = salary + type(self).count += 1 + + def display_profile(self): + print(f"Name: {self.name}") + print(f"Position: {self.position}") + print(f"Salary: ${self.salary}") + + +jane = Employee("Jane Doe", "Software Engineer", 90000) +jane.display_profile() + +john = Employee("John Doe", "Product Manager", 120000) +john.display_profile() + +print(f"Total employees: {Employee.count}") diff --git a/python-variables/matrix.py b/python-variables/matrix.py new file mode 100644 index 0000000000..4ead972bd9 --- /dev/null +++ b/python-variables/matrix.py @@ -0,0 +1,9 @@ +matrix = [ + [9, 3, 8], + [4, 5, 2], + [6, 4, 3], +] + +for i in matrix: + for j in i: + print(j) diff --git a/python-variables/scopes.py b/python-variables/scopes.py new file mode 100644 index 0000000000..b72fe20477 --- /dev/null +++ b/python-variables/scopes.py @@ -0,0 +1,24 @@ +def function(): + value = 42 + return value + + +# Global scope +global_variable = "global" + + +def outer_func(): + # Nonlocal scope + nonlocal_variable = "nonlocal" + + def inner_func(): + # Local scope + local_variable = "local" + print(f"Hi from the '{local_variable}' scope!") + + print(f"Hi from the '{global_variable}' scope!") + print(f"Hi from the '{nonlocal_variable}' scope!") + inner_func() + + +outer_func() diff --git a/python-variables/timeout.py b/python-variables/timeout.py new file mode 100644 index 0000000000..4779f865b9 --- /dev/null +++ b/python-variables/timeout.py @@ -0,0 +1,10 @@ +_timeout = 30 # in seconds + + +def get_timeout(): + return _timeout + + +def set_timeout(seconds): + global _timeout + _timeout = seconds