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



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

Loops and Lists


You can use both the for and while loops to iterate over the items within a list.

Using For Loops


When iterating over a list with a for loop, the syntax looks like when we used the range function previously; however, this time we use a temporary variable, the in keyword, and the name of the list. For each iteration, the temporary variable is assigned the item’s value. Let’s try it out:

# using a for loop to print all items in a list sports = [ "Baseball", "Hockey", "Football", "Basketball" ] for sport in sports: print(sport)

Go ahead and run that cell. Here we can see that this cell will output each item within the list. During the first iteration, the temporary variable “sport” is assigned “Baseball,” and once it prints it out, it moves on to the next item.

Using While Loops


While loops are always used for conditional looping. One great use case for a while loop with lists is removing an item. There are so many uses, this is just one of them:

# using the while loop to remove a certain value names = [ "Bob", "Jack", "Rob", "Bob", "Robert" ] while "Bob" in names:
names.remove("Bob") # removes all instances of 'Bob'
print(names)

Go ahead and run that cell. The output will be our names list without “Bob” in the list. We used the combination of the while loop with a conditional to check for our “Bob” value in the list and then continued to remove it until our condition was no longer true.

THURSDAY EXERCISES


  1. Remove Duplicates: remove all duplicates from the list below. hint: Use the

.count() method. the output should be [‘Bob’, ‘Kenny’, ‘Amanda’]
>>> names = ['Bob', 'Kenny', 'Amanda', 'Bob', 'Kenny']

  1. User Input: Use a while loop to continually ask the user to input a word, until they type “quit”. once they type a word in, add it to a list. once they quit the loop, use a for loop to output all items within the list.


today was important so that we could understand how to work with lists, whether it be a conditional statement or a loop. there are many methods out there that lists can use; we’ll go over more of them throughout the rest of this book.

Download 2,61 Mb.

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