Professional IronPython


Working with Tuples, Lists, and Arrays



Download 20,35 Mb.
Pdf ko'rish
bet77/453
Sana01.07.2022
Hajmi20,35 Mb.
#723045
1   ...   73   74   75   76   77   78   79   80   ...   453
Working with Tuples, Lists, and Arrays
 

 
57
The main difference between a 
tuple
and a 
list
is that you can change the content of a 
list
, but you 
can’t change the content of a 
tuple
. If you create a 
tuple
and try to change one of its elements, you 
get an error message. Listing 4-2 shows what happens when you try to change a 
tuple
and a 
list
.
lISTINg 4-2: 
 Changing tuples and lists
# Create the tuple 
MyTuple = ‘Red’, ‘Green’, ‘Blue’, ‘Yellow’
# Create the list 
MyList = [‘Red’, ‘Green’, ‘Blue’, ‘Yellow’]
# Attempt to change the tuple, which will result in an error.
try:
MyTuple[1] = ‘Orange’
except TypeError:
print ‘Couldn\‘t change the tuple.\n’
# Attempt to change the list.
try:
MyList[1] = ‘Orange’
except TypeError:
print ‘Couldn\‘t change the list.\n’
# Verify that the change worked.
print ‘Displaying the list content.’
for TheString in MyList:
print TheString
# Pause after the debug session.
raw_input(‘Press any key to continue...’)
The example attempts to change both a 
tuple
and a 
list
. However, when the example tries to 
change the 
tuple
, it generates an exception, as shown in Figure 4-2. Notice how the code uses a 
try...except
structure to catch the exception and display an error message onscreen. The 
except
clause can stand alone, but it’s better to provide a particular kind of exception, which is 
TypeError
in this case. The code does successfully modify the 
list
and displays the results in Figure 4-2.
FIguRE 4-2: 
Lists are mutable; tuples aren’t.
548592c04.indd 57
2/24/10 12:47:34 PM
www.finebook.ir



Download 20,35 Mb.

Do'stlaringiz bilan baham:
1   ...   73   74   75   76   77   78   79   80   ...   453




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish