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



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

CHALLENGE QUESTION


For this week’s challenge, I’d like you to create a program that asks a user to input a number and tells that user if the number they entered is a prime number or not. Remember that prime numbers are only divisible by one and itself and must be above the number 2. Create a function called “isPrime” that you pass the input into, and return a True or False value. Be sure to keep efficiency in mind when programming the function.

Monday: List Comprehension
List comprehension allows us to create a list filled with data in a single line. Rather than creating an empty list, iterating over some data, and appending it to the list all on separate lines, we can use comprehension to perform all these steps at once. It doesn’t improve performance, but it’s cleaner and helps reduce the lines of code within our program. With comprehension we can reduce two or more lines into one. Plus, it’s generally quicker to write.
To follow along with the content for today, let’s open up Jupyter Notebook from our
python_bootcamp” folder. Once it’s open, create a new file, and rename it to “Week_08.” Next, make the first cell markdown that has a header saying: “List Comprehension.” We’ll begin working underneath that cell.

List Comprehension Syntax


The syntax when using list comprehension depends on what you’re trying to write. The general syntax structure for list comprehensions looks like the following:
>>> *result* = [ *transform* *iteration* *filter* ]
For example, when you want to populate a list, the syntax would have the following
structure:
>>> name_of_list = [ item_to_append for item in list ]
However, when you want to include an if statement, the comprehension would look like the following:
>>> name_of_list = [ item_to_append for item in list if condition ]
The item will only be appended to the new list if the condition is met; otherwise, it won’t include it. Lastly, if you would like to include an else condition, it would look like the following:
>>> name_of_list = [ item_to_append if condition else item_to_append for item in list ]
When using the else conditional within list comprehension, the first item will be appended to the list only when the if statement proves True. If it is False, then the item that comes after the else statement will be appended to the list.

Download 2,61 Mb.

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