Learning Python: The Ultimate Guide for Beginners to Coding with Python with Useful Tools



Download 2,02 Mb.
Pdf ko'rish
bet48/126
Sana01.01.2022
Hajmi2,02 Mb.
#285662
1   ...   44   45   46   47   48   49   50   51   ...   126
Bog'liq
Learning Python The Ultimate Guide for Beginners to Coding with

More actions on a “for Loop”
You can perform whatever thing you want on each item when using a for loop. Instead of using a
new example, I will use our magician example. In this example, I will print a message indicating
that each magician did perfectly well.
magicians_names =[“Greg,” “Perpetual,” “Amos”]
for magician in magicians_names:
print(f “{magician.title()}, you did an excellent trick today!”)
The first two lines do not require any explanation. However, the only alteration in the code is the
third line, where we created a piece of unique information to every single magician, beginning with
his or her name. During the loop, it goes through the first value, which is “Greg” and prints the
message after the name. it does the same thing for the value “Perpetual” before running through the
loop  for  the  third  time  to  print  the  value  “Amos”  with  the  message.  The  output  will  show  a
customized message as follows:
Greg, you did an excellent trick today!
Perpetual, you did an excellent trick today!
Amos, you did an excellent trick today!
You can write many codes as possible inside a for loop. Each indented line that follows the line
“for  magician  in  magicians_name”  is  regarded  to  be  within  the  loop  with  each  indented  line
performed once for every single item. For instance, let us add another line to our previous example,
telling the magicians to keep the good work going every day.
magicians_names =[“Greg,” “Perpetual,” “Amos”]
for magician in magicians_names:
print(f “{magician.title()}, you did an excellent trick today!”)
print(f “Keep the good work going every day, (magician.title()}. \n”)
If  you  observe  clearly,  each  print()  method  is  properly  indented;  therefore,  each  line  will  be
executed once for the element in the list. However, (\n) in the second print line is a newline, which
requests for a blank line to be inserted after the last statement before going through the loop. With
this newline, the message becomes neatly grouped for the magician list. Let us see how the output
of the program will be.
Greg, you did an excellent trick today!
Keep the good work going every day
Perpetual, you did an excellent trick today!
Keep the good work going every day
Amos, you did an excellent trick today!


Keep the good work going every day
You see how neatly the output is; this makes your program understandable. You can add as many
lines of codes you want in a “for loop.” Ensure to include some lines of codes in this program to
understand the concept behind looping in Python.

Download 2,02 Mb.

Do'stlaringiz bilan baham:
1   ...   44   45   46   47   48   49   50   51   ...   126




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