||
Python Village - INI4: Conditions and Loops
Python中的条件语句if:
>>> a = 42
>>>
如果a小于10,输出'the number is less than 10';否则,输出'the number is greater or equal to 10’。
注意,if这一行最后需要有冒号,下一行print要空一格;然后else后要有冒号,下一行print前要空一格。
Python中的循环语句while:
>>>
初始值greetings为1,判断当greetings小于等于3时,执行print 'Hello! ' * greetings和greetings = greetings + 1,然后再判断greetings是否小于等于3决定是否再一次执行print 'Hello! ' * greetings和greetings = greetings + 1。一直到greetings等于4的时候就会终止循环,最终的输出结果就有了。
注意,不能创造一个无限循环,比如:
因为greetings = greetings + 0导致greetings始终为1,循环终止不了,就会一直输出Hello!
Python中的遍历语句for:
>>>
这个其实就是对某个list中所有的item都操作一遍。
Python中的range()函数:
>>>
>>>
>>>
>>>
Given: Two positive integers a and b (a<b<10000).
Return: The sum of all odd integers from a through b, inclusively.
100 200
7500
这个就是要计算从a到b之间所有奇整数的和(包括b)
>>>
其中a|1代表当a为偶数时,返回a+1;当a为奇数时,返回a。range(a|1, (b+1)|1, 2)就是从a到b内的所有奇数,如果b是奇数也包括在内,比如:
>>>
>>>
Rosalind is a platform for learning bioinformatics and programming through problem solving. Take a tour to get the hang of how Rosalind works.
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-9-21 02:42
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社