ghong6003的个人空间 https://blog.eetop.cn/1430538 [收藏] [复制] [分享] [RSS]

空间首页 动态 记录 日志 相册 主题 分享 留言板 个人资料

日志

Some Methods in Python

已有 479 次阅读| 2018-2-14 11:13 |个人分类:Python|系统分类:芯片设计

1. The methods of Variable:
String Methods:
    (1) name.endswith('a')   # if the variable ends with sring 'a'
    (2) name.startswith('a')
    (3) name.lower()             # Change the variable to lower string
    (4) name.upper()
    (5) name.isspace()         # if the variable is space
    (6) name.strip()               # Delete start and end space of the string
    (7) list = '1+2+3+4'.split('+')    # Split the string with specified symbol
Dict Methods:
    (1) d.values            
    (2) d.keys                         # Return the key list
    (3) d.items                        # Return the dict as tuple
          for key, value in d.items():
    (4) d.setdefault('name',[]) # Set [] to key 'name'
          d.setdefault('Lie',[]).append(my_sister)
    (5) d.get('key') # Get the value of key
    (6) Zip Function # P:79
          1) names = ['Guo','Hong','Li','Qing',]
               ages = ['5','10','15','20',]
               dict_v = zip(names,ages)
    (7) enumerate Function: P80
          1) for index,string in enumerate(strings):
                   if 'xxx' in string:
                       strings[index]='Replaced'
List Methods:
    (1)

Assert:
    (1) assert 0 < age <100 'Error return info' # P:76 like:if not condition: crash program
   
2. Two function like methods
    (1) input() and raw_input()
    (2) range(1,10) and xrange(1,10)

3. If sentence and while and for:
    (1) [x*x for x in range(10)] #82
    (2) [x*x for x in range(10) if x % 3 == 0]
    (3) [(x,y) for x in range(3) for y in range(4)]

4. Function
    (1) parameter in function  #P98
        1) def hello(greeting='Hello',name='Hong'): # Parameter with default value
             hello(name='Qing',greeting='Go') # Parameter don't care the location
         2) def print_params(title,*params): #P99, all other values save to params as tuple
         3) def print_params_2(x,y,z=3,*pospar,**keypar): #100, parse the parameter with key words.
              print_params_2(1,2,3,5,6,7,foo=1,bar=2)
    (2) Function lib
         1) map    #P109
 2) filter(fund,seq) #P109
         3) reduce(). #P110
         4) from random import choice; x=choice('string',[1,2,3]) #随机选择函数
         5)repr(x). #P115,返回数据类型的原生态
6) fun.__doc__ # display the help message which in the first line with ''' docInfo''''
5. module
import hello
hello.__name__
hello.__all__ #p173 only import the wanna function
  hello.__doc__ # display the doc which define at the 
hello.__file__ #Got the full path of this module
                              
10. Useful Module and it's functions
(1)import sys
sys.path.append('/home/hguo1/scripts/python') # must follow with full path
sys.path.appendusr('~/scripts/python')
(2)import sys, pprint
pprint.pprint(sys.path) # Get all the path which python used.
                dir(sys) # display all the function in sys module
sys.__all__ # all the use fun import through: import sys
help(copy.copy) # get the help info
copy.__file__ # get the full path of this module
(3)import webbrowser
 webbrowser.open('http://www.baidu.com')
(4)fileinput

点赞

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

  • 关注TA
  • 加好友
  • 联系TA
  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 1

    关注
  • 3

    粉丝
  • 0

    好友
  • 2

    获赞
  • 1

    评论
  • 1539

    访问数
关闭

站长推荐 上一条 /1 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-3-29 16:11 , Processed in 0.026989 second(s), 15 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部