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

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