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

2_Print_Statement.ipynb 11 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Print 函数"
  8. ]
  9. },
  10. {
  11. "cell_type": "markdown",
  12. "metadata": {},
  13. "source": [
  14. "`print` 是Python内置的一个函数,可以用下列不同的方式使用。\n",
  15. "\n",
  16. " - print(\"Hello World\")\n",
  17. " - print(\"Hello\", <Variable Containing the String>)\n",
  18. " - print(\"Hello\" + <Variable Containing the String>)\n",
  19. " - print(\"Hello %s\" % <variable containing the string>)"
  20. ]
  21. },
  22. {
  23. "cell_type": "code",
  24. "execution_count": 1,
  25. "metadata": {},
  26. "outputs": [
  27. {
  28. "name": "stdout",
  29. "output_type": "stream",
  30. "text": [
  31. "Hello World\n"
  32. ]
  33. }
  34. ],
  35. "source": [
  36. "print(\"Hello World\")"
  37. ]
  38. },
  39. {
  40. "cell_type": "markdown",
  41. "metadata": {},
  42. "source": [
  43. "在Python中,单引号、双引号和三引号用于表示字符串。\n",
  44. "大部分单引号用于声明一个字符。\n",
  45. "声明一行时使用双引号,声明段落/多行时使用三引号。"
  46. ]
  47. },
  48. {
  49. "cell_type": "code",
  50. "execution_count": 2,
  51. "metadata": {},
  52. "outputs": [
  53. {
  54. "name": "stdout",
  55. "output_type": "stream",
  56. "text": [
  57. "Hey\n",
  58. "line1line2\n"
  59. ]
  60. }
  61. ],
  62. "source": [
  63. "print('Hey')\n",
  64. "a = 'line1\\\n",
  65. "line2\\\n",
  66. "\\\n",
  67. "'\n",
  68. "print(a)"
  69. ]
  70. },
  71. {
  72. "cell_type": "code",
  73. "execution_count": 6,
  74. "metadata": {},
  75. "outputs": [
  76. {
  77. "name": "stdout",
  78. "output_type": "stream",
  79. "text": [
  80. "My name is Rajath Kumar M.P.\n",
  81. "\n",
  82. "I love Python.\n"
  83. ]
  84. }
  85. ],
  86. "source": [
  87. "print(\"\"\"My name is Rajath Kumar M.P.\n",
  88. "\n",
  89. "I love Python.\"\"\")"
  90. ]
  91. },
  92. {
  93. "cell_type": "markdown",
  94. "metadata": {},
  95. "source": [
  96. "字符串可以分配给变量 _string1_ 和string2,使用`print`语句时可以调用。"
  97. ]
  98. },
  99. {
  100. "cell_type": "code",
  101. "execution_count": 3,
  102. "metadata": {
  103. "scrolled": true
  104. },
  105. "outputs": [
  106. {
  107. "name": "stdout",
  108. "output_type": "stream",
  109. "text": [
  110. "Hello World\n",
  111. "Hello World !\n"
  112. ]
  113. }
  114. ],
  115. "source": [
  116. "string1 = 'World'\n",
  117. "print('Hello', string1)\n",
  118. "\n",
  119. "string2 = '!'\n",
  120. "print('Hello', string1, string2)"
  121. ]
  122. },
  123. {
  124. "cell_type": "markdown",
  125. "metadata": {},
  126. "source": [
  127. "字符串连接是两个字符串的“加法”。注意,在连接时,字符串之间不会有空格。"
  128. ]
  129. },
  130. {
  131. "cell_type": "code",
  132. "execution_count": 4,
  133. "metadata": {},
  134. "outputs": [
  135. {
  136. "name": "stdout",
  137. "output_type": "stream",
  138. "text": [
  139. "HelloWorld!\n"
  140. ]
  141. }
  142. ],
  143. "source": [
  144. "print('Hello' + string1 + string2)"
  145. ]
  146. },
  147. {
  148. "cell_type": "markdown",
  149. "metadata": {},
  150. "source": [
  151. "**%s** 用于引用包含字符串的变量。"
  152. ]
  153. },
  154. {
  155. "cell_type": "code",
  156. "execution_count": 5,
  157. "metadata": {},
  158. "outputs": [
  159. {
  160. "name": "stdout",
  161. "output_type": "stream",
  162. "text": [
  163. "Hello World\n"
  164. ]
  165. }
  166. ],
  167. "source": [
  168. "print(\"Hello %s\" % string1)"
  169. ]
  170. },
  171. {
  172. "cell_type": "markdown",
  173. "metadata": {},
  174. "source": [
  175. "相似地,当我们使用其他的数据类型时\n",
  176. "\n",
  177. " - %s -> string\n",
  178. " - %d -> Integer\n",
  179. " - %f -> Float\n",
  180. " - %o -> Octal\n",
  181. " - %x -> Hexadecimal\n",
  182. " - %e -> exponential\n",
  183. " \n",
  184. "这可以用于`print`函数本身内部的转换。"
  185. ]
  186. },
  187. {
  188. "cell_type": "code",
  189. "execution_count": 6,
  190. "metadata": {},
  191. "outputs": [
  192. {
  193. "name": "stdout",
  194. "output_type": "stream",
  195. "text": [
  196. "Actual Number = 18\n",
  197. "Float of the number = 18.000000\n",
  198. "Octal equivalent of the number = 22\n",
  199. "Hexadecimal equivalent of the number = 12\n",
  200. "Exponential equivalent of the number = 1.800000e+01\n"
  201. ]
  202. }
  203. ],
  204. "source": [
  205. "print(\"Actual Number = %d\" % 18)\n",
  206. "print(\"Float of the number = %f\" % 18)\n",
  207. "print(\"Octal equivalent of the number = %o\" % 18)\n",
  208. "print(\"Hexadecimal equivalent of the number = %x\" % 18)\n",
  209. "print(\"Exponential equivalent of the number = %e\" % 18)"
  210. ]
  211. },
  212. {
  213. "cell_type": "markdown",
  214. "metadata": {},
  215. "source": [
  216. "当引用多个变量时使用圆括号。"
  217. ]
  218. },
  219. {
  220. "cell_type": "code",
  221. "execution_count": 9,
  222. "metadata": {},
  223. "outputs": [
  224. {
  225. "name": "stdout",
  226. "output_type": "stream",
  227. "text": [
  228. "Hello World !\n"
  229. ]
  230. }
  231. ],
  232. "source": [
  233. "print(\"Hello %s %s\" % (string1,string2))"
  234. ]
  235. },
  236. {
  237. "cell_type": "markdown",
  238. "metadata": {},
  239. "source": [
  240. "## 1. 其他例子"
  241. ]
  242. },
  243. {
  244. "cell_type": "markdown",
  245. "metadata": {},
  246. "source": [
  247. "下面是使用print语句的其他不同方式。"
  248. ]
  249. },
  250. {
  251. "cell_type": "code",
  252. "execution_count": 10,
  253. "metadata": {},
  254. "outputs": [
  255. {
  256. "name": "stdout",
  257. "output_type": "stream",
  258. "text": [
  259. "I want to be printed here\n"
  260. ]
  261. }
  262. ],
  263. "source": [
  264. "print(\"I want to be printed %s\" % 'here')"
  265. ]
  266. },
  267. {
  268. "cell_type": "code",
  269. "execution_count": 11,
  270. "metadata": {},
  271. "outputs": [
  272. {
  273. "name": "stdout",
  274. "output_type": "stream",
  275. "text": [
  276. "_A_A_A_A_A_A_A_A_A_A\n"
  277. ]
  278. }
  279. ],
  280. "source": [
  281. "print('_A'*10)"
  282. ]
  283. },
  284. {
  285. "cell_type": "code",
  286. "execution_count": 12,
  287. "metadata": {},
  288. "outputs": [
  289. {
  290. "name": "stdout",
  291. "output_type": "stream",
  292. "text": [
  293. "Jan\n",
  294. "Feb\n",
  295. "Mar\n",
  296. "Apr\n",
  297. "May\n",
  298. "Jun\n",
  299. "Jul\n",
  300. "Aug\n"
  301. ]
  302. }
  303. ],
  304. "source": [
  305. "print(\"Jan\\nFeb\\nMar\\nApr\\nMay\\nJun\\nJul\\nAug\")"
  306. ]
  307. },
  308. {
  309. "cell_type": "code",
  310. "execution_count": 13,
  311. "metadata": {},
  312. "outputs": [
  313. {
  314. "name": "stdout",
  315. "output_type": "stream",
  316. "text": [
  317. "Jan\n",
  318. "Feb\n",
  319. "Mar\n",
  320. "Apr\n",
  321. "May\n",
  322. "Jun\n",
  323. "Jul\n",
  324. "Aug\n"
  325. ]
  326. }
  327. ],
  328. "source": [
  329. "print(\"\\n\".join(\"Jan Feb Mar Apr May Jun Jul Aug\".split(\" \")))"
  330. ]
  331. },
  332. {
  333. "cell_type": "code",
  334. "execution_count": 14,
  335. "metadata": {},
  336. "outputs": [
  337. {
  338. "name": "stdout",
  339. "output_type": "stream",
  340. "text": [
  341. "I want \\n to be printed.\n"
  342. ]
  343. }
  344. ],
  345. "source": [
  346. "print(\"I want \\\\n to be printed.\")"
  347. ]
  348. },
  349. {
  350. "cell_type": "code",
  351. "execution_count": 15,
  352. "metadata": {},
  353. "outputs": [
  354. {
  355. "name": "stdout",
  356. "output_type": "stream",
  357. "text": [
  358. "\n",
  359. "Routine:\n",
  360. "\t- Eat\n",
  361. "\t- Sleep\n",
  362. "\t- Repeat\n",
  363. "\n"
  364. ]
  365. }
  366. ],
  367. "source": [
  368. "print(\"\"\"\n",
  369. "Routine:\n",
  370. "\\t- Eat\n",
  371. "\\t- Sleep\\n\\t- Repeat\n",
  372. "\"\"\")"
  373. ]
  374. },
  375. {
  376. "cell_type": "markdown",
  377. "metadata": {},
  378. "source": [
  379. "# 2. `PrecisionWidth`和`FieldWidth`"
  380. ]
  381. },
  382. {
  383. "cell_type": "markdown",
  384. "metadata": {},
  385. "source": [
  386. "Fieldwidth是整个数字的宽度,精度是向右的宽度。你可以根据需要改变这些宽度。\n",
  387. "\n",
  388. "默认的精度宽度设置为6。"
  389. ]
  390. },
  391. {
  392. "cell_type": "code",
  393. "execution_count": 16,
  394. "metadata": {},
  395. "outputs": [
  396. {
  397. "data": {
  398. "text/plain": [
  399. "'3.121312'"
  400. ]
  401. },
  402. "execution_count": 16,
  403. "metadata": {},
  404. "output_type": "execute_result"
  405. }
  406. ],
  407. "source": [
  408. "\"%f\" % 3.121312312312"
  409. ]
  410. },
  411. {
  412. "cell_type": "markdown",
  413. "metadata": {},
  414. "source": [
  415. "注意,最多返回6个小数点。要指定小数点的数目,使用'%(fieldwidth).(precisionwidth)f'。"
  416. ]
  417. },
  418. {
  419. "cell_type": "code",
  420. "execution_count": 17,
  421. "metadata": {},
  422. "outputs": [
  423. {
  424. "data": {
  425. "text/plain": [
  426. "'3.12131'"
  427. ]
  428. },
  429. "execution_count": 17,
  430. "metadata": {},
  431. "output_type": "execute_result"
  432. }
  433. ],
  434. "source": [
  435. "\"%.5f\" % 3.121312312312"
  436. ]
  437. },
  438. {
  439. "cell_type": "markdown",
  440. "metadata": {},
  441. "source": [
  442. "如果字段宽度设置超过所需,则数据将自己对齐以调整到指定的值。"
  443. ]
  444. },
  445. {
  446. "cell_type": "code",
  447. "execution_count": 18,
  448. "metadata": {},
  449. "outputs": [
  450. {
  451. "data": {
  452. "text/plain": [
  453. "'-33.12131'"
  454. ]
  455. },
  456. "execution_count": 18,
  457. "metadata": {},
  458. "output_type": "execute_result"
  459. }
  460. ],
  461. "source": [
  462. "\"%9.5f\" % -33.121312312312"
  463. ]
  464. },
  465. {
  466. "cell_type": "markdown",
  467. "metadata": {},
  468. "source": [
  469. "零填充通过在字段宽度的开始处添加一个0来完成。"
  470. ]
  471. },
  472. {
  473. "cell_type": "code",
  474. "execution_count": 19,
  475. "metadata": {},
  476. "outputs": [
  477. {
  478. "data": {
  479. "text/plain": [
  480. "'00000000000003.12131'"
  481. ]
  482. },
  483. "execution_count": 19,
  484. "metadata": {},
  485. "output_type": "execute_result"
  486. }
  487. ],
  488. "source": [
  489. "\"%020.5f\" % 3.121312312312"
  490. ]
  491. },
  492. {
  493. "cell_type": "markdown",
  494. "metadata": {},
  495. "source": [
  496. "为了正确对齐,字段宽度中的空格可以留空,以便在使用负数时保持正确的对齐。"
  497. ]
  498. },
  499. {
  500. "cell_type": "code",
  501. "execution_count": 21,
  502. "metadata": {},
  503. "outputs": [
  504. {
  505. "name": "stdout",
  506. "output_type": "stream",
  507. "text": [
  508. " 3.121312\n",
  509. "-3.121312\n"
  510. ]
  511. }
  512. ],
  513. "source": [
  514. "print(\"% 9f\" % 3.121312312312)\n",
  515. "print(\"% 9f\" % -3.121312312312)"
  516. ]
  517. },
  518. {
  519. "cell_type": "markdown",
  520. "metadata": {},
  521. "source": [
  522. "通过在字段宽度的开始处添加一个+号,可以在正数的开始处返回'+'号。"
  523. ]
  524. },
  525. {
  526. "cell_type": "code",
  527. "execution_count": 23,
  528. "metadata": {},
  529. "outputs": [
  530. {
  531. "name": "stdout",
  532. "output_type": "stream",
  533. "text": [
  534. "+3.121312\n",
  535. "-3.121312\n"
  536. ]
  537. }
  538. ],
  539. "source": [
  540. "print(\"%+9f\" % 3.121312312312)\n",
  541. "print(\"% 9f\" % -3.121312312312)"
  542. ]
  543. },
  544. {
  545. "cell_type": "markdown",
  546. "metadata": {},
  547. "source": [
  548. "如上所述,当提到的字段宽度大于实际字段宽度时,数据右对齐。但是可以通过在字段宽度中指定一个负符号来实现左对齐。"
  549. ]
  550. },
  551. {
  552. "cell_type": "code",
  553. "execution_count": 16,
  554. "metadata": {},
  555. "outputs": [
  556. {
  557. "data": {
  558. "text/plain": [
  559. "'3.121 '"
  560. ]
  561. },
  562. "execution_count": 16,
  563. "metadata": {},
  564. "output_type": "execute_result"
  565. }
  566. ],
  567. "source": [
  568. "\"%-9.3f\" % 3.121312312312"
  569. ]
  570. }
  571. ],
  572. "metadata": {
  573. "kernelspec": {
  574. "display_name": "Python 3",
  575. "language": "python",
  576. "name": "python3"
  577. },
  578. "language_info": {
  579. "codemirror_mode": {
  580. "name": "ipython",
  581. "version": 3
  582. },
  583. "file_extension": ".py",
  584. "mimetype": "text/x-python",
  585. "name": "python",
  586. "nbconvert_exporter": "python",
  587. "pygments_lexer": "ipython3",
  588. "version": "3.6.9"
  589. }
  590. },
  591. "nbformat": 4,
  592. "nbformat_minor": 1
  593. }

机器学习越来越多应用到飞行器、机器人等领域,其目的是利用计算机实现类似人类的智能,从而实现装备的智能化与无人化。本课程旨在引导学生掌握机器学习的基本知识、典型方法与技术,通过具体的应用案例激发学生对该学科的兴趣,鼓励学生能够从人工智能的角度来分析、解决飞行器、机器人所面临的问题和挑战。本课程主要内容包括Python编程基础,机器学习模型,无监督学习、监督学习、深度学习基础知识与实现,并学习如何利用机器学习解决实际问题,从而全面提升自我的《综合能力》。