Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 24th, 2012, 11:13 AM
superversion970 superversion970 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 13 superversion970 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 18 sec
Reputation 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.

Reply With Quote
  #2  
Old December 24th, 2012, 11:22 AM
metulburr metulburr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 10 metulburr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 43 m 57 sec
Reputation 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

Reply With Quote
  #3  
Old December 24th, 2012, 11:47 AM
superversion970 superversion970 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 13 superversion970 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 18 sec
Reputation Power: 0
answer

Quote:
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.

Reply With Quote
  #4  
Old December 24th, 2012, 12:11 PM
metulburr metulburr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 10 metulburr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #5  
Old December 24th, 2012, 12:30 PM
superversion970 superversion970 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 13 superversion970 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #6  
Old December 24th, 2012, 12:39 PM
Nyktos Nyktos is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 74 Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level) 
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]
Comments on this post
superversion970 agrees: thank you.

Reply With Quote
  #7  
Old December 24th, 2012, 12:58 PM
superversion970 superversion970 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 13 superversion970 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 18 sec
Reputation Power: 0
it works!!

Quote:
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

Reply With Quote
  #8  
Old December 24th, 2012, 01:44 PM
superversion970 superversion970 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 13 superversion970 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 18 sec
Reputation Power: 0
I have another question

Quote:
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??

Reply With Quote
  #9  
Old December 24th, 2012, 01:56 PM
Nyktos Nyktos is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 74 Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level)Nyktos User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 2 h 22 m 37 sec
Reputation Power: 2
Quote:
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]

Reply With Quote
  #10  
Old December 24th, 2012, 02:04 PM
superversion970 superversion970 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 13 superversion970 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #11  
Old December 24th, 2012, 02:14 PM
metulburr metulburr is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 10 metulburr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 43 m 57 sec
Reputation 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

Reply With Quote
  #12  
Old December 24th, 2012, 02:18 PM
superversion970 superversion970 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 13 superversion970 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 18 sec
Reputation Power: 0
Thank you

Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Need help for a program

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap