Python Programming for Biology: Bioinformatics and Beyond


Appendix 1 Simplified language reference



Download 7,75 Mb.
Pdf ko'rish
bet447/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   443   444   445   446   447   448   449   450   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

Appendix 1

Simplified language reference

Keywords

The following table gives basic descriptions of the Python keywords, which are a reserved

part of the language and so cannot be used for variable names.

Keyword

Description

Example

and


Performs the logical AND

operation (conjunction) on two

values, yielding a true value if both

are true, and otherwise a false

value.

if x and y:



print("Both are true")

as

Used in conjunction with the with



keyword to define a variable that

holds a context manager object.

Alternatively used with import

statements to create synonyms for

external components.

[See examples for ‘with’ and

‘import’]

assert


Checks whether a statement is true,

and if not triggers an exception.

Often used to check the validity of

input values. The command is

ignored if Python is run in

optimised mode.

assert(isinstance(x, int))

Note: triggers an

AssertionError exception if the

test fails.

break


Causes the program execution

within current, innermost, iterative

loop (invoked with for or while) to

stop immediately.

for value in valueList:

if value > 0:

print('Loop stopped')

break


class

Defines a class: the construction of

a named type of Python object,

which may be based on one or more

other classes.

class


Biologist(Scientist):

field = 'Biology'

continue

Causes program execution within

current, innermost, iterative loop

for value in valueList:




(invoked with for or while) to skip

to the next iteration.

if value > 0:

print('Value skipped')

continue

def


Defines a named Python function

(subroutine), the code for which is

in an indented block.

def dotProduct(vec1,

vec2):

pairs = zip(vec1, vec2)



p = sum([a*b for a,b in

pairs])


return p

del


Deletes a variable or item, directly

from memory or from an enclosing

data structure.

p =


[‘Neptune’,‘Uranus’,‘Pluto’]

del p[2]


Note: deleted list item with

index 2 (Pluto).

elif

Used after an if statement to define



a further block of code that is

conditionally executed if a different

condition holds.

if x > 0:

print("x is positive")

elif x < 0:

print("x is negative")

else


Used after an if statement to define

a final block of code that is

executed if none of the conditions

after the if or elif statements hold.

Also used after try to define a block

that is executed if no error occurs,

and after for to define a block that

is executed if the for loop has not

been exited because of a break.

if x % 2:

print("x is odd")

else:


print("x is even")

except


Used after the try keyword to

provide a code block that catches

and deals with particular kinds of

error exception.

[See example for try]

exec


Executes Python statements that are

contained within text strings or a

file; allows the creation of dynamic

code.


text = ‘sum(range(1,100,3))’

exec(text)

finally

Used after the try keyword in order

to specify a block of code that is

always executed after exception

fileObj = open(name)

try:





handling, typically to perform

clean-up operations.

print(fileObj.readlines())

finally:


fileObj.close()

for


Creates a repeating, iterative loop

where a variable is assigned values

in turn from a collection or iterable

object.


total = 0.0

for value in

[1,3,5,7,9,11]:

total += value ** 0.5

from

Used in a statement to specify



which module to import a

component from.

from math import sqrt

y = sqrt(2.0)

global

Makes named variables accessible



in a global context, i.e. throughout

the whole file, even if they only

appear within inner, local code

blocks.


def func(text):

global value

value = len(text)

func('hello')

print(value) # 5

if

Used to execute a block of code



only on condition that a statement

holds. May be used in conjunction

with elif and else.

if x > 0:

print("Here if x is

positive")

import

Imports a named external module



so that it is available within the

current program. Can be used with

from to import a particular sub-

component and with as to create a

synonym for the module.

import math

y = math.sqrt(2.0)

import numpy as np

a = np.array([2,0,4,1])

in

Used in various situations. On its



own used in tests whether an item is

a member of a collection, giving

True if it is and False otherwise.

Also used after for and exec

keywords.

letters =

['G','C','A','T']

if x in letters:

print('x is in the

list')


[See also for and exec

keywords]

is

Compares two Python objects to



test whether they are the same

object, yielding True if they are and

False otherwise.

if x is None:

print("x not defined")



is not

Compares two Python objects to

test whether they are not the same

object, yielding True if they are

different and False if they are the

same object.

if x is not None:

print("x is defined")

lambda

Creates an unnamed (anonymous)



function. Useful for defining small

functions in place in larger

expressions.

data = [('A',4), ('B',9),

('C',2)]

data.sort(key=lambda x:

x[1])

Sorts on the second item.



not

Performs a logical NOT operation

(negation) on a value, giving True if

the value is false and False if the

value is true.

if not x:

print("x is false")

or

Performs the logical OR operation



(disjunction) on two values,

yielding a true value if one or other

is true, and otherwise a false value.

if x or y:

print("At least one is

true")


pass

A keyword that doesn’t actually do

anything except form part of a valid

Python syntax. Used as a

placeholder where code may be

filled in later.

if x is None:

pass # Add code later

else:

x += 2.0


print

Displays a line of text to screen

(standard out), with an option to

redirect the data, e.g. to a file. In

Python 2 print is a statement or a

function. In Python 3 it is a

function.

print ‘Hello’

print >> fileObj, “Data line”


Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   443   444   445   446   447   448   449   450   ...   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