78144666 发表于 2018-8-10 09:54:12

python全天课学习笔记

  if
  奇数 1分钟:
  >>> a=range(10)
  >>> a
  
  >>> n=0
  >>> while n<len(a):
  ...   if n%2==1:
  ...         print a
  ...   n+=1
  >>> n=0
  >>> while n<len(a):
  ...   if n%2==1:
  ...         print a
  ...   n+=1
  n=0
  >>> a="abcefghijk"
  >>> while n<len(a):
  ...   if n%2==1:
  ...         print a
  ...   n+=1
  int i
  >>> while i <= 10:
  ...   if i %2 != 0:
  ...         print i
  ...   i += 1
  >>>
  >> a =" 1 2 3 4 5 6 7 8 9 0"
  >>> a1 = a.split()
  >>> a1
  ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
  确定是否是fals
  查找一句中有多少字母
  s = “I am a boy”
  >>> count=0
  >>> word_list=s.split()
  >>> word_list
  ['I', 'am', 'a', 'boy!']
  >>> for i in word_list:
  ...   if 'a' in i:
  ...         count+=1
  ...
  >>> print count
  ncount = 0
  s = 'I ama aa boy'
  s_list = s.split()
  print s_list
  for i in range(len(s)):
  print s
  if s == 'a':
  ncount += 1
  print 'how many words :%s'%ncount
  统计长度
  >>> print len(word_list)
  4
  >>> len("abc")
  3
  >>> len({1:2})
  死循环
  while True:
  pass
  遍历
  >>> for i in range(1,11,2):
  ...   print i
  >>> for i in range(1,11,2):
  ...   print i >>> s="abcedfg"
  >>> for i in range(len(s)):
  ...   print s
  质数
  >>> 方法1:使用2-到它本身之间的所有数做除法,没有发生整除,则就是质数
  方法2:使用2-到它本身平方根之间的所有数做除法,没有发生整除,则就是质数
  #encoding=utf-8
  import math
  number=int(raw_input("input a number:"))
  for i in range(2,int(math.sqrt(number)+1)):
  if number %i ==0:
  print "%s is not prime number " %number
  break
  else:
  print "%s isprime number " %number
  小题
  a=
  >>> for i in a:
  ...   if isinstance(i,(list,tuple)):
  ...         for j in i:
  ...             print j
  ...   else:
  ...         print i
  for
  >>> for i in range(5):
  ...   print i
  ... else:
  ...   print "no break happens!"
  ...
  >>> for i in range(5):
  ...   print i
  ...   break
  ... else:
  ...   print "no break happens!"
  ...
  0
  while
  >>> n=2
  >>> while n>=2:
  ...   n-=1
  ... else:
  ...   print "no break happens"
  ...
  no break happens
  习题
  >>> for i in range(1,5):
  ...   for j in range(1,6):
  ...         print int(str(i)+str(j))
  >>> exec("print 'hello'")
  hello
  >>> eval("2*4")
  8
  Pass 和是否可迭代
  from collections import Iterable
  print isinstance('abc',Iterable)
  True
  退出多重循环方法:
  class getoutofloop(Exception): pass
  try:
  for i in range(5):
  for j in range(5):
  for k in range(5):
  if i == j == k == 3:
  raise getoutofloop()
  else:
  print i, '----', j, '----', k
  except getoutofloop:
  pass
  def test():
  for i in range(5):
  for j in range(5):
  for k in range(5):
  if i == j == k == 3:
  return
  else:
  print i, '----', j, '----', k
  test()
  作业一去重:使用尽可能多的方法实现list去重
  >>> set()
  set()
  >>> list(set())
  
  >>>
  >>> d={}
  >>> a=
  >>> for i in a:
  ...   d=None
  ...
  >>> print d.keys()
  
  算法:
  1 声明一个新的list
  2 把原list中的元素进行遍历,尝试放入到新list中
  3 如果要放入的元素已经在新list中存在了,就不再次放入了,否则就放入
  4 打印新list的内容
  a=
  for i in a:
  for j in range(a.count(i)-1):
  a.remove(i)
  print a
  七种方式去重
  # #coding=utf-8
  import time
  time_start=time.time()
  print u"列表去重的七种方法"
  print u"第一种测试方法"
  repeat_list=
  result=[]
  for i in repeat_list:
  if i not in result:
  result.append(i)
  print u"第一种去重结果: ",result
  print u"第二种测试方法"
  repeat_list=
  result={}
  print u"第二种去重结果: ",list(result.fromkeys(repeat_list))
  print u"第三种测试方法"
  repeat_list=
  print u"第三种去重结果: ",list(set(repeat_list))
  print u"第四种测试方法"
  repeat_list=
  import itertools
  def test_groupby(x):
  if x==1:
  print "lower"
  elif x>1 and x<4:
  print "middle"
  elif x>=4:
  print "higher"
  repeat_list=sorted(repeat_list)
  data=itertools.groupby(repeat_list,key=test_groupby)
  for i,j in data:
  print list(j)
  data=itertools.groupby(repeat_list)
  result=[]
  for i,j in data:
  result.append(i)
  print u"第四种去重结果: ",result
  print u"第五种测试方法"
  repeat_list=
  for i in :
  for x in range(repeat_list.count(i)-1):
  repeat_list.remove(i)
  print u"第五种去重结果: ",repeat_list
  print u"第六种测试方法"
  repeat_list=
  i=0
  while i<=len(repeat_list)-1:
  if repeat_list.count(repeat_list)>1:
  repeat_list.pop(i)
  else:
  i+=1
  print u"第六种去重结果: ",repeat_list
  print u"第七种测试方法"
  repeat_list=
  func=lambda x,y:x if y in x else x +
  print u"第七种去重结果: ",reduce(func,[[],]+repeat_list)
  print "_"*20
  print u"去重程序耗时%f" % (time.time()-time_start)
  print "_"*20
  time.sleep(3)
  Testing started at 17:38 ...
  列表去重的七种方法
  第一种测试方法
  第一种去重结果:
  第二种测试方法
  第二种去重结果:
  第三种测试方法
  第三种去重结果:
  第四种测试方法
  lower
  lower
  lower
  middle
  middle
  higher
  higher
  higher
  
  第四种去重结果:
  第五种测试方法
  第五种去重结果:
  第六种测试方法
  第六种去重结果:
  第七种测试方法
  第七种去重结果:
  ____________________
  去重程序耗时0.001000
  ____________________
  =================
  Process finished with exit code 0
  Empty test suite.
  3.实现数学中多项式求和公式的打印
  result=[]
  for i in range(6,-1,-1):
  if i == 0:
  result.append("a0")
  break
  result.append("a%sx^%s" %(i,i))
  print "+".join(result
  转换:
  >>> "*".join(["1","2","3"]).split()
  ['1*2*3']
  >>> "*".join(["1","2","3"]).split('*')
  ['1', '2', '3']
  统计名字列表中,各名字的首字母在名字列表中出现的次数
  第一种
  name_list=['foster',"janet",'jessus','david']
  count_dict={}
  for i in name_list:
  count_dict="".join(name_list).count(i)
  print count_dict
  第二种方法
  name_list=['foster',"janet",'jessus','david']
  count_dict={}
  for i in name_list:
  count=0
  for j in name_list:
  if j.count(i)>=1:
  count+=j.count(i)
  count_dict=count
  print count_dict
  输入三个数,判断是否能构成三角形
  import math
  a,b,c=input("please input three num a,b,c:")
  d=min(a,b,c)
  e=max(a,b,c)
  if d<=0:
  print "error"
  elif (a+b+c)>2*e:
  print U"能组成三角形"
  else:
  print u"不能组成三角形"    输入三个数,判断是否能构成三角形
  能构成三角形三边关系:
  三边都大于零
  两边之和大于第三边,两边之差小于第三边
页: [1]
查看完整版本: python全天课学习笔记