xiaowei8782088 发表于 2018-8-6 09:00:19

Python编程

  特点:
  (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----从左向右取出第一个字符
  'p'
  >>> py_str
  'n'
  >>> py_str[-1]
  'n'
  >>> py_str
  'hon'
  >>> py_str
  'ho'
  >>> py_str
  'ython'
  >>> py_str
  'python'
  >>> py_str
  'python'
  >>> py_str[:2]
  'py'
  >>> py_str[::2]---------步长值
  'pto'
  >>> py_str[::3]
  'ph'
  >>> py_str
  '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 = ]
  字典表示
  >>> 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 #列表可以取下标,下标对应的列表中的值
  print &quot;Your choice: %s, Computer's choice: %s&quot; %(player, computer) #提示信息,友好界面:相当于人出石头、计算机出布
  if player == computer: #如果两个变量值相等
  print '\033[32;1m平局\033[0m' #输出平局
  elif 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
  print &quot;Your choice: %s, Computer's choice: %s&quot; %(player, computer)
  if player == computer:
  print '\033[32;1m平局\033[0m'
  elif 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, sys.argv)
页: [1]
查看完整版本: Python编程