使用Python作为计算器

数值

1.python支持基本的数学运算符,而且应用python你可以像写数学公式那样简单明了。

eg:

>>> 2 + 2
4
>>> 50 - 5*6
20
>>> (50 - 5*6) / 4
5.0
>>> 8 / 5  # division always returns a floating point number
1.6

2.除法运算符号‘/’常常返回的是float类型,而‘//’返回的是整形,求余数符号是‘%’

eg:

>>> 17 / 3  # classic division returns a float
5.666666666666667
>>>
>>> 17 // 3  # floor division discards the fractional part
5
>>> 17 % 3  # the % operator returns the remainder of the division
2
>>> 5 * 3 + 2  # result * divisor + remainder
17
3.'**'符号表示求幂操作,‘=’用来赋值操作。

*变量不可以没有赋值就使用,所以做好在声明变量时就给赋值。


字符串

1.字符串常量可以使用单引号或者双引号包含,不过习惯上常常使用双引号,如果为了在字符串中出现单引号或者双引号,可以使用反斜杠‘\’进行声明。

eg:

>>> 'spam eggs'  # single quotes
'spam eggs'
>>> 'doesn\'t'  # use \' to escape the single quote...
"doesn't"
>>> "doesn't"  # ...or use double quotes instead
"doesn't"
2.如果你不想以\字符被解释为特殊字符,你可以通过在第一次报价,添加一个R使用原始字符串

eg:

>>> print('C:\some\name')  # here \n means newline!
C:\some
ame
>>> print(r'C:\some\name')  # note the r before the quote
C:\some\name
3.字符串可以跨多个行。一种方式是使用三引号:“”“…”“”或“”“…”。字符串的结尾会自动包含在字符串中,但是可以通过在行的结尾加上\\来防止这一点。

eg:

print("""\
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
""")
4.字符串可以通过‘+’来进行连接和通过‘*’进行重复。

eg:

>>> # 3 times 'un', followed by 'ium'
>>> 3 * 'un' + 'ium'
'unununium'

5.两个或两个以上的字符串(即那些包含在引号)下可以彼此自动连接。

注:几年只有被引号所包含的字符串常量才支持这样的操作,字符串变量和字符串表达式都不支持。

eg:

>>> 'Py' 'thon'
'Python'
>>> prefix = 'Py'
>>> prefix 'thon'  # can't concatenate a variable and a string literal
  ...
SyntaxError: invalid syntax
>>> ('un' * 3) 'ium'
  ...
SyntaxError: invalid syntax
6.字符串支持下标访问和区域访问。

eg:

>>> word = 'Python'
>>> word[0]  # character in position 0
'P'
>>> word[5]  # character in position 5
'n'
>>> word[0:2]  # characters from position 0 (included) to 2 (excluded)
'Py'
>>> word[2:5]  # characters from position 2 (included) to 5 (excluded)
'tho'
>>> word[:2] + word[2:]
'Python'
>>> word[:4] + word[4:]
'Python'
7.字符串不支持修改和越界访问。

eg:

>>> word[42]  # the word only has 6 characters
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> word[0] = 'J'
  ...
TypeError: 'str' object does not support item assignment
>>> word[2:] = 'py'
  ...
TypeError: 'str' object does not support item assignment
*如果想修改,可以通过定义新的变量。

8.len()函数可以返回字符串的长度。

eg:

>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)
34

列表

1.在python中,列表是最常用的类型之一。列表中可以包含相同类型的数据,也可以包含不同类型的数据。

eg:

>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
2.列表和字符串一样支持下标访问和区域访问,支持‘+’号操作。

eg:

>>> squares[0]  # indexing returns the item
1
>>> squares[-1]
25

>>> squares[:]
[1, 4, 9, 16, 25]
>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
3.列表可以对其中的元素进行修改。

>>> cubes = [1, 8, 27, 65, 125]  # something's wrong here
>>> 4 ** 3  # the cube of 4 is 64, not 65!
64
>>> cubes[3] = 64  # replace the wrong value
>>> cubes
[1, 8, 27, 64, 125]
append
4.append函数是往列表后添加数据,len函数返回列表长度

eg:

>>> cubes.append(216)  # add the cube of 6
>>> cubes.append(7 ** 3)  # and the cube of 7
>>> cubes
[1, 8, 27, 64, 125, 216, 343]
>>> letters = ['a', 'b', 'c', 'd']
>>> len(letters)
4




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值