After downloading and installing Python, type import this into the Python command line and see what happens.
# 安装Python并在命令行模式下输入import this查看Python之禅
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
Given: A string s of length at most 200 letters and four integers a, b, c and d.
Return: The slice of this string from indices a through b and c through d (with space in between), inclusively. In other words, we should include elements s[b] and s[d] in our slice.
Return: A file containing all the even-numbered lines from the original file. Assume 1-based numbering of lines.
# 读入一个文本文件,循环输出偶数行
>>> with open("rosalind_ini5.txt","r") as fh:
... for i, line in enumerate(fh):
... if i % 2 == 1: # i是从0开始的
... print(line,end="")
...
Some things in life are bad, they can really make you mad
Other things just make you swear and curse
When you're chewing on life's gristle, don't grumble give a whistle
This will help things turn out for the best
Always look on the bright side of life
Always look on the right side of life
If life seems jolly rotten, there's something you've forgotten
And that's to laugh and smile and dance and sing
When you're feeling in the dumps, don't be silly, chumps
Just purse your lips and whistle, that's the thing
So, always look on the bright side of death
Just before you draw your terminal breath
Life's a counterfeit and when you look at it
Life's a laugh and death's the joke, it's true
You see, it's all a show, keep them laughing as you go
Just remember the last laugh is on you
Always look on the bright side of life
And always look on the right side of life
Always look on the bright side of life
And always look on the right side of life
Return: A file containing all the even-numbered lines from the original file. Assume 1-based numbering of lines.
# 统计一个字符串中每个单词出现的频数,以空格分割
>>> s = "When I find myself in times of trouble Mother Mary comes to me Speaking words of wisdom let it be And in my hour of darkness she is standing right in front of me Speaking words of wisdom let it be Let it be let it be let it be let it be Whisper words of wisdom let it be And when the broken hearted people living in the world agree There will be an answer let it be For though they may be parted there is still a chance that they will see There will be an answer let it be Let it be let it be let it be let it be There will be an answer let it be Let it be let it be let it be let it be Whisper words of wisdom let it be Let it be let it be let it be let it be Whisper words of wisdom let it be And when the night is cloudy there is still a light that shines on me Shine until tomorrow let it be I wake up to the sound of music Mother Mary comes to me Speaking words of wisdom let it be Let it be let it be let it be yeah let it be There will be an answer let it be Let it be let it be let it be yeah let it be Whisper words of wisdom let it be"
>>> mydict = {}
>>> for word in s.split(" "):
... if word in mydict:
... mydict[word] += 1
... else:
... mydict[word] = 1
...
>>> for key,value in mydict.items():
... print(key + " " + str(value))
...
When 1
I 2
find 1
myself 1
in 4
times 1
of 11
trouble 1
Mother 2
Mary 2
comes 2
to 3
me 4
Speaking 3
words 7
wisdom 7
let 30
it 36
be 41
And 3
my 1
hour 1
darkness 1
she 1
is 4
standing 1
right 1
front 1
Let 6
Whisper 4
when 2
the 4
broken 1
hearted 1
people 1
living 1
world 1
agree 1
There 4
will 5
an 4
answer 4
For 1
though 1
they 2
may 1
parted 1
there 2
still 2
a 2
chance 1
that 2
see 1
night 1
cloudy 1
light 1
shines 1
on 1
Shine 1
until 1
tomorrow 1
wake 1
up 1
sound 1
music 1
yeah 2
# another way
>>> words = s.split(" ")
>>> for word in words:
... mydict[word] = words.count(word)
...
>>> for key,value in mydict.items():
... print(key, value)
# another way
>>> words = s.split(" ")
>>> for word in words:
... mydict[word] = 1 + mydict.get(word, 0) #利用字典自带的get方法
...
>>> for key,value in mydict.items():
... print(key, value)
...
A string is simply an ordered collection of symbols selected from some alphabet and formed into a word; the length of a string is the number of symbols that it contains.
An example of a length 21 DNA string (whose alphabet contains the symbols 'A', 'C', 'G', and 'T') is "ATGCTTCAGAAAGGTCTTACG."
Given: A DNA string s of length at most 1000 nt.
Return: Four integers (separated by spaces) counting the respective number of times that the symbols 'A', 'C', 'G', and 'T' occur in s.
# 输入一个DNA序列的字符串,统计这个DNA序列中A,C,G,T四个字符的数目,并以空格分隔输出
>>> DNA = "TGAGTTCCGTATCGTGAAAAAGGAAGGTTAGCCAGCCTTCTCACACGATGGCTATGTTGATTTGGTCGTAGGGACACTAGATACGCTGTTTCTCTTCTTAAAGATGGAACGATGTAACGCTGGATGGAAACTGAACTAGATAAGCCAGTAGTGTAAATGTTCGTACCTGACATATCACTGTTGCAAAATTGGGGATCTATCGAAGATCAGCGCACGCGCTTCAATTGTGTTTATGGACAAGGTCTTGCGGTCGATAGGTCTTCAGACAGGCAGCGTTCCCGCGACCTTCCTCGCGATTCCTCGGATGTGGAGTGGTAGAGAACAGCGATTGGTGCCGACCTTCTGAGGAAGAACCGTGCCAGTTTGCGACATTCGTCCCAACAAATTAACCATATCACGTTAAGGTTAGCCCCCTACTAAGTACAAAGACTGGGCATCAGAAGGGCTGCCCATGTAGATGATTTGTTTTGTCAAACAAACGCGTCCGGTGGGGGCGACTTAACCACCTTCTGGCGGTGCACATTACTGGGTTCTTGAGGTGCTAATGAGCATTGCGTTGAGCGTATCAATAACGCGACTGGCGATACACCGACAGGGTATTCCGTCTGGCGCCCTGAGACGGGGTTGGGACTCCGATAAGACGTGGTGCGACCCACCGACGTGTAAAACGCAGAAGTCTCTCCTCTACATCATTGTTAAACCGGGCTAGACAGCTGCGGTCTTCTCTCATAGGCACCACCCCAGTACCCTGCCAATAGTGCATCGTGTTCTGTGAGCAAGTGTTGTTGCGGTCGCACTTGTTCTTTGTCATACAGAGGCTCCTCCTCGACTTAAGACATGTTGAAACTGTCTCGCACTGACACGCAAGGTCGGTTCACAGTTCGACCGAGCTTCAGAATGCCCAATAAAGCCTTTTCAAATAATACAACCCTGAATGCAGTCCACACCACTTTA"
>>> mydict = {}
>>> for item in DNA:
... mydict[item] = 1 + mydict.get(item,0)
...
>>> for i in "ACGT":
... print(mydict[i], end = " ")
...
237 234 240 243
# another way
>>> count_A, count_C, count_G, count_T = [DNA.count(nuc) for nuc in "ACGT"] #列表推导式,利用字符串自带的count方法进行计数
>>> print(count_A,count_C,count_G,count_T)
237 234 240 243
# another way
>>> count_A = DNA.count("A")
>>> count_C = DNA.count("C")
>>> count_G = DNA.count("G")
>>> count_T = DNA.count("T")
>>> print(count_A,count_C,count_G,count_T)
237 234 240 243
In DNA strings, symbols 'A' and 'T' are complements of each other, as are 'C' and 'G'.
The reverse complement of a DNA string ss is the string scsc formed by reversing the symbols of ss, then taking the complement of each symbol (e.g., the reverse complement of "GTCA" is "TGAC").