1. 程式人生 > >if判斷+while迴圈

if判斷+while迴圈

1、常量​

     ​純大寫字母命名常量名,如:AGE_OF_OLDBOY=18

     常量的值是可以改變的,如:AGE_OF_OLDBOY=19

2、基本運算子

    (1)、算術運算​

        +、-、*、/​

​        print(10/3)      精度float

        print(10//3)    取整

        print(10**2)​    平方

    (2)、邏輯運算​

        a、增量賦值

             age=18

             age+=1

             print(age)​

        b、交叉賦值

           x =10

            y=20  ​

           # temp=x,

            # x=y

            # y=temp​

            x,y=y,x​

            print(x,y)​

        ​​c、鏈式賦值

             # x=10​

             # y=x

             # z=y​  ​

             x=y=z=10​

        d、解壓賦值

             l=[1.1,2.2,3.3,4.4,5.5]​

             a,b,c,d,e=l​

             print( a,b,c,d,e)​

              a,b,*_=l                       #取前兩個值

               print(a,b)

                a,*_,b=l                     #取第一個和最後一個值

                 print(a,b)​

                 *_,a,b=l                     #取最後兩個值

                  print(a,b)​

四、流程控制​ 1、if 判斷​

    (1)if 條件:

                    程式碼1

                    程式碼2

                     程式碼3

                      ......​

    (2)if 條件:

                    程式碼1

                    程式碼2

                     程式碼3

                      ......​

              else:

                    程式碼1

                    程式碼2

                     程式碼3

                      ......​

    (3)​if  條件1:

                    程式碼1

                    程式碼2

                     程式碼3

                      ......​

          elif  條件2:

                    程式碼1

                    程式碼2

                     程式碼3

                      ......​

       elif  條件3:

                    程式碼1

                    程式碼2

                     程式碼3

                      ......​

              else:

                    程式碼1

                    程式碼2

                     程式碼3

                      ......​

 2、while迴圈:條件迴圈

        while條件:

                   程式碼1

                    程式碼2​

                    程式碼3

                    .........​

    結束迴圈的方式​

        (1)while+break:break代表結束本層迴圈​

        (2)改條件的方式tag=True​

                while Tag:

                         程式碼塊

                          break

eg:​​

                tag=True

​                 while tag:

                         i = 0

​                         while i<<span se-mark="1">3:

                                 inp = input('請輸入年齡>>>')

                                 i = i + 1

​                                 if inp =='10':

                                 print('恭喜您猜對了')

                                 tag=False

                                 break

                         else:

                                 inp1=input('還想繼續玩嗎?y?>>')

                                 if inp1=='y':

                                 continue

        (3)while+continue:     continue​​表示結束本次迴圈,直接進入下一次

  while+else​結合

  while 條件:

             pass

    else​:

             print(‘else子程式碼塊只有在while迴圈沒有被break打斷的情況下才會執行’)​