Discuss Need help for a program in the Python Programming forum on Dev Shed. Need help for a program Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
Posts: 10
Time spent in forums: 5 h 43 m 57 sec
Reputation 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
Posts: 13
Time spent in forums: 2 h 45 m 18 sec
Reputation Power: 0
Thank you
Quote:
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?
Posts: 74
Time spent in forums: 1 Day 2 h 22 m 37 sec
Reputation Power: 2
Quote:
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]
Posts: 13
Time spent in forums: 2 h 45 m 18 sec
Reputation Power: 0
thank you
Quote:
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?