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

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