Python Basic data type
1.python Course Introduction
2. Data type
3. Sequence Type
1.python Introduction
1.python是一门编程语言,是一门完全面向对象的编程语言2.如果对语言进行分类,那么python是一门强类型,动态的语言(若类型:比如int可以编程float,python中数值类型不可变;动态类型:编译时不需要知道变量类型,python中变量无类型)
2. Numeric type
数值类型就是我们平常用来做计算的数字类型,在python中有如下几种数据类型#整型int#浮点型float#布尔型bool#复数型(在python中用小写j,表示虚部,用其它的字母不行)complex
Numeric types are primarily used for operations, so there are also the following operators
#数值运行常用运算符+ - * / //(整除,向下取整) %(取余) **(幂)#赋值运算符= += -= *= /= ....
The first three of the basic numeric types are commonly used, and the plural is used more in mathematics, but it is generally not used.
3. Sequence Type
在python中三种序列类型,序列类型简单来说就是对象里面的元素是有顺序的。字符串 str列表 list (可变)元祖 tuple序列类型作为基本的数据类型是必须掌握的,在python中会经常见到这三种数据类型。首先需要掌握定义和序列的通用操作。
1. Create a sequence
字符串用引号创建列表用中括号[]创建元祖用小括号()或者直接用逗号隔开各元素
2. General operation of the sequence
索引取值切片 索引:正向索引和反向索引类型的转换: str() list() tuple()相加 +, +=重复 *, *=检查成员 in, not in
3. Command
#查看内置命令dir(__builtins__)#今天的命令type:查看对象的类型id:查看对象的id,就像身份证help:帮助#变量命名规则‘‘‘字母 数字 下划线 组成字母跟下划线开头不是用关键字‘‘‘#数值类型a=1 #intprint(type(a))b=1.1 #floatprint(type(b))c=True #boold=False #boolprint(type(c),type(d))e=1+2j #complex(复数型)。只能用jprint(type(e))#查看关键字import keywordkeyword.kwlist#数值运算 + - * / //(取整) %(取余) **(幂运算,即几次方如: 2**3即2的三次方)#序列类型 1.字符串 h=‘哈哈‘ #str 单引号 q="qwe" #双引号 w=‘‘‘qwe‘‘‘ #三引号#如何写注释‘哈哈‘ #单行注释"qwe" #单行注释# #单行注释‘‘‘qwe‘‘‘ #多行注释 2.列表 li = [1,2,3,4,5,6] #中括号里面的元素用逗号隔开 >>> li = [‘a‘,‘b‘] >>> li2 = [‘哈哈‘,‘呵呵‘] >>> a=1 >>> b=2 >>> li3=[a,b] >>> q=[] #空列表#变量定义方式:>>> a=1>>> b=2>>> a=2;b=3>>> a,b=3,4>>> a,b=b,a 3.元祖 q = (1,2,3,4,5) #小括号里面的元祖用逗号隔开 w = 1,2,3 #用逗号隔开 e = 1, #只有一个元素是得加逗号
4. Index
The position of the element in the list, element, and array, starting at zero and ending at position len-1
5.len
View the length of the list, element, array.
6. Slicing
>>> q=[1,2,3,4,5,6,7,8,9,0]>>> q[1:3][2, 3]#第一个索引是要提取的第一个元素,第二索引是剩余部分后的第一个
7. Step: Positive and negative (direction) of the points
>>> q=[1,2,3,4,5,6,7,8,9,0]>>> q[1:6] #默认步长是1[2, 3, 4, 5, 6]>>> q[1:7][2, 3, 4, 5, 6, 7]#更改步长为2>>> q[1:8:2][2, 4, 6, 8]#更改步长为3>>> q[1:9:3][2, 5, 8]#取全部>>> q[::] #默认从第一个索引还是,最后一个索引结束[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]>>> q[::2][1, 3, 5, 7, 9]>>> q[:] #默认从区全部[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]#从第一个索引位置第六个索引位置>>> q[:6] [1, 2, 3, 4, 5, 6]#>>> q[1::2][2, 4, 6, 8, 0]#从后往前数>>> q[1::2][2, 4, 6, 8, 0]#>>> q[-1:-4:-1][0, 9, 8]
8. General operation of sequence type-+ + = *=
>>> li = [1,2,3,4]>>> li+[1,23][1, 2, 3, 4, 1, 23]>>> li2 = li + [1,23]>>> li2[1, 2, 3, 4, 1, 23]>>> li*2[1, 2, 3, 4, 1, 2, 3, 4]>>> li = li + li>>> li[1, 2, 3, 4, 1, 2, 3, 4]>>> li3 = [‘a‘,‘b‘]>>> li3+=li3>>> li3[‘a‘, ‘b‘, ‘a‘, ‘b‘]>>> a=1>>> a+=1>>> a2
9. Check Members
>>> li[1, 2, 3, 4, 1, 2, 3, 4]>>> 5 in liFalse>>> 5 not in liTrue
10. Forcing type conversions
>>> int(1.1)1>>> int(1.9)1>>> int(1+2j)Traceback (most recent call last): File "<pyshell#61>", line 1, in <module> int(1+2j)TypeError: can‘t convert complex to int>>> float(1)1.0>>> a=1>>> str(a)‘1‘>>> list(‘asd‘)[‘a‘, ‘s‘, ‘d‘]>>> a = list(‘asd‘)>>> a[‘a‘, ‘s‘, ‘d‘]
Python basic data type (i)-python3.0 Learning notes