While loop
结构:while 条件: 循环语句
Dead loop
while True: 没有break语句情况下
Terminating loops
First, change the condition (the concept of the sign bit): The flag is usually used as the mark bit by default.
flag = Truenum = 0while flag: print(num) num = num + 1 if num < 10: flag = False
Second, break terminates the loop: jump out of the loop body where it is located.
num = 0while True: print(num) num = num + 1 if num < 10: break
Calculate 1 + 2 + 3 .... 100 results (three ways) 1
result = 0count = 1while True: result += count count += 1 if count == 101: breakprint(result)
2
result = 0count = 1while count < 101: result += count count += 1print(result)
3
result = 0count = 1flag = Truewhile flag: result += count count += 1 if count == 101: flag = Falseprint(result)
Continue: Ends the loop and continues the next cycle.
while True: print(111) print(222) continue print(333)
The result is
111222111222...
While-else structure: If the while loop is interrupted by a break, the else code is not executed.
count = 1while count < 5: print(count) count = count + 1 if count == 3: breakelse: print(666)print(222)
The result is
12222
Application Scenarios:
- Verify user name password, re-enter this function requires a while loop.
- Infinite number of display pages, unlimited input ...
Formatted output
- To make a template, the parameters in some locations are dynamic, and like this, you need to format the output.
- Dynamic substitution of strings
name = input('请输入姓名:')age = int(input('请输入年龄:'))sex = input('请输入性别:')
% placeholder s data type is string D-digit
The first way:
msg = '你的名字是%s,你的年龄%d,你的性别%s' % (name,age,sex)print(msg)
The second way
msg = '你的名字是%(name1)s,你的年龄%(age1)d,你的性别%(sex1)s' % {'name1':name,'age1':age,'sex1':sex}print(msg)
Bug points in formatted output, only want to simply represent a%, should be expressed in percent
msg = '我叫%s,今年%d,我的学习进度1%%' % ('alex',28)print(msg)
Operator
== 比较的两边的值是否相等= 赋值运算!= 不等于+= 举例: count = count + 1 简写 count += 1-=*= : count = count * 5 简写 count *= 5/=**=//=...
Logical operators: And OR not precedence: () > Not > and > or first case, before and after conditions are comparison operations
print(1 < 2 or 3 > 1)print(1 < 2 and 3 > 4)print(1 < 2 and 3 > 4 or 8 < 6 and 9 > 5 or 7 > 2)print(1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8)print(1 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6)
In the second case, the conditions on both sides are numeric
'''x or y if x is True,return x'''print(1 or 2)print(1 and 2)print(5 or 2)print(5 and 2)print(0 or 2)print(-1 or 2)
conversion between INT and bool
- 0 The corresponding bool value is false, and not 0 is true.
- True 1, False 0
print(bool(100))print(bool(-1))print(bool(0))print(int(True))print(int(False))
Perverted interview questions: Thinking
print(1 > 2 or 3 and 4 < 6)print(2 or 3 and 4 < 6)
The answer is:
True2如果 x or y 语句中x为真,即为x,否则为y 真 or 真 --> x 真 or 假 --> x 假 or 真 --> y 假 or 假 --> y>>> 3 or 43>>> 3 or 03>>> 0 or 33>>> '' or 00如果 x and y 语句中x为假,即为x,否则为y 真 and 真 --> y 真 and 假 --> y 假 and 假 --> x 假 and 真 --> x
深层次理解:由于or的特性是只要一个为真就判定为真,即判断x为真后,不用再来判断y。若x为假后才会判定y,所以是结果为y。 这样的话程序就能以最小的步数来执行并得到结果。由于and的特性是只要一个为假就判定为假,即判断x为假后,不用再来判断y。若x为真后才会判定y,所以是结果为y。 这样的话程序就能以最小的步数来执行并得到结果。
Python Foundation II