苍天有泪 发表于 2018-8-10 12:54:56

Python基础复习

  #!/usr/bin/python
  # -*- coding:utf-8 -*-
  temp = "hackfreer"
  temp1 = 1.5
  print "%s,%.1f"%(temp,temp1)
  ######################################
  #判断结构复习
  def compare(number):
  if(number > 10):
  return 0
  if(number == 10):
  return 10
  if(number < 10):
  return 1
  num = input(&quot;Please input a number:&quot;)
  print compare(num)
  ######################################
  #while循环结构复习
  def circle(array):
  i = 0
  while i < len(array):
  print array
  i += 1
  tmp = raw_input(&quot;Please enter a num string:&quot;).split(&quot;,&quot;)
  circle(tmp)
  ######################################
  #元组复习
  tuple = (&quot;I&quot;,&quot;Love&quot;,&quot;You&quot;,&quot;And&quot;,&quot;You&quot;)
  print tuple
  print tuple
  a = &quot;HackBy:&quot;
  b = &quot;Hackfreer&quot;
  tmp = (a,b)
  print tmp
  ######################################
  tup1 = (1,2,3,4,5,6,7)
  for a in map(None,tup1):
  print a
  ######################################
  #列表实例复习
  list = [&quot;Hello&quot;,&quot;World&quot;,&quot;I&quot;,&quot;Love&quot;,&quot;You&quot;]
  print list
  for i in map(None,list):
  print i
  list.append(&quot;Fuck&quot;)
  print list
  list.insert(1,&quot;Fuck&quot;)
  print list
  list.reverse()
  print list
  ######################################
  #字典实例复习
  dict = {&quot;a&quot;:&quot;Hello&quot;,&quot;b&quot;:&quot;World&quot;}
  print dict
  for i in map(None,dict):
  print i
  print &quot;%s,%(a)s,%(b)s&quot;%{&quot;a&quot;:&quot;Hello&quot;,&quot;b&quot;:&quot;World&quot;}
  ######################################
  #apply()函数实例,将变量传递给函数
  def useApply(x = 1,y = 2):
  return x * y
  print apply(useApply,(1,4))
  #filter()函数实例复习,对数据指定函数过滤
  def useFilter(i):
  if i > 0:
  return i
  print filter(useFilter,range(-8,10))
  #map()函数实例复习,避免使用循环
  def useMap(x):
  return x ** x
  print map(useMap,range(1,3))
  #Buffer()函数实例复习,定位取值
  print buffer(&quot;helloworld&quot;,1,4)
  ####################################
  #Lambda函数实例复习
  x = lambda a,b:a + b
  print x(1,2)
  ####################################
  #字符串的格式化
  str1 = &quot;Code By Hackfreer&quot;
  str2 = &quot;Version 3.0&quot;
  print &quot;%s %s&quot;%(str1,str2)
  print str1.center(20)
  print str2.ljust(10)
  #字符串的转义
  string1 = &quot;Helllo\tThe\tWorld\n&quot;
  print len(string1)
  string2 = r&quot;Helllo\tThe\tWorld\n&quot;
  print len(string2)
  #字符串的连接实例
  strs1 = &quot;hello&quot;
  strs2 = &quot;the&quot;
  strs3 = &quot;world&quot;
  strs4 = &quot;too&quot;
  result = strs1+strs2+strs3
  result += strs4
  print result
  rs = [&quot;My&quot;,&quot;Name&quot;,&quot;is&quot;,&quot;Hackfreer&quot;]
  result1 = &quot; &quot;.join(rs)
  print result1
  #字符串的比较复习
  pass1 = 123456
  pass2 = &quot;123456&quot;
  if pass1 == pass2:
  print &quot;Same&quot;
  else:
  print &quot;Different&quot;
  if str(pass1) == pass2:
  print &quot;Same&quot;
  else:
  print &quot;Different&quot;
  #字符串的查找与分割复习

  word = &quot;select * from admin where>  print word
  print word.split(&quot; &quot;)
  #
  import re
  f1 = file(&quot;my.txt&quot;,&quot;r&quot;)
  for i in map(None,f1):
  print i
  count = 0
  s = &quot;&quot;
  for s in f1.readlines():
  li = re.findall(&quot;my&quot;,s)
  if len(li) > 0:
  count = count + li.count(&quot;my&quot;)
  print &quot;This file have &quot;+str(count)+&quot; my&quot;
  f1.close()
页: [1]
查看完整版本: Python基础复习