骞没蕴 发表于 2018-8-9 13:17:54

python 1-12336621

  用正则给ip对应的mac分割
  # catipmac.txt
  192.168.4.5   121212452242
  192.168.4.2   242426231251
  192.168.4.3   242426231324
  #vim ipmac.txt
  1    192.168.4.5   12:12:12:45:22:42
  2    192.168.4.2   24:24:26:23:12:51
  3    192.168.4.3   24:24:26:23:13:24
  ~
  ~
  ~
  ~
  ~
  :%s /\(..\)\(..\)\(..\)\(..\)\(..\)\(..\)$/\1:\2:\3:\4:\5:\6/g
  python 1
  支持tab键补全
  # vim /usr/local/bin/tab.py
  1 import readline
  2 import rlcompleter
  3
  4 readline.parse_and_bind('tab: complete')
  # vim ~/.bash_profile
  14 PYTHONSTARTUP=/usr/local/bin/tab.py
  15 export PATH PYTHONSTARTUP
  # source ~/.bash_profile
  # python
  >>> p(两下tab)
  pass       pow(       print      print(   property(
  >>> if 3 > 0:   (顶语句)
  ...print 'no'(缩进)
  ...
  no
  >>> if 10 > 5:
  ...print "ok"
  ...print "yes"   (这两个都是上面的子语句,缩进要一样)
  ...
  ok
  yes
  >>> if'hello world!':
  ...print 2
  ...
  2
  print 后面字母一定要用引号
  >>> print "tom's a pet "
  tom's a pet
  >>> print 'hello world!'
  hello world!
  >>> print 'hello    world!'
  hello    world!
  >>> print 'hello, world!'
  hello, world!
  >>> print 'hello', 'world!'
  hello world!
  >>> print 'hello',   'world!'
  hello world!
  >>> print 'hello'+ 'world!'
  helloworld!
  >>> username=raw_input("username:")
  username:xixi
  >>> print username(输出显示)
  xixi
  >>> username    (输出的是变量字符)
  'xixi'
  >>> 3 + 4
  7
  >>> '3' + "4"
  '34'
  >>> number=raw_input("number:")   记住:raw_input里面都是字符
  number:10
  >>> number
  '10'
  >>> number + 5
  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  TypeError: cannot concatenate 'str' and 'int' objects
  >>> number + '5'
  '105'
  >>> int(number) + 5
  15
  >>> int(number)+5
  15
  >>> 3+5
  8
  # cat day01.py
  #-*- coding: utf8 -*-
  username=raw_input('username:')
  print 'Welocme', username
  print 'Welcome '+ username
  print '你好' +username
  # python day01.py
  username:tom
  Welocme tom
  Welcome tom
  你好tom
  ————————————————————————————————————————————
  (调用模块)
  #vim /root/star.py
  hi = 'hello world'
  def pstar():                           函数
  print '*' * 20
  #pyhton   (/root下)
  Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
   on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> import star
  >>> star.hi
  'hello world'
  >>> star.pstar()
  ********************
  # cat yy.py
  hi = 'hello '
  def ps():
  print 1 * 20
  # python
  >>> import yy
  >>> yy.hi
  'hello '
  >>> yy.ps()
  20
  ——————————————————————————————————————————————
  ___________________________________________________________________________________________________
  编辑模块的帮助说明:
  # cat star.py
  #-*- coding: utf8 -*-
  '''演示程序                                       (加三引号 帮助说明)
  这仅仅是一个包含变量和函数
  '''
  hi = 'hello world'
  def pstar():
  '用于打印20个星号'               (这是字符串,加引号)
  print '*' * 20
  # python
  >>> import star
  >>> help(star)
  Help on module star:
  NAME
  star - 演示程序
  FILE
  /root/python/star.py
  DESCRIPTION
  这仅仅是一个包含变量和函数
  FUNCTIONS
  pstar()
  用于打印20个星号
  DATA
  hi = 'hello world'
  ——————————————————————————————————————————————————————
  变量:第一个字符只能是大小写字母或下划线 后续字符只能是字母数字和下划线
  >>> print 100
  100
  >>> 100 + 5
  105
  >>> 100 /5
  20
  >>> spedd = 100
  >>> spedd+5
  105
  >>> a=10
  >>> a=a+10
  >>> a
  20
  >>> a +=1
  >>> a
  21
  >>> -a
  -21
  >>> a
  21
  >>> 5/3
  1
  >>> 5.0/3
  1.6666666666666667
  >>> 5%3
  2
  >>> 5**2
  25
  >>> 5**3
  125
  >>> 5//3.0
  1.0
  >>> 5/3.0
  1.6666666666666667
  >>> 3==3
  True
  >>> 3=3
  File "<stdin>", line 1
  SyntaxError: can't assign to literal
  >>> 3>=3
  True
  >>> 3>3
  False
  >>> a=10
  >>> if a=10 :
  File "<stdin>", line 1
  if a=10 :
  ^
  SyntaxError: invalid syntax
  >>> 3 !=4
  True
  >>> 10 <20 <30
  True
  >>> 10 <20 >15
  True
  >>> not 10*20 < 10+20 and 5 > 3
  True
  >>> not( 10*20 < 10+20) and 5 > 3
  True
  >>> not False and True
  True
  >>> True +1
  2
  >>> False +1
  1
  >>> 023
  19
  >>> 11
  11
  >>> 011
  9
  >>> 0x23
  35
  >>> 0b12
  File "<stdin>", line 1
  0b12
  ^
  SyntaxError: invalid syntax
  >>> 0b11
  3
  >>> 0b10
  2
  SyntaxError: EOL while scanning string literal
  >>> os.chmod('day01.py',755)
  >>> os.chmod('star.py',0755)
  # ll
  总用量 12
  -rw-r--r--. 1 root root 133 3月21 10:50 day01.py
  -rw-r--r--. 1 root root 172 3月21 11:29 star.py
  -rw-r--r--. 1 root root 372 3月21 11:29 star.pyc
  # ll
  总用量 12
  --wxrw--wt. 1 root root 133 3月21 10:50 day01.py
  -rwxr-xr-x. 1 root root 172 3月21 11:29 star.py
  -rw-r--r--. 1 root root 372 3月21 11:29 star.pyc
  >>> name='tom'
  >>> name
  'tom'
  >>> print name
  tom
  >>> print 'name'
  name
  >>> print 'hello %s' % name
  hello tom
  >>> print '%s is %s student' % (name,23)
  tom is 23 student
  >>> py_str='python'
  >>> py_str
  'y'
  >>> py_str
  'p'
  >>> len(py_str)
  6
  >>> py_str
  'n'
  >>> py_str[-1]
  'n'
  >>> py_str[-6]
  'p'
  >>> py_str多取一个
  'th'
  >>> py_str
  'thon'
  >>> py_str
  'thon'
  >>> py_str
  'thon'
  >>> py_str[-4:]
  'thon'
  >>> py_str[-1:]
  'n'
  >>> py_str[-2:]
  'on'
  >>> py_str[-3:]
  'hon'
  >>> py_str[:]
  'python'
  >>> py_str[::2]
  'pto'
  >>> py_str[:2]
  'py'
  >>> py_str
  'yhn'
  >>> py_str[::-1]
  'nohtyp'
  >>> py_str[:-1]
  'pytho'
  >>> py_str + 'is good'
  'pythonis good'
  >>> py_str *3
  'pythonpythonpython'
  >>> print 'py_str *3'
  py_str *3
  >>> print '%s *3'% py_str
  python *3
  >>> 't' in py_str
  True
  >>> 'th' in py_str
  True
  >>> 'to' in py_str
  False
  >>> 'to' notin py_str
  True
  >>> 'hello'.upper()
  'HELLO'
  >>> py_str.upper()
  'PYTHON'
  >>> py_str.isalpha()
  True
  >>> py_str
  'python'
  >>> print '+%s+' % ('-' * 48)
  +------------------------------------------------+
  >>> print "+%s+" % py_str.center(48)
  +                     python                     +
  >>> py_str.center(48)
  '                     python                     '
  >>> py_str.center(48,"#")
  '#####################python#####################'
  >>> py_str.ljust(48,'#')
  'python##########################################'
  >>> py_str.rjust(48,'#')
  '##########################################python'
  >>> '         hello world!    '.strip()
  'hello world!'
  >>> '         hello world!    '
  '         hello world!    '
  >>> '         hello world!    '.lstrip()
  'hello world!    '
  >>> '         hello world!    '.rstrip()
  '         hello world!'
  >>> >>> 't' in py_str
  File "<stdin>", line 1
  >>> 't' in py_str
  ^
  SyntaxError: invalid syntax
  >>> True
  True
  >>> >>> 'th' in py_str
  File "<stdin>", line 1
  >>> 'th' in py_str
  ^
  SyntaxError: invalid syntax
  >>> True
  True
  >>> >>> 'to' in py_str
  File "<stdin>", line 1
  >>> 'to' in py_str
  ^
  SyntaxError: invalid syntax
  >>> False
  False
  >>> >>> 'to' notin py_str
  File "<stdin>", line 1
  >>> 'to' notin py_str
  ^
  SyntaxError: invalid syntax
  >>> True
  True
  >>> print "+%s+" %('-' * 48)
  +------------------------------------------------+
  >>> print "py_str%spy_str" %('-' * 48)
  py_str------------------------------------------------py_str
  >>> print "%s%s%s" %(py_str,"-" * 48,py_str)
  python------------------------------------------------python
  列表
  >>> alist = ]
  >>> len(alist)
  4
  >>> 10 in alist
  True
  >>> 1 in alist
  False
  >>> alist
  ['bob', ]
  >>> alist
  ['bob']
  >>> alist
  
  >>> alist
  
  >>> alist=
  >>> alist[-1]=100(该最后一个值)
  >>> alist
  
  >>> alist.append(200)   (追加一个值)
  >>> alist
  
  >>> alist.insert(3,"tom")   (插入一个值)
  >>> alist
  
  >>> blist=
  >>> blist
  
  >>> blist.sort()
  >>> blist
  
  >>> blist.pop()
  453
  >>> blist
  
  元组
  >>> atuple=(10, 20, 30, 'tom', 40, 'bo', 100, 200)   (元组是用(),不可变)
  >>> atuple
  (10, 20, 30, 'tom', 40, 'bo', 100, 200)
  >>> atuple
  (30, 'tom')
  >>> atuple[-1]=300
  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  TypeError: 'tuple' object does not support item assignment
  >>> len(atuple)
  8
  字典
  >>> adict={'name': 'bob','age':22}
  >>> adict
  {'age': 22, 'name': 'bob'}
  >>> len(adict)
  2
  >>> 22 in adict
  False
  >>> 'age' in adict
  True
  >>> adict['age']
  22
  修改和增加
  >>> adict['age']=25
  >>> adict['age']
  25
  >>> adict['email']='bob@tedu.cn'
  >>> adict
  {'age': 25, 'name': 'bob', 'email': 'bob@tedu.cn'}
  >>> adict.get('phone')
  >>> print adict.get('phone')
  None
  >>> adict.get('age')
  25
  >>> alist
  
  >>> blist=alist
  >>> blist
  
  >>> blist.pop()
  200
  >>> blist
  
  >>> alist
  
  >>> clist=alist[::]
  >>> clist
  
  >>> clist.pop()
  100
  >>> clist
  
  >>> alist
  
  练习:
  # vim if1.py
  1 a=10
  2
  3 if a>5:
  4print 'yes'
  5 else:
  6   print 'error'
  # python if1.py
  yes
  非0打印非空打印
  >>> if -0.0:
  ...   print 'yes'
  ...
  ...
  >>> if 3:
  ...print 'yes'
  ...
  yes
  >>> if ' ':
  ...print 'yes'
  ...
  ...
  yes
  >>> if '':
  ...print 'yes'
  ...
  (一个脚本如果输入的用户不是bob,密码不是123456,就报错,或就对的)
  # vim if2.py
  1 username=raw_input('username:')
  2 password=raw_input('password:')
  3 if username=="bob" and password=="123456":
  4print 'login successful '
  5 else:
  6print 'login incorrect'
  # python if2.py
  username:bob
  password:123456
  login successful
  # vim if3.py
  1 import getpass
  2
  3 username=raw_input('username:')
  4 password=getpass.getpass('password:')
  5
  6 if username=="bob" and password=="123456":
  7print 'login successful '
  8 else:
  9print 'login incorrect'
  # python if3.py
  username:bob
  password:
  login successful
页: [1]
查看完整版本: python 1-12336621