Basis for python (if, while, for)

A, if the comparison operator

Operators description Examples
== Check the value of two operands are equal, if it is True, the contrary is false (1 == 1) to True
! = Check the value of two operands are equal, if not equal True, equal to false (1! = 2) is True
> Check whether the value is greater than the right to the left, if it is True, the contrary is false The (2> 1) is True
< Check the value is less than the left to the right, if it is True, the contrary is false Such as (5 <2) to False
>= Check whether the value is greater than or equal value to the left to the right, if it is True, the contrary is false The (2> = 2) to True
<= Check whether the value is less than equal to the value on the right to the left, if it is True, the contrary is false (1 <= 5) is True

1.1 (Example a):

Basis for python (if, while, for)

1.2 (Example II):

Basis for python (if, while, for)

Two, if logical operators

逻辑运算符:not、and、or
or:if判断的多个条件只要有一个那么久认为条件成立
and:if判断多个条件都得成立才会认为是真,只要有一个不成立那么就是假
not:取反,真变假,假变真

2.1 (Example a, or usage):

Suppose a couple need a loan, as long as a person of 2 people then you can go to the bank. Here is the code to achieve
Basis for python (if, while, for)

2.2 (Example Two, and Usage)

Or the couple went back to bank loans but this requires both husband and wife must go before they can loan. Here is the code to achieve
NOTE: sublime can not run input so here replaced pycharm (personal feeling sublime font prettiest)
Founded:
Basis for python (if, while, for)
not established:
Basis for python (if, while, for)

2.3 (three cases, not usage)

注解:注意这里的if判断条件有()需要先计算()里的内容,也就是说a>0并且a<=50这个条件计算完本来是对的,但是前面还有一个not运算符,所以应该变为a<=0(或许你会疑惑为什么有一个等于号,因为是取反啊)并且a>50所以最终不成立。重点:not是取反、取反、取反!!!

Basis for python (if, while, for)

Three, if the branch if-elif

if判断多个条件的时候就需要用到elif,当然不用elif也可以实现,那为什么还要有elif?

Suppose you want to determine the score 60-70 judged d, 70-80 judged c, 80-90 to b, 90-100 is a, then you do not need so elif write. Codes are as follows

例一(写多个if):
score = int(input('请输入一个分数:'))
if 100 >= score >=90:
    print("A")
if 90 > score >=80:
    print("B")
if 80> score >= 70:
    print("C")
if 60> score >= 0:
    print("D")
if score <0 or score >100:
    print("输入错误!")

例二(if接else里面再接if):
score = int(input("请输入你的分数:"))
if 100 >= score >=90:
    print("A")
else:
    if 90 > score >=80:
        print("B")
    else:
        if 80 > score >=70:
            print("c")
        else:
            if 70 > score >=60:
                print("D")
            else:
                print("输入错误!")
例三(使用elif)
score = int(input('请输入一个分数:'))
if 100 >= score >=90:
    print("A")
elif 90 > score >=80:
    print("B")
elif 80> score >= 70:
    print("C")
elif 60> score >= 0:
    print("D")
else:
    print("输入错误!")
总结:
        if只可以判断单个条件,elif可以判断多个条件并且elif只要有一个成立那么其他的elif语句就不再执行,这就是我们为什么要使用elif的原因,你可能有一个疑问,我写多个if也可以实现啊,就像"例一"那样,请注意if的确也可以实现,但是你写多个if他是全部执行的,假如你有200行if判断语句第一个if就已经符合条件了,但是剩下的199个if也会执行,如果换成elif剩下的199个就不会执行,这个概念要知道。

Four, while circulation

Three processes 4.1 program execution

4.1.1 顺序执行
         程序从上向下执行
4.1.2 选择执行
        从上向下执行走到某一段有可能继续向下执行,也有可能执行另一段代码,继续向下走
4.1.3 循环执行
        程序执行到某一段的时候一直执行该段代码

4.2 while the use of loop

Code execution cycle can be achieved there are two kinds of statements, the following is the while loop

while 条件:
        条件满足时执行的代码

Examples of a 4.3while (10 Print Print "Naturally handsome")

i = 1

while i<=10:
    print("老夫最帅")

    i+=1

该代码最终的输出结果就是10次"老夫最帅",需要注意的是while后面的条件一定要是可结束的(例如该代码判断是i<=10,如果下面没有i+=1那么这段代码会一直执行下去)后面的条件也可以写成True代表一直循环下去,某些情况下我们会用到。

该代码内部执行逻辑,从上向下执行,i最初的值是1,再向下判断while后面的条件i<=10,
现在i是1所以条件满足,那么输出"老夫最帅",之后i+=1(这个就相当于i = i+1),现在i=2,
再次循环检测条件依然满足,第10次,i=10条件满足输出老夫最帅,i接着+1,再次断 条件,现在i=11不满足条件,什么也不执行

4.4 while循环中的break和countinue

break:终止本次循环
continue:终止本轮循环

例一(while循环中的break使用)

注意:不管是break还是countinue得作用范围都是一层,假如while循环里还嵌套一个while循环,在嵌套中的while循环使用break,只能中断里面的while而外面的while会继续执行,这里不再具体举例。

a = 1

while True:
    if a==10:
        break
    else:
        print(a)
    a+=1

Basis for python (if, while, for)
代码注释:a=1,开始循环,循环条件True,代表一直循环下去(没有遇到break的情况下),if判断如果a=10那么就break终止整个while循环,第一次开始执行a=1,输出a,然后a+1.执行到第10次,a=10,break终止不再执行

例二(while循环中的countinue使用)

a = 1

while a<=10:
    a+=1
    if a==5:
        continue
    else:
        print(a)

Basis for python (if, while, for)
代码注释:a=1,while后面的条件判断,条件通过,执行if判断假如现在a是5那么终止本次循环,注意这里并没有终止这个while而是a=5的时候不再做操作,回到头重新执行,也就是a+1=6,然后再次判断,这就是countinue,如果这里换成break那么整个循环就结束了。

五 if的嵌套使用(if的大概使用已经说完,这里详细说下if的嵌套使用)

    嵌套:字面意思可以理解为一个里面套着一个,那什么是if嵌套? 看下面的一个小例子。

以下就是if嵌套的一个框架

if 条件:
    print("条件满足")
    if  条件:
        条件满足执行的代码

5.1 if嵌套的具体使用

看一个进火车站的例子,先判断是否有票,如果有那么再安检(网上的一个例子,借鉴下^_^)

ticket = 1  #1表示有车票    2表示没有
kinfeLengrh = 1 #cm

#先判断是否有火车票,如果有代码继续执行
if ticket==1:
    print("车票正确,进入车站,等待安检")

    #判断刀的长度
    if kinfeLengrh <=1:
        print("可以坐车回家了")
    else:
        print("安检未通过,小刀被没收")
else:
        print("兄弟,帅如老夫也得买票的.......")

代码执行结果
Basis for python (if, while, for)

六 for循环

for循环用来遍历某一对象,通俗来说就是把循环中的值从第一个访问到最后一个

for循环语句结构如下:

for i in 循环的对象:
                做的操作

6.1 实例一(循环一个字符串)

可以看到一次吧name的值给打印出来了,需要注意一下for循环后面的i只是一个变量名而已,叫什么都可以
Basis for python (if, while, for)

这就是for循环最基础的一个使用,下一章写数据类型的时候会结合来使用,会详细说一下

6.2 for循环中的break和countinue

break for loop
Basis for python (if, while, for)
code comments: here and while loop is the same but the cycle of things have changed it, in order for the value of the name, the value is equal to j when the end of the break for the entire cycle

countinue for loop
Basis for python (if, while, for)

supplement:

上面有一个a+=1这个是复合赋值运算符
Operators description Examples
+= Addition assignment operator c + = a is equivalent to c = c + a
-= Subtraction assignment operator c- = a is equivalent to c = ca
*= Multiplication assignment operator C = C = A is equivalent to C A
/= Division assignment operator c / = a is equivalent to c = c / a
%= Remainder assignment operator c% = a is equivalent to c = c% a
**= Power assignment operator c ** = a is equivalent to c = c *** a
/= Division assignment operator c / = a is equivalent to c = a / c
//= Assignment operator take divisible c // = a is equivalent to c = a // c

Guess you like

Origin blog.51cto.com/12020040/2400892