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



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

Dictionary Comprehension


Not only can you use comprehension on lists but also Python dictionaries as well. The syntax structure is the exact same, except you need to include a key-value pair instead of a single number to insert into the dictionary. Let’s create a dictionary of even numbers as keys, where the value is the key squared:

# creating a dictionary of even numbers and square values using comprehension numbers = [ x for x in range(10) ] squares = { num : num**2 for num in numbers if num % 2 == 0 } print(squares)

Go ahead and run the cell. We’ll get the following: “{0: 0, 2: 4, 4: 16, 6: 36, 8: 64}”. We were able to add each key-value pair using comprehension while checking to see if they were an even number with the conditional statement.

MONDAY EXERCISES


  1. Degree Conversion: Using list comprehension, convert the following list to Fahrenheit. Currently, the degrees are in Celsius temperatures. the conversion formula is “(9/5) * C + 32”. your output should be [ 53.6, 69.8, 59, 89.6 ].

>>> degrees = [ 12, 21, 15, 32 ]

  1. User Input: ask the user to input a single integer up to and including 100. Generate a list of numbers that are exactly divisible by that number up to and including 100 using list comprehension. For example, if the number 25 was input, then the output should be [ 25, 50, 75, 100 ].


today’s focus was all about generating lists using a concept called list comprehension. depending on the expression needed, you’ll use a certain syntax structure. Comprehension doesn’t improve performance; instead it reduces the lines needed in our code to perform the same task. It can also improve readability.
Tuesday: Lambda Functions
Lambda functions, otherwise known as anonymous functions, are one-line functions within Python. Like list comprehension, lambda functions allow us to reduce the lines of code we need to write within our program. It doesn’t work for complicated functions but helps to improve readability of smaller functions.
To follow along with this lesson, let’s continue from our previous notebook file
Week_08” and simply add a markdown cell at the bottom that says, “Lambda Functions.

Download 2,61 Mb.

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