python 编程中遇到的基本问题

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值