Algorithms For Dummies


append(-5) and press Enter. When you type ListA



Download 7,18 Mb.
Pdf ko'rish
bet184/651
Sana15.07.2021
Hajmi7,18 Mb.
#120357
1   ...   180   181   182   183   184   185   186   187   ...   651
Bog'liq
Algorithms

append(-5) and press Enter. When you type ListA and press Enter again, you see 

that Python has added –5 to the end of the list. You may find that you need to 

remove items again, and you do that by using the 

remove()


 function. For example, 

type ListA.remove(-5) and press Enter. When you list 

ListA

 again by typing ListA 



and pressing Enter, you see that the added entry is gone.


88

 

   


  PART 1 

 Getting Started

Lists also support concatenation by using the plus (+) sign to add one list to another. 

For example, if you type ListX 

= ListA + ListB and press Enter, you find that the 

newly created 

ListX

 contains both 



ListA

 and 


ListB

 in it, with the elements of 

ListA

 coming first.



Creating and using tuples

A tuple is a collection used to create complex lists, in which you can embed one 

tuple within another. This embedding lets you create hierarchies with tuples. A 

hierarchy can be something as simple as the directory listing of your hard drive or 

an organizational chart for your company. The idea is that you can create complex 

data structures using a tuple.

Tuples are immutable, which means that you can’t change them. You can create a 

new tuple with the same name and modify it in some way, but you can’t modify 

an existing tuple. Lists are mutable, which means that you can change them. So a 

tuple can seem at first to be at a disadvantage, but immutability has all sorts of 

advantages, such as being more secure as well as faster. In addition, immutable 

objects are easier to use with multiple processors. To see how you can work with 

tuples, start a copy of IPython and type the following code:

MyTuple = (1, 2, 3, (4, 5, 6, (7, 8, 9)))

MyTuple

 is nested three levels deep. The first level consists of the values 1, 2, 3, 

and a tuple. The second level consists of the values 4, 5, 6, and yet another tuple. 

The third level consists of the values 7, 8, and 9. To see how this works, type the 

following code into IPython:

for Value1 in MyTuple:

    if type(Value1) == int:

        print(Value1)

    else:

        for Value2 in Value1:

            if type(Value2) == int:

                print("\t", Value2)

            else:

                for Value3 in Value2:

                   print("\t\t", Value3)

When you run this code, you find that the values really are at three different levels. 

You can see the indentations showing the level:



CHAPTER 4


Download 7,18 Mb.

Do'stlaringiz bilan baham:
1   ...   180   181   182   183   184   185   186   187   ...   651




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2025
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