Exercise Source: Python for BIOLOGISTS:A complete programming course for beginner
1 #!/bin/python2 #calculate the at content of a DNA seq3 4 defGet_at_content (DNA, Sig_figs = 2):#sig_figs=2 is the default parameter Sig_figs5Length =Len (DNA)6A_count = Dna.upper (). Count ('A')#Use str.upper () and Str.count () method7T_count = Dna.upper (). Count ('T')8At_content = (A_count + t_count)/length9 returnRound (at_content, Sig_figs)#using return in many cases is better than printTen #Use round () function, which is the number of decimal places to set the value One A assertGet_at_content ("ATCG") = = 0.5#The Assert statement is used to test. Very useful -Test_dna ="ATGCATGCAACTGTAGC" - Print(Get_at_content (Test_dna, 1))#at this point, the Sig_figs is assigned a value of 1 the Print(Get_at_content (Test_dna))#when there is no definite assignment, the default parameter value is called - Print(Get_at_content (Test_dna, 3)) - Print(Get_at_content (DNA ="Atcggtagtcgtagcgtagcagt", Sig_figs = 2))#Compare a complete function call
Operation Result:
0.50.530.5290.48
Counts the at-content of a given sequence and sets the number of decimal places