Basic programming that --python

  1, let Python help you choose a random drink it!

  import random

  listC = [ 'JDB', 'sprite', 'coke', 'Rebel horizon', 'coconut']

  print (random.choices (listC), type (random.choices (listC))) # choices function returns a list of data types

  print (random.choice (listC), type (random.choice (listC))) # choice function returns the string class

  2, listA Menus have been stored in the point, so Python help you add a 'Pork' to remove a 'boiled Gansi'.

  listA = [ 'boiled Gansi', 'tofu', 'shrimp', 'vegetables', 'fried eggs, tomatoes']

  listA.append ( 'Pork')

  print (LIST)

  listA.remove ( 'boiled Gansi')

  print (LIST)

  3, dictMenu stored in the afternoon you double package (including coffee and snacks 2 parts 2 parts) prices, so Python help compute and output the total spending.

  dictMenu = { 'cappuccino': 32 'Mocha': 30, 'green tea cake': 28 'Brownie': 26}

  Sum = 0

  for i in dictMenu.values():

  Sum += i

  print(Sum)

  4, obtains an input a positive integer N, the inverted output of the positive integer, do not consider anomalies

  s = input()

  print (eval (s [:: - 1])) # eval function converts the input string s based on the content in the content type for the corresponding

  5, given a number 123456, use 25 width, alignment to the right printouts, using the plus '+' filling.

  print('{:+>25}'.format(123456))

  6, given a number 12345678.9, increase the thousands separator symbol set the width to 30, alignment to the right printouts, padded with spaces

  print('{:>30,}'.format(12345678.9))

  7, given an integer number 0x1010, please order the output of the Python language in hexadecimal, decimal, octal and binary representation, comma-separated

  print('0x{0:x},0o{0:o},{0},0b{0:b}'.format(0x1010))

  8, to obtain a character string input by the user, output a wholly lowercase

  s = input()

  print(s.lower())

  A string 9, obtaining user input, wherein the output of the number of occurrences of a character

  s = input()

  print(s.count('a'))

  10, to obtain a character string input by the user, wherein the replacement string appears 'py' is 'python', outputting the replacement string.

  s = input()

  print(s.replace('py', 'Python'))

  11, to obtain a set of user-entered digits, comma separated, wherein the maximum value of the output

  data = input()

  a = data.split ( ',') # a list type is

  b = []

  for i in a:

  b.append(i)

  print(max(b))

  12, s = '9e10' is a floating-point number string, the string contains a decimal point or using scientific notation, the programming determines whether the floating point number string s. If it is then output True, otherwise output False. (2 methods)

  s = '9e10'

  if type(eval(s) == type(0.0)):

  print('True')

  else:

  print('False')

  s = '9e10'

  print('True' if type(eval(s)) == type(0.0) else 'False')

  13, s = '123' is an integer string, integer programming determines whether the string s. If it is then output True, otherwise output Fasle. Code requires no more than 2 rows

  s = '123'

  print('True' if type(eval(s)) == type(1) else 'False')

  14, ls is a list, as follows: ls = [123, '456', 789, '123', 456, '798'], each seeking its integral elements and.

  ls = [123 '456' 789, '123' 456 '798']

  Sum = 0

  for item in ls:

  if type(item) == type(123):

  Sum += item

  print(Sum)

  15, while True: may constitute an 'infinite loop'. Please write a program using the cycle of death following functions: obtaining user input loop until the user input until the character y or Y, and exit the program. (Two examples are given)

  while True:

  s = input()

  if s in ['y', 'Y']:

  break

  while True:

  s = input()

  if s== 'y' or s== 'Y':

  exit()

  16, please write a program, get silent when user input, the input value is obtained in addition to the user input 100 is calculated, the result of normal operation on the output, and quit, never quit error.

  try: Wuxi flow How much: http: //www.bhnfkyy.com/

  a = eval(input())

  print(100 / a, type(100 / a)) # float

  except:

  pass

  17, following function returns the square two numbers, please add the code at the horizontal line

  def ipsum (b):

  return a ** 2 + b ** 2

  if __name__ == '__main__':

  T1 = ipsum (2, 2)

  print(t1)

  18, following function returns the default value of the square of an integer number of two and, if only to a variable, the other variables 10

  def ipsum (a, b = 10);

  return (a ** 2 + b ** 2), a + b

  if __name__ == '__main__':

  T1, T2 = ipsum (2)

  print(t1, t2)

  19, following function returns the square and at the same time and the two numbers and two numbers, please add the code at the horizontal line

  def ipsum (b):

  return (a ** 2 + b ** 2), a + b

  if __name__ == '__main__':

  T1, T2 = ipsum (2, 2)

  print(t1, t2)

  20, the following function returns the square of two numbers and n

  n = 2

  def ipsum (b):

  global n

  return (a ** 2 + b ** 2) * n

  if __name__ == '__main__':

  print (ipsum (2, 3))

  21, PyIntaller source Python library used to be packed. Given a source file py.py, please give be packaged into an executable command file:

  pyinstaller -F py.py

  22, PyInstaller source Python library used to be packed. Given a source file py.py and an icon file py.ico, please use these two files packaged produce an executable file:

  pyinstaller -I py.ico -F py.py

  23, txt represent some Chinese text, please add the code, the output of all the possible results of the text word

  import jieba

  txt = 'People's Republic of China Ministry of Education Examination Center'

  ls = jieba.lcut(txt, cut_all=True)

  print (ls)

  [ 'China', 'Chinese people', 'People's Republic of China', 'People's Republic of China Ministry of Education,' 'Chinese', 'the people', 'People's Republic', 'republican', 'Republic', 'state religion', ' education ',' Ministry of Education '' Education examination Center ',' test ',' center ']

  24, open a file a.txt, if the file does not exist, create, there is an exception and an alarm is generated

  try:

  f = open('a.txt', 'x')

  except:

  print ( 'file exists, please read carefully!')

  25, ls is a list, as follows: ls = [123, '456', 789, '123', 456, '789'], an increase in the element 789 '012'

  ls = [123 '456' 789, '123' 456 '789']

  ls.insert(3, '012')

  print (ls)

  [123, ‘456’, 789, ‘012’, ‘123’, 456, ‘789’]

  26, ls is a list, as follows: ls = [123, '456', 789, '123', 456, '789'], using remove () method, using single statement, deleting element 789.

  ls = [123 '456' 789, '123' 456 '789']

  ls.remove(789)

  print (ls)

  27, is a list of ls, as follows: ls = [123, '456', 789, '123', 456, '789'], please list ls reverse printing.

  ls = [123 '456' 789, '123' 456 '789']

  print (ls [:: - 1])

  [‘789’, 456, ‘123’, 789, ‘456’, 123]

  28, is a list of ls, as follows: ls = [123, '456', 789, '123', 456, '789'], the first number in the list ls position 789 appears once printed. Be careful not to direct output number, using a list operation method.

  ls = [123 '456' 789, '123' 456 '789']

  print(ls.index(789))

  29, d is a dictionary, as follows: d = {123: '123', 456: '456', 789: '789'}, please add the following code, the value of the output of all the dictionary d in list form.

  d = {123: '123', 456: '456', 789: '789'}

  print(list(d.values()))

  30, d is a dictionary, as follows: d = {123: '123', 456: '456', 789: '789'}, all the keys in the dictionary d outputs a list form.

  d = {123: '123', 456: '456', 789: '789'}

  print(list(d.keys()))

Guess you like

Origin www.cnblogs.com/dream8023/p/11039258.html