Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7764125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:49:54+00:00 2026-06-01T14:49:54+00:00

why the next python line is not a syntax error? If it is really

  • 0

why the next python line is not a syntax error? If it is really not then how can I use it and in which situation it will be useful?

 a= range(10)[1:3]=[2,3]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:49:55+00:00Added an answer on June 1, 2026 at 2:49 pm

    Python supports multiple assignments on the same line:

    >>> a = b = c = 2
    >>> a
    2
    >>> b
    2
    >>> c
    2
    

    Since you are assigning a first to a list(a mutable object in python) and then assigning both part of that list(which is mutable) and a to the contents of a third item, python is ok with it. If you had replaced your middle value with something that wasn’t mutable, you would get an error.

    >>> a = 'foo' = 'bar'
      File "<input>", line 1
    SyntaxError: can't assign to literal
    
    >>> a = {}['foo'] = 'bar'
    >>> a
    'bar'
    

    Interestingly, a line like this still throws an error, because you are attempting to assign the values 3 and 4 to the literal values 1 and 2.

    >>> a = [1, 2] = [3, 4]
      File "<input>", line 1
    SyntaxError: can't assign to literal
    

    While this does not:

    >>> a = [1, 2][:] = [3, 4]
    >>> a
    [3, 4]
    

    The key in this case is that by using slice notation you are assigning to the range inside of the list, instead of reassigning to the literal values in the list.

    Additionally, the lines

    >>> a = [[], []] = [{}, {}]
    >>> a
    [{}, {}]
    

    and

    >>> a = [b, c] = 3, 4
    >>> b
    3
    >>> c
    4
    

    Are valid, the first one because the inner lists and dicts are both mutable and iterable, and the second one because you are assigning 3, and 4 to the variables b and c instead of literals(thanks Sven :D).

    Edit To answer the op’s final question.

    >>> a = 1
    >>> b = 2
    >>> a,b = b,a = a,b
    >>> a
    2
    >>> b
    1
    

    This just comes down to the final(right-most) assignment of b, a = a, b. Observe:

    >>> a, b = 'a', 'b'
    >>> a, b = b, a = a, b = b, a
    >>> a
    'b'
    >>> b
    'a'
    >>> a, b = b, a = b, a
    >>> a
    'a'
    >>> b
    'b'
    

    It’s confusing, but the way I interpret this is that a and b are not re-evaluated / assigned new values until the statement is finished, so the assignment that matters is the one farthest to the right. If the last statement is a, b = a, b or b, a = b, a, a and b’s values don’t change. If it is a, b = b, a or b, a = a, b, the values are switched.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

next() in python does not work. What is an alternative to reading next line
I'm using Python Cheetah for template generation and I can't get it to use
I'm trying to understand an estrange behavior in Python. I have the next python
I'm going to start of by noting that I have next to no python
What's the best Python idiom for this C construct? while ((x = next()) !=
Python 2.6.2 >>> call_iter = iter(lambda x: x + 1, 100) >>> call_iter.next() Traceback
so i've begun google's python class, and have not had that much difficulty with
I've been looking at questions like these but I'm not really finding what I'm
I am new to Python Regex and regex is not my strong suite. so
I'm attempting to use Paramiko (Python SSH library) to read a remote file, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.