运算符
算术运算符 + - */
+:字符串的拼接,列表的合并会用到+号
赋值运算符 = 、+=、-=
a+=1: a=a+1
a-=1 : a= a-1
比较运算符 == 、 >、 >=、<、<= 、!=
运算结果:布尔值 True False
print (3==4)
print (3!=4)
print (3>4)
print (3<4)
print('get'=='GET') 结果为False,python区分大小写
print('get'.upper()=='GET') 结果为True
逻辑运算符 and 、or 、not (not>and>or)
运算结果为布尔值 True 、False
a =10
b=-5
c=0
print(a>0 and b>0) #且、与 左右两边同时满足才为真
print(a>0 or b>=) #或 左右两边只要一边满足即可
成员运算符 in 、not in
运算结果:布尔值 True 、False
str_1 ='hello'
print('o'in str_1)
print('1'in str_1)
t=[1,'hello',666,9.09]
print('h'in t) 结果为false
d={'name':'xiaocc','age':22}
print('22' in d) #结果为false ,原因是字典是根据key值去找
if语句
条件判断、根据条件去进行判断去进行处理
语法:(代码是从上到下执行的)
if 条件表达式:
代码
else:
代码
例子:
gift ='巧克力+鲜花'
if gift =='巧克力+鲜花':
print('表白成功')
else:
print('表白失败')
# if 、elif必须要加条件表达式 else 不能加任何条件
#非0、非空的数据为true 、为空和为0的数据表示为false
#只要返回值是True 或者是false 都可以作为if 或者else 后面的表达式
a = {}
if a:
print('吃饭')
else:
print('看电影')
#练习题:
从控制台获取一个成绩 根据成绩判断
如果>80优秀,>70良好,>=60及格,<60不及格,分数在0-100之间
score =input('请输入你的成绩:')
if score.isdigit():
score =int(score)
if score>100 or score<0:
print('数据范围在0-100')
elif score>80:
print('优秀')
elif score>70:
print('良好')
elif score>60:
print('及格')
else:
print('不及格')
else:
print('输入的数据类型有误')
for循环
s='python13'
L=[1,0.2,'xiaocc','番茄','土豆']
t =(1,5,6,'hi','ok')
d={'name':'xiaocc','age':18,'money':'10w'}
1)for item in s: #遍历,利用for循环,依次访问s里面的每一个元素,赋值给item这个变量
print(item)
2)for item in s:
a+=1
print('..........')
print('{0}hhah'.format(a))
for循环可以访问指定的数据里面的元素、还可以利用遍历去控制循环次数
练习:d={'name':'xiaocc','age':18,'money':'10w'},根据for循环,依次打印字典d里面的value值
for i in d.values:
print(i)
p =[[1,2,3],[4,5,6],[7,8,9]] 打印子列表的所有函数
for item i p: #[1,2,3],[4,5,6],[7,8,9]
for i in item: #[1,2,3]
print(i)