Python基礎學習筆記 Day 4

2021-10-06 07:52:26 字數 1671 閱讀 3545

目錄

1、函式的基本使用:

1·1、函式簡介與功能:

1·2、函式的定義:

1·3、函式的使用:

2、函式的引數傳遞:

2.1、可選引數傳遞:

2.2、引數名稱傳遞:

2.3、函式的返回值:

3、變數作用域:

3.1、區域性變數:

3.2、區域性變數:

4、**復用: 

def 《函式名》(《引數列表》):

《函式體》

return 《返回值列表》

def factorial(n):

s = 1

for i in range(1,n+1):

s *= i

return s

《函式名》(《實際賦值引數列表》)
def factorial(n):

s = 1

for i in range(1,n+1):

s *= i

return s

msg = factorial(10)

print("10的階乘是:",msg)

def 《函式名》(《非可選引數列表》,《可選引數》 = 《預設值》):

《函式體》

return 《返回值列表》

def multiply(x,y = 10):

print(x*y)

multiply(99)

>>>990

multiply(99,2)

>>>198

《函式名》(《引數名》 = 《實際值》)

def multiply(x,y = 10):

print(x*y)

multiply(y = 2,x = 99)

>>>198

def multiply(x,y):

return x*y,x+y

print(multiply(x=99,y=2))

>>>(198, 101)

def manyret(x):

try:

if x>0:

return x+1

else:

return x-1

except:

return 0

print(manyret(x=2))

>>>3

def multiply(x,y):

z = x*y

return z

print(multiply(x=99,y=2))

>>>198

global 《全域性變數》
n = 2 #n是全域性變數

def multiply(x,y):

global n

return x*y*n #使用全域性變數n

print(multiply(99,2))

>>>396

Python基礎學習筆記 Day 1

注釋 python的注釋分兩種 1 單行注釋 使用 號對文字進行注釋 例 print hello world 列印字串 hello world 2 多行注釋 使用 一對三引號對文字進行注釋 例 python是一種跨平台的計算機程式語言。是乙個高層次的結合了解釋性 編譯性 互動性和物件導向的指令碼語言...

Python基礎學習筆記 Day 3

程式有三種基本結構組成 1.順序結構 2.分支結構 3.迴圈結構 if 條件 語句 判斷使用者輸入數字的奇偶性 s eval input 請輸入乙個整數 if s 2 0 print 這是乙個偶數!print 您輸入的數是 s 判斷使用者輸入的數字的特性 s eval input 請輸入乙個整數 i...

python學習筆記day1 基礎入門

區別一 python2預設的編碼方式是ascaii碼 解決方式在檔案首部加 encoding utf 8 python3預設的編碼方式是utf 8 變數 1.python變數必須由數字,字母,下劃線組成,且不能又數字開頭。2.不能是python中的關鍵字 and as assert break cl...