GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing python-course.eu — 5. Assignment Expressions | Python Tutorial | python-course.eu 5. Assignment ExpressionsBy Bernd Klein. Last modified: 30 Nov 2023. On this page ➤ IntroductionThis section in our Python tutorial explores the "assignment expression operator" affectionately known to many as "the walrus operator. So, you can call it either the "walrus operator" or the "assignment expression operator," and people in the Python programming community will generally understand what you mean. Despite being introduced in Python version 3.8, we can still view it as a relatively recent addition to the language. The assignment expressions have been discussed in PEP 572 and this is what was written about the naming:
The purpose of this feature is not a new way to assign objects to variables, but it gives programmers a convenient way to assign variables in the middle of expressions. You might still be curious about the origin of the term "walrus operator." It's affectionately named this way because, with a touch of imagination, the operator's characters resemble the eyes and tusks of a walrus. We will introduce the assignment expression by comparing it first with a simple assignment statement. A simple assignment statement can also be replaced by an assignment expression, even though it looks clumsy and is definitely not the intended use case of it: x = 5 # can be written as: (x := 5) # valid, but not recomended! # the brackets are crucial OUTPUT:5 First you may wonder about the brackets, when you look at the assignment expression. It is not meant as a replacement for the simple "assignment statement". Its primary role is to be utilized within expressions The following code shows a proper usecase: x = 4.56 z = (square := x**2) - (6.6 / square) print(z) OUTPUT:20.476194644506 Though you may argue that the following code might be even clearer: x = 4.56 square = x**2 z = square - (6.6 / square) print(z) OUTPUT:20.476194644506 Here's another Python code example that demonstrates a similar scenario. First with the walrus operator: # With the walrus operator n = int(input("Please give a number: ")) if (square := n ** 2) > 3: print("The number is greater than 3.") print(f"The square of {n} is {square}.") OUTPUT:The number is greater than 3. The square of 42 is 1764. Now without the walrus operator: n = int(input("Please give a number: ")) square= n ** 2 if square > 3: print("The number is greater than 3.") print(f"The square of {n} is {square}.") OUTPUT:The number is greater than 3. The square of 42 is 1764. Live Python training See our Python training courses Beneficial applicationsNow, let's explore some examples where the usage of the assignment expression is really beneficial compared to code not using it. def f(x): return x + 4 numbers = [3, 7, 2, 9, 12] odd_numbers = [result for x in numbers if (result := f(x)) % 2] odd_numbers OUTPUT:[7, 11, 13] You can observe that if we choose to avoid using the assignment expression in the list comprehension, we would need to call the function twice. So using the assgment expression makes the code in this context more efficient. def f(x): return x + 4 numbers = [3, 7, 2, 9, 12] odd_numbers = [f(x) for x in numbers if f(x) % 2] odd_numbers OUTPUT:[7, 11, 13] Use Case: Regular ExpressionsThere is also a great advantage when we use regular expressions. Like in our previous examples we present again two similiar code snippets, the first one without and the second one with the walrus operator: import re txt = """The Python training course started at 2022-02-4 the other one at 2022-01-24 only one date per line, if at all the dates may also be in this format 2020/10/15 or 20-10-04""" for line in txt.split('\n'): date = re.search(r'(\d{2,4})[-/](\d{2})[-/](\d{2})', line) if date: year, month, day = date.groups() print(f"Found date: {year}-{month}-{day}") OUTPUT:Found date: 2022-01-24 Found date: 2020-10-15 Found date: 20-10-04 Now, the code using assignment expressions: import re txt = """The Python training course started at 2022-02-4 the other one at 2022-01-24 only one date per line, if at all the dates may also be in this format 2020/10/15 or 20-10-04""" # Split the text into lines and search for dates in each line for line in txt.split('\n'): # Using the assignment expression to find and store the date match if (date := re.search(r'(\d{2,4})[-/](\d{2})[-/](\d{2})', line)): year, month, day = date.groups() print(f"Found date: {year}-{month}-{day}") OUTPUT:Found date: 2022-01-24 Found date: 2020-10-15 Found date: 20-10-04 Assignment Expression in File ReadingFixed-width FilesFiles in which each line has the same length are often referred to as "fixed-width" or "fixed-length" files. In these files, each line (or record) is structured so that it contains a predefined number of characters, and the fields within the lines have fixed positions. The files can be read by using the with open('training_course_info.txt') as fh: while ((data := fh.read(53)) != ''): print(data.rstrip()) OUTPUT:Python Training Course for Beginners Berlin 08 Python Intermediate Course Hamburg 06 Python Advanced Training Course Frankfurt 08 Now the same in traditional coding style: with open('training_course_info.txt') as fh: data = fh.read(52) while data: print(data.rstrip()) data = fh.read(52) OUTPUT:Python Training Course for Beginners Berlin 08 Python Intermediate Course Hamburg 0 6 Python Advanced Training Course Frankfurt 08 The benefits for the most recent code snippet are
However, a significant drawback of this approach is that it requires the explicit assignment and reassignment of the Using
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |