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



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

TUESDAY EXERCISES

1.

Divisible by Three: Write a for loop that prints out all numbers from 1 to 100 that are divisible by three.

2.

Only Vowels: Ask for user input, and write a for loop that will output all the vowels within it. For example:
>>> "Hello" "eo"

today was spent learning all about for loops and how they work. Looping allows us to run the same lines of code several times.
Wednesday: While Loops
We’ll be going over the other type of loop today, the while loop. Yesterday we saw how loops work, and why we would use a for loop. A while loop is generally used when you need to loop based on a condition rather than counting. Today will be all about condition-based looping.
To follow along with this lesson, let’s continue from our previous notebook file
Week_04” and simply add a markdown cell at the bottom that says, “While Loops.”

Writing a While Loop


Like a for loop, the while loop starts out with the keyword “while”. Following that, we have a conditional like we would use to write an if statement. Let’s see an example:

# writing your first while loop health = 10 while health > 0: print(health) health -= 1 # forgetting this line will result in infinite loop

Go ahead and run that cell. This will continue to print out the value of health until the condition is met. In this case, once health is no longer greater than zero, the loop stops running. On the last line, we decrement health by one, so each iteration reduces health closer to zero. If we didn’t decrement health at any point in time, this would become an infinite loop (which is bad).

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   60   61   62   63   64   65   66   67   ...   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