Python学习【一】数字 、 字符串、列表

一、数字

常用运算符
+加法
-减法
*

乘法

**幂运算 (x**y 表示x的y次方)
/除法(只返回浮点类型)
//

整数除法(返回求整除法)

()用于分组
%求模运算
=

变量赋值

 

注意:

①变量在使用前必须”定义”(赋值)。

②浮点数有完整的支持,整数和浮点数的混合运算中,整数会被转化成浮点数

③python还内建支持复数,使用后缀 j 或者 J表示虚数的部分(例如 3+5j)

 

二、字符串

!!!不可变数据类型,若要不同的字符串需要创建一个新的字符串!!!

Python可以用过 单引号 ('.......') 或者 双引号 ("..........") 来标识字符串,用 ( \ ) 来转义引号

单引号和双引号大致相同,区别在于 使用单引号时要对单引号进行转义, 使用双引号时要对双引号进行转义。

 

在交互式解释器中,输出的字符串会用引号引起来,特殊字符会用反斜杠转义。虽然可能和输入看上去不太一样,但是两个字符串是相等的。如果字符串中只有单引号而没有双引号,就用双引号引用,否则用单引号引用。print() 函数生成可读性更好的输出, 它会省去引号并且打印出转义后的特殊字符:

>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'
>>> print('"Isn\'t," she said.')
"Isn't," she said.
>>> s = 'First line.\nSecond line.'  # \n means newline
>>> s  # without print(), \n is included in the output
'First line.\nSecond line.'
>>> print(s)  # with print(), \n produces a new line
First line.
Second line.

如果你前面带有 \ 的字符被当作特殊字符,你可以使用 原始字符串,方法是在第一个引号前面加上一个 r:

>>> 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

上面都是输出单行的字符串,或者使用 \n 来输出多行,但是也可以直接按照多行文本的格式直接输出,可以使用三引号 ("""...""") 或者 ('''...''')。行尾的换行符会自动包含到字符串中,可以通过在行尾加上 \ 来避免这个行为(即吸收换行符)。

可以通过以下两个例子来对比理解:

print("""\
Usage: thingy [OPTIONS]\
     -h                        Display this usage message
     -H hostname               Hostname to connect to
""")

结果:

Usage: thingy [OPTIONS]     -h                        Display this usage message
     -H hostname               Hostname to connect to

如果不在第二行添加 \

print("""\
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
""")

输出结果:

Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to

字符串可以通过 + 连接(类似C++ 中的 String), 可以通过 * 表示有多少个重复:

3*'zjj'+ 'beautiful' 

字符串可以通过 索引来访问字符 或者 通过切片来访问子串

(索引可以为负,表示从右边开始计算,解决了C中负数下标为非法访问的问题)

 +---+---+---+---+---+---+
 | P | y | t | h | o | n |
 +---+---+---+---+---+---+
   0   1   2   3   4   5   6
  -6  -5  -4  -3  -2  -1

访问单个字符:

>>> word = 'Python'
>>> word[0]  # character in position 0
'P'
>>> word[5]  # character in position 5
'n'
>>> 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]  # character from the beginning to position 2 (excluded)
'Py'
>>> word[4:]  # characters from position 4 (included) to the end
'on'
>>> word[-2:] # characters from the second-last (included) to the end
'on'

Python 能够优雅地处理那些没有意义的切片索引:一个过大的索引值(即下标值大于字符串实际长度)将被字符串实际长度所代替,当上边界比下边界大时(即切片左值大于右值)就返回空字符串:

>>> word[4:42]
'on'
>>> word[42:]
''

三、列表

Python中最常用的复合数据类型(即列表中的元素不必是同一类型),写作中括号之间的一列逗号分隔的值。

类似于字符串,列表可以被索引和切片,不同于字符串,列表允许修改元素。

>>> squares = [1, 4, 9, 16, 25]
>>> squares
[1, 4, 9, 16, 25]
>>> squares[0]  # indexing returns the item
1
>>> squares[-1]
25
>>> squares[-3:]  # slicing returns a new list
[9, 16, 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]  # 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() 方法 在列表的末尾添加新的元素:

>>> 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', 'e', 'f', 'g']
>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> # replace some values
>>> letters[2:5] = ['C', 'D', 'E']
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> # now remove them
>>> letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']
>>> # clear the list by replacing all the elements with an empty list
>>> letters[:] = []
>>> letters
[]

允许嵌套列表(创建一个包含其它列表的列表):

>>> a = ['a', 'b', 'c']
>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值