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



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

If-Else Statements


Let’s take it one step further now and add in an else statement. This time we’ll append the string “Even” when the number is divisible by two; otherwise, we’ll append the string “Odd”:

# using if/else statements within list comprehension
nums = [ "Even" if x % 2 == 0 else "Odd" for x in range(10) ] # generates
a list of even/odd strings print(nums)

Go ahead and run the cell. This will output a list of strings that represent the numbers odd or even value. Here we append the string “Even” when the if conditional is True; otherwise, the else statement will be hit and append the string “Odd”. The same representation of code without comprehensions can be found in the following:
>>> nums = [ ] >>> for x in range(10):
>>> if x % 2 == 0:
>>> nums.append("Even") >>> else:
>>> nums.append("Odd")
We’ve reduced the lines of code from six down to one. Comprehensions are great for quick generation of data; however, it becomes more difficult when the conditions are larger. Comprehensions don’t allow for the use of elif statements, only if/else statements.

List Comprehension with Variables


Comprehension is great for generating data from other lists as well. Let’s take a list of numbers and generate a separate list of those numbers squared, using comprehension:

# creating a list of squared numbers from another list of numbers using list comprehension nums = [2, 4, 6, 8]
squared_nums = [ num**2 for num in nums ] # creates a new list of squared numbers based on nums
print(nums)

Go ahead and run the cell. We’ll get an output of [ 4, 16, 36, 64 ]. For this example, we were able to generate the squared numbers by appending the expression “num∗∗2”.
The same representation of code without comprehension would look like the following:
>>> squared_nums = [ ] >>> for num in nums:
>>> squared_nums.append(num**2)
In this example, we were able to reduce the lines needed from three to one.

Download 2,61 Mb.

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