python基礎之一

來源:互聯網
上載者:User

標籤:you   china   while   條件   continue   語句   NPU   列表   方式   

1.基礎資料型別 (Elementary Data Type)

整型——int;字串——str; 列表——list; 元祖:tuple; 字典:dict

2.基礎資料型別 (Elementary Data Type)用法

#整型i = 2#字串a = "nihao"#列表,List是有序的,所以要訪問List的話顯然要通過序號來訪問,就像是數組的下標一樣,一樣是下標從0開始list = [1,‘aa‘,3]#元祖,Tuple可以看做是一種“不變”的List,訪問也是通過下標,用小括弧()表示yuanzu = (4,5.4,‘bb‘)#字典,Key-Value索引值對,用{}表示d = {‘a‘: 32, ‘b‘:43}#輸出顯示print(i)print(a)print(list)print(list[1])print(yuanzu)print(d)print(d[‘a‘])

3.if 迴圈使用

if 用法三:

----------------普通結構-----------------------

 if 條件:

    if語句塊

else:      else語句
--------多條件----------
if 條件:    語句塊elif  條件2:    語句塊elif  條件3:    語句塊else:
    else語句

--------if條件嵌套使用(執行個體)-------name = input("請輸入資訊:")
if  name.endswith(‘hello‘):    if name.startswith(‘china‘):        print ‘welcome to {}‘.format(name)    elif name.startswith(‘japan‘):        print ‘say you {}‘.format(name)    else:        print ‘輸入有誤,重新輸入‘else:    print  ‘遊戲結束---->‘

4.for迴圈使用

fruits=[‘banana‘,‘apple‘,‘mango‘]for fruit in fruits:    print("當前字母:"+fruit)

  

#另外一種執行遍曆的方式是通過索引fruits=[‘orange‘,‘apple‘,‘banana‘]for index in range(len(fruits)):    print("當前水果:"+fruits[index])

  

for num in range(10,20):    for in range(1,num):     if num%i =0:     j= num/i     print(‘%d 等於 %d * %d‘ % (num,i,j))     break    else     print(num+"是一個質數")

  

5.while迴圈使用

count = 0while count < 5:   print(count+"小於5")   count = count+1else:   print(count+"不是小於5的數字")

6.break 和continue 使用 (一般用於迴圈中)

i = 1while i<10:  i+=1  if i%2 = 0   #非雙數時跳過迴圈     continue;  print(i)  #列印出雙數  i = 1 while 1:  #判斷條件為1,表示必定成立    i+=1 print(i)  #列印出1~10 if i > 10: #如果大於10則退出迴圈    break 

  

 


 

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.