標籤:python基礎 class rom 記錄 res min div 接收 用法
---- PygLatin
1
介紹函數的調用,就是直接函數名
def square(n):
squared = n**2
print "%d squared is %d." % (n, squared)
return squared
square(10)
2
介紹函數可以使用參數的傳遞
把函數的參數改為base,exponent。調用函數傳入37和4
def power(base,exponent):
result = base**exponent
print "%d to the power of %d is %d." % (base, exponent, result)
power(37,4)
3
介紹*的用法,比如我們傳入一個字串,那麼我們可以使用*name接收,然後可以利用name來輸出。不一定使用name,可以是任何的名字
def favorite_actors(*name):
print "Your favorite actors are:" , name
favorite_actors("Michael Palin", "John Cleese", "Graham Chapman")
4
介紹函數體裡面還可以調用另外的函數
def fun_one(n):
return n * 5
def fun_two(m):
return fun_one(m) + 7
5
介紹Python裡面可以匯入很多的系統的模組,就像
c語言
的include
1 匯入math模組,import math
Ps:假設我們沒有匯入math模組的時候,那麼執行print sqrt(25)的時候會報錯
2 執行print math.sqrt(25),加了一個math說明調用系統的庫函數
import math
print math.sqrt(25)
6
Impor
t
我們還可以只是單獨的匯入模組裡面的方法
從math模組裡面值匯入sqrt函數
from math import sqrt
print sqrt(25)
7
Import 使用from module import *
來表示從模組裡面匯入所有的函數
8
Import from module import *方法的缺點就是,如果我們匯入了很多的模組,那麼可能導致出現相同的函數名,因此我們最好是使用import module,然後使用module.name
1 介紹了第一個函數max(),比如max(1,2,3)返回的是3 (min函數是類似的)
2 max()函數的參數是一個數組,返回數組中的最大值
3 使用max函數來得到一個數組的最大值
maximum = max(4,0,-3,78)
print maximum
9
介紹
絕對值
函數abs()
,
返回的值永遠是正數,比如abs(-5)返回的是5
absolute = abs(-42)
print absolute
10
介紹type函數的使用,type函數返回的是當前這種資料的類型,比如int , float等
Python基礎點記錄2