Python成長之路 第一篇 《Python基礎》

來源:互聯網
上載者:User

標籤:lin   編譯   utf8   put   style   bin   演算法   習題   使用   

1.python檔案命名

         - 尾碼名可以是任意的,但為規範便於識別,尾碼名應為 .py

2.兩種執行方式

     python解譯器   py檔案路徑      python   進入解譯器: 即時輸入並擷取到執行結果

3.解譯器路徑

         在Linux系統中應添加  #!/user/bin/env python    , windows系統中可不添加

4.編碼

         # -*- coding:utf8 -*-  (在python3中可不加,python只要出現中文頭部必須加)

         ascill  只能編譯英文

         unicode 萬國編碼,至少16個位元組

         utf-8   能用多少位元組表示就用多少表示

5.變數名

         由字母,數字,底線組成

         PS:  數字不能開頭,不能是關鍵字,最好不和python內建的東西重複

6.條件陳述式

          縮排用4個空格

          a.  input : 永遠等待,知道使用者輸入了值,就會將輸入的值賦值給一個東西

          b.  n1 = “liu”  單等號是賦值

               n1 ==“liu”  雙等號是比較

          c.  if 條件1:

                        pass

               elif 條件2:

                        pass

               elif 條件3:

                        pass

                print("end")

                每個條件依次判斷

             d. and 表示和,or 表示或

                 if n1 =="liu" and n2 =="xue"

                            print("OK")

                  else:

                             print("OK")

              PS :  pass代指空代碼,無意義,表示過

7.基礎資料型別 (Elementary Data Type)

              字串: n1 = "liu"   n2 = "xue"  引號之中的東西稱為字串

              數字: age=19       weight=60    height=178

              演算法:

                    字串: 

                                加法:

                                          n1 = "liu"   n2 = "xue"    n3 = n1+n2

                                          #n3=="liuxue"

                                 乘法:

                                           n1 = 1    n2 = n1*10

                      數字:

                                           n1 = 5     n2 = 2

                                           n3 = n1+n2

                                           n3 = n1-n2

                                           n3 = n1*n2

                                           n3 = n1 / n2

                                           n3 = n1 % n2  (取餘)

                                           n3 = n1 ** n2 (乘方)

8.迴圈

          死迴圈

          while 1==1:

                   print("OK")

9.練習題

          1.使用while迴圈輸入 1 2 3 4 5 6     8 9 10

                 

count = 1while count <= 10 :    num = count    if count == 7 :        pass    else:        print(num)    count += 1

            2.求1-100的所有數的和

     

n = 1s = 0while n <= 100:    s = s + n    n += 1print(s)

             3、輸出 1-100 內的所有奇數

count = 1while count <= 100 :    if count%2 == 0:        pass    else:        print(count)    count += 1

              4、輸出 1-100 內的所有偶數

count = 1while count <= 100 :    if count%2 == 0:        print(count)    else:        pass    count += 1

              5、求1-2+3-4+5 ... 99的所有數的和

n = 1s = 0while n < 100:    num = n % 2    if num == 0:        s = s - n    else:        s = s + n    n = n + 1print(s)

              6、使用者登陸(三次機會重試)

count = 0while count < 3:    name1 = input("name:")    passwd1 = input("passwd:")    count += 1

 

                                          

 

 

 

    

Python成長之路 第一篇 《Python基礎》

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.