Article Directory
- 1.if judge sentences
- 2. Logical Operators
- 3.if-else
- 4.if ... elif ... else ... statement format
- 5.while cycle
- 1. while loop format
- 2. Exercise: print cycle 5 times
- 3. Results:
- 4. Application of classical (1 to calculate the cumulative and 100 (100 and comprising 1))
- 5.while's nest
- 6.for cycle
- 7.break和continue
1.if judge sentences
1. if statement determines the basic format of presentation
-
if statement is used to judge, using the following format:
To determine if a condition:
the condition is true, things to do -
demo1: (demo in Chinese means: presentation, case)
age = 30
print("------if判断开始------")
if age >= 18:
print("我已经成年了")
print("------if判断结束------")
- operation result:
------if判断开始------
我已经成年了
------if判断结束------
demo2:
age = 16
print("------if判断开始------")
if age >= 18:
print("我已经成年了")
print("------if判断结束------")
operation result:
------if判断开始------
------if判断结束------
Small summary:
-
Two or more demo only variable value is not the same age, but lead to different results; able to see if the action determination statement:'s code block is executed only when certain conditions are met, otherwise the block statement is not executed.
-
Note: The code indentation is a tab key, or 4 spaces
2. Logical Operators
>>> ## 逻辑运算符
...
>>> # and : 左右表达式都为True,整个表达式结果才为 True
... if (1 == 1) and (10 > 3):
... print("条件成立!")
...
条件成立!
>>> # or : 左右表达式有一个为True,整个表达式结果就为 True
... if (1 == 2) or (10 > 3):
... print("条件成立!")
...
条件成立!
>>> # not:将右边表达式的逻辑结果取反,Ture变为False,False变为True
... if not (1 == 2):
... print("条件成立!")
...
条件成立!
>>>
About the use of logical operators extend the use of digital python:
# 逻辑运算符中,1直接表示true,0表示false,and运算符中要一直判断到最后的那个条件执行最后的条件,or只要一个成立,后边的就不会继续执行了
print(0 and True) # 0
print(1 and True) # True
print(0 and 1) # 0
print(0 or 1) # 1
print(1 and 0) # 0
print(1 and 10) #10
print(1 or 10) # 1
3.if-else
1. if-else format using
if conditions:
to do what the conditions are met 1
thing to do when the condition 2
do what the conditions are met 3
... (omitted) ...
the else:
things to do 1 does not meet the conditions
to do when the condition is not satisfied 2 things
to do when things do not satisfy the conditions 3
... (omitted) ...
demo1
chePiao = 1 # 用1代表有车票,0代表没有车票
if chePiao == 1:
print("有车票,可以上火车")
print("终于可以见到Ta了,美滋滋~~~")
else:
print("没有车票,不能上车")
print("亲爱的,那就下次见了")
Result 1: There are circumstances ticket
有车票,可以上火车
终于可以见到Ta了,美滋滋~~~
Results: No ticket situation
没有车票,不能上车
亲爱的,那就下次见了
4.if ... elif ... else ... statement format
1. elif function
elif use the following format:
if xxx1:
事情1
elif xxx2:
事情2
elif xxx3:
事情3
Description:
- When xxx1 meet, do 1 thing, then end the whole if
- When xxx1 not met, the judge xxx2, if xxx2 met, the implementation of 2 things, then the end of the whole if
- When xxx1 not satisfied, xxx2 not be satisfied if xxx3 met, then execute 3 things, then the end of the whole if
demo:
score = 77
if score>=90 and score<=100:
print('本次考试,等级为A')
elif score>=80 and score<90:
print('本次考试,等级为B')
elif score>=70 and score<80:
print('本次考试,等级为C')
elif score>=60 and score<70:
print('本次考试,等级为D')
elif score>=0 and score<60:
print('本次考试,等级为E')
2. Notes
- Can be used with else
if 性别为男性:
输出男性的体重
...
elif 性别为女性:
输出女性的体重
...
else:
第三种性别的体重
...
5.while cycle
1. while loop format
while 条件:
条件满足时,做的事情1
条件满足时,做的事情2
条件满足时,做的事情3
...(省略)...
2. Exercise: print cycle 5 times
i = 0
while i < 5:
print("当前是第%d次执行循环" % (i + 1))
print("i=%d" % i)
i+=1
3. Results:
当前是第1次执行循环
i=0
当前是第2次执行循环
i=1
当前是第3次执行循环
i=2
当前是第4次执行循环
i=3
当前是第5次执行循环
i=4
4. Application of classical (1 to calculate the cumulative and 100 (100 and comprising 1))
#encoding=utf-8
i = 1
sum = 0
while i <= 100:
sum = sum + i
i += 1
print("1~100的累积和为:%d" % sum)
5.while's nest
Requirements: Graphic prints as follows:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Reference Code:
i = 1
while i <= 5:
j = 1
while j <= 5:
print("*", end=" ")
j += 1
print()
i += 1
6.for cycle
In Python for loop can iterate any sequence of items, such as a list or a string and so on.
6.1 for loop format
for 临时变量 in 列表或者字符串等可迭代对象:
循环满足条件时执行的代码
demo1: a loop string
name = 'dakeai'
for x in name:
print(x)
Results are as follows:
d
a
k
e
a
i
demo2: an array cycle
for i in range(5):
print(i)
'''
效果等同于 while 循环的:
i = 0
while i < 5:
print(i)
i += 1
'''
Results are as follows:
0
1
2
3
4
7.break和continue
Termination cycle and the cycle continues. . .