{"id":5344,"date":"2020-02-03T23:27:26","date_gmt":"2020-02-03T17:57:26","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=5344"},"modified":"2026-11-13T11:12:29","modified_gmt":"2026-11-13T05:42:29","slug":"the-walrus-operator-in-python-3-8","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/the-walrus-operator-in-python-3-8\/","title":{"rendered":"What Is the Walrus Operator in Python? A Complete Guide to Assignment Expressions"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Post Date:<\/strong> January 29, 2020<br><strong>Last Updated:<\/strong> November 10, 2026<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Python continues to introduce features that help developers write cleaner and more efficient code. One such feature is the <strong>Walrus Operator (<\/strong><code><strong>:=<\/strong><\/code><strong>)<\/strong>, introduced in Python 3.8.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also known as an <strong>Assignment Expression<\/strong>, the Walrus Operator allows developers to assign a value to a variable and use that value within the same expression.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is called the Walrus Operator because the <code>:=<\/code> symbol resembles the eyes and tusks of a walrus.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, we will explore what the Walrus Operator is, how it works, and how to use it with practical Python examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Is the Walrus Operator in Python?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Walrus Operator (<code>:=<\/code>) is an assignment expression that assigns a value to a variable while also returning that value as part of an expression.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before Python 3.8, developers generally had to assign a value separately before using it in a condition.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With the Walrus Operator, both operations can be combined into a single expression.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">variable := expression<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if (number := 27) % 2 == 0:\n    print(\"Given number is even\")\nelse:\n    print(\"Given number is odd\")<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Given number is odd<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>number := 27<\/code> assigns the value <code>27<\/code> to the variable <code>number<\/code>.<\/li>\n\n\n\n<li>The assigned value is immediately used in the modulus operation.<\/li>\n\n\n\n<li>If the remainder is zero, the number is even; otherwise, it is odd.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The variable can also be used later in the program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to Use the Walrus Operator in Python<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s explore a few practical examples to understand how assignment expressions work.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1: Check Whether a Number Is Odd or Even<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Consider the following example using the Walrus Operator:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if (number := 27) % 2 == 0:\n    print(\"Given number is even\")\nelse:\n    print(\"Given number is odd\")<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, the variable is assigned and checked within the same expression.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Given number is odd<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Compare the Traditional Assignment with the Walrus Operator<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Before Python 3.8, you would typically write:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">number = 27\n\nif number % 2 == 0:\n    print(\"Given number is even\")\nelse:\n    print(\"Given number is odd\")<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Using the Walrus Operator, the same logic can be written as:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if (number := 27) % 2 == 0:\n    print(\"Given number is even\")\nelse:\n    print(\"Given number is odd\")<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Both examples produce the same output.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The second approach combines assignment and condition checking. However, the Walrus Operator should be used when it improves readability rather than simply reducing the number of lines.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 3: Use the Walrus Operator with a While Loop<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Walrus Operator is also useful in loops when a value needs to be assigned and checked before processing.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">while (user_input := input(\"Enter a value (or type 'exit' to stop): \")) != \"exit\":\n    print(\"You entered:\", user_input)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this example:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>The user enters a value.<\/li>\n\n\n\n<li>The entered value is assigned to <code>user_input<\/code>.<\/li>\n\n\n\n<li>The condition checks whether the value is different from <code>exit<\/code>.<\/li>\n\n\n\n<li>If the condition is true, the program prints the entered value.<\/li>\n\n\n\n<li>The loop stops when the user enters <code>exit<\/code>.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">This avoids writing a separate assignment statement before the condition.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 4: Use the Walrus Operator with len()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Walrus Operator can also help avoid calculating the same value more than once.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">names = [\"John\", \"David\", \"Michael\", \"Robert\"]\n\nif (count := len(names)) > 3:\n    print(f\"The list contains {count} names.\")<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">The list contains 4 names.<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, <code>len(names)<\/code> is evaluated once, and the resulting value is assigned to <code>count<\/code>. The same value is then used in the condition and output.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of the Walrus Operator<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Walrus Operator provides several benefits:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Reduced Repetition:<\/strong> Combines assignment and condition checking when both operations belong together.<\/li>\n\n\n\n<li><strong>Improved Efficiency:<\/strong> Helps avoid repeating calculations or function calls when their results are needed more than once.<\/li>\n\n\n\n<li><strong>Cleaner Code:<\/strong> Can make certain loops and conditional expressions more concise.<\/li>\n\n\n\n<li><strong>Better Readability:<\/strong> Keeps related operations together when used appropriately.<\/li>\n\n\n\n<li><strong>Flexible Expressions:<\/strong> Allows assignment expressions in supported conditions, loops, and comprehensions.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Note<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before using the Walrus Operator, keep these points in mind:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>The Walrus Operator was introduced in Python 3.8.<\/li>\n\n\n\n<li>It uses the <code>:=<\/code> syntax, not the traditional <code>=<\/code> assignment operator.<\/li>\n\n\n\n<li>Parentheses are required in certain expression contexts, including the odd\/even example shown above.<\/li>\n\n\n\n<li>It should be used only when it makes the code easier to understand.<\/li>\n\n\n\n<li>Python 3.8 reached end-of-life on October 7, 2024. For new projects, use a currently supported Python release.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The Walrus Operator is a useful addition to Python that allows developers to assign values and use them within the same expression.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Introduced in Python 3.8, it can simplify certain conditional statements, loops, and other expressions by avoiding unnecessary repetition.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, shorter code is not always better code. Use assignment expressions where they improve clarity, maintainability, and readability.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding features like the Walrus Operator helps Python developers write cleaner and more expressive programs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. What is the Walrus Operator in Python?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Walrus Operator (<code>:=<\/code>) is an assignment expression that assigns a value to a variable and returns that value within the same expression.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Which Python version introduced the Walrus Operator?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Walrus Operator was introduced in Python 3.8.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. What is the difference between = and := in Python?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>=<\/code> operator performs a regular assignment, whereas <code>:=<\/code> assigns a value as part of an expression and also returns that value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4. Can the Walrus Operator be used in a while loop?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. It can assign a value and evaluate a condition in the same expression, making it useful for processing input or reading data until a stopping condition is reached.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">5. Does the Walrus Operator improve Python performance?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Not necessarily. Its primary benefit is reducing repeated calculations and making certain expressions more concise. It does not automatically make a program faster.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.python.org\/3\/whatsnew\/3.8.html\" target=\"_blank\" rel=\"noopener\">What&#8217;s New in Python 3.8 \u2013 Official Python Documentation<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/docs.python.org\/3\/reference\/expressions.html#assignment-expressions\" target=\"_blank\" rel=\"noopener\">Assignment Expressions \u2013 Python Language Reference<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/peps.python.org\/pep-0572\/\" target=\"_blank\" rel=\"noopener\">PEP 572 \u2013 Assignment Expressions<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/devguide.python.org\/versions\/\" target=\"_blank\" rel=\"noopener\">Status of Python Versions \u2013 Python Developer&#8217;s Guide<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/python-basics\/\">Python \u2013 Basics<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-pass-two-or-more-lists-in-for-loop-using-python\/\">HOW TO PASS TWO OR MORE LISTS IN FOR LOOP USING PYTHON<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Need Assistance?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Need help with Python development, application programming, or implementing efficient software solutions?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <a href=\"https:\/\/pheonixsolutions.com\/web-development\/\" data-type=\"link\" data-id=\"https:\/\/pheonixsolutions.com\/web-development\/\">Pheonix Solutions<\/a> technical team can assist with software development, application architecture, and technology implementation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For assistance, <a href=\"https:\/\/pheonixsolutions.com\/contact\/\" data-type=\"link\" data-id=\"https:\/\/pheonixsolutions.com\/contact\/\">contact us<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Post Date: January 29, 2020Last Updated: November 10, 2026 Introduction Python continues to introduce features that help developers write cleaner [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":13256,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[292,745],"class_list":["post-5344","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-architecture","tag-python","tag-walrus-operator","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1oc","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2020\/02\/Screenshot-2026-11-12-at-6.23.48-pm.png","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/5344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=5344"}],"version-history":[{"count":10,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/5344\/revisions"}],"predecessor-version":[{"id":13262,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/5344\/revisions\/13262"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media\/13256"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=5344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=5344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=5344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}