Python Programming for Biology: Bioinformatics and Beyond


Appendix 2 Selected standard type methods and



Download 7,75 Mb.
Pdf ko'rish
bet456/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   452   453   454   455   456   457   458   459   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

Appendix 2

Selected standard type methods and

operations

Operations common to strings, lists and tuples

The  table  below  lists  most  of  the  functions  that  may  be  applied  to  Python  sequence

(ordered) collection types. For Python 2 the sequence types are buffer, bytearray, list, str,

tuple,  unicode  and  xrange.  For  Python  3  these  types  are  bytearray,  bytes,  memoryview,

list, range, str and tuple.

1

Operation



Description

Example

x in seq


x not in seq

Determines whether an item is or is not in a

collection, giving True or False

accordingly.

myList =

[3,1,4,1,5,9]

2 in myList #

False


8 not in myList

# True


seqA + seqB

Generates a new collection which is the

combination of two collections of the same

type, in order.

tupleA =

('G','C')

tupleB = ('A',

'T')


tupleA + tupleB

('G', 'C', 'A',

'T')

seq * n


n * seq

Generates a new collection based by

repeating the items of a collection a

number of times.

'AB' * 4 #

'ABABABAB'

5 * [0] #

[0,0,0,0,0]

seq[i]

Accesses an item in an ordered collection



by using a positional index, which starts

from zero.

text = 'Banana'

text[0] # 'B'

nums =

[1,4,9,16,25]

nums[2] # 9

seq[i:j]


Generates another, usually smaller


collection, by accessing a range of items

from an ordered collection, from a starting

positional index, up to but not including a

second index. If unspecified, the starting

index defaults to zero and the end defaults

to the end of the collection. Negative index

numbers count from the end.

text = 'Banana'

text[1:5] #

'anan'


x =

[1,4,9,16,25]

x[2:4] #

[9,16]


x[:4] #

[1,4,9,16]

x[1:] #

[4,9,16,25]

x[1:-1] #

[4,9,16]


seq[i:j:k]

Generates another, usually smaller

collection, by accessing a range of items

from an ordered collection, using positional

indices (see above) and a step size to skip

certain indices. Step may be negative for

reverse direction.

x =


[1,4,9,16,25]

x[0:5:2] #

[1,9,25]

x[::2] #

[1,9,25]

x[::-1] #

[25,16,9,4,1]

len(seq)


Gives the total number of items in the

collection; its size.

len('Banana') #

6

len((1,4,9,16))



# 4

len([]) # 0

min(seq)

Retrieves the item from a collection that

has the smallest value. This does not work

on multi-dimensional NumPy arrays.

x = [10, 5, 1,

10, 3]


min(x) # 1

min('ABCDE') #

'A'

max(seq)


Retrieves the item from a collection that

has the largest value. This does not work

on multi-dimensional NumPy arrays.

x = (10, 5, 1,

10, 3)

max(x) # 10



max('abcdefgh')

# 'h'


seq.index(item)

Retrieves the (first) positional index at

which an item is found in a collection.

Assumes the item is present and gives a

ValueError if not.

text = 'Banana'

text.index('a')

# 1


x =

[10,5,1,10,3]




x.index(3) # 4

seq.count(item)

Counts the number of occurrences of a

given item within a collection.

text = 'Banana'

text.count('a')

# 3

String methods

The  table  below  describes  selected  methods  that  relate  to  standard  Python  textual  string

objects,  which  you  would  call  using  the  dot  notation  string.method(arg).  Separate

descriptions  for  string  formatting  (both  old  and  new  style)  and  the  regular  expression

module re are given in

Appendix 4

 and

Appendix  5



 respectively.  Note  that  most  objects

can be converted to a string representation

2

with str(x).



It should be noted that there are big changes in Python between version 2 and version 3

with regard to handling Unicode. In Python 3 all strings are Unicode, so there is no need

for  unicode(x)  or  u”  syntax;

3

 any  ‘\u’  escaped  codes  in  a  string  will  naturally  be



recognised as Unicode characters. Also, the binary data type is introduced, which may be

created with bytes(), which is immutable, or bytearray(), which is mutable. The bytes type

may be used for general binary data, which may include binary encoded Unicode. Before

Python 3 the normal string type was used for binary data.




Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   452   453   454   455   456   457   458   459   ...   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