Professional IronPython



Download 20,35 Mb.
Pdf ko'rish
bet72/453
Sana01.07.2022
Hajmi20,35 Mb.
#723045
1   ...   68   69   70   71   72   73   74   75   ...   453
Using for . . . in
The 
for...in
loop is the best way to process lists of things in most cases. You’ll find the 
for...in
loop used with both arrays and collections in many situations (rather than the 
while
loop, which can 
appear somewhat clumsy and can perform poorly for list processing). Listing 3-5 shows a 
for...in
loop in action.
LISTINg 3-5: 
 Looping through data using for...in
# Create an array of strings.
MyList = ‘Hello’, ‘Goodbye’, ‘Red’, ‘Green’
# Process the array. 
for ThisString in MyList:
# Display the individual values.
print ‘The current value is:’, ThisString
# Pause after the debug session.
raw_input(‘Press any key to continue...’)
The code begins by creating an array. This is a very simple array, but it demonstrates just how 
easy IronPython makes certain programming tasks. You don’t have to worry about doing any-
thing odd when creating the array. In addition, you’ll find that arrays, like strings, come with a 
wealth of methods. For example, if you want to add a new member to an array, you simply call 
on the 
append()
method to perform the task. The site at 
http://docs.python.org/dev/3.0/
library/array.html
describes array methods and types in greater detail. You’ll see many of 
these array methods demonstrated in Chapter 4.
548592c03.indd 51
2/24/10 12:47:30 PM
www.finebook.ir


52

CHAPTER 3
ImplementIng Structured language elementS
After the code creates the array, it uses the 
for...in
loop to process it. The interpreter automati-
cally calls the loop code once for each of the values in the array. The individual array values appear in 
ThisString
. In this case, the code merely prints out the values of 
ThisString
, as shown in Figure 3-3.
FIgURE 3-3: 
The for loop is exceptionally efficient at list processing.
The 
for...in
loop always has two parts: a target and an expression list. The target can be another 
expression list, in which case you can nest 
for...in
loops to process the target. It’s possible to dig 
down into just about any hierarchy of objects using a 
for...in
loop.
Using while
The 
while
loop might not be quite as pretty as the 
for...in
loop, but it serves an important pur-
pose. The 
for...in
loop works with a fixed number of elements. The 
while
loop can work with 
an arbitrary number of elements, or can remain running until told to stop. You use a 
while
loop in 
situations where you don’t know how many times a loop will occur during design time. Of course, 
this means that you must give the 
while
loop a positive method of ending, as shown in Listing 3-6.

Download 20,35 Mb.

Do'stlaringiz bilan baham:
1   ...   68   69   70   71   72   73   74   75   ...   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