python入門第一篇

來源:互聯網
上載者:User

標籤:nis   ffffff   bsp   輸入   ***   練習題   glob   練習   lambda   

day.1

a.Python基礎

  1.基礎

print(‘hello world‘)

  2.運行環境

#檔案內部:#    #!/usr/bin/env python#檔案名稱:#    ./2.py#Linux環境下才能運行##在py3環境不用加,py2頭部必須加==>環境內預設使用ASCII解釋,所以中文解釋不通## -*- coding:utf8 -*-

  3.變數==>可以理解為未知數x(大霧)

  注意:變數只能由字母、數字、底線組成。

      特例:1.不能以數字開頭

         2.python關鍵字不能使用:[‘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內建的東西重複***

  4.條件陳述式 ① if ==> else

 1 """格式如下 2 if(怎麼怎麼怎麼): 3     處理什麼什麼 4 else:(else可以省略) 5     處理什麼什麼 6 """ 7 #例如# 8 password = input(請輸入密碼:) 9 if(password = 123456):    #預設密碼為123456# #注意if和else後有個冒號#10     print("密碼正確")11 else:12     print("密碼錯誤")

         ② if ==> elif

1 user_name = input("請輸入登入賬戶")2 if("user_name == administrator"):3     print("歡迎您,超級管理員")4 elif("user_name == 禁花")5     print("歡迎您,禁花")6 elif("user_name == guest")7     print("歡迎您,來賓")8 else:9     print("請註冊新賬戶或者使用來賓賬戶登陸")

 

 

        ③ while ==> break & continue

1 """2 格式如下3 while True4     處理怎麼怎麼5     迴圈直到條件不為真。6 """7 #break強行打斷迴圈#8 #continue退出當前迴圈,繼續下次迴圈#

 

練習題:                                                                                                         

 

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

1 count = 12 while (count <= 10):3     if(count == 7):4         count = count + 15         continue6     else:7         print (count)8     count = count + 1
練習題1

 

 

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

1 count = 12 a1 = 03 while (count <= 100):4     a1 = a1 + count5     count = count + 16 print(a1)
練習題2

 

 

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

1 count = 12 while(count <= 100):3     if (count % 2 == 1):4         print(count)5     else:6         pass7     count = count + 1
練習題3

 

 

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

(參考練習題3)

 

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

1 count = 12 a1 = 03 while(count<100):4     if(count % 2 == 1):5         a1 = a1 + count6     else:7         a1 = a1 - count8     count = count + 19 print(a1)
練習題5

 

 

6、使用者登陸(三次機會重試)(輸對提示密碼正確、輸錯提示密碼錯誤、錯誤3次後提示明天再登陸)

 1 count = 4 2 while(count > 0): 3     passkey = input(‘請輸入密碼:‘) 4     if(passkey == "123456"): 5         print("密碼正確") 6         break 7     else: 8         print("密碼錯誤,剩餘次數為") 9         count = count - 110         print(count)11         if(count == 0):12             print("請明天重試")13         else:14             pass
練習題6

 

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.