Python数据之 基本语法元素的格式输出

  from operator import itemgetter

  from datetime import date, time, datetime, timedelta

  from math import exp, log, sqrt

  import re

  #Print a simple string

  print("Output #1 : I'm exited to learn Python.")

  #两个数值相加

  x = 4

  y = 5

  z = x + y

  print("Output #2 : Four plus five equals {0:d}.".format(z))

  '''

  输出格式解释

  {} : 一个占位符,表示这里将要传入print语句中的一个具体的值

  0 : 指向format()方法中的第一个参数

  : : 用来分隔传入的值和它的格式

  '''

  #两个列表相加

  a = [1, 2, 3, 4]

  b = ["first", "second", "third", "fourth"]

  c = a + b

  print("Output #3 : {0}, {1}, {2}".format(a, b, c))

  #整数

  x = 9

  print("Output #4 : {0}".format(x))

  print("Output #5 : {0}".format(3 ** 4))

  print("Output #6 : {0}".format(int(8.3) / int(2.7)))

  #浮点数

  print("Output #7 : {0:.3f}".format(8.3 / 2.7)) #保留3位小数

  y = 2.5 * 4.8

  print("Output #8 : {0:.1f}".format(y)) #保留一位小数

  r = 8 / float(3)

  print("Output #9 : {0:.2f}".format(r)) #保留2位小数

  print("Output #10 : {0:.4f}".format(8.0 / 3)) #保留4位小数

  #调用math库中的函数

  print("Output #11 : {0:.4f}".format(exp(3))) #e^3

  print("Output #12 : {0:.2f}".format(log(4))) #ln4

  print("Output #13 : {0:.1f}".format(sqrt(81))) #平方根

  #字符串

  print("Output #14 : {0:s}".format('I\'m enjoying learning Python.'))

  print("Output #15 : {0:s}".format("This is a long string. Without the backslash it \

  would run off of the page on the right in the text \

  editor and be very difficult to read and edit. \

  By using the backslash you can split the long string \

  into smaller strings on separate lines so that the whole string \

  is easy to view in the text editor."))

  print("Output #16 : {0:s}".format('''You can use triple single quotes for

  multi-line comment strings.'''))

  print("Output #17 : {0:s}".format("""You can also use triple double quotes for

  multi-line comment strings."""))

  string1 = "This is a " #定义字符串string1

  string2 = "short string." #定义字符串string2

  sentence = string1 + string2 #字符串连接

  print("Output #18 : {0:s}".format(sentence))

  print("Output #19 : {0:s} {1:s}{2:s}".format("she is", "very "*7, "beautiful."))

  m = len(sentence) #字符串长度

  print("Output #20 : {0:d}".format(m))

  #split函数

  string1 = "My deliverable is due in May"

  string1_list1 = string1.split() #split默认用空格对字符串拆分

  string1_list2 = string1.split(" ", 2) #2表示只用前两个空格拆分,is due in May 被划分到一起

  print("Output #21 : {0}".format(string1_list1))

  print("Output #22 : FIRST PIECE:{0} SECOND PIECE:{1} THIRD PIECE:{2}".format(string1_list2[0], string1_list2[1], string1_list2[2]))

  string2 = "Your, deliverable, is, due, in, June"

  string2_list = string2.split(",") #用,进行拆分

  print("Output #23 : {0}".format(string2_list))

  print("Output #24 : {0} {1} {2}".format(string2_list[1], string2_list[5], string2_list[-1]))

  #join函数 组合拆分后的子字符串

  print("Output #25 : {0}".format(",".join(string2_list)))

  #strip函数 删除指定不想要的字符

  string3 = " Remove unwanted characters from this string.\t\t \n"

  print("Output #26 : string3:{0:s}".format(string3)) #原始字符串

  string3_lstrip = string3.lstrip()

  print("Output #27 : lstrip:{0:s}".format(string3_lstrip)) #从左侧删除

  string3_rstrip = string3.rstrip()

  print("Output #28 : rstrip:{0:s}".format(string3_rstrip)) #从右侧删除

  string3_strip = string3.strip()

  print("Output #29 : strip:{0:s}".format(string3_strip)) #从两侧删除

  string4 = "$$Here\'s another string that has unwanted characters.__---++"

  print("Output #30 : {0:s}".format(string4))

  string4 = "$$The unwanted characters have been removed.__---++"

  string4_strip = string4.strip('$_-+')

  print("Output #31 : {0:s}".format(string4_strip))

  #replace函数

  string5 = "Let\'s replace the spaces in this sentence with other characters"

  string5_replace = string5.replace(" ", "!@!")

  print("Output #32 (with !@!) : {0:s}".format(string5_replace))

  string5_replace = string5.replace(" ", ",")

  print("Output #33 (with commas) : {0:s}".format(string5_replace))

  #lower、upper、capitalize函数

  string6 = "Here\'s WHAT Happens WHEN You Use lower."

  print("Output #34 : {0:s}".format(string6.lower())) #将字符串字母变成小写

  string7 = "Here\'s what Happens when You Use UPPER."

  print("Output #35 : {0:s}".format(string7.upper())) #将字符串字母大写

  string5 = "here\'s WHAT Happens WHEN you use Capitalize."

  print("Output #36 : {0:s}".format(string5.capitalize())) #将字符串首字母大写,其他字母小写

  string5_list = string5.split() #将字符串拆分

  print("Output #37 (on each word) : ") #将拆分后的子字符串首字母大写,其他字母小写

  for word in string5_list:

  print("{0:s}".format(word.capitalize()))

  #正则表达式与模式匹配

  string = "The quick brown fox jumps over the lazy dog."

  string_list = string.split()

  pattern = re.compile(r"The", re.I) #re.I确保模式不区分大小写

  count = 0

  for word in string_list:

  if pattern.search(word):

  count += 1

  print("Output #38 : {0:d}".format(count))

  #在字符串中每次找到模式时将其打印出来

  string = "The quick brown fox jumps over the lazy dog."

  string_list = string.split()

  pattern = re.compile(r"(?PThe)", re.I)

  print("Output #39 : ")

  for word in string_list:

  if pattern.search(word):

  print("{:s}".format(pattern.search(word).group('match_word')))

  #使用字母“a”替换替换字符串中的the

  string = "The quick brown fox jumps over the lazy dog."

  string_to_find = r"The"

  pattern = re.compile(string_to_find, re.I)

  print("Output #40 : {:s}".format(pattern.sub("a", string)))

  #日期

  #打印今天的日期,以及年月日

  today = date.today()

  print("Output #41 : today : {0!s}".format(today))

  print("Output #42 : {0!s}".format(today.year))

  print("Output #43 : {0!s}".format(today.month))

  print("Output #44 : {0!s}".format(today.day))

  current_datetime = datetime.today()

  print("Output #45 : {0!s}".format(current_datetime))

  #使用datedelta计算一个新日期

  one_day = timedelta(days=-1)

  yesterday = today + one_day

  print("Output #46 : yesterday : {0!s}".format(yesterday))

  eight_hours = timedelta(hours=-8)

  print("Output #47 : {0!s} {1!s}".format(eight_hours.days, eight_hours.seconds))

  #计算出两个日期之间的天数

  date_diff = today - yesterday

  print("Output #48 : {0!s}".format(date_diff))

  print("Output #49 : {0!s}".format(str(date_diff).split()[0]))

  #根据一个日期对象创建一个特定格式的字符串

  print("Output #50 : {:s}".format(today.strftime('%m/%d/%Y')))

  print("Output #51 : {:s}".format(today.strftime('%b %d %Y')))

  print("Output #52 : {:s}".format(today.strftime('%Y-%m-%d')))

  print("Output #53 : {:s}".format(today.strftime('%B %d,%Y')))

  #根据一个表示日期的字符串

  #创建一个带有特殊格式的datetime对象

  date1 = today.strftime('%m/%d/%Y')

  date2 = today.strftime('%b %d,%Y')

  date3 = today.strftime('%Y-%m-%d')

  date4 = today.strftime('%B %d,%Y')

  #基于四个具有不同日期格式的字符串

  #创建两个datetime对象和两个date对象

  print("Output #54 : {!s}".format(datetime.strptime(date1, '%m/%d/%Y')))

  print("Output #55 : {!s}".format(datetime.strptime(date2, '%b %d,%Y')))

  #仅显示日期部分

  print("Output #56 : {!s}".format(datetime.date(datetime.strptime(date3, '%Y-%m-%d'))))

  print("Output #57 : {!s}".format(datetime.date(datetime.strptime(date4, '%B %d,%Y'))))

  #创建列表

  a_list = [1, 2, 3]

  print("Output #58 : {}".format(a_list))

  print("Output #59 : a_list has {} elements.".format(len(a_list)))

  print("Output #60 : the maximum value in a_list is {}.".format(max(a_list)))

  print("Output #61 : the minimum value in a_list is {}.".format(min(a_list)))

  another_list = ['printer', 5, ['star', 'circle', 5]]

  print("Output #62 : {}".format(another_list))

  print("Output #63 : another_list also has {} elements.".format(len(another_list)))

  print("Output #64 : 5 is in another_list {} time.".format(another_list.count(5)))

  #索引值

  print("Output #65 : {}".format(a_list[0]))

  print("Output #66 : {}".format(a_list[1]))

  print("Output #67 : {}".format(a_list[2]))

  print("Output #68 : {}".format(a_list[-1]))

  print("Output #69 : {}".format(a_list[-2]))

  print("Output #70 : {}".format(a_list[-3]))

  print("Output #71 : {}".format(another_list[2]))

  print("Output #72 : {}".format(another_list[-1]))

  #列表切片

  print("Output #73 : {}".format(a_list[0:2]))

  print("Output #74 : {}".format(another_list[:2]))

  print("Output #75 : {}".format(a_list[1:3]))

  print("Output #76 : {}".format(another_list[1:]))

  #复制列表

  a_new_list = a_list[:]

  print("Output #77 : {}".format(a_new_list))

  #列表连接

  a_long_list = a_list + another_list

  print("Output #78 : {}".format(a_long_list))

  #使用in与not in

  a = 2 in a_list

  print("Output #79 : {}".format(a))

  if 2 in a_list:

  print("Output #80 : 2 is in {}.".format(a_list))

  b = 6 not in a_list

  print("Output #81 : {}".format(b))

  if 6 not in a_list:

  print("Output #82 : 6 is not in {}".format(a_list))

  #追加、删除和弹出元素

  a_list.append(4)

  a_list.append(5)

  a_list.append(6)

  print("Output #83 : {}".format(a_list))

  a_list.remove(5)

  print("Output #84 : {}".format(a_list))

  a_list.pop()

  a_list.pop()

  print("Output #85 : {}".format(a_list))

  #列表反转

  a_list.reverse()

  print("Output #86 : {}".format(a_list))

  a_list.reverse()

  print("Output #87 : {}".format(a_list))

  #列表排序

  unordered_list = [3, 5, 1, 7, 2, 8, 4, 9, 0, 6]

  print("Output #88 : {}".format(unordered_list))

  list_copy = unordered_list[:]

  list_copy.sort()

  print("Output #89 : {}".format(list_copy))

  print("Output #90 : {}".format(unordered_list))

  #sort排序函数

  my_lists = [[1, 2, 3, 4], [4, 3, 2, 1], [2, 4, 1, 3]]

  my_lists_sorted_by_index_3 = sorted(my_lists, key=lambda index_value:index_value[3])

  print("Output #91 : {}".format(my_lists_sorted_by_index_3))

  #使用itemgetter()对列表集合按照两个索引位置来排序

  my_lists = [[123, 2, 2, 444], [22, 6, 6, 444], [354, 4, 4, 678], [236, 5, 5, 678], [578, 1, 1, 290], [461, 1, 1, 290]]

  my_lists_sorted_by_index_3_and_0 = sorted(my_lists, key=itemgetter(3, 0))

  print("Output #92 : {}".format(my_lists_sorted_by_index_3_and_0))

  #元组

  #创建元组

  my_tuple = ('x', 'y', 'z')

  print("Output #93 : {}".format(my_tuple))

  print("Output #94 : my_tuple has {} elements".format(len(my_tuple)))

  print("Output #95 : {}".format(my_tuple[1]))

  longer_tuple = my_tuple + my_tuple

  print("Output #96 : {}".format(longer_tuple))

  #元组解包

  one, two, three = my_tuple

  print("Output #97 : {0} {1} {2}".format(one, two, three))

  var1 = 'red'

  var2 = 'robin'

  print("Output #98 : {} {}".format(var1, var2))

  var1, var2 = var2, var1 #元组彼此交换

  print("Output #99 : {} {}".format(var1, var2))

  #元组转换成列表

  my_list = [1, 2, 3]

  my_tuple = ('x', 'y', 'z')

  print("Output #100 : {}".format(tuple(my_list)))

  print("Output #101 : {}".format(tuple(my_tuple)))

  #字典

  empty_dict = { }

  a_dict = {'one':1, 'two':2, 'three':3}

  print("Output #102 : {}".format(a_dict))

  print("Output #103 : a_dict has {!s} elements".format(len(a_dict)))

  another_dict = {'x':'printer', 'y':5, 'z':['star', 'circle', 9]}

  print("Output #104 : {}".format(another_dict))

  print("Output #105 : another_dict also has {!s} elements".format(len(another_dict)))

  #使用键来引用字典中特定的值

  print("Output #106 : {}".format(a_dict['two']))

  print("Output #107 : {}".format(another_dict['z']))

  #使用copy复制一个字典

  a_new_dict = a_dict.copy()

  print("Output #108 : {}".format(a_new_dict))

  #使用keys、 value、items

  print("Output #109 : {}".format(a_dict.keys()))

  a_dict_keys = a_dict.keys()

  print("Output #110 : {}".format(a_dict_keys))

  print("Output #111 : {}".format(a_dict.values()))

  print("Output #112 : {}".format(a_dict.items()))

  print("Output #113 : {}".format(a_dict.keys()))

  #使用判断关键字

  if 'y' in another_dict:

  print("Output #114 : y is a key in another_dict : {}".format(another_dict.keys()))

  if 'c' not in another_dict:

  print("Output #115 : c is not a key in another_dict : {}".format(another_dict.keys()))

  print("Output #116 : {!s}".format(a_dict.get('three')))

  print("Output #117 : {!s}".format(a_dict.get('four')))

  print("Output #118 : {!s}".format(a_dict.get('four', 'Not in dict')))

  #排序 郑州人流医院哪家好 http://www.85827120.com/

  print("Output #119 : {}".format(a_dict))

  dict_copy = a_dict.copy()

  ordered_dict1 = sorted(dict_copy.items(), key=lambda item:item[1])

  print("Output #120 : (order by keys) : {}".format(ordered_dict1))

  ordered_dict2 = sorted(dict_copy.items(), key=lambda item:item[1])

  print("Output #121 : (order by keys) : {}".format(ordered_dict1))

  ordered_dict3 = sorted(dict_copy.items(), key=lambda x:x[1], reverse=True)

  print("Output #122 : (order by values, descending) : {}".format(ordered_dict3))

  ordered_dict4 = sorted(dict_copy.items(), key=lambda x:x[1], reverse=False)

  print("Output #123 : (order by values, ascending) : {}".format(ordered_dict4))

  #控制流

  #if-else判断

  x = 5

  if x > 4 or x != 9:

  print("Output #124: {}".format(x))

  else:

  print("Output #125: x is not greater than 4")

  # if-elif-else 判断

  if x > 6:

  print("Output #126: x is greater than six")

  elif x > 4 and x == 5:

  print("Output #127: {}".format(x*x))

  else:

  print("Output #128: x is not greater than 4")

  # for 循环

  y = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

  z = ['Annie', 'Betty', 'Claire', 'Daphne', 'Ellie', 'Franchesca', 'Greta', 'Holly', 'Isabel', 'Jenny']

  print("Output #129:")

  for month in y:

  print("{!s}".format(month))

  print("Output #130: (index value: name in list)")

  for i in range(len(z)):

  print("{0!s}: {1:s}".format(i, z[i]))

  print("Output #131: (access elements in y with z's index values)")

  for j in range(len(z)):

  if y[j].startswith('J'):

  print("{!s}".format(y[j]))

  print("Output #132:")

  for key, value in another_dict.items():

  print("{0:s}, {1}".format(key, value))

  #for循环的简化

  my_data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

  rows_to_keep = [row for row in my_data if row[2] > 5] #for循环的简化方式

  print("Output #133 (list comprehension): {}".format(rows_to_keep))

  my_data = [(1, 2, 3), (4, 5, 6), (7, 8, 9), (7, 8, 9)]

  set_of_tuples1 = {x for x in my_data}

  print("Output #134 (set comprehension): {}".format(set_of_tuples1))

  set_of_tuples2 = set(my_data)

  print("Output #135 (set function): {}".format(set_of_tuples2))

  my_dictionary = {'customer1': 7, 'customer2': 9, 'customer3': 11}

  my_results = {key: value for key, value in my_dictionary.items() if value > 10}

  print("Output #136 (dictionary comprehension): {}".format(my_results))

  # while循环

  print("Output #137:")

  x = 0

  while x < 11:

  print("{!s}".format(x))

  x += 1

  # 函数定义

  def getMean(numericValues):

  return sum(numericValues)/len(numericValues) if len(numericValues) > 0 else float('nan')

  my_list = [2, 2, 4, 4, 6, 6, 8, 8]

  print("Output #138 (mean): {!s}".format(getMean(my_list)))

  # 异常

  def getMean(numericValues):

  return sum(numericValues)/len(numericValues)

  my_list2 = [ ]

  # 简单版本

  try:

  print("Output #139: {}".format(getMean(my_list2)))

  except ZeroDivisionError as detail:

  print("Output #139 (Error): {}".format(float('nan')))

  print("Output #139 (Error): {}".format(detail))

  # 复杂版本

  try:

  result = getMean(my_list2)

  except ZeroDivisionError as detail:

  print("Output #140 (Error): {}".format(float('nan')))

  print("Output #140 (Error): {}".format(detail))

  else:

  print("Output #140 (The mean is): {}".format(result))

  finally:

  print("Output #140 (Finally): The finally block is executed every time")

posted @ 2020-05-06 16:42  tiana_Z  阅读(250)  评论(0编辑  收藏  举报