python學習第一篇

來源:互聯網
上載者:User

標籤:mat   org   資料   字串   inf   shandong   hello   star   分享圖片   

一:python安裝及開發工具(pycharm)安裝

  1.在python官網(https://www.python.org/)安裝最新版的python檔案。

  2.下載pycharm最新版並安裝(www.jetbrains.com)

二:第一個python程式(Hello World)

  

print(‘hello World‘)

 

三:變數

  1.變數聲明:

1 name = ‘jone‘2 name2="jone"#字元變數要用‘xx‘或者"xx"3 num = 1
View Code

   2.輸出變數:

1 print(name,name2)2 #或者print(name+name2)

四:使用者互動式輸入

1 name = input(‘name:‘)2 print("Hello",name)

顯示name:提示使用者輸入資料,假設為‘jone’將jone賦值給name這個變數

五:格式化輸出

1.

 1 name = ‘jone‘ 2 age = ‘18‘ 3 address = ‘shandong‘ 4 info =‘‘‘ 5     ------‘‘‘+name+‘‘‘------- 6     name:‘‘‘+name+‘‘‘ 7     age:‘‘‘+age+‘‘‘ 8     address:‘‘‘+address+‘‘‘ 9     ‘‘‘10 print(info)

不推薦使用該方法且只能食醋出字串,不能輸出int型

2.

 1 name = ‘jone‘ 2 age = 18 3 address = ‘shandong‘ 4 info =‘‘‘ 5     ------%s------- 6     name:%s 7     age:%d 8     address:%s 9     ‘‘‘%(name,name,age,address)10 print(info)

每一個小括弧裡的值對應一個格式輸出的值

3.

 1 name = ‘jone‘ 2 age = 18 3 address = ‘shandong‘ 4 info =‘‘‘ 5     ------{name1}------- 6     name:{name1} 7     age:{age1} 8     address:{address1} 9     ‘‘‘.format(name1 = name,10         age1 = age,11         address1 = address)12 print(info)

4.

name = ‘jone‘age = 18address = ‘shandong‘info =‘‘‘    ------{0}-------    name:{0}    age:{1}    address:{2}    ‘‘‘.format(name,        age,        address)print(info)

 

六:if...語句
1 #猜年齡2 age_of_me = 203 age =int(input(‘guess age:‘))4 if age == age_of_me:5     print(‘get it‘)6 elif age > age_of_me:7     print(‘too old‘)8 else:print(‘too young‘)

 

七:while迴圈
 1 #猜年齡 2 age_of_me = 20 3 i = 0 4 while i<3: 5     age = int(input(‘guess age:‘)) 6     if age == age_of_me: 7         print(‘get it‘) 8         break 9     elif age > age_of_me:10         print(‘too old‘)11         i += 112         continue13     else:14         print(‘too young‘)15         i += 116 else:17     print(‘game over‘)    #只有迴圈正常結束才會運行,被berak中斷的不會執行

 

八:for迴圈:

1.

for i in range(10):    print(i)

補充:range函數range(1,2,3)        * range函數是一個用來建立算數級數序列的通用函數,返回一個[start, start + step, start + 2 * step, ...]結構的整
                                                                  數序列

  1.開始值,預設預設為0

  2.結束值,不可預設

  3.每個x個數返回一個

2.猜年齡

 1 #猜年齡 2 age_of_me = 20 3 i = 0 4 for i in range(3): 5     age = int(input(‘guess age:‘)) 6     if age == age_of_me: 7         print(‘get it‘) 8         break 9     elif age > age_of_me:10         print(‘too old‘)11         i += 112         continue13     else:14         print(‘too young‘)15         i += 116 else:17     print(‘game over‘)      #只有迴圈正常結束才會運行,被berak中斷的不會執行

 

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.