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



Download 2,61 Mb.
bet129/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   125   126   127   128   129   130   131   132   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Generating a List of Numbers


Let’s try generating a list of numbers from 0 all the way up to 100 using list comprehension:

# create a list of ten numbers using list comprehension
nums = [ x for x in range(100) ] # generates a list from 0 up to 100 print(nums)

Go ahead and run the cell. You’ll notice that we output a list that includes 100 numbers. List comprehension has allowed us to build out this list within a single line rather than writing out the for loop and append statement on separate lines. The comprehension from the preceding cell is an exact representation of the following code:
>>> nums = [ ] >>> for x in range(100):
>>> nums.append(x)
As you can see, we’ve reduced three lines down to one using comprehension. This doesn’t improve performance but does reduce the number of lines within our code. It becomes more apparent in larger programs, and I highly recommend that you try to use comprehension when possible. Going forward we’ll begin to use list comprehension when building out lists.

If Statements


Earlier, we went over how the syntax changes when including an if statement in your comprehension. Let’s try an example by making a list of only even numbers:

# using if statements within list comprehesion
nums = [ x for x in range(10) if x % 2 == 0 ] # generates a list of even numbers up to 10
print(nums)

Go ahead and run the cell. For this comprehension, the variable x only gets appended to the list when the condition proves True. In our case, the condition is True when the current value of x is divisible by two. In the following, you’ll find the same code that is needed without using comprehension:
>>> nums = [ ] >>> for x in range(10):
>>> if x % 2 == 0:
>>> nums.append(x)
This time we were able to reduce four lines of code down to one. This can often improve readability of your code.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   125   126   127   128   129   130   131   132   ...   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