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


Making a List Using range() Function



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

Making a List Using range() Function
You can exchange a number list by using the list() function. Nevertheless, you need to wrap the
list() within the range() function. With this, your final outcome will be similar to a number list. We
will  use  our  previous  example  of  a  number  range  to  illustrate  this  example.  You  will  see  the
difference between the previous program code and this new one.
num = list(range(1, 16))
print(num)
Our output will be as follows:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
It  is  also  possible  to  skip  some  numbers  in  the  list  by  using  range()  function.  If  we  add  a  third
argument to the range(), the interpreter uses that value to perform a step size. For instance, we want
to  print  even  numbers  from  1  to  16.  What  the  program  does  is,  to  begin  with,  the  value  2  and
increases that value by 2. In order words, it starts at 2 and repeatedly adds 2 to the initial number
and continue until it reaches the last number.
Even_num = list(range(2, 16, 2))
print(even_num)
The output will be as follows:
2, 4, 6, 8, 10, 12, 14
Let us try another example.
odd_num = list(range(1, 16, 2))
print(even_num)
The output will be as follows:
1, 3, 5, 7, 9, 11, 13, 15
number_squares=[]
for numb in range(1, 15)
numbers = numb**2
number_squares.append(numbers)
print(number_squares)


Besides getting even and odd numbers, you can generate any set of numbers or operations you can
envision. Programming is about thinking outside the box. For instance, you can find the square of
any number of your choice. In this example below, we will make a list comprising of the first 15
square numbers. When you want to represent exponents in Python, we use double asterisks (**).
This is the program to illustrate the first 15 square numbers.
Initially,  the  program  will  look  confusing;  however,  I  will  explain  it  line  by  line.  We  began  by
creating an empty list and called it “number_squares.” In line two, we created a loop and request
each “numb,” ranging from 1 to 15, by using the range() function. Then, in line three, we raised the
current  “numb”  to  its  second  power  before  assigning  it  to  the  variable  “numbers”;  in  line  four,
every  new  “numb”  of  numbers  is  joined  to  our  list  number_squares.  Lastly,  the  list  of
number_squares is printed when the loop is finished. Then, the outcome of the program will be:
1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225
We can rewrite this code concisely by omitting the variable “numbers” while appending each new
“numb” directly to our list. Let us see how that is possible.
number_squares=[]
for numb in range(1, 15)
number_squares.append(numb**2)
print(number_squares)
As  you  can  see,  the  two  different  approaches  create  a  complex  list.  Remember,  we  omitted  the
temporary  variable  “numbers”  in  the  last  approach.  With  this  method,  you  make  your  program
easier  to  read.  However,  the  goal  is  to  focus  on  the  one  you  understand  and  not  to  rush  things.
Review the two approaches and look for the one that is more efficient.

Download 2,02 Mb.

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