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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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": 4,
  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": 5,
  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": 7,
  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": 8,
  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": 10,
  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": 11,
  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. "**NOTE: 当引用多个变量时使用圆括号。**"
  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": 14,
  276. "metadata": {},
  277. "outputs": [
  278. {
  279. "name": "stdout",
  280. "output_type": "stream",
  281. "text": [
  282. "_A_A_A_A_A_A_A_A_A_A\n"
  283. ]
  284. }
  285. ],
  286. "source": [
  287. "print('_A'*10)"
  288. ]
  289. },
  290. {
  291. "cell_type": "code",
  292. "execution_count": 15,
  293. "metadata": {},
  294. "outputs": [
  295. {
  296. "name": "stdout",
  297. "output_type": "stream",
  298. "text": [
  299. "Jan\n",
  300. "Feb\n",
  301. "Mar\n",
  302. "Apr\n",
  303. "May\n",
  304. "Jun\n",
  305. "Jul\n",
  306. "Aug\n"
  307. ]
  308. }
  309. ],
  310. "source": [
  311. "print(\"Jan\\nFeb\\nMar\\nApr\\nMay\\nJun\\nJul\\nAug\")"
  312. ]
  313. },
  314. {
  315. "cell_type": "code",
  316. "execution_count": 17,
  317. "metadata": {},
  318. "outputs": [
  319. {
  320. "name": "stdout",
  321. "output_type": "stream",
  322. "text": [
  323. "Jan\n",
  324. "Feb\n",
  325. "Mar\n",
  326. "Apr\n",
  327. "May\n",
  328. "Jun\n",
  329. "Jul\n",
  330. "Aug\n"
  331. ]
  332. }
  333. ],
  334. "source": [
  335. "print(\"\\n\".join(\"Jan Feb Mar Apr May Jun Jul Aug\".split(\" \")))"
  336. ]
  337. },
  338. {
  339. "cell_type": "code",
  340. "execution_count": 20,
  341. "metadata": {},
  342. "outputs": [
  343. {
  344. "name": "stdout",
  345. "output_type": "stream",
  346. "text": [
  347. "I want \\n to be printed.\n"
  348. ]
  349. }
  350. ],
  351. "source": [
  352. "print(\"I want \\\\n to be printed.\")"
  353. ]
  354. },
  355. {
  356. "cell_type": "code",
  357. "execution_count": 22,
  358. "metadata": {},
  359. "outputs": [
  360. {
  361. "name": "stdout",
  362. "output_type": "stream",
  363. "text": [
  364. "\n",
  365. "Routine:\n",
  366. "\t- Eat (\\t)\n",
  367. "\t- Sleep\n",
  368. "\t- Repeat\n",
  369. "\n"
  370. ]
  371. }
  372. ],
  373. "source": [
  374. "print(\"\"\"\n",
  375. "Routine:\n",
  376. "\\t- Eat (\\\\t)\n",
  377. "\\t- Sleep\\n\\t- Repeat\n",
  378. "\"\"\")"
  379. ]
  380. },
  381. {
  382. "cell_type": "markdown",
  383. "metadata": {},
  384. "source": [
  385. "## 4. `PrecisionWidth`和`FieldWidth`"
  386. ]
  387. },
  388. {
  389. "cell_type": "markdown",
  390. "metadata": {},
  391. "source": [
  392. "Fieldwidth是整个数字的宽度,精度是向右的宽度。你可以根据需要改变这些宽度。\n",
  393. "\n",
  394. "默认的精度宽度设置为6。"
  395. ]
  396. },
  397. {
  398. "cell_type": "code",
  399. "execution_count": 23,
  400. "metadata": {},
  401. "outputs": [
  402. {
  403. "data": {
  404. "text/plain": [
  405. "'3.121312'"
  406. ]
  407. },
  408. "execution_count": 23,
  409. "metadata": {},
  410. "output_type": "execute_result"
  411. }
  412. ],
  413. "source": [
  414. "\"%f\" % 3.121312312312"
  415. ]
  416. },
  417. {
  418. "cell_type": "markdown",
  419. "metadata": {},
  420. "source": [
  421. "注意,最多返回6个小数点。要指定小数点的数目,使用'%(fieldwidth).(precisionwidth)f'。"
  422. ]
  423. },
  424. {
  425. "cell_type": "code",
  426. "execution_count": 24,
  427. "metadata": {},
  428. "outputs": [
  429. {
  430. "data": {
  431. "text/plain": [
  432. "'3.12131'"
  433. ]
  434. },
  435. "execution_count": 24,
  436. "metadata": {},
  437. "output_type": "execute_result"
  438. }
  439. ],
  440. "source": [
  441. "\"%.5f\" % 3.121312312312"
  442. ]
  443. },
  444. {
  445. "cell_type": "markdown",
  446. "metadata": {},
  447. "source": [
  448. "如果字段宽度设置超过所需,则数据将自己对齐以调整到指定的值。"
  449. ]
  450. },
  451. {
  452. "cell_type": "code",
  453. "execution_count": 25,
  454. "metadata": {},
  455. "outputs": [
  456. {
  457. "data": {
  458. "text/plain": [
  459. "'-33.12131'"
  460. ]
  461. },
  462. "execution_count": 25,
  463. "metadata": {},
  464. "output_type": "execute_result"
  465. }
  466. ],
  467. "source": [
  468. "\"%9.5f\" % -33.121312312312"
  469. ]
  470. },
  471. {
  472. "cell_type": "markdown",
  473. "metadata": {},
  474. "source": [
  475. "零填充通过在字段宽度的开始处添加一个0来完成。"
  476. ]
  477. },
  478. {
  479. "cell_type": "code",
  480. "execution_count": 28,
  481. "metadata": {},
  482. "outputs": [
  483. {
  484. "data": {
  485. "text/plain": [
  486. "'00000000000003.12131'"
  487. ]
  488. },
  489. "execution_count": 28,
  490. "metadata": {},
  491. "output_type": "execute_result"
  492. }
  493. ],
  494. "source": [
  495. "\"%020.5f\" % 3.121312312312"
  496. ]
  497. },
  498. {
  499. "cell_type": "markdown",
  500. "metadata": {},
  501. "source": [
  502. "为了正确对齐,字段宽度中的空格可以留空,以便在使用负数时保持正确的对齐。"
  503. ]
  504. },
  505. {
  506. "cell_type": "code",
  507. "execution_count": 29,
  508. "metadata": {},
  509. "outputs": [
  510. {
  511. "name": "stdout",
  512. "output_type": "stream",
  513. "text": [
  514. " 3.121312\n",
  515. "-3.121312\n"
  516. ]
  517. }
  518. ],
  519. "source": [
  520. "print(\"% 9f\" % 3.121312312312)\n",
  521. "print(\"% 9f\" % -3.121312312312)"
  522. ]
  523. },
  524. {
  525. "cell_type": "markdown",
  526. "metadata": {},
  527. "source": [
  528. "通过在字段宽度的开始处添加一个+号,可以在正数的开始处返回'+'号。"
  529. ]
  530. },
  531. {
  532. "cell_type": "code",
  533. "execution_count": 30,
  534. "metadata": {},
  535. "outputs": [
  536. {
  537. "name": "stdout",
  538. "output_type": "stream",
  539. "text": [
  540. "+3.121312\n",
  541. "-3.121312\n"
  542. ]
  543. }
  544. ],
  545. "source": [
  546. "print(\"%+9f\" % 3.121312312312)\n",
  547. "print(\"% 9f\" % -3.121312312312)"
  548. ]
  549. },
  550. {
  551. "cell_type": "markdown",
  552. "metadata": {},
  553. "source": [
  554. "如上所述,当提到的字段宽度大于实际字段宽度时,数据右对齐。但是可以通过在字段宽度中指定一个负符号来实现左对齐。"
  555. ]
  556. },
  557. {
  558. "cell_type": "code",
  559. "execution_count": 25,
  560. "metadata": {},
  561. "outputs": [
  562. {
  563. "data": {
  564. "text/plain": [
  565. "'3.121 '"
  566. ]
  567. },
  568. "execution_count": 25,
  569. "metadata": {},
  570. "output_type": "execute_result"
  571. }
  572. ],
  573. "source": [
  574. "\"%-9.3f\" % 3.121312312312"
  575. ]
  576. }
  577. ],
  578. "metadata": {
  579. "kernelspec": {
  580. "display_name": "Python 3",
  581. "language": "python",
  582. "name": "python3"
  583. },
  584. "language_info": {
  585. "codemirror_mode": {
  586. "name": "ipython",
  587. "version": 3
  588. },
  589. "file_extension": ".py",
  590. "mimetype": "text/x-python",
  591. "name": "python",
  592. "nbconvert_exporter": "python",
  593. "pygments_lexer": "ipython3",
  594. "version": "3.5.4"
  595. }
  596. },
  597. "nbformat": 4,
  598. "nbformat_minor": 1
  599. }

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