python3函數(shù),Python3函數(shù)傳參

python3中,定義一個函數(shù),遍歷字符串,使用print后調(diào)用函數(shù)會有None,換成return結(jié)果又不對?

第一個圖中,在執(zhí)行 print(bianli__str('sjsjsj'))這句時,首先執(zhí)行print括號中的內(nèi)容,即進入bianli__str函數(shù),執(zhí)行了函數(shù)中的print語句,打印的內(nèi)容是字符串的每一個字符,然后退出函數(shù),執(zhí)行print這個操作,print的內(nèi)容是你定義的函數(shù),這個函數(shù)本身是沒有值的,所以是None

我們一直強調(diào)成都網(wǎng)站建設、網(wǎng)站建設對于企業(yè)的重要性,如果您也覺得重要,那么就需要我們慎重對待,選擇一個安全靠譜的網(wǎng)站建設公司,企業(yè)網(wǎng)站我們建議是要么不做,要么就做好,讓網(wǎng)站能真正成為企業(yè)發(fā)展過程中的有力推手。專業(yè)網(wǎng)絡公司不一定是大公司,創(chuàng)新互聯(lián)作為專業(yè)的網(wǎng)絡公司選擇我們就是放心。

解決的方法就是調(diào)用函數(shù)的時候不用再print了,如下圖:

第二個圖中結(jié)果不對是因為return就會退出函數(shù),所以打完第一個字符后就退出了

python3函數(shù)定義的格式問題

-:標記返回函數(shù)注釋,信息作為.__annotations__屬性提供,__annotations__屬性是字典。鍵return是用于在箭頭后檢索值的鍵。但是在Python中3.5,PEP 484 - Type Hints附加了一個含義:-用于指示函數(shù)返回的類型。它似乎也將在未來版本中強制執(zhí)行。

eg:

def test() - [1, 2, 3, 4, 5]:

pass

print(test.__annotations__)

輸出:

{'return': [1, 2, 3, 4, 5]}

python3--內(nèi)置函數(shù)

python的常用內(nèi)置函數(shù)

1.abs() 函數(shù)返回數(shù)字的絕對值

abs(-40)=40

2. dict() 函數(shù)用于創(chuàng)建一個字典

dict()

{} ? ? ?#創(chuàng)建一個空字典類似于u={},字典的存取方式一般為key-value

例如u = {"username":"tom", ?"age":18}

3. help() 函數(shù)用于查看函數(shù)或模塊用途的詳細說明

help('math')查看math模塊的用處

a=[1,2,3,4]

help(a)查看列表list幫助信息

4.dir()獲得當前模塊的屬性列表

dir(help)

['__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

5.min() 方法返回給定參數(shù)的最小值 /參數(shù)可以為序列

a=? min(10,20,30,40)

a

10

6. next() 返回迭代器的下一個項目

it = iter([1, 2, 3, 4, 5])

next(it)

1

next(it)

2

7. id() 函數(shù)用于獲取對象的內(nèi)存地址

a=12

id(a)

1550569552

8.enumerate() 函數(shù)用于將一個可遍歷的數(shù)據(jù)對象(如列表、元組或字符串)組合為一個索引序列,同時列出數(shù)據(jù)和數(shù)據(jù)下標,一般用在 for 循環(huán)當中。

a=["tom","marry","leblan"]

list(enumerate(a))

[(0, 'tom'), (1, 'marry'), (2, 'leblan')]

9. oct() 函數(shù)將一個整數(shù)轉(zhuǎn)換成8進制字符串

oct(15)

'0o17'

oct(10)

'0o12'

10. bin() 返回一個整數(shù) int 或者長整數(shù) long int 的二進制表示

bin(10)

'0b1010'

bin(15)

'0b1111'

11.eval() 函數(shù)用來執(zhí)行一個字符串表達式,并返回表達式的值

eval('2+2')

4

12.int() 函數(shù)用于將一個字符串會數(shù)字轉(zhuǎn)換為整型

int(3)

3

int(3.6)

3

int(3.9)

3

int(4.0)

4

13.open() 函數(shù)用于打開一個文件,創(chuàng)建一個file對象,相關的方法才可以調(diào)用它進行讀寫

f=open('test.txt')

14.str() 函數(shù)將對象轉(zhuǎn)化為適于人閱讀的形式

str(3)

'3'

15. bool() 函數(shù)用于將給定參數(shù)轉(zhuǎn)換為布爾類型,如果沒有參數(shù),返回 False

bool()

False

bool(1)

True

bool(10)

True

bool(10.0)

True

16.isinstance() 函數(shù)來判斷一個對象是否是一個已知的類型

a=5

isinstance(a,int)

True

isinstance(a,str)

False

17. sum() 方法對系列進行求和計算

sum([1,2,3],5)

11

sum([1,2,3])

6

18. super() 函數(shù)用于調(diào)用下一個父類(超類)并返回該父類實例的方法。super 是用來解決多重繼承問題的,直接用類名調(diào)用父類方法

class ? User(object):

? def__init__(self):

class Persons(User):

? ? ? ? super(Persons,self).__init__()

19. float() 函數(shù)用于將整數(shù)和字符串轉(zhuǎn)換成浮點數(shù)

float(1)

1.0

float(10)

10.0

20. iter() 函數(shù)用來生成迭代器

a=[1,2,3,4,5,6]

iter(a)

for i in iter(a):

... ? ? ? ? print(i)

...

1

2

3

4

5

6

21.tuple 函數(shù)將列表轉(zhuǎn)換為元組

a=[1,2,3,4,5,6]

tuple(a)

(1, 2, 3, 4, 5, 6)

22.len() 方法返回對象(字符、列表、元組等)長度或項目個數(shù)

s = "playbasketball"

len(s)

14

a=[1,2,3,4,5,6]

len(a)

6

23. property() 函數(shù)的作用是在新式類中返回屬性值

class User(object):

?def __init__(self,name):

? ? ? ? ? self.name = name

def get_name(self):

? ? ? ? ? return self.get_name

@property

?def name(self):

? ? ? ? ?return self_name

24.type() 函數(shù)返回對象的類型

25.list() 方法用于將元組轉(zhuǎn)換為列表

b=(1,2,3,4,5,6)

list(b)

[1, 2, 3, 4, 5, 6]

26.range() 函數(shù)可創(chuàng)建一個整數(shù)列表,一般用在 for 循環(huán)中

range(10)

range(0, 10)

range(10,20)

range(10, 20)

27. getattr() 函數(shù)用于返回一個對象屬性值

class w(object):

... ? ? ? ? ? ? s=5

...

a = w()

getattr(a,'s')

5

28. complex() 函數(shù)用于創(chuàng)建一個復數(shù)或者轉(zhuǎn)化一個字符串或數(shù)為復數(shù)。如果第一個參數(shù)為字符串,則不需要指定第二個參數(shù)

complex(1,2)

(1+2j)

complex(1)

(1+0j)

complex("1")

(1+0j)

29.max() 方法返回給定參數(shù)的最大值,參數(shù)可以為序列

b=(1,2,3,4,5,6)

max(b)

6

30. round() 方法返回浮點數(shù)x的四舍五入值

round(10.56)

11

round(10.45)

10

round(10.45,1)

10.4

round(10.56,1)

10.6

round(10.565,2)

10.56

31. delattr 函數(shù)用于刪除屬性

class Num(object):

...? ? a=1

...? ? b=2

...? ? c=3.

.. print1 = Num()

print('a=',print1.a)

a= 1

print('b=',print1.b)

b= 2

print('c=',print1.c)

c= 3

delattr(Num,'b')

print('b=',print1.b)

Traceback (most recent call last):? File "", line 1, inAttributeError: 'Num' object has no attribute 'b'

32. hash() 用于獲取取一個對象(字符串或者數(shù)值等)的哈希值

hash(2)

2

hash("tom")

-1675102375494872622

33. set() 函數(shù)創(chuàng)建一個無序不重復元素集,可進行關系測試,刪除重復數(shù)據(jù),還可以計算交集、差集、并集等。

a= set("tom")

b = set("marrt")

a,b

({'t', 'm', 'o'}, {'m', 't', 'a', 'r'})

ab#交集

{'t', 'm'}

a|b#并集

{'t', 'm', 'r', 'o', 'a'}

a-b#差集

{'o'}

python3 函數(shù) 用 try except

def?silly_function(a):

try:

print(int(a)?+?a)

except?ValueError:

print("Hmmm...I?can't?use?that?value")

except?TypeError:

print("Your?input?is?invalid!")

silly_function(2)

Python3之字符串格式化format函數(shù)詳解(上)

概述

在Python3中,字符串格式化操作通過format()方法或者f'string'實現(xiàn)。而相比于老版的字符串格式化方式,format()方法擁有更多的功能,操作起來更加方便,可讀性也更強。該函數(shù)將字符串當成一個模板,通過傳入的參數(shù)進行格式化,并且使用大括號{}作為特殊字符代替%。

位置設定

默認位置

不指定格式化位置,按照默認順序格式化

示例結(jié)果:

設置位置

設置數(shù)字順序指定格式化的位置

示例結(jié)果:

設置關鍵字

設置關鍵字指定格式化的內(nèi)容

示例結(jié)果:

參數(shù)傳遞

我們可以傳入各種類型參數(shù)格式化字符串,即不限于字符串變量或數(shù)字等。

元組傳參

利用元組傳參,傳參形式 *tuple

示例結(jié)果:

字典傳參

示例結(jié)果:

列表傳參

示例結(jié)果:

python3 有多少內(nèi)置函數(shù)

我剛剛數(shù)了下Python3.x一共有153個內(nèi)置函數(shù)

具體如下:

['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError', 'Exception', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'WindowsError', 'ZeroDivisionError', '_', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']

本文名稱:python3函數(shù),Python3函數(shù)傳參
標題來源:http://www.muchs.cn/article4/hsseoe.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App設計、手機網(wǎng)站建設、App開發(fā)、靜態(tài)網(wǎng)站、定制開發(fā)、用戶體驗

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作