Python运算符
1、python的算数运算符
a = 10 b = 3 c = 0 c = a + b print("1 - c 的值为:", c) c = a - b print("2 - c 的值为:", c) c = a * b print("3 - c 的值为:", c) c = a / b print("4 - c 的值为:", c) c = a % b print("5 - c 的值为:", c)
运行结果:
2、Python成员运算符
a = 10 b = 20 list = [1, 2, 3, 4, 5]; if (a in list): print("1 - 变量 a 在给定的列表中 list 中") else: print("1 - 变量 a 不在给定的列表中 list 中") if (b not in list): print("2 - 变量 b 不在给定的列表中 list 中") else: print("2 - 变量 b 在给定的列表中 list 中") # 修改变量 a 的值 a = 2 if (a in list): print("3 - 变量 a 在给定的列表中 list 中") else: print("3 - 变量 a 不在给定的列表中 list 中")
运行结果:
3、Python条件语句
flag = False name = "luren" if name == 'python': flag = True print("welcome boss") else: print(name)
运行结果:
luren
4、Python while循环
a = 1 while a < 10: print(a) a += 2
运行结果:
1、3、5、7、9
复杂点的例子:
numbers = [12,37,5,42,8,3] even = [] odd = [] while len(numbers) > 0 : number = numbers.pop() #pop()方法默认移除列表中最后一个元素 if(number % 2 == 0): even.append(number) else: odd.append(number) print(even) print(odd)
运行结果:
5、while循环
当前字母 : P
当前字母 : y
当前字母 : t
当前字母 : h
当前字母 : o
当前字母 : n
当前水果 : banana
当前水果 : apple
当前水果 : mango
Good bye!