Python入门笔记三(注意知识点)

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

a=[1,2,3,4,5]
b=a
a[0]=‘1’
print(a)
[‘1’, 2, 3, 4, 5]
#关键在输出的b

print(b)
[‘1’, 2, 3, 4, 5]

7: int ,str,tuple值类型(不可变的)
list ,set,dict 引用类型(可变的)

b=‘hello’
id(b)#b的位置
1688794751528

b=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)
1688794426696

a[0]=‘1’
id(a)
1688794426696

a=(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]#访问元组
3

a[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 assignment

a[3][2]=‘4’#列表可变
print(a)
(1, 2, 3, [1, 2, ‘4’])

二:运算符
python不存在变量的定义,直接用即可
不存在C++/C–即自增自减运算

b=1
b+=b>=1#从右向左运算
print(b)
2

print(b>=1)
True

int(True)
1

b=b+True
b=b+1

字符串;元组;列表均可比较

**

查看ASCII码用ord

**

is和==的区别

a=1
b=1.0
a==b
True

a is b
False

专门判断类型的函数
isinstance(,)
第一种:

a=‘hello’
isinstance(a,str)
True

isinstance(a,int)
False

第二种isinstance(,(int,str,float))

isinstance(a,(int,str,float))
True

isinstance(a,(int,float))
False
在这里插入图片描述

2018.12.30(下午)
1.表达式的定义
表达式(Expression)是运算符(operator)和操作数(operand)所构成的序列

优先级表
在这里插入图片描述

2.优先级:
not >and>or
算术运算符>比较运算符>逻辑运算符

发布了22 篇原创文章 · 获赞 0 · 访问量 442

猜你喜欢

转载自blog.csdn.net/qq_36956082/article/details/85345952