设为首页 收藏本站
查看: 849|回复: 0

[经验分享] python 1-12336621

[复制链接]

尚未签到

发表于 2018-8-9 13:17:54 | 显示全部楼层 |阅读模式
  用正则给ip对应的mac分割
  [root@room1pc01 桌面]# cat  ipmac.txt
  192.168.4.5   121212452242
  192.168.4.2   242426231251
  192.168.4.3   242426231324
  [root@room1pc01 桌面]#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
  [root@room1pc01 python]# cat day01.py
  #-*- coding: utf8 -*-
  username=raw_input('username:')
  print 'Welocme', username
  print 'Welcome '+ username
  print '你好' +  username
  [root@room1pc01 python]# 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)
  [GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> import star
  >>> star.hi
  'hello world'
  >>> star.pstar()
  ********************
  [root@room1pc01 桌面]# cat yy.py
  hi = 'hello '
  def ps():
  print 1 * 20
  [root@room1pc01 桌面]# python
  >>> import yy
  >>> yy.hi
  'hello '
  >>> yy.ps()
  20
  ——————————————————————————————————————————————
  ___________________________________________________________________________________________________
  编辑模块的帮助说明:
  [root@room1pc01 python]# cat star.py
  #-*- coding: utf8 -*-
  '''演示程序                                       (加三引号 帮助说明)
  这仅仅是一个包含变量和函数
  '''
  hi = 'hello world'
  def pstar():
  '用于打印20个星号'               (这是字符串,加引号)
  print '*' * 20
  [root@room1pc01 python]# 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)
  [root@room1pc01 python]# 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
  [root@room1pc01 python]# 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[1]
  'y'
  >>> py_str[0]
  'p'
  >>> len(py_str)
  6
  >>> py_str[5]
  'n'
  >>> py_str[-1]
  'n'
  >>> py_str[-6]
  'p'
  >>> py_str[2:4]  多取一个
  'th'
  >>> py_str[2:6]
  'thon'
  >>> py_str[2:10]
  'thon'
  >>> py_str[2:]
  '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[1::2]
  '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' not  in 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' not  in py_str
  File "<stdin>", line 1
  >>> 'to' not  in 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 = [10,20,'bob',[1,2]]
  >>> len(alist)
  4
  >>> 10 in alist
  True
  >>> 1 in alist
  False
  >>> alist[2:4]
  ['bob', [1, 2]]
  >>> alist[2:3]
  ['bob']
  >>> alist[0:1]
  [10]
  >>> alist[0:2]
  [10, 20]
  >>> alist=[10,20,30,40,"bo","b"]
  >>> alist[-1]=100  (该最后一个值)
  >>> alist
  [10, 20, 30, 40, 'bo', 100]
  >>> alist.append(200)   (追加一个值)
  >>> alist
  [10, 20, 30, 40, 'bo', 100, 200]
  >>> alist.insert(3,"tom")   (插入一个值)
  >>> alist
  [10, 20, 30, 'tom', 40, 'bo', 100, 200]
  >>> blist=[12,45,64,12,453]
  >>> blist
  [12, 45, 64, 12, 453]
  >>> blist.sort()
  >>> blist
  [12, 12, 45, 64, 453]
  >>> blist.pop()
  453
  >>> blist
  [12, 12, 45, 64]
  元组
  >>> atuple=(10, 20, 30, 'tom', 40, 'bo', 100, 200)   (元组是用(),不可变)
  >>> atuple
  (10, 20, 30, 'tom', 40, 'bo', 100, 200)
  >>> atuple[2:4]
  (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
  [10, 20, 30, 'tom', 40, 'bo', 100, 200]
  >>> blist=alist
  >>> blist
  [10, 20, 30, 'tom', 40, 'bo', 100, 200]
  >>> blist.pop()
  200
  >>> blist
  [10, 20, 30, 'tom', 40, 'bo', 100]
  >>> alist
  [10, 20, 30, 'tom', 40, 'bo', 100]
  >>> clist=alist[::]
  >>> clist
  [10, 20, 30, 'tom', 40, 'bo', 100]
  >>> clist.pop()
  100
  >>> clist
  [10, 20, 30, 'tom', 40, 'bo']
  >>> alist
  [10, 20, 30, 'tom', 40, 'bo', 100]
  练习:
  [root@room1pc01 python]# vim if1.py
  1 a=10
  2
  3 if a>5:
  4  print 'yes'
  5 else:
  6   print 'error'
  [root@room1pc01 python]# python if1.py
  yes
  非0打印非空打印
  >>> if -0.0:
  ...   print 'yes'
  ...
  ...
  >>> if 3:
  ...  print 'yes'
  ...
  yes
  >>> if ' ':
  ...  print 'yes'
  ...
  ...
  yes
  >>> if '':
  ...  print 'yes'
  ...
  (一个脚本如果输入的用户不是bob,密码不是123456,就报错,或就对的)
  [root@room1pc01 python]# vim if2.py
  1 username=raw_input('username:')
  2 password=raw_input('password:')
  3 if username=="bob" and password=="123456":
  4  print 'login successful '
  5 else:
  6  print 'login incorrect'
  [root@room1pc01 python]# python if2.py
  username:bob
  password:123456
  login successful
  [root@room1pc01 python]# 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":
  7  print 'login successful '
  8 else:
  9  print 'login incorrect'
  [root@room1pc01 python]# python if3.py
  username:bob
  password:
  login successful

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-549222-1-1.html 上篇帖子: python - time 下篇帖子: 初学python
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表