python代碼練習(每天一小時)

來源:互聯網
上載者:User

標籤:splay   closed   div   列表   col   display   max   odi   sed   

 1 #-*-  coding :utf-8  -*- 2 print("#1.函數") 3 def my_abs(x): 4     if x>=0: 5         return x; 6     else: 7         return -x; 8 print(my_abs(-10)) 9 10 #遞迴函式11 print("#2.遞迴函式")12 def fact(n):13     if n==1:14         return 1;15     return n*fact(n-1);16 17 print(fact(4))18 19 #3.切片:取元素20 print("#3.切片")21 L = list(range(100))22 print(L[10:20]);23 24 #4.迭代:在Python中,迭代是通過for ... in來完成的25 print("#4.迭代")26 27 e={‘a‘:1,‘b‘:2,‘c‘:3,‘d‘:4}28 for key in e:29     print(key)30 print(‘------------------‘)31 for e in ‘abcd‘:32     print(e)33 34 #5.列表產生式:列表產生式即List Comprehensions,是Python內建的非常簡單卻強大的可以用來建立list的產生式。35 print("#5.列表產生式")36 37 L=[]38 for x in range(2,10):39     L.append(x*x)40 print(L)41 42 #6.產生器43 print("#6.產生器")44 L=[x*x for x in range(10)]45 print(L)46 print("---斐波拉契數列------")47 def fb(max):48     n,a,b=0,0,149     while  n<max:50         print(b)51         a,b=b,a+b52         n=n+153     return ‘done‘54 print(fb(5))55 56 #7.迭代器57 print("#7.迭代器")58 from collections import Iterable59 print(isinstance([], Iterable))
View Code

運行效果:

#1.函數10#2.遞迴函式24#3.切片[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]#4.迭代abcd------------------abcd#5.列表產生式[4, 9, 16, 25, 36, 49, 64, 81]#6.產生器[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]---斐波拉契數列------11235done#7.迭代器True

 

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.