#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    13
    Rep Power
    0

    Need help for a program


    Here is the program I am trying to run in python 3.3.



    a=(3,4,7)
    print (a)
    print (a[2])
    ...a[0:2]=[1,12]
    print (a)

    first 3 lines are working. but there may be some problem in last 2 line. It is showing error telling invalid syntax.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0
    might it be the
    Code:
    ...
    within the traceback

    also tuples are immutable thus they cannot be reassigned by index as such as line 4
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    13
    Rep Power
    0

    answer


    Originally Posted by metulburr
    might it be the
    Code:
    ...
    within the traceback

    also tuples are immutable thus they cannot be reassigned by index as such as line 4
    I am newbie to python. please give me a specific correction for the code that will run.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0
    well the syntax is due to the ..., which i am assuming you copied and pasted from an example, which in that case it just meant something was excluded for simplicity.

    Anyways, the create a new var, concatanate the sliced tuple with a single tuple containing the list

    Code:
    a=(3,4,7) 
    print(a) 
    print(a[2]) 
    b = a[0:2] + ([1,2],) 
    print(b)
    the result will be a second tuple with the desired spliced original tuple and the new list
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    13
    Rep Power
    0

    Thank you


    Originally Posted by metulburr
    well the syntax is due to the ..., which i am assuming you copied and pasted from an example, which in that case it just meant something was excluded for simplicity.

    Anyways, the create a new var, concatanate the sliced tuple with a single tuple containing the list

    Code:
    a=(3,4,7) 
    print(a) 
    print(a[2]) 
    b = a[0:2] + ([1,2],) 
    print(b)
    the result will be a second tuple with the desired spliced original tuple and the new list


    thank you for your reply . here assign valued for 'a' is (3,4,7) but i want to change the value from (3,4,7) to (5,67,7) by writing code. is it possible?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    4
    Originally Posted by superversion970
    thank you for your reply . here assign valued for 'a' is (3,4,7) but i want to change the value from (3,4,7) to (5,67,7) by writing code. is it possible?
    Well, you can reassign the name a by just doing a = (5, 67, 3).

    You can't change it in-place because tuples are immutable. If you want to do that, you'll need to make it a list (using square brackets) instead.
    Code:
    >>> a = [3, 4, 7]
    >>> a[0] = 5
    >>> a[1] = 67
    >>> a
    [5, 67, 7]

    Comments on this post

    • superversion970 agrees : thank you.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    13
    Rep Power
    0

    it works!!


    Originally Posted by Nyktos
    Well, you can reassign the name a by just doing a = (5, 67, 3).

    You can't change it in-place because tuples are immutable. If you want to do that, you'll need to make it a list (using square brackets) instead.
    Code:
    >>> a = [3, 4, 7]
    >>> a[0] = 5
    >>> a[1] = 67
    >>> a
    [5, 67, 7]



    thank you. it really works
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    13
    Rep Power
    0

    I have another question


    Originally Posted by Nyktos
    Well, you can reassign the name a by just doing a = (5, 67, 3).

    You can't change it in-place because tuples are immutable. If you want to do that, you'll need to make it a list (using square brackets) instead.
    Code:
    >>> a = [3, 4, 7]
    >>> a[0] = 5
    >>> a[1] = 67
    >>> a
    [5, 67, 7]


    i want to change the value of 'a' from (3,4,7) to (3,4). means that I want to remove 7 from 'a' and make it (3,4). is it possible by writing program??
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    4
    Originally Posted by superversion970
    i want to change the value of 'a' from (3,4,7) to (3,4). means that I want to remove 7 from 'a' and make it (3,4). is it possible by writing program??
    A tuple cannot be changed in any way after it is created; this is what is meant by "immutable". Once again, though, it is trivial with lists:
    Code:
    >>> a = [3, 4, 7]
    >>> del a[2]
    >>> a
    [3, 4]
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    13
    Rep Power
    0

    thank you


    Originally Posted by Nyktos
    A tuple cannot be changed in any way after it is created; this is what is meant by "immutable". Once again, though, it is trivial with lists:
    Code:
    >>> a = [3, 4, 7]
    >>> del a[2]
    >>> a
    [3, 4]
    again thank you for your answer. it also worked for me . I have another question . I want to change the value of 'a' from [3,4,7] to [3,4,7,5,9]. that means I want to add some value in 'a'. how is it possible by writing program?
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    10
    Rep Power
    0
    lists have an append() method that you can use, which will by default place the value at the end of the list.


    Code:
    a = []
    
    a.append(1)
    print(a)
    a.append(2)
    print(a)

    Comments on this post

    • superversion970 agrees : thank you :)
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    13
    Rep Power
    0

    Thank you


    Originally Posted by metulburr
    lists have an append() method that you can use, which will by default place the value at the end of the list.


    Code:
    a = []
    
    a.append(1)
    print(a)
    a.append(2)
    print(a)


    thank u. it worked for me.

IMN logo majestic logo threadwatch logo seochat tools logo