python筆記2

來源:互聯網
上載者:User

顯示使用者輸入
user = raw_input('輸入名字:') //raw_input是內建的函數
====>輸入名字:root
print 'name is :',user===>name is :root

//int()字串轉數字
num = raw_input('')
1024
print '%d' % (int(num)*2)
2048

#為注釋

符號:+,-,*,/。基本符號
//浮點除法,%取餘,**乘方
比較子 >,>=,<,<=,==,!=
邏輯運算:and or not

數字類型:int,long,bool,float,complex
字串:pystr='Python'
iscool = 'is cool!'
pystr[0]===>'P' //取第一個字元
pystr[2:5]===>'tho'  //取2--5的索引
iscool[:2]==>'is' //取從頭到索引2
iscool[3:]==>'cool!'//取索引3到最後
iscool[-1]===>'!'  //取最後一位
pystr+iscool===>'Pythonis cool!'  //字元串連
pystr+' '+iscool==>'Python is cool!'
pystr*2===>'PythonPython'   //輸出2遍
'_'*20===>'____________________' //輸出20個_
pystr='''python
... is cool'''
pystr===>'python/nis cool'
print pystr====>python
is cool


數組和中繼資料
aList =[1,2,3,4]
aList===>[1,2,3,4]
aList[0]===>1
aList[2:]===>[3,4]
aList[:3]===>[1,2,3]
aList[1]=5
aList===>[1,5,3,4]

元組不能被修改
aTuple =('robots',77,93,'try')
aTuple===>('robots',77,93,'try')
aTuple[:3]==>('robots',77,93)
aTuple[1]=5    //出錯

字典   //類似map
aDict = {'host':'earch'}
aDict['port'] = 80
aDict===>{'host':'earch','port':80}
aDict.keys()==>['host','port']
aDict['host']===>'earch'

聯繫我們

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