首页 > 代码库 > python学习之路---编程风格规范

python学习之路---编程风格规范

1、导入模块:

import os
from os import system
import SocketServer as SS  --别名
2、使用Tab键:
[root@cheeron python]# cat tab.py
#python startup file
import sys
import readline
import rlcompleter
import os
#tab completion
readline.parse_and_bind(‘tab: complete‘)
#history file
histfile = os.path.join(os.environ[‘HOME‘], ‘.pythonhistory‘)
#try:
#       readline.read_history_file(histfile)
#except IOError:
#       pass
3、格式化输出:
#!/usr/bin/env python
this_year=2015
name=raw_input(‘what is your name: ‘)
age=int(raw_input(‘how old are you? ‘))
sex=raw_input(‘how old are you? ‘)
dep=raw_input(‘how old are you? ‘)
messages = ‘‘‘ information of the company staff:
        Name:%s
        Age :%s
        Gender :%s
        Dep :%s
        ‘‘‘ % (name,age,sex,dep)
print messages
 
 
4、标准化输出:
  1 #!/usr/bin/env python
  2 import sys
  3 user = ‘cheeron‘
  4 times=0
  5 while True:
  6   if times<3:
  7     name = raw_input(‘what is your name: ‘).strip()
  8     if len(name) == 0:
  9         print ‘name is not empty,please input a name‘
 10         continue
 11     elif user == name:
 12         pass
 13     else:
 14         print "%s is not right,please sign in agin!" % name
 15         times+=1
 16         continue
 17     break
 18   else:
 19     sys.exit()
 20 age = int(raw_input(‘how old are you? ‘))
 21 sex = raw_input(‘Please input your gender: ‘)
 22 dep = raw_input(‘Please input your job: ‘)
 23 print "welcome to sign in",name
 
 1 #!/usr/bin/env python
  2 import sys
  3 user = ‘cheeron‘
  4 times=0
  5 while True:
  6     name = raw_input(‘what is your name: ‘).strip()
  7     if len(name) == 0:
  8         print ‘name is not empty,please input a name‘
  9         continue
 10     for i in range(3):
 11         name = raw_input(‘what is your name: ‘).strip()
 12         if user == name:
 13             pass
 14         else:
 15             print "%s is not right,please sign in agin!" % name
 16             continue
 17         break
 18     else:
 19         print "has login 3 times,exit"
 20         sys.exit()
 21     break
 22 age = int(raw_input(‘how old are you? ‘))
 23 sex = raw_input(‘Please input your gender: ‘)
 24 dep = raw_input(‘Please input your job: ‘)
 25 print "welcome to sign in",name

python学习之路---编程风格规范