Getting python basis for white Yo, Chapter II

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/luyue0830/article/details/93043539

Because I also began to learn from their current base, so we sent directly to the finishing notes come together so that we can learn from you, when I learned what extent will be issued, so do not give up ah! The front is missing something, I'll make up late, there are questions you can leave a message Ha, what I download and install packages made up in two days, and now there is no extra time


and if else, to pay attention to the following points elif statement:
. 1, else, elif into sub-blocks can not be used independently
2, an if statement may comprise a plurality of elif statements, but only one end else statement
else while in , for the role loop in
python, you can use the while and for loop else clause, it will be executed only after the end of the cycle,
if you use the break statement then block the else clause will be skipped. So pay attention to the else clause and break statements can not be used at the same time!


[Shining picture content must break out of their hands when practicing it, or remember]
if ... elif ... else statement

if the statement is executed only once
while loop as long as the judgment is true, would have been executed
Example: while loop, if not conditions, would have been circulating, endless loop.
Here Insert Picture Description
* Each cycle is decremented by one until i is reduced to 0, the end of the cycle just performed 100 times.
while loop
Here Insert Picture Description


true true, to false false [Boolean]
print (3 <5) Results: true
[calculated in print bracket is Boolean operations, the result is a Boolean value of the terminal appears]
only when the condition is true, IF, and after the while statement only We will continue to perform


Boolean operations in three ways:
1. to compare two values:
Example:
Here Insert Picture Descriptionjust enter the wrong password, it will execute the statement after else, and enter the correct password will not be executed else behind the statement
Here Insert Picture Description


2. Direct numerical computation to do:
false: False, 0, '', [], {}, None null values (these are the shell, which is empty, so are false)
true: True, except any number of 0 / float, 'string', {388}, {1: 'spring', 2: 'autumn'} (which are inherently things, it is really a)
BOOL () function View Analyzing data genuine
two compared values: six kinds comparison mode: ==, =,>, <,> =, <=!
Here Insert Picture Description


3. operation between Boolean value:
and, or, not, in, not in five kinds of operations

and是和,两个条件一定要符合条件
or是或,两个条件有一个符合条件就可以
例:这里用的是and,所以必须两个条件都要满足才能是Ture。(如果把and换成or,就会是真的True,因为or里面有一个符合就可以啦)
Here Insert Picture Description上面有四个值,因为a<b和c<d里面c<d是不正确的,所以是假的False,执行了就只显示False啦,如果我们把c<d改成c>d 那就会是True了,下图:
Here Insert Picture Description
not是翻转的意思
not True 为假
not False 为真

in 是判断一个元素是否在一堆数据之中。【not in】反之
例:print后面就是打印出执行后的结果,图片内有print语句,所以终端结果也是两个结果。
上面的写了list里面包含了1、2、3、4、5,然后a=1,第一条print就问了,a在list里面?下面第一个答案是True(真的),就是在的意思。第二条用的是not in 是反之的意思,就表示a=1,但是我要说反话,所以结果就出现了False。
Here Insert Picture Description
bool()函数查看数据判断真假(当你使用了bool()这个函数,返回到结果就只有两个选择,Ture or False)。
in 和 not in 可以用来判断字典{}中是否存在某个【键】(这个字典指这个符号{},列表是这个符号[],另一篇会说明的)
例:键和值是一体的,中间用“ :”号分隔,不是一体的就用逗号隔开了。
Here Insert Picture Description
这个图内包含3个键值,法国巴黎,日本东京,中国北京。
上面说了,in 和 not in 是判断字典内是否存在某个“键”的,所以a=法国,如果你写a=巴黎,结果就不是True真的了,结果就会是False假的,因为在这个字典内,巴黎属于“值”,而不是“键”。


循环里的4种新语句:

一、break语句(打破),用来结束循环,一般写作if…break
例:就像公式差不多,后面有例子,可以套用看一下
一定要注意格式。
Here Insert Picture Description


例:for循环代码
Here Insert Picture Description
【本来会循环5次,因为i==2的时候触发,因为都是从0开始算的,0,1,2,所以等于2的时候被break(打破)语句打断了,所以就结束了】所以终端显示了3遍。


例:while循环代码
Here Insert Picture Description
解析一下哈,不要嫌我墨迹……
第一行:首先定义了i是等于0的
第二行:用了while循环,写了i<5,不能超过5嘛
第三行:print终端打印“窗外的麻雀”
第四行:指每一次执行都要加一个1
第五行:当i加到等于3的时候就触发了这条语句(如果这个是等于5以上数字,拿结果也是最多不超过5遍的)
第六行:break结束了


例:
Here Insert Picture Description
开始解析了,如果有说错的地方留言告诉我哈,毕竟共同学习哈哈
第一行:用的是while True就代表一直循环,除非当结果为真的时候,才结束循环。 (while循环还记得吗,True真的)
第二行:打印“今天要还花呗了……”
第三行:t=input(‘今天发工资了吗?’)是提出的问题,是需要自己在终端进行回答的,如果不回答,就不会在进行下去了,再者回答的答案不是正确的,那就会继续循环问你,知道回答正确才可以呦(仔细看我图片上面的备注呀)
第四行:if t="发工资啦“,第三行定义了t=input今天发工资了吗,所以if t=”发工资啦“就是正确答案,回答了第三行的正确答案。如果你在终端没有回答正确就返回第1行重新执行命令问你了。
第五行:如果你在执行终端的时候回答了正确答案(就代表结果是真的),就会执行第5行的命令了,break结束循环。
第六行:当第五行执行成功后,代表回答对了问题才会最后执行最后一句今天发工资……


例:
Here Insert Picture Description
【用input() 函数获取到了一个数据。第3行:如果这个数据等于之前设定的密码,那么就第4行:结束循环。如果这个数据不等于之前设定的密码,那就回到第1行while True继续循环。】
Here Insert Picture Description
【看这个图和上一个有什么区别嘛?这个执行的结果,第一句请输入密码只执行了一次,即使你回答错误了,也没有循环的,回答错误时循环的代码是第三行呦,因为第一行不在while里面,所以不循环。】(一定要注意 格式)


二、continue语句【继续的意思,这个子句也是在循环内部使用的】
(当某个条件被满足的时候,触发continue语句,将跳过之后的代码,直接回到循环的开
它的写法:相当于公式把,套用。
Here Insert Picture Description
例:continue(继续)语句搭配for循环
Here Insert Picture Description
解析:
第一行:range决定了最终次数的数量,从0开始算的,0、1、2、3、4共5次
第二行:打印”开心每一天“
第三行:i= =3,代表到3的时候触发命令,也就是0、1、2、3,就是第四次的时候触发,所以看我的截图就知道了,绿色的框内只有一个”开心每一天“而没有了print(‘这句话在i等于3的时候打印不出来’)这句话呦!
第四行:就是当i==3的时候,返回到第一行开始执行,下面的print(‘这句话在i等于3的时候打印不出来’)这句话就不执行了
【continue语句【继续的意思,这个子句也是在循环内部使用的】
(当某个条件被满足的时候,触发continue语句,将跳过之后的代码,直接回到循环的开始)】
第五行:就是执行了。(第五遍的时候就是两句话了,只有第四遍的时候是一句话)


例:
Here Insert Picture Description
(这三个问题只有连续答对了,才会执行最后的那句”全部答对了“这句话,中间有一个回答错误就提前使用了continue提前循环开始,要返回到第一行从新提问并回答。)
仔细看第3行,这几个答案都用的”!=“不等于。所以当不等于这个答案的时候,判断为真,所以就循环到最开始重新提问。
三、pass语句(跳过的意思)
用法:注意格式,打出来执行试一下呦。
Here Insert Picture Description
意思是,当a>=80的时候就pass跳过,不执行print的语句了。如果输入的答案是小于80的,执行的时候就会打印出print后面的语句了。
Here Insert Picture DescriptionHere Insert Picture Description
四、else语句
公式:
Here Insert Picture Description
例:
Here Insert Picture Description
Here Insert Picture Description
四种循环语句的学习就结束了,下面来再带大家做个练习题吧。
例:
这款衣服的价格为200元,但是我需要你来猜这个衣服的价格是多少,猜对了提示“正确”,猜小了提示“太少了”,猜多了提示“太多了”
Here Insert Picture Description
在等于200时,下面多写了break结束循环,所以当答对的时候就结束循环,不再继续问了。如果答不对,会一直循环提问的。
那我们在加一个限制,就是你的回答即使错误,也只循环3次,当答错3次后,也不会在循环问你了。如果答对了,就会终止的,答对的时候不会循环3次的,只针对错误的时候。
Here Insert Picture Description
可以看到把while True去掉了,上面有说过的,代表一直循环,除非结果为真时,才会结束循环。但这次我们有3次的次数限制,所有不需要一直循环。
Here Insert Picture Description
In the for ... in ... the cycle can also be used break, under normal circumstances, three chances to run out after the end of the cycle, but if you guessed right the first time, also need to end the cycle. The former we can use range (3) to solve, which relied on the break statement.
for ... in ... else statement can be used with. Meaning that in the end ... for ... in loop, the else statement in the command. But if for ... in ... is because the break ended else statement in the command will not be executed.


Currently first here, waiting for me to update it. There do not understand can leave a message Ha.

Guess you like

Origin blog.csdn.net/luyue0830/article/details/93043539