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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures"
  8. ]
  9. },
  10. {
  11. "cell_type": "markdown",
  12. "metadata": {},
  13. "source": [
  14. "# Print Statement"
  15. ]
  16. },
  17. {
  18. "cell_type": "markdown",
  19. "metadata": {},
  20. "source": [
  21. "The **print** statement can be used in the following different ways :\n",
  22. "\n",
  23. " - print(\"Hello World\")\n",
  24. " - print(\"Hello\", <Variable Containing the String>)\n",
  25. " - print(\"Hello\" + <Variable Containing the String>)\n",
  26. " - print(\"Hello %s\" % <variable containing the string>)"
  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. "In Python, single, double and triple quotes are used to denote a string.\n",
  51. "Most use single quotes when declaring a single character. \n",
  52. "Double quotes when declaring a line and triple quotes when declaring a paragraph/multiple lines."
  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. ]
  66. }
  67. ],
  68. "source": [
  69. "print('Hey')"
  70. ]
  71. },
  72. {
  73. "cell_type": "code",
  74. "execution_count": 3,
  75. "metadata": {},
  76. "outputs": [
  77. {
  78. "name": "stdout",
  79. "output_type": "stream",
  80. "text": [
  81. "My name is Rajath Kumar M.P.\n",
  82. "\n",
  83. "I love Python.\n"
  84. ]
  85. }
  86. ],
  87. "source": [
  88. "print(\"\"\"My name is Rajath Kumar M.P.\n",
  89. "\n",
  90. "I love Python.\"\"\")"
  91. ]
  92. },
  93. {
  94. "cell_type": "markdown",
  95. "metadata": {},
  96. "source": [
  97. "Strings can be assigned to variable say _string1_ and _string2_ which can called when using the print statement."
  98. ]
  99. },
  100. {
  101. "cell_type": "code",
  102. "execution_count": 1,
  103. "metadata": {
  104. "scrolled": true
  105. },
  106. "outputs": [
  107. {
  108. "name": "stdout",
  109. "output_type": "stream",
  110. "text": [
  111. "Hello World\n",
  112. "Hello World !\n"
  113. ]
  114. }
  115. ],
  116. "source": [
  117. "string1 = 'World'\n",
  118. "print('Hello', string1)\n",
  119. "\n",
  120. "string2 = '!'\n",
  121. "print('Hello', string1, string2)"
  122. ]
  123. },
  124. {
  125. "cell_type": "markdown",
  126. "metadata": {},
  127. "source": [
  128. "String concatenation is the \"addition\" of two strings. Observe that while concatenating there will be no space between the strings."
  129. ]
  130. },
  131. {
  132. "cell_type": "code",
  133. "execution_count": 2,
  134. "metadata": {},
  135. "outputs": [
  136. {
  137. "name": "stdout",
  138. "output_type": "stream",
  139. "text": [
  140. "HelloWorld!\n"
  141. ]
  142. }
  143. ],
  144. "source": [
  145. "print('Hello' + string1 + string2)"
  146. ]
  147. },
  148. {
  149. "cell_type": "markdown",
  150. "metadata": {},
  151. "source": [
  152. "**%s** is used to refer to a variable which contains a string."
  153. ]
  154. },
  155. {
  156. "cell_type": "code",
  157. "execution_count": 6,
  158. "metadata": {},
  159. "outputs": [
  160. {
  161. "name": "stdout",
  162. "output_type": "stream",
  163. "text": [
  164. "Hello World\n"
  165. ]
  166. }
  167. ],
  168. "source": [
  169. "print(\"Hello %s\" % string1)"
  170. ]
  171. },
  172. {
  173. "cell_type": "markdown",
  174. "metadata": {},
  175. "source": [
  176. "Similarly, when using other data types\n",
  177. "\n",
  178. " - %s -> string\n",
  179. " - %d -> Integer\n",
  180. " - %f -> Float\n",
  181. " - %o -> Octal\n",
  182. " - %x -> Hexadecimal\n",
  183. " - %e -> exponential\n",
  184. " \n",
  185. "This can be used for conversions inside the print statement itself."
  186. ]
  187. },
  188. {
  189. "cell_type": "code",
  190. "execution_count": 3,
  191. "metadata": {},
  192. "outputs": [
  193. {
  194. "name": "stdout",
  195. "output_type": "stream",
  196. "text": [
  197. "Actual Number = 18\n",
  198. "Float of the number = 18.000000\n",
  199. "Octal equivalent of the number = 22\n",
  200. "Hexadecimal equivalent of the number = 12\n",
  201. "Exponential equivalent of the number = 1.800000e+01\n"
  202. ]
  203. }
  204. ],
  205. "source": [
  206. "print(\"Actual Number = %d\" % 18)\n",
  207. "print(\"Float of the number = %f\" % 18)\n",
  208. "print(\"Octal equivalent of the number = %o\" % 18)\n",
  209. "print(\"Hexadecimal equivalent of the number = %x\" % 18)\n",
  210. "print(\"Exponential equivalent of the number = %e\" % 18)"
  211. ]
  212. },
  213. {
  214. "cell_type": "markdown",
  215. "metadata": {},
  216. "source": [
  217. "When referring to multiple variables parenthesis is used."
  218. ]
  219. },
  220. {
  221. "cell_type": "code",
  222. "execution_count": 4,
  223. "metadata": {},
  224. "outputs": [
  225. {
  226. "name": "stdout",
  227. "output_type": "stream",
  228. "text": [
  229. "Hello World !\n"
  230. ]
  231. }
  232. ],
  233. "source": [
  234. "print(\"Hello %s %s\" % (string1,string2))"
  235. ]
  236. },
  237. {
  238. "cell_type": "markdown",
  239. "metadata": {},
  240. "source": [
  241. "## Other Examples"
  242. ]
  243. },
  244. {
  245. "cell_type": "markdown",
  246. "metadata": {},
  247. "source": [
  248. "The following are other different ways the print statement can be put to use."
  249. ]
  250. },
  251. {
  252. "cell_type": "code",
  253. "execution_count": 9,
  254. "metadata": {},
  255. "outputs": [
  256. {
  257. "name": "stdout",
  258. "output_type": "stream",
  259. "text": [
  260. "I want %d to be printed here\n"
  261. ]
  262. }
  263. ],
  264. "source": [
  265. "print(\"I want %%d to be printed %s\" %'here')"
  266. ]
  267. },
  268. {
  269. "cell_type": "code",
  270. "execution_count": 5,
  271. "metadata": {},
  272. "outputs": [
  273. {
  274. "name": "stdout",
  275. "output_type": "stream",
  276. "text": [
  277. "_A_A_A_A_A_A_A_A_A_A\n"
  278. ]
  279. }
  280. ],
  281. "source": [
  282. "print('_A'*10)"
  283. ]
  284. },
  285. {
  286. "cell_type": "code",
  287. "execution_count": 6,
  288. "metadata": {},
  289. "outputs": [
  290. {
  291. "name": "stdout",
  292. "output_type": "stream",
  293. "text": [
  294. "Jan\n",
  295. "Feb\n",
  296. "Mar\n",
  297. "Apr\n",
  298. "May\n",
  299. "Jun\n",
  300. "Jul\n",
  301. "Aug\n"
  302. ]
  303. }
  304. ],
  305. "source": [
  306. "print(\"Jan\\nFeb\\nMar\\nApr\\nMay\\nJun\\nJul\\nAug\")"
  307. ]
  308. },
  309. {
  310. "cell_type": "code",
  311. "execution_count": 12,
  312. "metadata": {},
  313. "outputs": [
  314. {
  315. "name": "stdout",
  316. "output_type": "stream",
  317. "text": [
  318. "Jan\n",
  319. "Feb\n",
  320. "Mar\n",
  321. "Apr\n",
  322. "May\n",
  323. "Jun\n",
  324. "Jul\n",
  325. "Aug\n"
  326. ]
  327. }
  328. ],
  329. "source": [
  330. "print(\"\\n\".join(\"Jan Feb Mar Apr May Jun Jul Aug\".split(\" \")))"
  331. ]
  332. },
  333. {
  334. "cell_type": "code",
  335. "execution_count": 12,
  336. "metadata": {},
  337. "outputs": [
  338. {
  339. "name": "stdout",
  340. "output_type": "stream",
  341. "text": [
  342. "I want \\n to be printed.\n"
  343. ]
  344. }
  345. ],
  346. "source": [
  347. "print(\"I want \\\\n to be printed.\")"
  348. ]
  349. },
  350. {
  351. "cell_type": "code",
  352. "execution_count": 13,
  353. "metadata": {},
  354. "outputs": [
  355. {
  356. "name": "stdout",
  357. "output_type": "stream",
  358. "text": [
  359. "\n",
  360. "Routine:\n",
  361. "\t- Eat\n",
  362. "\t- Sleep\n",
  363. "\t- Repeat\n",
  364. "\n"
  365. ]
  366. }
  367. ],
  368. "source": [
  369. "print \"\"\"\n",
  370. "Routine:\n",
  371. "\\t- Eat\n",
  372. "\\t- Sleep\\n\\t- Repeat\n",
  373. "\"\"\""
  374. ]
  375. },
  376. {
  377. "cell_type": "markdown",
  378. "metadata": {},
  379. "source": [
  380. "# PrecisionWidth and FieldWidth"
  381. ]
  382. },
  383. {
  384. "cell_type": "markdown",
  385. "metadata": {},
  386. "source": [
  387. "Fieldwidth is the width of the entire number and precision is the width towards the right. One can alter these widths based on the requirements.\n",
  388. "\n",
  389. "The default Precision Width is set to 6."
  390. ]
  391. },
  392. {
  393. "cell_type": "code",
  394. "execution_count": 14,
  395. "metadata": {},
  396. "outputs": [
  397. {
  398. "data": {
  399. "text/plain": [
  400. "'3.121312'"
  401. ]
  402. },
  403. "execution_count": 14,
  404. "metadata": {},
  405. "output_type": "execute_result"
  406. }
  407. ],
  408. "source": [
  409. "\"%f\" % 3.121312312312"
  410. ]
  411. },
  412. {
  413. "cell_type": "markdown",
  414. "metadata": {},
  415. "source": [
  416. "Notice upto 6 decimal points are returned. To specify the number of decimal points, '%(fieldwidth).(precisionwidth)f' is used."
  417. ]
  418. },
  419. {
  420. "cell_type": "code",
  421. "execution_count": 15,
  422. "metadata": {},
  423. "outputs": [
  424. {
  425. "data": {
  426. "text/plain": [
  427. "'3.12131'"
  428. ]
  429. },
  430. "execution_count": 15,
  431. "metadata": {},
  432. "output_type": "execute_result"
  433. }
  434. ],
  435. "source": [
  436. "\"%.5f\" % 3.121312312312"
  437. ]
  438. },
  439. {
  440. "cell_type": "markdown",
  441. "metadata": {},
  442. "source": [
  443. "If the field width is set more than the necessary than the data right aligns itself to adjust to the specified values."
  444. ]
  445. },
  446. {
  447. "cell_type": "code",
  448. "execution_count": 16,
  449. "metadata": {},
  450. "outputs": [
  451. {
  452. "data": {
  453. "text/plain": [
  454. "' 3.12131'"
  455. ]
  456. },
  457. "execution_count": 16,
  458. "metadata": {},
  459. "output_type": "execute_result"
  460. }
  461. ],
  462. "source": [
  463. "\"%9.5f\" % 3.121312312312"
  464. ]
  465. },
  466. {
  467. "cell_type": "markdown",
  468. "metadata": {},
  469. "source": [
  470. "Zero padding is done by adding a 0 at the start of fieldwidth."
  471. ]
  472. },
  473. {
  474. "cell_type": "code",
  475. "execution_count": 17,
  476. "metadata": {},
  477. "outputs": [
  478. {
  479. "data": {
  480. "text/plain": [
  481. "'00000000000003.12131'"
  482. ]
  483. },
  484. "execution_count": 17,
  485. "metadata": {},
  486. "output_type": "execute_result"
  487. }
  488. ],
  489. "source": [
  490. "\"%020.5f\" % 3.121312312312"
  491. ]
  492. },
  493. {
  494. "cell_type": "markdown",
  495. "metadata": {},
  496. "source": [
  497. "For proper alignment, a space can be left blank in the field width so that when a negative number is used, proper alignment is maintained."
  498. ]
  499. },
  500. {
  501. "cell_type": "code",
  502. "execution_count": 18,
  503. "metadata": {},
  504. "outputs": [
  505. {
  506. "name": "stdout",
  507. "output_type": "stream",
  508. "text": [
  509. " 3.121312\n",
  510. "-3.121312\n"
  511. ]
  512. }
  513. ],
  514. "source": [
  515. "print \"% 9f\" % 3.121312312312\n",
  516. "print \"% 9f\" % -3.121312312312"
  517. ]
  518. },
  519. {
  520. "cell_type": "markdown",
  521. "metadata": {},
  522. "source": [
  523. "'+' sign can be returned at the beginning of a positive number by adding a + sign at the beginning of the field width."
  524. ]
  525. },
  526. {
  527. "cell_type": "code",
  528. "execution_count": 19,
  529. "metadata": {},
  530. "outputs": [
  531. {
  532. "name": "stdout",
  533. "output_type": "stream",
  534. "text": [
  535. "+3.121312\n",
  536. "-3.121312\n"
  537. ]
  538. }
  539. ],
  540. "source": [
  541. "print \"%+9f\" % 3.121312312312\n",
  542. "print \"% 9f\" % -3.121312312312"
  543. ]
  544. },
  545. {
  546. "cell_type": "markdown",
  547. "metadata": {},
  548. "source": [
  549. "As mentioned above, the data right aligns itself when the field width mentioned is larger than the actualy field width. But left alignment can be done by specifying a negative symbol in the field width."
  550. ]
  551. },
  552. {
  553. "cell_type": "code",
  554. "execution_count": 20,
  555. "metadata": {},
  556. "outputs": [
  557. {
  558. "data": {
  559. "text/plain": [
  560. "'3.121 '"
  561. ]
  562. },
  563. "execution_count": 20,
  564. "metadata": {},
  565. "output_type": "execute_result"
  566. }
  567. ],
  568. "source": [
  569. "\"%-9.3f\" % 3.121312312312"
  570. ]
  571. }
  572. ],
  573. "metadata": {
  574. "kernelspec": {
  575. "display_name": "Python 3",
  576. "language": "python",
  577. "name": "python3"
  578. },
  579. "language_info": {
  580. "codemirror_mode": {
  581. "name": "ipython",
  582. "version": 3
  583. },
  584. "file_extension": ".py",
  585. "mimetype": "text/x-python",
  586. "name": "python",
  587. "nbconvert_exporter": "python",
  588. "pygments_lexer": "ipython3",
  589. "version": "3.5.2"
  590. }
  591. },
  592. "nbformat": 4,
  593. "nbformat_minor": 1
  594. }

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