Contact QQ: 970586718
Blog address: https://blog.csdn.net/m0_46521785
Select structure
multi-branch structure
if (条件A):
pass
elif (条件B):
pass
elif (条件C):
pass
elif ...:
pass
else:
pass
When writing a program that requires multiple branches, do not use multiple if substitutions, because the value of the variable may be changed in the first if and lead to the second if
Implementation process
When executing the if statement, the condition will be calculated first to determine whether the final result is True or False. If it is True, it will enter the program block of the if statement, otherwise the next condition will be judged.
Conditional nesting
You can write another if statement for each level of if statement, as long as the indentation is ensured. In Python, indentation is a hierarchical relationship.
a = 1
b = 2
if a>0:
if b<0:
print(111)
else:
print(222)
else:
if b<0:
print(666)
else:
print(777)
Practice questions
-
Judgment of multiples
程序的功能是判断一个整数是否能被7或11整除,若能被7或11整除,则输出"Yes",否则输出"No",请补充程序。
-
Prime number judgment
输入一个数字判断它是不是素数(质数)
-
Input the integers x, y, z and determine the relationship between x 3+y 3+z^3 and 1000
输入整数x,y,z,若x^3+y^3+z^3>1000,则输出x^3+y^3+z^3-1000的结果,否则输出三个数之和。 注:用eval()函数结合input()函数同时输入3个整数,输入数字时用逗号分隔
-
Marriage question
输入年龄和性别,判断你当前的个人情况! 男性小于30岁显示:young,30岁到36岁之间显示:marriageable age,大于36岁显示:old。 女性小于25岁显示:young,25岁到30岁之间显示:marriageable age,大于30岁显示:old。
Loop structure
for loop (known number of loops)
for i in x
x here is an iterable object. What is an iterable object? It means that it can represent each element in turn.
Common iterable objects include strings, tuples, lists, dictionaries, and sets
a = 'hzau'
for i in a:
print(i)
print('#'*30)
b = list(a) # 创建一个列表,同时学习一下,直接对字符串使用list强制转化的话,每一个字符都对应一个元素
print(b)
for i in b:
print(i)
range(a,b)
Range is an iterator. Just like a factory can produce products, an iterator can generate iterable objects.
The result it gets is a range object, and the constructed content is a pseudo-list of integers from a (if a is not written, it starts from 0 by default) to b (excluding b) (named by Teacher Hu Bin)
What does it mean? You will know by looking at the code below.
a = range(1,10)
print(a)
print(list(a)) # 看看a里到底包含什么东西
Through print(list(a)) we can simply understand that range(1,10) is equivalent to creating a list whose elements are 1-9
for i in range(1,10):
print(i)
for i in range(x)
It is often written as for i in range(len(x)). Why is it written like this? Because I write the index and the element in a one-to-one correspondence.
a = 'hzau'
for i in range(len(a)):
print(i,a[i])
What for i in x can do, for i in range(x) can do, so it is recommended to use for i in range(x), and vice versa is wrong.
(Think carefully about this sentence, just like what I want to say below: anything that the for loop can do, the while loop can do, but the opposite is wrong)
When are the two forms of for used?
It’s still an old saying: It depends on the topic, and the specific situation should be analyzed in detail.
You can write the code first and find that for i in x cannot be written at the end, then you can change it to for i in range(x)
If you don’t want to use your brain, just write it in the form of for i in range(x), because for i in range(x) can do everything that for i in x can do.
while loop (known loop condition)
The structure is as follows
while 条件:
循环体
When the condition is met, the contents of the loop body will be executed, for example
x=0
while x<=5:
print(x)
x+=1
#结果为0 1 2 3 4 5
infinite loop
When the judgment condition of while is always True, the program will always be in the loop and cannot execute the following code, as follows
while 1:
print(666)
However, when a judgment condition is added to the infinite loop, it can play a good role. For example, we use a while loop instead of a for loop.
mystr = 'hzau'
index = 0
while True: # 创建死循环
if index >= len(mystr): # 设置跳出循环的条件
break
print(mystr[index])
index += 1
Some students can see that this is actually equivalent to
mystr = 'hzau'
index = 0
while index >= len(mystr): # 创建死循环
print(mystr[index])
index += 1
Yes, that's right, but obviously the first piece of code requires less brains and simpler logic
continue与break
- continue is to end the current cycle, no longer execute the code behind the loop body of the current cycle, and continue to execute the next cycle.
- break ends the entire loop
for i in range(3):
if i==1:
continue
print(i,666)
print('###########################')
for i in range(3):
if i==1:
break
print(i,777)
This is often described during exams: break is used to jump out of the innermost loop. After breaking out of the loop, the program continues execution after the loop code. [This sentence is correct]
break can only jump out of the innermost loop, which means it can only end the loop structure closest to it. Why is it said closest? Because loops can also be nested, you will understand if you look at the following example.
for i in range(3):
for j in range(3):
if j==1:
break
print('i=%d,j=%d'%(i,j))
So what should you do if you want to jump out of the outer layer? You can use the method of creating a flag. What is creating a flag? It is to create a condition and judge how to write code later based on the condition status. This function can only be understood.
flag = 1
for i in range(3):
if not flag: # 这么写为什么是对的呢?起到的作用和 if flag==0 一样吗?
break
for j in range(3):
if j==1:
flag = 0
break
print('i=%d,j=%d'%(i,j))
Choice between for loop and while loop
Many students are very confused about whether to use a for loop or a while loop in the program. In fact, just remember one sentence and it will be OK.
- Use for loop to know the number of loops
- Only know the loop condition using while loop
What the for loop can do, the while loop can do, but the opposite is wrong (experience it carefully)
In fact, we often use for loops when writing programs. In comparison, while loops are used less often.
for-else loop and while-else loop
有个了解就可以了,知道有这么种形式,考的不是很多
After executing the loop body, if it is not broken, the content in else will continue to be executed.
x=0
while x<=5:
print(x)
x+=1
else:
print('进入else啦')
###########################
x=0
while x<=5:
if x==1:
print(i)
break
print(x)
x+=1
else:
print('进入else啦')
Practice questions
Before doing the question, think about whether you are looping a constant number of times or knowing the looping conditions, and then decide which looping method to use.
-
arithmetic
已知y=1+1/3+1/5+…+1/2n-1: 求y<3时的最大n值以及最大n值对应的y值(y值保留小数点后2位)。
-
Xiaoli swimming
小玉开心的在游泳,可是她很快难过的发现,自己的力气不够,游泳好累哦。已知小玉第一步能游2米,可是随着越来越累,力气越来越小,她接下来的每一步都只能游出上一步距离的98%。 现在小玉想知道,如果要游到距离x米的地方,她需要游多少步呢。请你编程解决这个问题。
-
Monkey eats peach
猴子摘下若干个桃子,第一天吃了桃子的一半多一个,以后每天吃了前一天剩下的一半多一个,到第n天吃以前发现只剩下一个桃子。 编写程序实现:据输入的天数计算并输出猴子共摘了几个桃子
-
Sum
输入一个数,判断哪三个正整数的和与这个数字相等
Thinking about the problem
-
What can represent True? Can 1 represent True? Is there any other way of representation?
-
Do you think there is something wrong with the code below?
a = 1 print(a+True)
-
When to use for loop and when to use while loop
-
Do you remember clearly the functions of continue and break?
-
Have you ever heard of for-else loops and while-else loop structures?
-
Write code to solve the following problems
输入一个数字n,返回n以内的斐波那契数列。 注:斐波拉契数列由0和1开始,之后的数就是由之前的两数相加而得出:0, 1, 1, 2, 3, 5, 8, 13, 21 【样例输入】(输入大于3的整数值) input a number please:200 【样例输出】 1,1,2,3,5,8,13,21,34,55,89,144,