标签:
习题来源: Python for Biologists: A complete programming course for beginner
1 #!/bin/python 2 # calculate the AT content of a DNA seq 3 4 def get_at_content(dna, sig_figs = 2): # sig_figs=2 为默认参数 sig_figs 5 length = len(dna) 6 a_count = dna.upper().count(‘A‘) # use str.upper() and str.count() method 7 t_count = dna.upper().count(‘T‘) 8 at_content = (a_count + t_count) / length 9 return round(at_content, sig_figs) # 使用return 在很多情况下比 print 好 10 # use round() function ,就是设置数值的小数点位数 11 12 assert get_at_content("ATCG") == 0.5 # assert 语句用来测试。非常有用 13 test_dna = "ATGCATGCAACTGTAGC" 14 print(get_at_content(test_dna, 1)) # 此时,将sig_figs 赋值为 1 15 print(get_at_content(test_dna)) # 当没有明确的赋值时, 即调用默认参数值 16 print(get_at_content(test_dna, 3)) 17 print(get_at_content(dna = "ATCGGTAGTCGTAGCGTAGCAGT", sig_figs = 2)) # 比较完整的函数调用
运行结果:
uubuntu$ python3 chapter5_1_py3.py
0.5
0.53
0.529
0.48
标签:
原文地址:http://www.cnblogs.com/OA-maque/p/4817420.html