Python Learning notes-Little Kee

Source: Internet
Author: User

1. Strings String

Infer whether a character (char) is a number or a letter

#推断是否为字母#推断是否为数字

Infer whether a string is empty

ifnot#推断是否为空,true表示空

Adding content to a string

‘‘.join(‘love‘)

Get string fixed-length strings

>>>str = str1[1:3#得到从下标1開始到下标3之前的字符,下标3的字符不算

String substitution

>>>str.replace(‘abc‘,‘cde‘)
2. List of lists

Infer if a list is empty

>>>if len(lists):   #true表示空#向列表中加入数据>>>lists.append(‘1‘)#得到list某个值的下标>>>lists.index(‘1‘)
3. File Operation

Read the file

>>>with open(‘filename‘,‘r‘as f1:

Write a file

>>>with open(‘filename‘,‘w‘as f2:>>> ......>>> f2.write(‘my name is ldw.‘)

Get the entire folder file

>>>import os>>>files = os.listdir("/ifs/home/liudiwei")>>>forin files:    #onefile即递归得到的每一个文件>>> ...               

To infer whether the current folder exists and does not exist to create a folder:

ifnotos.path.exists(outdir): #假设outdir不存在,则创建该文件夹        os.makedirs(outdir)

Python runs the current system command

os.system(‘[command]‘)  #command为想要运行的命令
4. Collection Operations

Create a Collection

s = set(‘thisisset‘)

adding elements

s.add(‘z‘)

Set length

len(s)

Inferred Collection Non-empty

iflen(s):   #假设长度为0,返回false

Infer that a collection has an element

‘k‘in s    #或者能够是 ‘k‘ not in s

Recycling elements

del s
5. Floating-point Division

Set A and B as two integers. Dividing two numbers requires floating-point results

c=float(a)/float(b)

If you need to keep decimals

c=float(‘%.3f‘float(a)/float(b))
6. Building an array

Generates an array of 8000*4 matrices

 def Genmatrix():aa=[' A ',' C ',' D ',' E ',' F ',' G ',' H ',' I ',' K ',' L ',' M ',' N ',' P ',' Q ',' R ',' S ',' T ',' V ',' W ',' Y ',] triplet = [] forIinchRange (len (AA)): forJinchRange (len (AA)): forKinchRange (len (AA)): Triplet.append (Aa[i]+aa[j]+aa[k]) rna=[' A ',' U ',' G ',' C '] Matrix = [[0  forColinchRange (len (RNA))] forRowinchRange (len (triplet))]returnTriplet,rna,matrix'
7. Dictionaries

Python dictionaries are more useful, similar to maps in java/c++. Key-value pairs. In practice, Python can find value based on the key.
Defined

dict_map = {}#赋值dict_map[‘number‘2#查看是否存在某个键dict_map.has_key(‘number‘)#if exist, return True,else return False

Python Learning notes-Little Kee

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.