python 學習分享-基礎篇

來源:互聯網
上載者:User

標籤:標識   input   pre   名稱   conf   資料   注釋   span   blog   

1、python起手式

寫下第一個代碼,列印‘hello world’

print(‘hello world‘)

2、變數

變數是為了儲存資訊,在程式中被調用,標識資料名稱或類型。

變數定義的規則:

變數名只能是字母,數字或底線的任意組合

變數名的第一個字元不能是數字

關鍵字不能生命為變數

為了方便讀取等,變數命名一般都遵循變數本身的意義,如名字命名為name,年齡為age等等。

name = "Laay"  #字串類型stringage = 21  #int類型province = "shandong"NameOfTwinsGf = "hanmeimei"name_of_twins_gf = "hanmeimei"address = “shanghai”address1 = addressaddress = “beijing”   #address1 賦的是值,而不是變數,所以不變

3 注釋

#       單行注釋,最多不超過80個字元

‘‘‘ ‘‘‘   多行注釋

4 格式化字串

name = input("input your name:")age =int( input("input your age:"))job = input("input your job:")print("name is :",name)print("age is :",age)print("job is :",job)msg = ‘‘‘ infomation of user %s:-----------------------------name:  %sage :  %d job :  %s -----------------------------‘‘‘ % (name,name,age,job)print(msg)

5 if else條件

_username = ‘Laay’_password = ‘Laay’username = input(‘input your name :‘)password = input(‘input your password:‘)if _username == username and _password == password:    print(‘login..‘)else:    print(‘username or password is correct...‘)age = 22guess_num = int(input(‘input your num:‘))if guess_num == age :    print(‘congratulations! you got it.‘)elif guess_num >age:    print(‘think smaller!‘)else:    print(‘think bigger!‘)

 

6 while 迴圈

age = 22counter = 0 for i in range(10)    if counter<3:        guess_num = int(input(‘input your num:‘))        if guess_num == age :            print(‘congratulations! you got it.‘)            break        elif guess_num >age:                    print(‘think smaller!‘)        else:           print(‘think bigger!‘)    else:        continue_confirm = input(‘Do you want to contnue because you are stupid:‘)        if countinue_confirm == ‘y‘            counter   =  0            ciontinue        else:            print(‘bye‘)            break    count + = 1

 

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.