Skip to content

Commit

Permalink
Merge pull request #591 from realpython/python-variables-update
Browse files Browse the repository at this point in the history
Sample code for the article on variables
  • Loading branch information
brendaweles authored Oct 17, 2024
2 parents 754c56e + 7d14b80 commit f4d57c4
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 0 deletions.
3 changes: 3 additions & 0 deletions python-variables/README.md
Original file line number Diff line number Diff line change
@@ -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/).
18 changes: 18 additions & 0 deletions python-variables/colors.py
Original file line number Diff line number Diff line change
@@ -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),
# }
22 changes: 22 additions & 0 deletions python-variables/employees.py
Original file line number Diff line number Diff line change
@@ -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}")
9 changes: 9 additions & 0 deletions python-variables/greeting.py
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions python-variables/matrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
matrix = [
[9, 3, 8],
[4, 5, 2],
[6, 4, 3],
]

for i in matrix:
for j in i:
print(j)
24 changes: 24 additions & 0 deletions python-variables/scopes.py
Original file line number Diff line number Diff line change
@@ -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()
10 changes: 10 additions & 0 deletions python-variables/timeout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
_timeout = 30 # in seconds


def get_timeout():
return _timeout


def set_timeout(seconds):
global _timeout
_timeout = seconds
10 changes: 10 additions & 0 deletions python-variables/toggle.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f4d57c4

Please sign in to comment.