You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
3.6 KiB

2 years ago
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "1. 计算日平均气温"
  8. ]
  9. },
  10. {
  11. "cell_type": "code",
  12. "execution_count": 1,
  13. "metadata": {},
  14. "outputs": [
  15. {
  16. "name": "stdout",
  17. "output_type": "stream",
  18. "text": [
  19. "13.25\n",
  20. "13.25\n"
  21. ]
  22. }
  23. ],
  24. "source": [
  25. "t_Day = [10, 13, 18, 12]\n",
  26. "#方法1:使用算术表达式\n",
  27. "avg = (t_Day[0] + t_Day[1] + t_Day[2] + t_Day[3]) / 4\n",
  28. "print(avg)\n",
  29. "#方法2:使用内置函数计算\n",
  30. "avg = sum(t_Day) / len(t_Day)\n",
  31. "print(avg)"
  32. ]
  33. },
  34. {
  35. "cell_type": "markdown",
  36. "metadata": {},
  37. "source": [
  38. "2. 计算月平均气温"
  39. ]
  40. },
  41. {
  42. "cell_type": "code",
  43. "execution_count": 2,
  44. "metadata": {},
  45. "outputs": [
  46. {
  47. "name": "stdout",
  48. "output_type": "stream",
  49. "text": [
  50. "31.677419354838708\n",
  51. "31.677419354838708\n"
  52. ]
  53. }
  54. ],
  55. "source": [
  56. "t_Day = [34,32,33,33,35,36,36,36,35,34,33,32,32,32,31,32,32,30,31,30,30,31,32,29,27,29,31,32,30,26,26] \n",
  57. "#下标访问方法\n",
  58. "s = 0\n",
  59. "n = 0\n",
  60. "for i in range(len(t_Day)):\n",
  61. " s = s + t_Day[i]\n",
  62. " n = n + 1\n",
  63. "avg = s / n \n",
  64. "print(avg)\n",
  65. "#序列迭代访问方法\n",
  66. "s = 0\n",
  67. "n = 0\n",
  68. "for x in t_Day:\n",
  69. " s = s + x \n",
  70. " n = n + 1 \n",
  71. "avg = s / n \n",
  72. "print(avg)"
  73. ]
  74. },
  75. {
  76. "cell_type": "markdown",
  77. "metadata": {},
  78. "source": [
  79. "3.使用用户自定义函数计算平均值"
  80. ]
  81. },
  82. {
  83. "cell_type": "code",
  84. "execution_count": 3,
  85. "metadata": {},
  86. "outputs": [
  87. {
  88. "name": "stdout",
  89. "output_type": "stream",
  90. "text": [
  91. "31.677419354838708\n"
  92. ]
  93. }
  94. ],
  95. "source": [
  96. "def getAvg(L):\n",
  97. " return sum(L) / len(L)\n",
  98. "avg = getAvg(t_Day)\n",
  99. "print(avg)"
  100. ]
  101. },
  102. {
  103. "cell_type": "markdown",
  104. "metadata": {},
  105. "source": [
  106. "4.读取文件中的数据到列表"
  107. ]
  108. },
  109. {
  110. "cell_type": "code",
  111. "execution_count": 4,
  112. "metadata": {},
  113. "outputs": [
  114. {
  115. "name": "stdout",
  116. "output_type": "stream",
  117. "text": [
  118. "['34\\n', '32\\n', '33\\n', '33\\n', '35\\n', '36\\n', '36\\n', '36\\n', '35\\n', '34\\n', '33\\n', '32\\n', '32\\n', '32\\n', '31\\n', '32\\n', '32\\n', '30\\n', '31\\n', '30\\n', '30\\n', '31\\n', '32\\n', '29\\n', '27\\n', '29\\n', '31\\n', '32\\n', '30\\n', '26\\n', '26']\n",
  119. "[34, 32, 33, 33, 35, 36, 36, 36, 35, 34, 33, 32, 32, 32, 31, 32, 32, 30, 31, 30, 30, 31, 32, 29, 27, 29, 31, 32, 30, 26, 26]\n"
  120. ]
  121. }
  122. ],
  123. "source": [
  124. "L = list(open(\"201308tq.txt\"))\n",
  125. "print(L)\n",
  126. "for i in range(len(L)):\n",
  127. " L[i] = int(L[i])\n",
  128. "print(L)"
  129. ]
  130. },
  131. {
  132. "cell_type": "code",
  133. "execution_count": null,
  134. "metadata": {},
  135. "outputs": [],
  136. "source": []
  137. }
  138. ],
  139. "metadata": {
  140. "kernelspec": {
  141. "display_name": "Python 3",
  142. "language": "python",
  143. "name": "python3"
  144. },
  145. "language_info": {
  146. "codemirror_mode": {
  147. "name": "ipython",
  148. "version": 3
  149. },
  150. "file_extension": ".py",
  151. "mimetype": "text/x-python",
  152. "name": "python",
  153. "nbconvert_exporter": "python",
  154. "pygments_lexer": "ipython3",
  155. "version": "3.7.3"
  156. },
  157. "toc": {
  158. "base_numbering": 1,
  159. "nav_menu": {},
  160. "number_sections": true,
  161. "sideBar": true,
  162. "skip_h1_title": false,
  163. "title_cell": "Table of Contents",
  164. "title_sidebar": "Contents",
  165. "toc_cell": false,
  166. "toc_position": {},
  167. "toc_section_display": true,
  168. "toc_window_display": false
  169. }
  170. },
  171. "nbformat": 4,
  172. "nbformat_minor": 2
  173. }