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

[经验分享] Python编程

[复制链接]

尚未签到

发表于 2018-8-6 09:00:19 | 显示全部楼层 |阅读模式
  特点:
  (1):易学:python关键字少、结构简单、语法清晰
  易读:没有其他语言通常用来访问变量、定义代码块和进行模式匹配的命令式符号
  (2):内存管理器:内存管理是由python解释器负责的
  >>> py_str = 'python'
  >>> len(py_str)-----输出变量字符的长度
  6
  >>> 't' in py_str---------“t”是否在py_str变量中
  True
  >>> 'th' in py_str
  True
  >>> 'to' not in py_str
  True
  >>> py_str[0]----从左向右取出第一个字符
  'p'
  >>> py_str[5]
  'n'
  >>> py_str[-1]
  'n'
  >>> py_str[3:]
  'hon'
  >>> py_str[3:5]
  'ho'
  >>> py_str[1:6]
  'ython'
  >>> py_str[0:6]
  'python'
  >>> py_str[0:]
  'python'
  >>> py_str[:2]
  'py'
  >>> py_str[::2]---------步长值
  'pto'
  >>> py_str[::3]
  'ph'
  >>> py_str[1::3]
  'yo'
  >>> py_str[::-1]
  'nohtyp'
  >>> py_str + '   cool'------相加表示拼接
  'python   cool'
  >>> py_str.upper()------转换大写
  'PYTHON'
  >>> py_str.lower()------转换小写
  'python'
  >>> py_str.isdigit()----判断是否是数字
  False
  >>> py_str.center(50, '#')------------居中对齐
  '######################python######################'
  >>> py_str.ljust(50)------左对齐
  'python
  列表
  >>> alist = [10, 20, 'dc', 'alice', [1,3]]
  字典表示
  >>> adict = {'name': 'hanjie', 'age':22}
  >>> len(adict)---------两个字符长对,看中间的逗号分隔
  2
  >>> adict--------箭直对类型
  {'age': 22, 'name': 'hanjie'}
  >>> 'name' in adict-----判断'name'是否在adict
  True
  >>> adict['age'] = 21--------改变'age'的值
  >>> adict
  {'age': 21, 'name': 'hanjie'}
  >>> adict['email'] = 'hj@qq.com'
  >>> adict
  {'age': 21, 'name': 'hanjie', 'email': 'hj@qq.com'}------增加变量
  if语句:
  #coding: utf8
  import getpass------------------表示python中的一个模块
  username = raw_input("请输入用户名: ")
  passwd = getpass.getpass("请输入用户密码: ")---------第一个getpass表示模块,第二个表示模块中的一个方法
  if username == "bob" and passwd == "123456":---------python中的并且和或者用“and”、“or”表示
  print "Login successful"
  else:
  print "Login incorrect"
  PYTHON解释器中的判断
  >>> if 5 > 8:(python判断是必须要冒号分隔)、if、else是评级;两个print是一个评级;输入else时必须要用冒号分隔
  ...   print 'yes'
  ... else:
  ...   print 'no'
  ...
  no
  在python中随机猜一个数:
  #coding: utf8
  import random
  num = random.randint(1, 10)
  answer = int(raw_input("请猜一个数: "))
  if answer > num:
  print "猜大了"
  elif answer < num:
  print &quot;猜小了&quot;
  else:
  print &quot;猜对了&quot;
  print num
  Python中三引号的区别:列b = &quot;hello,\n
  world&quot;
  c=&quot;&quot;&quot;hello'
  world!&quot;&quot;&quot;  #三引号就相当于双引号里面的\n,字符串越多\n就多,使用起来不方便,也不方便查看, 所以就使用三引号来实现
  If语句的写法:
  #coding: utf8
  import random  #导入random模块,是产生随机数的模块
  all_choices = ['石头', '剪刀', '布'] #定义一个列表,将选择添加在列表中
  win_list = [['石头', '剪刀'], ['剪刀', '布'], ['石头', '布']] #定一个用户赢的列表,列表中的元素仍然是一个列表
  prompt = &quot;&quot;&quot;(0)石头  #定义一个变量,将提示语写入这个变量中
  (1)剪刀
  (2)布
  请选择(0/1/2):&quot;&quot;&quot;
  computer = random.choice(all_choices) #随机选一个值
  ind = int(raw_input(prompt)) #因为输入的是字符串类型,所以将字符串转变为整型
  player = all_choices[ind] #列表可以取下标,下标对应的列表中的值
  print &quot;Your choice: %s, Computer's choice: %s&quot; %(player, computer) #提示信息,友好界面:相当于人出石头、计算机出布
  if player == computer: #如果两个变量值相等
  print '\033[32;1m平局\033[0m' #输出平局
  elif [player,computer] in win_list: #如果列表在win_list中
  print '\033[31;1m你赢了\033[0m'  #输入你赢了
  else:
  print '\033[31;1m你输了\033[0m'  #输出你输了
  pwin = 0  #人赢的次数
  cwin = 0  #电脑赢得次数
  import random
  all_choices = ['石头', '剪刀', '布']
  win_list = [['石头', '剪刀'], ['剪刀', '布'], ['石头', '布']]
  prompt = &quot;&quot;&quot;(0)石头
  (1)剪刀
  (2)布
  请选择(0/1/2):&quot;&quot;&quot;
  while pwin < 2 and cwin < 2: #人和电脑赢的次数都不够两次时就的循环下面语句
  computer = random.choice(all_choices)
  ind = int(raw_input(prompt))
  player = all_choices[ind]
  print &quot;Your choice: %s, Computer's choice: %s&quot; %(player, computer)
  if player == computer:
  print '\033[32;1m平局\033[0m'
  elif [player,computer] in win_list:
  pwin += 1
  print '\033[31;1m你赢了\033[0m'
  else:
  cwin += 1
  print '\033[31;1m你输了\033[0m'
  Break语句的写法:
  import random
  num = random.randint(1, 10)
  while True:
  answer = int(raw_input(&quot;请猜一个数: &quot;))
  if answer > num:
  print &quot;猜大了&quot;
  elif answer < num:
  print &quot;猜小了&quot;
  else:
  print &quot;猜对了&quot;
  break
  print num
  For语句的写法:
  import random
  num = random.randint(1, 10)
  for i in range(3):
  answer = int(raw_input(&quot;请猜一个数: &quot;))
  if answer > num:
  print &quot;猜大了&quot;
  elif answer < num:
  print &quot;猜小了&quot;
  else:
  print &quot;猜对了&quot;
  break
  else:
  print num
  纯粹if语句结构
  #coding: utf8
  import random
  computer = random.choice(['石头', '剪刀', '布'])
  player = raw_input('请出拳头(石头/剪刀/布): ')
  print &quot;your choice: %s, Computer's choice: %s&quot; %(player, computer)
  if player == '石头':
  if computer == '石头':
  print '平局'
  elif computer == '剪刀':
  print '你赢了'
  else:
  print '你输了'
  elif player == '剪刀':
  if computer == '石头':
  print '你输了'
  elif computer == '剪刀':
  print '平局'
  else:
  print '你赢了'
  else:
  if computer == '石头':
  print '你赢了'
  elif computer == '剪刀':
  print '你输了'
  else:
  print '平局'
  #coding: utf8
  num = int(raw_input(&quot;请输入您的成绩: &quot;))
  if num >= 90:
  print &quot;优秀&quot;
  elif num >= 80:
  print &quot;好&quot;
  elif num >= 70:
  print &quot;良&quot;
  elif num >= 60:
  print &quot;及格&quot;
  else:
  print &quot;你要努力了&quot;
  #coding: utf8
  num = int(raw_input(&quot;分数: &quot;))
  if 60 <= num < 70:
  print '及格'
  elif 70 <= num < 80:
  print '良'
  elif 80 <= num < 90:
  print '好'
  elif num >= 90:
  print '优秀'
  else:
  print '你要努力了'
  sum = 0
  i = 1
  while i <= 100:
  sum += i
  i += 1
  print sum
  while i <= 100:
  sum = sum + i
  i = i + 2
  print sum
  result = 0
  counter = 0
  while counter < 100:
  counter += 1
  if counter % 2 == 1:
  continue
  result += counter
  print result
  sum = 0
  i = 2
  while i <= 100:
  sum += i
  i += 2
  print sum
  生成八随机密码: for循环
  import random
  all_chs = '0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'
  result = ''
  for i in range(8):
  ch = random.choice(all_chs)
  result += ch
  print result
  调用函数生成8位随机密码
  import random
  import string
  all_chs = string.letters + string.digits #表示大小写字母和数字的调用
  def gen_pass(n=8):
  result = ''
  for i in range(n):
  ch = random.choice(all_chs)
  result += ch
  return result
  if __name__ == '__main__':
  print gen_pass()
  print gen_pass(10)
  将/bin/ls拷贝到/root/目录下,简单写法
  f1 = open('/bin/ls')
  f2 = open('/root/ls', 'w')
  data = f1.read()
  f2.write(data)
  f1.close()
  f2.close()
  while语句的写法:
  src_fname = '/bin/ls'  #定义源文件src_fname
  dst_fname = '/root/ls' #定义目标文件dst_fname
  src_fobj = open(src_fname)
  dst_fobj = open(dst_fname, 'w')
  while True:
  data = src_fobj.read(4096)
  if data == '':
  break
  dst_fobj.write(data)
  src_fobj.close()
  dst_fobj.close()
  定义函数的调用的写法:
  import sys
  def copy(src_fname, dst_fname):
  src_fobj = open(src_fname)
  dst_fobj = open(dst_fname, 'w')
  while True:
  data = src_fobj.read(4096)
  if data == '':
  break
  dst_fobj.write(data)
  src_fobj.close()
  dst_fobj.close()
  copy(sys.argv[1], sys.argv[2])

运维网声明 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-547352-1-1.html 上篇帖子: Python学习14:如何把Python代码写成一行? 下篇帖子: Python回顾与整理1:Python基础
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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