python小執行個體(1),python執行個體

來源:互聯網
上載者:User

python小執行個體(1),python執行個體

1.

  題目:有四個數字:1、2、3、4,能組成多少個互不相同且無重複數位三位元?各是多少?

1 for i in range(1,5):2     for j in range(1,5):3         for k in range(1,5):4             if(i!=k)and(i!=j)and(j!=k):5                 print (i,j,k)

函數原型:range(start, end, scan):

參數含義:start:計數從start開始。預設是從0開始。例如range(5)等價於range(0, 5);

              end:技術到end結束,但不包括end.例如:range(0, 5) 是[0, 1, 2, 3, 4]沒有5

              scan:每次跳躍的間距,預設為1。例如:range(0, 5) 等價於 range(0, 5, 1)

 

1 for i in range(5):2     print (i) #這裡的i的取值是從0到4,並不會因為下面i+=2這個指派陳述式而改變它的值3     i+=2  4     print (i)5     print("end")

運行結果
python test01.py
0
2
end
1
3
end
2
4
end
3
5
end
4
6
end

 

1 i=02 while i<5:3     print(i)4     i+=25     print(i)6     print("end")

運行結果
 python test01.py
0
2
end
2
4
end
4
6
end

 

2.

  圓面積

1 def area_circle(r):2     PI=3.143     return PI*r**24 5 print(area_circle(4))6 print(area_circle(7))7 print(area_circle(9))

 

3.

  計算1加到100,(n^2+1)n從1到100

 1 def fun1(i): 2     return i 3  4 def fun2(i): 5     return i**2+1 6  7 def funt(start,end,fun): 8     sum=0 9     for i in range(start,end+1):10         sum+=fun(i)11     return sum12 13 print(funt(1,100,fun1))14 print(funt(1,100,fun2))

 

聯繫我們

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