-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #591 from realpython/python-variables-update
Sample code for the article on variables
- Loading branch information
Showing
8 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
# } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |