python 常用运算符
1-成员运算符 in 、not in
str = 'shxt'
for i in str :
if i == 'x'
break
print('当前字符为:%s' %i)
输出结果: 当前字符:s
当前字符:h
for i in str :
if i == 'x'
continue
print('当前字符为:%s' %i)
输出结果: 当前字符:s
当前字符:h
当前字符:t
for i in str :
if i == 'x'
pass
print('当前字符为:%s' %i)
输出结果: 当前字符:s
当前字符:h
当前字符:x
当前字符:t
2-倒序
str = 'shxt'
print str[::-1]#倒序
print str
输出结果: txhs
print str[0:5] 或者str[:5]
输出结果:shxt #前闭后开
3-运算符总结
逻辑运算符 & | and or
成员运算符 in not in
eg:
name_list = ["张三","李四","王五"]
name = input("请输入一个姓名")
if name in name_list
print('该同学%s在列表里' %name)
else: print('该同学%s不在列表里' %name)
身份运算符 is 其效果等同 == 目的都是判断引用型变量对应的值的地址空间是否为同一个 返回false 或者 true
eg : listOne = [1,2,3]
listTwo = [2,3,4]
print (listOne is listTwo)
输出结果:false
网友评论