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


Performing Statistics on Number List



Download 2,02 Mb.
Pdf ko'rish
bet56/126
Sana01.01.2022
Hajmi2,02 Mb.
#285662
1   ...   52   53   54   55   56   57   58   59   ...   126
Bog'liq
Learning Python The Ultimate Guide for Beginners to Coding with

Performing Statistics on Number List
Python has certain functions that allow you to perform statistics operation on a number list. For
instance, you can execute operations such as finding the sum, minimum, and maximum numbers in
a particular list.
>>>numbers= [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
>>>min(numbers)
11
>>>max(numbers)
20
>>>sum(numbers)
155
This example is just to show you how possible you can manipulate numbers in a list. You can do
the same even if the list contains a million numbers.


List Comprehensions
Previously, I explained how you could create a list of squares using four lines of code. However,
there is a simpler approach, which allows you to use a single line. A list comprehension comprises
of  a  “for  loop”  with  the  creation  of  new  elements  in  the  list,  and  repeatedly  append  each  new
element. Although it is advisable for beginners to use the list comprehension, however, I included
them for you to be aware of them. let us use an example to show you how you can rewrite this
program on a line to create a square of the first 15 numbers.
number_squares = [numbers**2 for numb in range(1, 15)]
print(number_squares)
We  started  by  creating  a  descriptive  name  (number_squares)  for  our  list.  We  then  open  a  set  of
square brackets, which allows us to define the expression “[numbers**2 for numb in range(1, 15)]”
for the numbers we want to keep track of in our new list. Furthermore, a loop is created to repeat
the  expression  pending  when  the  condition  is  completed.  The  loop  goes  through  the
range()function, with values from 1 to 15. If you observe carefully, we didn’t include a colon at the
end of the loop. When we run the program after saving it, our output will be as what we got in our
previous example.
1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225
Because the list comprehension isn’t advisable for beginners, it doesn’t mean you can’t give it a
try. It takes consistent practice to understand the list comprehension before it will be worthwhile to
create lists comfortably.

Download 2,02 Mb.

Do'stlaringiz bilan baham:
1   ...   52   53   54   55   56   57   58   59   ...   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