Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet67/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   63   64   65   66   67   68   69   70   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Adding Items


When you need to add items to your lists, Python has two different methods for doing so. .append( )
Append will always add the value within the parenthesis to the back of the list. Let’s see:

# adding an item to the back of a list using append nums = [10, 20] nums.append(5) print(nums) # outputs [10, 20, 5]

Go ahead and run that cell. We declared a list with two items in it to start and then added the integer value of 5 to the back of the list.

.insert( )


The second method to add items to a list is using insert. This method requires an index to insert a value into a specific location. Let’s see an example:

# adding a value to the beginning of the list words = [ "ball", "base" ] nums.insert(0, "glove") # first number is the index, second is the value

Go ahead and run that cell. The output will result in [‘glove’, ‘ball’, ‘basex’]. Glove is in the zero index now because we specified that index within our insert method.

Removing Items


There are several ways to remove items from a list, the following are the main two methods.

.pop( )


By default, the pop method removes the last item in the list; however, you can specify an index to remove as well. This method is also widely used to save the removed item too. When pop is used, it not only removes the item but also returns it. This allows us to save that value into a variable to be used later:

# using pop to remove items and saving to a variable to use later items = [5, "ball", True] items.pop( ) # by default removes the last item removed_item = items.pop(0) # removes 5 and saves it into the variable print(removed_item, "\n", items)

Go ahead and run that cell. Using pop, we can see that it removed the True item first, then the element in index zero, which happens to be the integer 5. While popping it out of the list, we saved it into a variable, which we later output along with the new list.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   63   64   65   66   67   68   69   70   ...   200




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