1. 程式人生 > >《像個電腦科學家一樣思考Python》——學習筆記1

《像個電腦科學家一樣思考Python》——學習筆記1

學習是享受生活的樂趣——吾本

本書結構:

魔教口號!

 print("Hello,World!")

資料型別:整型,浮點型和字串,元組…還有什麼呢? 使用自帶函式type()查詢

>>> a=(912,2,3,3,2)
>>> type(a)
<class 'tuple'>
>>> type(a[1])
<class 'int'>
>>> type(a)
<class 'tuple'>
>>> 

字串的特殊加法和乘法,字元fdd's串s'fas'f本來是不可以做數學計算的,但是,如果你這樣子輸入的話……#——我用來表示輸出啦

print("重要的事情說三遍","我是一個大帥哥!"*3)
#——重要的事情說三遍 我是一個大帥哥!我是一個大帥哥!我是一個大帥哥!



def myfistfunction():
	print("重要的事情說三遍","我是一個大帥哥!"*3)
	print(type(myfistfunction))

	
>>> myfistfunction()
#——重要的事情說三遍 我是一個大帥哥!我是一個大帥哥!我是一個大帥哥!
#——<class 'function'>

函式,很重要的方法,改造世界的方法,並且是你也能夠成為馬雲那種可以複製的程式碼

形參和實參——形參就是函式定義的那個引數,實參是呼叫的時候給的引數,比如定義一個加法函式jiafa()

def jiafa(a,b):
	pass
	sum=a+b
	print("sum=%d"%sum)
	print("a+b=",a+b)
	return(a+b)

>>> jiafa(3,2)
sum=5
a+b= 5
5

a,b是形參,3,2是實參,懂不,形參是區域性引數,local只存在函式內部

練習1,函式也可以當形參,厲害了

def do_twice(f,s):
    f(s)
    f(s)

def print_spam(s):
    print('spam=%d,%d'%(s+3,s))
    
do_twice(print_spam,2)
#output——
spam=5,2
spam=5,2

def do_four(f,s):
    do_twice(f,s)
    do_twice(f,s)
    
do_four(print_spam,2)

#output——
spam=5,2
spam=5,2
spam=5,2
spam=5,2

還要改成do_four(),那就呼叫do_twice()咯

練習2,喪心病狂打印表格……不換行列印 end=' '

def draw():
    print('+',end=' ')
    print('-')
    print('+')
    
draw()
#################
+ -
+


#笨辦法

def draw():
    print('+ - - - - + - - - - +')
    print('|',end='         ')
    print('|',end='         ')
    print('|')
    print('|',end='         ')
    print('|',end='         ')
    print('|')
    print('|',end='         ')
    print('|',end='         ')
    print('|')
    print('|',end='         ')
    print('|',end='         ')
    print('|')
    print('+ - - - - + - - - - +')
    print('|',end='         ')
    print('|',end='         ')
    print('|')
    print('|',end='         ')
    print('|',end='         ')
    print('|')
    print('|',end='         ')
    print('|',end='         ')
    print('|')
    print('|',end='         ')
    print('|',end='         ')
    print('|')
    print('+ - - - - + - - - - +')
draw()

'''
+ - - - - + - - - - +
|         |         |
|         |         |
|         |         |
|         |         |
+ - - - - + - - - - +
|         |         |
|         |         |
|         |         |
|         |         |
+ - - - - + - - - - +
'''

呀呀呀呀呀呀,太激動了,花了1個小時寫成功了,多少行多少列都可以,手敲的,慢慢驗證,然後發現規律,yasi,後面知道這叫增量開發,牛逼

def re(f,num):
    for i in range(num):
        f()        
    
def add():
    print('+',end=' ')
    
def jian():
    print('-',end=' ')
    
def shu():    
    print('|',end='         ')

def calen_heng(c):
    for i in range(c):
        add()
        re(jian,4)
        if i==c-1:
            add()
            print()
            
def calen_shu(c):
    for i in range(c):
        shu()
        if i==c-1:
            shu()
            print()

    
    
def draw_form(r,c):#行和列r,c
    for i in range(r):
        pass
        calen_heng(c)
        calen_shu(c)
        calen_shu(c)
        calen_shu(c)
        if i==r-1:
            calen_heng(c)
    
    

draw_form(8,8)

厲不厲害? 

 遞迴,蛇吃自己的尾巴會怎麼樣,實際上函式呼叫自己是合法的

費馬定理!就玩一下哈哈哈

#費馬定理,如果n=2的情況,那就是找直角三角形的邊哈哈哈
for n in range(2,100):
    for a in range(1,100):
        for b in range(1,100):
            for c in range(1,100):
                cq=c**n
                if cq==a**n+b**n:
                    print('天哪,費馬錯了',a,b,c)
                else:
                    pass
                    #print('不',n,a,b,c)
'''
天哪,費馬錯了 3 4 5
天哪,費馬錯了 4 3 5
天哪,費馬錯了 5 12 13
天哪,費馬錯了 6 8 10
天哪,費馬錯了 7 24 25
天哪,費馬錯了 8 6 10
天哪,費馬錯了 8 15 17
天哪,費馬錯了 9 12 15
天哪,費馬錯了 9 40 41
天哪,費馬錯了 10 24 26
天哪,費馬錯了 11 60 61
天哪,費馬錯了 12 5 13
天哪,費馬錯了 12 9 15
天哪,費馬錯了 12 16 20
天哪,費馬錯了 12 35 37
天哪,費馬錯了 13 84 85
天哪,費馬錯了 14 48 50
天哪,費馬錯了 15 8 17
天哪,費馬錯了 15 20 25
天哪,費馬錯了 15 36 39
天哪,費馬錯了 16 12 20
天哪,費馬錯了 16 30 34
天哪,費馬錯了 16 63 65
天哪,費馬錯了 18 24 30
天哪,費馬錯了 18 80 82
天哪,費馬錯了 20 15 25
天哪,費馬錯了 20 21 29
天哪,費馬錯了 20 48 52
天哪,費馬錯了 21 20 29
天哪,費馬錯了 21 28 35
天哪,費馬錯了 21 72 75
天哪,費馬錯了 24 7 25
天哪,費馬錯了 24 10 26
天哪,費馬錯了 24 18 30
天哪,費馬錯了 24 32 40
天哪,費馬錯了 24 45 51
天哪,費馬錯了 24 70 74
天哪,費馬錯了 25 60 65
天哪,費馬錯了 27 36 45
天哪,費馬錯了 28 21 35
天哪,費馬錯了 28 45 53
天哪,費馬錯了 30 16 34
天哪,費馬錯了 30 40 50
天哪,費馬錯了 30 72 78
天哪,費馬錯了 32 24 40
天哪,費馬錯了 32 60 68
天哪,費馬錯了 33 44 55
天哪,費馬錯了 33 56 65
天哪,費馬錯了 35 12 37
天哪,費馬錯了 35 84 91
天哪,費馬錯了 36 15 39
天哪,費馬錯了 36 27 45
天哪,費馬錯了 36 48 60
天哪,費馬錯了 36 77 85
天哪,費馬錯了 39 52 65
天哪,費馬錯了 39 80 89
天哪,費馬錯了 40 9 41
天哪,費馬錯了 40 30 50
天哪,費馬錯了 40 42 58
天哪,費馬錯了 40 75 85
天哪,費馬錯了 42 40 58
天哪,費馬錯了 42 56 70
天哪,費馬錯了 44 33 55
天哪,費馬錯了 45 24 51
天哪,費馬錯了 45 28 53
天哪,費馬錯了 45 60 75
天哪,費馬錯了 48 14 50
天哪,費馬錯了 48 20 52
天哪,費馬錯了 48 36 60
天哪,費馬錯了 48 55 73
天哪,費馬錯了 48 64 80
天哪,費馬錯了 51 68 85
天哪,費馬錯了 52 39 65
天哪,費馬錯了 54 72 90
天哪,費馬錯了 55 48 73
天哪,費馬錯了 56 33 65
天哪,費馬錯了 56 42 70
天哪,費馬錯了 57 76 95
天哪,費馬錯了 60 11 61
天哪,費馬錯了 60 25 65
天哪,費馬錯了 60 32 68
天哪,費馬錯了 60 45 75
天哪,費馬錯了 60 63 87
天哪,費馬錯了 63 16 65
天哪,費馬錯了 63 60 87
天哪,費馬錯了 64 48 80
天哪,費馬錯了 65 72 97
天哪,費馬錯了 68 51 85
天哪,費馬錯了 70 24 74
天哪,費馬錯了 72 21 75
天哪,費馬錯了 72 30 78
天哪,費馬錯了 72 54 90
天哪,費馬錯了 72 65 97
天哪,費馬錯了 75 40 85
天哪,費馬錯了 76 57 95
天哪,費馬錯了 77 36 85
天哪,費馬錯了 80 18 82
天哪,費馬錯了 80 39 89
天哪,費馬錯了 84 13 85
天哪,費馬錯了 84 35 91
'''

00點04分,2018年9月23日,P53

第六章,有返回值的函式