2-数据类型和序列类型
Posted tiantiancode
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2-数据类型和序列类型相关的知识,希望对你有一定的参考价值。
视频1:复习环境
-----------------------------------------------------------------
视频2:变量及定义 (结合----01数值类型和序列类型.ppt)
xshell中练习:
查看关键字
>>> import keyword
>>> keyword.kwlist
[‘False‘, ‘None‘, ‘True‘, ‘and‘, ‘as‘, ‘assert‘, ‘break‘, ‘class‘, ‘continue‘, ‘def‘, ‘del‘, ‘elif‘, ‘else‘,
‘except‘, ‘finally‘, ‘for‘, ‘from‘, ‘global‘, ‘if‘, ‘import‘, ‘in‘, ‘is‘, ‘lambda‘, ‘nonlocal‘, ‘not‘,
‘or‘, ‘pass‘, ‘raise‘, ‘return‘, ‘try‘, ‘while‘, ‘with‘, ‘yield‘]
-----------------------------------------------------------------
视频3:数值类型数据
数值类型
查看类型
计算
精确导入模块
向上取整,小数补满
-----------------------------------------------------------------
视频4:序列类型数据
字符串--有序排列
>>> str1=‘yanzhi‘
>>> str1
‘yanzhi‘
>>> str2="""
... 123
... abc
... hhh
... """
>>> str2
‘
123
abc
hhh
‘
>>>
列表--有序排列
>>> li
[11, 22, ‘aa‘, ‘bb‘]
>>> type(str2)
<class ‘str‘>
>>> type(li)
<class ‘list‘>
>>>
元组--有序排列
>>> tu=(11,33,‘nihao‘,‘你好‘)
>>> tu
(11, 33, ‘nihao‘, ‘你好‘)
>>> type(tu)
<class ‘tuple‘>
>>>
--字符串,列表,元祖都是有序排列-测试
索引取值
>>> str3=‘123456789‘
>>> str3[0]
‘1‘
>>> str3[1]
‘2‘
>>> li=[11,22,34]
>>> li[0]
11
>>> li[2]
34
>>> tu=(88,888,‘999‘)
>>> tu[0]
88
>>> tu[2]
‘999‘
>>>
-----------------------------------------------------------------
视频5:下标索引和切片
切片,下标左右
>>> str4=‘python‘
>>> str4[1:2]
‘y‘
>>> str4[2:3]
‘t‘
>>> str4[2:4]
‘th‘
>>>
>>>
>>> li=[11,22,‘aa‘,‘bbc‘]
>>> li=[11,22,33,‘ab‘,‘cde‘]
>>> li[2:5]
[33, ‘ab‘, ‘cde‘]
>>> li=[1,2,3,4,5,6,‘aa‘,‘bb‘,‘vv‘]
>>> li[4:7]
[5, 6, ‘aa‘]
>>> li
[1, 2, 3, 4, 5, 6, ‘aa‘, ‘bb‘, ‘vv‘]
>>> li[2:5:2] //区间中隔2个取一个数
[3, 5]
>>> li[::2]
[1, 3, 5, ‘aa‘, ‘vv‘]
>>> li[::3]
[1, 4, ‘aa‘]
>>> li[::4]
[1, 5, ‘vv‘]
>>>
>>> li[-1]
‘vv‘ //反向切片
-----------------------------------------------------------------
视频6:序列类型之间相互转换
强制转换
>>> str1=‘python‘
>>> list1=[1,2,3,4]
>>> tu=(11,22,33,44)
>>> a=str(list1)
>>> type(a)
<class ‘str‘>
>>>
>>> b=str(tu)
>>> b
‘(11, 22, 33, 44)‘
>>> type(b)
<class ‘str‘>
>>>
>>> list(str1)
[‘p‘, ‘y‘, ‘t‘, ‘h‘, ‘o‘, ‘n‘]
>>> list(tu)
[11, 22, 33, 44]
>>>
>>>
>>> tuple(str1)
(‘p‘, ‘y‘, ‘t‘, ‘h‘, ‘o‘, ‘n‘)
>>>
-----------------------------------------------------------------
视频7:变量引用和拆包
引用
>>> a = 111
>>> b = a
>>> b
111
>>> a
111
拆包
>>> li = [11,22]
>>> aa,bb = li
>>> aa
11
>>> bb
22
>>> li = [11,22,33]
>>> q,w,e = li
>>> q
11
>>> w
22
>>> e
33
作业讲解:
数值类型:整数,浮点数,复数,布尔
序列类型:字符串,列表,元祖
2,通过下标取出li[1,2,3,4,5]中的3,4,5:
li=[1,2,3,4,5]
li[2:5]
3,通过切片取出:年,月,日
str1=‘20180607‘
str1[0:4]
str1[4:6]
str1[6:8]
老毛桃装机网站
msdn i tell you ----系统网站
以上是关于2-数据类型和序列类型的主要内容,如果未能解决你的问题,请参考以下文章