Python数据类型

Python常用数据类型:

  1. 整数
  2. 浮点数
  3. 列表
  4. 字典
  5. Numpy数组

字符串string

  • 字符串文本可使用三引号进行分行
  • 字符串可以用“+”进行连接,用“*”进行重复
  • 字符串可以被索引及分割
    注意切片索引的范围
  • 字符串使用len()查看长度
  • Python字符串不可以被更改
#三引号分行#
print('''\
     Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
     ''')
#“+”连接,“*”重复
a='hel'
b='lo'
print(a+b)
c=3*a
print(c)
#直接连接和使用“+”连接
print('py''thon')
print(c+'llo')
text = ('Put several strings within parentheses '
            'to have them joined together.')
print(text)
#索引
world='python'
print(world[0])
print(world[-1])
print(world[0:2])
#len()
len(text)

运行结果:

 Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
hello
helhelhel
python
helhelhelllo
Put several strings within parentheses to have them joined together.
p
n
py

列表list

  • 列表内容写在“[ ]”之间,列表中的元素数据类型可以不相同
  • 列表可以被索引与切片
  • 列表可以使用“+”相加
  • 列表可以被改变
  • 列表长度的计算使用len()函数
  • 列表可以嵌套形成多维列表
  • 列表常用函数
    list.append(x):把一个元素添加到列表的结尾,相当于 a[len(a):] = [x]
    list.extend(L):将一个给定列表中的所有元素都添加到另一个列表中,相当于 a[len(a):] = L
    list.insert(i, x):在指定位置插入一个元素。第一个参数是准备插入到其前面的那个元素的索引
    list.remove(x):删除列表中值为 x 的第一个元素。如果没有这样的元素,就会返回一个错误
    list.pop([i]):从列表的指定位置删除元素,并将其返回。如果没有指定索引,a.pop() 返回最后一个元素。元素随即从列表中被删除(方法中 i 两边的方括号表示这个参数是可选的,而不是要求你输入一对方括号,你会经常在Python 库参考手册中遇到这样的标记)。
    list.clear():从列表中删除所有元素。相当于 del a[:]
    list.index(x):返回列表中第一个值为 x 的元素的索引。如果没有匹配的元素就会返回一个错误。
    list.count(x):返回 x 在列表中出现的次数
    list.sort():对列表中的元素就地进行排序
    list.reverse():就地倒排列表中的元素
    list.copy():返回列表的一个浅拷贝。等同于 a[:]

  • 列表可以做堆栈使用

  • 列表当作队列使用

  • 列表推导式:列表推导式为从序列中创建列表提供了一个简单的方法。普通的应用程式通过将一些操作应用于序列的每个成员并通过返回的元素创建列表,或者通过满足特定条件的元素创建子序列
#列表中元素数据类型可不同
L=[1,2,1.5,'a','B']
print(L)
#列表的索引与切片
L1=L[:]#所有的切片操作都会返回一个包含请求的元素的新列表。这意味着下面的切片操作返回列表一个新的(浅)拷贝副本:
print(L1)
print(L[1:4])
print(L[0])
#"+"操作
L2=L+[38,46,5,200]
print(L2)
#列表可以被改变
L[0]=90
print(L)
#len()计算列表长度
len(L2)
print(len(L2))
***运行结果***:
[1, 2, 1.5, 'a', 'B']
[1, 2, 1.5, 'a', 'B']
[2, 1.5, 'a']
1
[1, 2, 1.5, 'a', 'B', 38, 46, 5, 200]
[90, 2, 1.5, 'a', 'B']
9
#列表的函数使用
a=[66.25,333,333,1,124.5]
print(a.count(333))
a.insert(2,-1)
a.append(333)
print(a)
print(a.index(333))
a.remove(333)
print(a)
a.reverse()
print(a)
a.sort()
print(a)
a.pop()
print(a)

***运行结果***
2
[66.25, 333, -1, 333, 1, 124.5, 333]
1
[66.25, -1, 333, 1, 124.5, 333]
[333, 124.5, 1, 333, -1, 66.25]
[-1, 1, 66.25, 124.5, 333, 333]
[-1, 1, 66.25, 124.5, 333]`
#列表当做堆栈使用
stack = [3, 4, 5]
stack.append(6)
stack.append(7)
print(stack)
stack.pop()
print(stack)
stack.pop()
print(stack)
stack.pop()
print(stack)
#列表当做队列使用
from collections import deque
queue=deque(['Eric','John','Mike'])
queue.append('Terry')
queue.append('Graham')
print(queue)
queue.popleft()
print(queue)
queue.popleft()
print(queue)
***运行结果***
[3, 4, 5, 6, 7]
[3, 4, 5, 6]
[3, 4, 5]
[3, 4]
deque(['Eric', 'John', 'Mike', 'Terry', 'Graham'])
deque(['John', 'Mike', 'Terry', 'Graham'])
deque(['Mike', 'Terry', 'Graham'])
#列表推导式
squares=[]
for x in range(10):
    squares.append(x**2)
print(squares)
#使用匿名函数简化
squares1=list(map(lambda x:x**2, range(10)))
print(squares1)
#简化方法二
squares2=[x**2 for x in range(10)]
print(squares2)
squares3= [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
print(squares3)
***运行结果***
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值