8000 Sample code for the article on variables · realpython/materials@d716ae6 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit d716ae6

Browse files
committed
Sample code for the article on variables
1 parent a6c7824 commit d716ae6

File tree

6 files changed

+86
-0
lines changed

6 files changed

+86
-0
lines changed

python-variables/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Variables in Python: Usage and Best Practices
2+
3+
This folder provides the code examples for the Real Python tutorial [Variables in Python: Usage and Best Practices](https://realpython.com/python-variables/).

python-variables/colors.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
colors: dict[str, str] = {
2+
"red": "#FF0000",
3+
"green": "#00FF00",
4+
"blue": "#0000FF",
5+
"yellow": "#FFFF00",
6+
"black": "#000000",
7+
"white": "#FFFFFF",
8+
}
9+
10+
11+
# colors: dict[str, tuple[int, int, int]] = {
12+
# "Red": (255, 0, 0),
13+
# "Green": (0, 255, 0),
14+
# "Blue": (0, 0, 255),
15+
# "Yellow": (255, 255, 0),
16+
# "Black": (0, 0, 0),
17+
# "White": (255, 255, 255),
18+
# }

python-variables/employees.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Employee:
2+
count = 0
3+
4+
def __init__(self, name, position, salary):
5+
self.name = name
6+
self.position = position
7+
self.salary = salary
8+
type(self).count += 1
9+
10+
def display_profile(self):
11+
print(f"Name: {self.name}")
12+
print(f"Position: {self.position}")
13+
print(f"Salary: ${self.salary}")
14+
15+
16+
jane = Employee("Jane Doe", "Software Engineer", 90000)
17+
jane.display_profile()
18+
19+
john = Employee("John Doe", "Product Manager", 120000)
20+
john.display_profile()
21+
22+
print(f"Total employees: {Employee.count}")

python-variables/matrix.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
matrix = [
2+
[9, 3, 8],
3+
[4, 5, 2],
4+
[6, 4, 3],
5+
]
6+
7+
for i in matrix:
8+
for j in i:
9+
print(j)

python-variables/scopes.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def function():
2+
value = 42
3+
return value
4+
5+
6+
# Global scope
7+
global_variable = "global"
8+
9+
10+
def outer_func():
11+
# Nonlocal scope
12+
nonlocal_variable = "nonlocal"
13+
14+
def inner_func():
15+
# Local scope
16+
local_variable = "local"
17+
print(f"Hi from the '{local_variable}' scope!")
18+
19+
print(f"Hi from the '{global_variable}' scope!")
20+
print(f"Hi from the '{nonlocal_variable}' scope!")
21+
inner_func()
22+
23+
24+
outer_func()

python-variables/timeout.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
_timeout = 30 # in seconds
2+
3+
4+
def get_timeout():
5+
return _timeout
6+
7+
8+
def set_timeout(seconds):
9+
global _timeout
10+
_timeout = seconds

0 commit comments

Comments
 (0)
0