Python Programming for Biology: Bioinformatics and Beyond



Download 7,75 Mb.
Pdf ko'rish
bet20/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   16   17   18   19   20   21   22   23   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

Data collections

Python has several inbuilt collection data types, which are used to contain other items. The

basic types of data containers in Python are lists, tuples, sets and dictionaries.

Lists

A  list  in  Python  is  a  data  structure  that  can  contain  a  sequence  of  other  objects,  of

potentially different types,

4

in a specific order. Lists can have objects added to them and



removed from them and they can be empty. Also, lists can refer to the same object more

than once, at different positions in the sequence. For example, you could store the number

of days in each month of a year as a list, as illustrated below. Often in Python programs

you will be accessing the elements contained in a list by referring to a specific position (an




index) within that list and by going through all the elements in a list in their given order. In

Python we use square brackets to specify the beginning and end of a list:

days = [31, 28.243, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

matrix = [[-1, 0, 0], [0, 1, 0], [0, 0, 3]]



Defining lists in Python; a simple list of numbers (both integers and floating point) and a second list

which contains three sub-lists, each representing a row in a matrix.

Tuples

A tuple is a data structure that is very much like a list, but which you cannot change once

it  is  created.  In  Python  a  tuple  may  be  defined  using  round  brackets  ‘()’.  Although  you

cannot  change  its  items,  a  tuple  is  used  to  contain  a  sequence  of  elements  in  a  specific

order,  and  the  different  positions  of  this  sequence  can  be  interrogated.  The  contents  of  a

tuple are defined in their entirety when the tuple object is made. Having a kind of list that

you cannot change may seem like a pointless data structure, but tuples are a surprisingly

useful type of object. If you know that a sequence should definitely not have any elements

modified,  added  or  deleted,  then  you  can  use  a  tuple  to  ensure  that  it  is  not  possible  to

deviate  from  this  plan:  for  example,  if  you  want  to  specify  a  vector  with  exactly  three

spatial  coordinates,  e.g.  (x,  y,  z),  using  a  tuple  ensures  that  you  can’t  have  an  invalid

vector  with  too  few  or  too  many  values.  Similarly,  tuples  are  used  where  you  have

elements that you know always go together; accordingly you could use tuples to specify a

text  font  like  (‘helvetica’,  10)  or  (‘roman’,  12),  where  you  must  have  two  elements  to

represent the name and the size of the font, and if you were to redefine the font you would

have to specify both. Tuples, unlike lists, can be used as keys to refer to data in dictionary

data structures (see the Dictionaries section below).

Sets

Sets, like lists and tuples, are data containers that encompass a collection of other objects.

However, unlike lists and tuples, the elements are in no particular order and the elements

cannot  be  repeated  in  a  set.  A  notable  use  for  sets  is  when  you  have  some  data  that  you

know,  or  suspect,  contains  repeat  objects.  By  placing  such  data  within  a  set  any

duplication will be removed. For example, you might have a list containing the colours of

different  items;  if  you  put  these  colours  into  a  set  object  you  can  find  out  how  many

different  colours  were  used.  Also  sets  can  be  useful  because  you  can  easily  perform  set

operations, for example, to find the items that two collections have in common; this would

be trickier using lists or tuples.

females = set(['marge', 'maude', 'lisa', 'maggie', 'edna'])

simpsons = set(['homer', 'marge', 'bart', 'lisa', 'maggie'])

print(females & simpsons)

Defining  Python  sets  and  performing  set  operations;  here  finding  an  intersection  (common

elements).



There  is  actually  another  variety  of  set,  called  a  frozen  set.  These  are  the  same  as

regular  sets  with  the  exception,  as  the  name  suggests,  that  they  cannot  be  altered  once

created (just like tuples). A useful consequence of this is that they can be used as keys to

extract data from dictionary data structures (see below).




Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   16   17   18   19   20   21   22   23   ...   514




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