Python Reviewing Notes

# how to get help with python built-in functions

1. dir

# here a code of python
a = 'strings'
dir(a)


Then all attrs or functions of a as a string will be presented.


2. help

# help funtion, to get the detailed explaination of a certain function
a = 'strings'
help(a.rsplit)

  1 #Help on built-in function rsplit:
  2 #
  3 #rsplit(...)
  4 #    S.rsplit([sep [,maxsplit]]) -> list of strings
  5 #
  6 #    Return a list of the words in the string S, using sep as the
  7 #    delimiter string, starting at the end of the string and working
  8 #    to the front.  If maxsplit is given, at most maxsplit splits are
  9 #    done. If sep is not specified or is None, any whitespace string
 10 #    is a separator.


3. .__doc__

a.rsplit.__doc__     #this way can also find the detailed info of rsplit function


# get map

b = ['ee', 'aa']
c = { i:b[i] for i in range(len(b)) }
# c --> {0: 'ee', 1:'aa'}


# search map with default keys.

# D = {1:'aa'; 2:'bb'}

value = D.get('x', 0) # if 'x' not in D, then return 0 instead
# 0

# or in this way
value = D['x'] if 'x' in D else 0
# 0

NOTE:

x if y else z  # if y is true, then return x, else return z

# decimal, precise type of number

from decimal import Decimal
Decimal('0.1') ** 4
# get Decimal('0.0001') , a precise number over float type

# fraction,  such as 1/7, 1/9

# fractions, such as 1/7
from fractions import Fraction
x = Fraction(1, 7)
y = Fraction(1, 7) ** 3
# y --> Fraction(1, 343)

# set

>>> a = set('helloworld')
>>> a
set(['e', 'd', 'h', 'l', 'o', 'r', 'w'])
>>> b = set(['e', 'd', 'a'])
>>> a|b
set(['a', 'r', 'e', 'd', 'w', 'h', 'l', 'o'])
>>> a&b
set(['e', 'd'])

# concatenation of strings

>>> title = 'w' 'ee'
>>> title
'wee'
>>> title = 'w' + 'ee'
>>> title
'wee'
>>> title = ('w'
... 'eee')
>>> title
'weee'

# escape string

>>> c = 'C:\py\codes\t\ex'
>>> c
'C:\\py\\codes\t\\ex'
#\p --> \\p, \t --> \t
#if no certain characters after '\', then '\' --> '\\'

# turn off escaping string function

>>> c = 'C:\py\codes\t\ex'
>>> print c
C:\py\codes	\ex
>>> c = r'C:\py\codes\t\ex'
>>> print c
C:\py\codes\t\ex

#ASCII

>>> print '\x61\x00\x61'  #'\x61' --> chr(97) --> 'a'
aa
>>> print '\x61\x08\x61'   # '\x08' --> backspace
a

# convert into int from strings

>>> int('0xff', 16)
255

#format strings with dict

>>> temp = '%(name)s is a guy, %(t)10.3f' % {'name': 'jk', 't': 1.7}
>>> temp
'jk is a guy,      1.700'











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值