【易错题集】Python掌握不牢固的点

字符串


>>> a=9
>>> b=2
>>> a%b
1
>>> a//b
4
>>> x=[1,2,3,4,5,6,7,8,9]
>>> x.pop()
9
>>> x=[1,2,3,4,5,6,7,8,9]
>>> x.pop(-1)
9
>>> int('3.14')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '3.14'
>>> print(str(2*int(float('3.6')))*3)
666

字符串类型的’3.6’经过 float()转化成数值类型的3.6;
int()函数对数值类型的3.6进行向下取整得到数值3;
接着执行 2\3等于6,6经过 str()转化成字符串’6’;
最后,字符串 ‘6’\3可以得到字符串的’666’。

关系型运算符优先级高到低为:not and or

x = True
y = False
z = False

if x or y and z:
    print("yes")
else:
    print("no")

参数为*的函数

def greetPerson(*name):
    print('Hello', name)

greetPerson('Student', 'Google')

结果
*name是不定长参数,可以接收0到多个值,在调用函数时参数会自动组装为一个tuple,所以输出结果为Hello (‘Student’, ‘Google’)。

猜你喜欢

转载自blog.csdn.net/weixin_44991673/article/details/109896516