Python Programming for Biology: Bioinformatics and Beyond



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

locals, globals)

Like exec() (see above) but

operates on a named file.

Dynamically reads and

executes the Python code in

the file. Allows optional

dictionary arguments to

specify global and local

scope variable names. Not

available in Python 3.

execfile(fileName)

file(name, mode)

Creates a file object. Little

if isinstance(obj,




used because open(),

described below, is generally

used instead, but useful for

checking data types. Not

available in Python 3.

file):


print("Object is a

file")


float(val)

Converts a number or string

into its best floating point

equivalent.

print(float(‘7’))

print(float(‘+18.57e12’) /

2.0)

format(val,



specification)

Formats a string value

according to a specified

representation. See

Appendix

4

below for new-style



formatting codes.

format(3.14159,‘E’)

format(‘DNA’, ‘>4s’)

Results are ‘3.141590E+00’

and ‘DNA’.

frozenset(vals)

Creates a frozen set from a

given object; an immutable

unordered collection of non-

repeating items. Sometimes

used to convert sets into the

immutable equivalent so they

may be used as keys in

dictionaries.

l = [0,1,2,3,1,2,3,2,1,0,1,3]

x = frozenset(l)

print(x)

Result is frozenset([0, 1, 2,

3]).

getattr(obj, name,



defaultVal)

Retrieves a particular named

attribute from a specified

Python object. Sometimes

used as an alternative to the

dot notation when the

attribute name is a variable.

import math

print(getattr(math,

'pi'))


print(math.pi)

Results are both

3.141592653589793.

globals()

Gives a dictionary of variable

name and object pairs that are

available in the outermost,

global context of the

program’s execution.

varDict = globals()

print(varDict)

hasattr(obj, name)

Determines whether a Python

object has a particular named

attribute. Gives True if it

does and False otherwise.

import math

hasattr(math,'sin') #

True

hasattr(math,'apple')



# False


help(obj)

Gets help documentation for

a given Python object.

print(help(float))

fileObj = open(‘a.txt’)

print(help(fileObj))

hex(val)

Creates a text string

representing the hexadecimal

(i.e. base 16) version of an

integer number.

print(hex(2**24-1))

Result is ‘0xffffff’.

id(obj)


Gives the unique number that

identifies a particular Python

object. Such numbers do not

change within a Python

session, but they will

(usually) be different for a

new session.

print(id(7))

print(id(float))

print(id(None))

input(prompt)

Prompts the user at the

command line for keyboard

input, i.e. so the entered

values can be used in the

program.


x = input(‘Enter value:’)

print(“Value is: “, x)

int(val, base)

Converts a number or string

into an integer representation.

Optionally takes the radix

number for which base to

use.


print(int(34.96)) # Base 10

print(int(‘1000100’,2)) #

Base 2

Results are 34 and 68.



isinstance(obj,

class)


Determines whether one

object is derived from a

specified class (or subclass

thereof), returning True if it

is and False otherwise.

isinstance(7, int) # True

isinstance(7, float) # False

issubclass(class1,

class2)

Determines whether one

object class is a subclass of

another, returning True if it is

and False otherwise.

issubclass(Protein,

Molecule)

len(vals)

Gives the size of an object,

e.g. the number of items in a

collection.

letters = [‘G’,‘C’,‘A’,‘T’]

print(len(letters)) # 4

list(vals)

Creates a Python list

t = (‘V’,‘I’,‘L’,‘A’,‘M’,‘P’)




collection from a specified

object, which must be

iterable. Can be used on a list

to make a copy.

x = list(t)

y =


list(enumerate(range(7,11)))

First example converts

tuple to list. Second

converts iterator, giving [(0,

7), (1, 8), (2, 9), (3, 10)].

locals()


Gives a dictionary of variable

name and object pairs that are

available in the innermost,

local context at a point in the

program’s execution.

for x in range(5):

varDict = locals()

print(varDict)

Results show x changes

locally, in the loop.

long(val, base)

Converts val into a long

integer (i.e. of arbitrary

length): this is mostly

redundant. Not available in

Python 3.

[See Mathematics section]

map(func, vals,

…)

Takes an iterable object, like



a list, and applies a function

to each item, generating a

new list (in Python 2) or a

map iterator (in Python 3).

This function is largely

redundant and it is more

commonplace to use a list

comprehension instead.

vals = [30.0, 31.0, 28.25]

ints1 = map(int, vals)

ints2 = [int(x) for x in vals]

Note: ints1 and ints2 are the

same in Python 2, but in

Python 3 ints1 is a map

iterator.

max(vals)

or

max(val1, val2,



…)

Finds the minimum value of

a collection (or other iterable

object) or list of arguments.

This does not work on multi-

dimensional NumPy arrays.

print(max(3,11, 9, 5))

# 11


l = [30.0, 31.0, 28.25]

print(max(l)) # 31.0

min(vals)

or

min(val1, val2,



…)

Finds the minimum value of

a collection (or other iterable

object) or list of arguments.

This does not work on multi-

dimensional NumPy arrays.

print(min(8, 12, 3, 34)) # 3

l = [30.0, 31.0, 28.25]

print(min(l)) # 28.25

object()


Creates a basic, blank,

featureless object. The class

x = object()

print(dir(x))




of the object is the superclass

of all Python objects.

oct(val)

Creates a text string

representing the octal (i.e.

base 8) version of an integer

number.

print(oct(2**24-1))

Result is ‘077777777’ in

Python 2 and ‘0o77777777’

in Python 3.

open(fileName,



mode)

Opens a file on disk for

reading or writing by creating

a file type object. The second

argument is a mode string

that specifies whether the file

is for reading (‘r’), writing

(‘w’), appending (‘a’) etc.

The default mode is reading.

fObj1 = open(inName,

'rU')

line = fObj1.readline()



fObj2 = open(outName,

'w')


fObj2.write('Hello

world\n')

ord(char)

Gives the ASCII code

number for a character string.

Performs the inverse

operation to chr().

i = ord(‘Z’)-1

print(i, chr(i))

Result is 89, Y.

pow(val1, val2,

modulo)

Raises a number to a given

power, equivalent to using

the ‘**’ operator.

x = pow(2, 8)

x = 2**8 # Same

print(text, sep,


Download 7,75 Mb.

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