python 隨機數使用方法,推導以及字串,雙色球小程式執行個體,python隨機數

來源:互聯網
上載者:User

python 隨機數使用方法,推導以及字串,雙色球小程式執行個體,python隨機數

如下所示:

#隨機數的使用import random #匯入randomrandom.randint(0,9)#制定隨機數0到9i=random.sample(range(1,34),6)#輸出6個隨機數,範圍是1到34i.sort()#排序方法,排序時更改原數組,無傳回值sorted(i)#排序函數,排序時不影響原數組,產生新的排序後資料print('----------------用上述的隨機數做一個雙色球---------------------')sj=random.sample(range(1,34),6)print(sj,random.randint(1,17))#if多值判斷n=4if n in (4,5,6,8):  print('元組裡面有',n,'這個值')else:  print('元組裡面不存在',n,'這個值')#利用while迴圈輸出1dao100的和i=0he=0while i<100 :  he=he+i  i+=1print(he)#list集合使用冒泡排序從大道小排序li=[9,5,1,8,7,3]for i in range(0,len(li)):  for j in range(0,len(li)-i-1):    if li[j]<li[j+1]:      li[j],li[j+1]=li[j+1],li[j]print(li)#字串的使用(方法,與參數)zf=('我的家鄉在河南!')print(zf[4])#跟據下標輸出,帶第4個print(zf[4:])#下標後面的字元print(zf[2:5])#兩下標之間的字元print(zf[1:2]*2)#輸出的字串*2倍print(zf.index('南'))#輸出指定自負串的下標print(zf.replace('的','得',1))#替換字串,只替換一個print(zf.endswith('南'),5,6)#最後的是否存在print('家' in zf)#字元是否存在其中#列表print('---------------列表------------------')lb=['abc','123']print(lb.append('456'))#添加單個lb+=['a','b']#添加多個lb.remove('a')#刪去一個o=lb.pop(0)#指定下標輸出lb.insert(2,'125')#指定下標修改lb.clear()#刪除所有print(lb)print('--------推導--------')td=['Hi','Hello','word','python','Height','wall','Hight']print([s for s in td if s.startswith('H')])

以上這篇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.