python 编程中遇到的基本问题

来源:互联网 发布:怼的网络意思 编辑:程序博客网 时间:2024/05/18 01:24

1. 怎样查找函数的定义?

help(str.startswith)

Help on method_descriptor:

startswith(...)
    S.startswith(prefix[, start[, end]]) -> bool
    
    Return True if S starts with the specified prefix, False otherwise.
    With optional start, test S beginning at that position.
    With optional end, stop comparing S at that position.
    prefix can also be a tuple of strings to try.

1.1 使用的str 不是 string

1.2 使用import xxx 再后使用

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'requests' is not defined
>>> import requests
>>> help(requests)

2. 怎样判断字符串,list 是否为空 和None

>>> s = ''
>>> len(s)
0

>>> lst = []
>>> len(lst)
0


>>> lst is None
False
>>> lst11 is None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'lst11' is not defined
>>> type(None)
<type 'NoneType'>


字符串空,列表空等不能用None来判断,就是谁他们都不是None

Help on NoneType object:
class NoneType(object)
 |  Methods defined here:
 |  
 |  __hash__(...)
 |      x.__hash__() <==> hash(x)
 |  
 |  __repr__(...)
 |      x.__repr__() <==> repr(x)

None 的用途是什么,暂且理解为: void数据类型,可接收任何赋值


3. 关于 +

同种类型才能相加

如字符串: str1 = '1qaz' + '1qaxff'

列表: lst1 = [1,2] + [(1,3),(1,3)] + ['1qq']

但是,添加元素不能用: +, 两者不是同种类型

ls1 += '1qq', 意图时ls1 添加元素, 但实际结果如下:自动把字符串转换为list

>>> ls1 = ['1q']
>>> ls1 += '1qq'
>>> print ls1
['1q', '1', 'q', 'q']


4. python 函数的调用

参数不存在数据类型和变量之说,只有变量

def fun(s1, nub):不像 C

void fun(char* s1, int nub)


0 0
原创粉丝点击