Python入门笔记三(注意知识点)
2018.12.30(上午)
一:变量(名字)
A*3+B+A
1:定义变量(赋值符号”=“)
例子:
A=[1,2,3,4,5,6]
print(A)
[1, 2, 3, 4, 5, 6]B=[1,2,3]
A*3+B+A
[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3, 1, 2, 3, 4, 5, 6]
编程基础一方面体现在命名可读性强,变量名有意义
2:变量名只能是字母数字下划线,首字母不能是数字,其他符号也不行
3:系统保留的关键字不能用作命名
最好不要用type用作命名type = 1
type(1)#此处此处相当于“1(1)”
Traceback (most recent call last):
File “”, line 1, in
TypeError: ‘int’ object is not callable
4:python区分大小写
5:变量没有类型区分,整型,字符串,元组,列表,集合等等均可
6:
a=1
b=a
a=3
print(b)
1a=[1,2,3,4,5]
b=a
a[0]=‘1’
print(a)
[‘1’, 2, 3, 4, 5]
#关键在输出的bprint(b)
[‘1’, 2, 3, 4, 5]
7: int ,str,tuple值类型(不可变的)
list ,set,dict 引用类型(可变的)
b=‘hello’
id(b)#b的位置
1688794751528b=b+‘python’
id(b)#加上其他字符串后不再是原来字符串
1688794755376‘python’[0]
‘p’‘python’[0]=‘o’#字符串不可改变
Traceback (most recent call last):
File “”, line 1, in
TypeError: ‘str’ object does not support item assignment
8:tuple list区别
tuple(元素)不可变
list(元素)可变
例子:
>>> a=[1,2,3]
id(a)
1688794426696a[0]=‘1’
id(a)
1688794426696a=(1,2,3)
a[0]=‘1’#无法改变
Traceback (most recent call last):
File “”, line 1, in
TypeError: ‘tuple’ object does not support item assignment
b=[1,2,3]
b.append(4)#list增添一个元素
print(b)
[1, 2, 3, 4]c=(1,2,3)
c.append(4)#tuple不可增添
Traceback (most recent call last):
File “”, line 1, in
AttributeError: ‘tuple’ object has no attribute ‘append’
总结:写程序最好不要用列表,因为多个程序在一块会引起元素改变,而元组不会,能用元组就用元组
9:元组访问/类似于一维数组二维数组
a=(1,2,3,[1,2,4])
a[2]#访问元组
3a[3]
[1, 2, 4]a=(1,2,3,[1,2,[‘a’,‘b’,‘c’]])
a[3][2][1]
‘b’a=(1,2,3,[1,2,4])
a[2]=‘3’#元组不可变
Traceback (most recent call last):
File “”, line 1, in
TypeError: ‘tuple’ object does not support item assignmenta[3][2]=‘4’#列表可变
print(a)
(1, 2, 3, [1, 2, ‘4’])
二:运算符
python不存在变量的定义,直接用即可
不存在C++/C–即自增自减运算
b=1
b+=b>=1#从右向左运算
print(b)
2print(b>=1)
Trueint(True)
1b=b+True
b=b+1
字符串;元组;列表均可比较
**
查看ASCII码用ord
**
is和==的区别
a=1
b=1.0
a==b
Truea is b
False
专门判断类型的函数
isinstance(,)
第一种:
a=‘hello’
isinstance(a,str)
Trueisinstance(a,int)
False
第二种isinstance(,(int,str,float))
isinstance(a,(int,str,float))
Trueisinstance(a,(int,float))
False
2018.12.30(下午)
1.表达式的定义
表达式(Expression)是运算符(operator)和操作数(operand)所构成的序列
优先级表
2.优先级:
not >and>or
算术运算符>比较运算符>逻辑运算符