1.if elif else a = int(input(“age:”)) if a > 35: print(“老了”) elif a < 18: print(“大好的时光”) else: print(“emo”) 输出 age:22 emo age:55 老了 age:10 大好的时光
2.比较运算符 a == b a等于b a < b a > b a >= b a <= b a != b a不等于b a is b a和b是同一个对象 a is not b a和b不是同一个对象 a in b a是容器b的成员 a not in b a不是容器b的成员
3.assert 断言 在测试是使用,assert设置一个条件,如果没有满足则报错 x = int(input(“a:”)) b = x assert 10 < x < 100 , ‘错误’ 没在范围内时 输出 a:9 Traceback (most recent call last): File “E:\文本\python\test\test2.py”, line 142, in assert 10 < x < 100 , ‘错误’ AssertionError: 错误
- while循环 比如用while打印一个1到100
a = 1 while a <= 100: print(a) a += 1 输出 1 2 3 4 5 6 7 …
5.for循环 打印一个1到100 for a in range(1,101): print(a) 输出 1 2 3 4 5 …
6.跳出循环 break 找出小于10000的最大平方值 from math import sqrt for a in range(9999,0,-1): b = sqrt(a) if b == int(b): print(a) break 输出 9801
7.while true/break while True: word = input(“please enter a word:”) if not word:break print(“The word was”, word) 输出 please enter a word:desk The word was desk please enter a word:
8.while中的else from math import sqrt for a in range(10,0,-1): b = sqrt(a) if b == int(b): print(a) break else: print(“没有找到!”) 输出 没有找到! 9
9.pass 什么都不做 a =input(“输入一句话:”) if a ==“你好!”: print(“你也好!”) elif a !=“你好”: pass elif a == “你好坏”: print(“我好爱”) 如果去掉pass 这段代码不能运行 哎 一看就知,不说了
|