码迷,mamicode.com
首页 > 编程语言 > 详细

Python3_无题_1

时间:2018-08-15 20:34:49      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:html   []   include   rac   ace   tor   err   floor   har   

Python3_无题_1


以下是我在看文档时,觉的需要注意的地方
对照地址:https://docs.pythontab.com/python/python3.5/index.html

#几种运算
>>> 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
>>> 5 ** 2 # 5 squared
25

#变量在使用前必须 “定义”(赋值),否则会出错:
>>> # try to access an undefined variable
... n
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name ‘n‘ is not defined

#交互模式中,最近一个表达式的值赋给变量 _
#round( x [, n] ) : x--数字表达式,n--示从小数点位数,其中 x 需要四舍五入,默认值为 0。
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06

#字符串
>>> word = ‘Python‘
>>> word[0] # character in position 0
‘P‘
>>> word[5] # character in position 5
‘n‘
>>> word[-0] #-0就是0
‘P‘
>>> word[-1] # last character
‘n‘
>>> word[-2] # second-last character
‘o‘
>>> word[-6]
‘P‘

>>> 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‘
>>> word[4:42] #尾端越界,选择实际长度
‘on‘
>>> word[42:] #首端越界,为空
‘‘

#python的字符串是不可变的
>>> word[0] = ‘J‘
...
TypeError: ‘str‘ object does not support item assignment
>>> word[2:] = ‘py‘
...
TypeError: ‘str‘ object does not support item assignment

#列表的元素可以不为同一类型
>>> a = [1,2,3,‘4‘,‘56‘]
>>> a
[1, 2, 3, ‘4‘, ‘56‘]
#列表可索引,切片,‘+‘
>>> 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]
#列表是可变的
>>> cubes = [1, 8, 27, 65, 125]
>>> cubes[3] = 64
>>> cubes
[1, 8, 27, 64, 125]
#列表切片可以赋值
>>> letters = [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘]
>>> letters[2:5] = []
>>> letters
[‘a‘, ‘b‘, ‘f‘, ‘g‘]
#列表可嵌套
>>> a = [1,2,3,[1,2,3]]
>>> a
[1, 2, 3, [1, 2, 3]]
#让输出不换行
>>> a, b = 0, 1
>>> while b < 1000:
... print(b, end=‘,‘)
... a, b = b, a+b
...
1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,

Python3_无题_1

标签:html   []   include   rac   ace   tor   err   floor   har   

原文地址:https://www.cnblogs.com/IMWU/p/9483482.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!