python学习笔记 day1
稍微有点编程基础的小编只把个人认为稍微需要注意的点记录在这啦~欢迎交流!
变量、运算符与数据类型
is
& is not
和==
& !=
的区别
is
,is not
对比的是两个变量的内存地址==
,!=
对比的是两个变量的值- 比较的两个变量,指向的都是地址不可变的类型(str等),那么
is
,is not
和==
,!=
是完全等价的。 - 对比的两个变量,指向的是地址可变的类型(list,dict,tuple等),则两者是有区别的。
运算符的优先级
- 一元运算符优于二元运算符。例如
3 ** -2
等价于3 ** (-2)
。 - 先算术运算,后移位运算,最后位运算。例如
1 << 3 + 2 & 7
等价于(1 << (3 + 2)) & 7
。 - 逻辑运算最后结合。例如
3 < 4 and 4 < 5
等价于(3 < 4) and (4 < 5)
。
【例】
print(-3 ** 2) # -9
print(3 ** -2) # 0.1111111111111111
print(1 << 3 + 2 & 7) # 0
print(-3 * 2 + 5 / -2 - 4) # -12.5
print(3 < 4 and 4 < 5) # True
浮点型
【例】
print(1, type(1))
# 1 <class 'int'>
print(1., type(1.))
# 1.0 <class 'float'>
a = 0.00000023
b = 2.3e-7
print(a) # 2.3e-07
print(b) # 2.3e-07
有时候我们想保留浮点型的小数点后 n
位。可以用 decimal
包里的 Decimal
对象和 getcontext()
方法来实现。
import decimal
from decimal import Decimal
【例】getcontext()
显示了 Decimal
对象的默认精度值是 28 位 (prec=28
)。
b = Decimal(1) / Decimal(3)
print(b)
# 0.3333333333333333333333333333
【例】使 1/3 保留 4 位,用 getcontext().prec
来调整精度。
decimal.getcontext().prec = 4
c = Decimal(1) / Decimal(3)
print(c)
# 0.3333
布尔型
除了直接给变量赋值 True
和 False
,还可以用 bool(X)
来创建变量,其中 X
可以是
- 基本类型:整型、浮点型、布尔型
- 容器类型:字符串、元组、列表、字典和集合
【例】bool
作用在基本类型变量:X
只要不是整型 0
、浮点型 0.0
,bool(X)
就是 True
,其余就是 False
。
print(type(0), bool(0), bool(1))
# <class 'int'> False True
print(type(10.31), bool(0.00), bool(10.31))
# <class 'float'> False True
print(type(True), bool(False), bool(True))
# <class 'bool'> False True
【例】bool
作用在容器类型变量:X
只要不是空的变量,bool(X)
就是 True
,其余就是 False
。
print(type(''), bool(''), bool('python'))
# <class 'str'> False True
print(type(()), bool(()), bool((10,)))
# <class 'tuple'> False True
print(type([]), bool([]), bool([1, 2]))
# <class 'list'> False True
print(type({}), bool({}), bool({'a': 1, 'b': 2}))
# <class 'dict'> False True
print(type(set()), bool(set()), bool({1, 2}))
# <class 'set'> False True
【总结】确定bool(X)
的值是 True
还是 False
,就看 X
是不是空,空的话就是 False
,不空的话就是 True
。
- 对于数值变量,
0
,0.0
都可认为是空的。 - 对于容器变量,里面没元素就是空的。
获取信息类型
type(object)
获取类型信息isinstance(object, classinfo)
判断一个对象是否是一个已知的类型。
注:
type()
不会认为子类是一种父类类型,不考虑继承关系。isinstance()
会认为子类是一种父类类型,考虑继承关系。
如果要判断两个类型是否相同推荐使用 isinstance()
。
print()函数
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
- 将对象以字符串表示的方式格式化输出到流文件对象file里。其中所有非关键字参数都按
str()
方式进行转换为字符串输出; - 关键字参数
sep
是实现分隔符,比如多个参数输出时想要输出中间的分隔字符; - 关键字参数
end
是输出结束时的字符,默认是换行符\n
; - 关键字参数
file
是定义流输出的文件,可以是标准的系统输出sys.stdout
,也可以重定义为别的文件; - 关键字参数
flush
是立即把内容输出到流文件,不作缓存。
【例】每次输出结束都用end
设置的参数&
结尾,并没有默认换行。
shoplist = ['apple', 'mango', 'carrot', 'banana']
print("This is printed with 'end='&''.")
for item in shoplist:
print(item, end='&')
print('hello world')
# This is printed with 'end='&''.
# apple&mango&carrot&banana&hello world
【例】item
值与'another string'
两个值之间用sep
设置的参数&
分割。由于end
参数没有设置,因此默认是输出解释后换行,即end
参数的默认值为\n
。
shoplist = ['apple', 'mango', 'carrot', 'banana']
print("This is printed with 'sep='&''.")
for item in shoplist:
print(item, 'another string', sep='&')
# This is printed with 'sep='&''.
# apple&another string
# mango&another string
# carrot&another string
# banana&another string
练习题
- 怎样对python中的代码进行注释?
#
表示单行注释,''' '''
和""" """
表示区间注释 - python有哪些运算符,这些运算符的优先级是怎样的?
- python 中
is
,is not
与==
,!=
的区别是什么? - python 中包含哪些数据类型?这些数据类型之间如何转换?