From d679c807ed570fa3b89a20185eb1c233128c1979 Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Wed, 2 Oct 2024 15:07:32 +0200 Subject: [PATCH 1/2] 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 From df1e22dd262d6b6337754bbe7423f377693bf23e Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Mon, 7 Oct 2024 18:10:21 +0200 Subject: [PATCH 2/2] TR updates, first round --- python-variables/greeting.py | 9 +++++++++ python-variables/scopes.py | 4 ++-- python-variables/toggle.py | 10 ++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 python-variables/greeting.py create mode 100644 python-variables/toggle.py diff --git a/python-variables/greeting.py b/python-variables/greeting.py new file mode 100644 index 0000000000..45d87235d2 --- /dev/null +++ b/python-variables/greeting.py @@ -0,0 +1,9 @@ +def greet(name, verbose=False): + if verbose: + print(f"Hello, {name}! It's great to see you!") + else: + print(f"Hello, {name}!") + + +greet("Pythonista") +greet("Pythonista", verbose=True) diff --git a/python-variables/scopes.py b/python-variables/scopes.py index b72fe20477..f770538425 100644 --- a/python-variables/scopes.py +++ b/python-variables/scopes.py @@ -15,9 +15,9 @@ 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!") - print(f"Hi from the '{global_variable}' scope!") - print(f"Hi from the '{nonlocal_variable}' scope!") inner_func() diff --git a/python-variables/toggle.py b/python-variables/toggle.py new file mode 100644 index 0000000000..129a199b55 --- /dev/null +++ b/python-variables/toggle.py @@ -0,0 +1,10 @@ +toggle = True + +for _ in range(4): + if toggle: + print(f"✅ toggle is {toggle}") + print("Do something...") + else: + print(f"❌ toggle is {toggle}") + print("Do something else...") + toggle = not toggle