Skip to content

Commit

Permalink
Add example code for assignment expressions (Ch 3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptmcg committed Sep 10, 2022
1 parent 3ec81a7 commit fbec274
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions 03_The_Python_Language/assignment_expressions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import re

# := in an if/elif statement

re_match = re.match(r'Name: (\S)', input_string)
if re_match:
print(re_match.groups(1))

# collapsed version using :=
if (re_match := re.match(r'Name: (\S)', input_string)):
print(re_match.groups(1))


# := in a while statement

current_value = get_next_value()
while current_value is not None:
if not filter_condition(current_value):
continue # BUG! Current_value is not advanced to next
# ... do some work with current_value ...
current_value = get_next_value()

# collapsed version using :=
while (current_value := get_next_value()) is not None:
if not filter_condition(current_value):
continue # no bug, current_value gets advanced in while statement
# ... do some work with current_value ...


# := in a list comprehension filter
def safe_int(s):
try:
return int(s)
except Exception:
return None


input_strings = ['1', '2', 'a', '11']

valid_int_strings = [safe_int(s) for s in input_strings
if safe_int(s) is not None]

# collapsed version using :=
valid_int_strings = [int_s for s in input_strings
if (int_s := safe_int(s)) is not None]

0 comments on commit fbec274

Please sign in to comment.