人生苦短,我用Python 第一篇,python第一篇

來源:互聯網
上載者:User

人生苦短,我用Python 第一篇,python第一篇

 

 

一、變數名

  自訂變數名只能用 數字、字母、底線組成。

  使用變數前,需對其先賦值

   注意:1.變數名不能以數字開頭;

        2.不能是關鍵字;例如:'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec',

             'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise',

             'return', 'try', 'while', 'with', 'yield'

             3.最好不要和Python中內建的東西重複;另外在寫代碼時兩個單詞最好用底線串連,如 use_name;

關於注釋:單行注釋    #.............內容..................

     多行注釋    """...........內容.............."""

 

二、條件陳述式        1.if基本語句

    if 條件:
      內部代碼塊       #內部塊代碼不能與 if 頂齊,一般留四個空格,即一個Tab鍵
      內部代碼塊
    else:
      ...

      print('....')
例如:
    if 1 == 1:
      print("歡迎學習Python")
      print("歡迎來到部落格園")
     
    else:
      print("歡迎來到西南石油大學")

  2. if 支援嵌套  
    if 1 == 1:
      if 2 == 2:
        print("歡迎學習Python")
        print("歡迎來到部落格園")
      else:
        print("歡迎來到西南石油大學")
    else:
      print("歡迎")

      3. if elif

    inp = input('請輸入成績:')

    if inp == "90":
      print('優秀')
    elif inp == "80":
      print('良好')
    elif inp == "70":
      print(中等')
    else:
      print('及格')

    print('........end.......')

          pass:如果條件後面什麼都不執行,就可以用pass,Pass代指空代碼,無意義,僅僅用於表示代碼塊

例如
    if 1==1:
      pass
    else:
      print('sb')

三、基礎資料型別 (Elementary Data Type)1.字串

到目前為止我們所認知的字串就是引號內的所有東西,我們也把字串叫做文本,文本和數字是截然不同的。

如 讓兩個數字相加和讓兩個字元相加 

在數字加上引號再相加就變成了字串拼接。

在建立一個字串時,就要在字元兩邊加上引號,可以是單引號,雙引號,還可以是三重引號 ,但是不能混搭。 

如 name = "我是焦帥峰"
    name = '我是焦帥峰'
    name = """我是焦帥峰"""
    name = '''我是焦帥峰'''

例如:我們在輸入字串 Let's go,如果用單引號,就會報錯。

這個時候解決辦法有兩種:一是用雙引號

二是使用轉義符號(\)對字串中引號進行轉義

 

註:字串不能以 \ 結尾,在字串中 \ 表示字串還沒有結束,換行繼續的意思

2.算術操作符

   +      -        *      /      %      **      // 

前四個就是加減乘除

 % 表示取餘數

// 表示取商

**表示冪

 

3.while 迴圈

  while 條件:

    條件為真執行的操作

死迴圈

 while 1==1:

     執行操作

練習題

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

a=0while a<10:    a+=1    if a==7 :        pass    else:        print(a)

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

a=0s=0while a<100:    a+=1    s=s+aprint(s)

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

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

 

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

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

 

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

n = 1                    s = 0 # s是之前所有數的總和                    while n < 100:                        temp = n % 2                        if temp == 0:                            s = s - n                            else:                            s = s + n                                                n = n + 1                                        print(s)

 

6.使用者登入(三次重試機會)

 

i=0while i<3:    a = input("請輸入使用者名稱:")    if a=="1234":        print("登入成功")    else:        print("登入失敗")    i=i+1print("請稍後再試")

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

---恢複內容結束---

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.