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

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

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