TickingClock的个人博客分享 http://blog.sciencenet.cn/u/TickingClock

博文

Rosalind 2 - Variables and Some Arithmetic

已有 2873 次阅读 2017-10-18 09:09 |个人分类:Python Learning|系统分类:科研笔记

Python Village - INI2: Variables and Some Arithmetic


变量Variables,是计算机语言中能储存计算结果或能表示值抽象概念。


在Python中,基础的数据类型分为strings和numbers。numbers有两类:integers(整数,包括正数与负数)和floats(浮点数,带有小数点)。可以通过下面的方法赋予变量以数值:


>>> a = 324
>>> b = 24
>>> c = a - b
>>> print 'a - b is', c
a - b is  300

>>>


其中,a、b、c均是integers,而'a - b is’是一个string,Python返回的内容就是“a - b is  300”。


几种常用的计算符号:


>>> 2 + 3
5
>>>

(加法)


>>> 5 - 2
3
>>>

(减法)


>>> 3 * 4
12
>>>

(乘法)


>>> 15 / 3
5
>>>

(除法)


>>> 18 % 5
3
>>>

(余数)


>>> 2 *** 3
8
>>>

(幂运算)


注意:当除法运算遇到余数时,python只返回正数:


>>> 18/5
3

>>>


如果想得到后面的剩余的值,就必须将18(默认为integers类型数据)改为floats数据类型:


>>> 18.0/5
3.6

>>>


或者


>>> float(18)/5
3.6

>>>


在Python中,单个“=”号代表给变量赋值,比如“a = 3”,就是把3赋给变量a;而两个“==”就是相等的意思,比如:


>>> a == 2
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

>>>


提示出现错误,a为变量,没有进行赋值,所以并不等于2


>>> a = 2
>>> a
2
>>> a == 2
True

>>>


首先给变量a赋值为2,然后“a == 2”返回值为“True”,说明a与2相等。


在Python中,string类型数据就是字符、数字及其他符号的集合,可以用“=”给变量赋值string类型数据:


>>> a = "Hello"
>>> a
'Hello'

>>>


string类型数据必须加上"或者',内部可以随意加上引号:


>>> b = 'Project "Rosalind"'
>>> b
'Project "Rosalind"'

>>>


string类型数据的运算与numbers的运算稍微有所差异:


>>> a = 'Rosalind'
>>> b = 'Franklin'
>>> c = '!'
>>> print a + ' ' + b + c*3
Rosalind Franklin!!!

>>>


Problem


Given: Two positive integers a and b, each less than 1000.

Return: The integer corresponding to the square of the hypotenuse of the right triangle whose legs have lengths a and b.


Sample Dataset


3 5


Sample Output


34


这个就是给你两个数,然后计算两个数平方之和


>>> a = 3
>>> b = 5
>>> c = a ** 2 + b ** 2
>>> print c
34

>>>


Over


Rosalind is a platform for learning bioinformatics and programming through problem solving. Take a tour to get the hang of how Rosalind works.


P.S. 欢迎关注微信公众号:微信号Plant_Frontiers


https://wap.sciencenet.cn/blog-3158122-1081309.html

上一篇:Rosalind 1 - Installing Python
下一篇:Cell:通过基因组编辑为作物改良提供数量性状变异
收藏 IP: 221.181.145.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-6-8 04:46

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部