入门python(1)

网上整理了些python入门文档供大家学习。
  1. 类型和运算
寻求帮助:
dir(obj) # 简单的列出对象obj所包含的方法名称,返回一个字符串列表
help(obj.func) # 查询obj.func的具体介绍和用法

eg:
>>> a = 'abc'
>>> dir(a)
['__add__', '__class__', ...]
>>> help(a.lower)
Help on built-in function lower:

lower(...)
    S.lower() -> string

    Return a copy of the string S converted to lowercase.
测试类型的三种方法,一般使用第三种
if type(D) == type({}): print("D is dict")
if type(D) == dict: print("D is dict")
if isinstance(D, dict): print("D is dict")

>>> D = {'a': 1}
>>> type(D)
<type 'dict'>
>>> if type(D) == type({}):
...     print 'D is dict'
...
D is dict
>>> if isinstance(D, dict):
...     print 'D is dict'
...
D is dict
Python数据类型
 "数字类型:int, float"
 "字符串类型:str, bytes"
 "元组:tuple"
 "集合:set"
 "布尔类型:True, False"
数字的表达式操作符
yield x                                      # 生成器函数发送协议
lambda args: expression                      # 生成匿名函数
x if y else z                                # 三元选择表达式
x and y, x or y, not x                       # 逻辑与、逻辑或、逻辑非
x in y, x not in y                           # 成员对象测试
x is y, x is not y                           # 对象实体测试
x<y, x<=y, x>y, x>=y, x==y, x!=y             # 大小比较,集合子集或超集值相等性操作符
1 < a < 3                                    # Python中允许连续比较
x|y, x&y, x^y                                # 位或、位与、位异或
x<<y, x>>y                                   # 位操作:x左移、右移y位
+, -, *, /, //, %, **                        # 真除法、floor除法:返回不大于真除法结果的整数值、取余、幂运算
-x, +x, ~x                                   # 一元减法、识别、按位求补(取反)
x[i], x[i:j:k], x(……)                        # 索引、分片、调用
int(3.14),  float(3)                         # 强制类型转换
repr和str显示格式的区别
repr格式:目标是准确性 eval(repr(object)) == object
str格式:目标是可读性
函数str() 用于将值转化为适于人阅读的形式,而repr() 转化为供解释器读取的形式,某对象没有适于人阅读的解释形式的话, str() 会返回与repr()
等同的值。因为__str__使用包含了对象的__repr__,当无法__str__时候,会调用__repr__。
集合set
set是一个无序不重复元素集
set支持union(联合), intersection(交), difference(差)
set支持x in set, len(set), for x in set
set不记录元素位置或者插入点, 因此不支持indexing, slicing, 或其它类序列的操作
eg:
>>> s1 = set([9, 9, 1, 1, 3, 5, 7])
>>> s1
set([7, 9, 3, 5, 1])
>>> s2 = set([1, 2, 3, 4, 5])
a = s1 | s2        s1.union(s2)            # s1 和 s2的并集
>>> s1 | s2
set([1, 2, 3, 4, 5, 7, 9])

b = s1 & s2        s1.intersection(s2)     # s1 和 s2的交集
>>> s1 & s2
set([1, 3, 5])

c = s1 – s2        s1.difference(s2)       # 求差集(项在s1中, 但不在s2中)
>>> s1 -s2
set([9, 7])

add和remove方法
>>> s1.add(100)
>>> s1
set([1, 3, 100, 5, 7, 9])
>>> s1.remove(100)
>>> s1
set([1, 3, 5, 7, 9])
内置str处理函数
str.upper()  str.lower()  str.swapcase()  str.capitalize()  str.title()     # 全部大写,全部小写、大小写转换,首字母大写,每个单词的首字母都大写
 str.find('t',start,end)       # 查找字符串,可以指定起始及结束位置搜索
 str.rfind('t')                # 从右边开始查找字符串
 str.count('t')                # 查找字符串出现的次数
 #上面所有方法都可用index代替,不同的是使用index查找不到会抛异常,而find返回-1
 str.replace('old','new')      # 替换函数,替换old为new,参数中可以指定maxReplaceTimes,即替换指定次数的old为new
 str.startswith('start')                 # 是否以start开头
 str.endswith('end')                     # 是否以end结尾
 str.isalnum()  str.isalpha()  str.isdigit()   str.islower()   str.isupper()  # 判断字符串是否全为字符和数字组成、字符、数字、小写、大写、
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值