Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 25.66 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 19,
  6. "metadata": {},
  7. "outputs": [
  8. {
  9. "name": "stdout",
  10. "output_type": "stream",
  11. "text": [
  12. "Hello, Python!\n"
  13. ]
  14. }
  15. ],
  16. "source": [
  17. "# Try your first Python output\n",
  18. "\n",
  19. "print('Hello, Python!')"
  20. ]
  21. },
  22. {
  23. "cell_type": "code",
  24. "execution_count": 5,
  25. "metadata": {},
  26. "outputs": [
  27. {
  28. "name": "stdout",
  29. "output_type": "stream",
  30. "text": [
  31. "3.6.8 |Anaconda, Inc.| (default, Dec 30 2018, 01:22:34) \n",
  32. "[GCC 7.3.0]\n"
  33. ]
  34. }
  35. ],
  36. "source": [
  37. "# Check the Python Version\n",
  38. "\n",
  39. "import sys\n",
  40. "print(sys.version)"
  41. ]
  42. },
  43. {
  44. "cell_type": "code",
  45. "execution_count": 6,
  46. "metadata": {},
  47. "outputs": [
  48. {
  49. "name": "stdout",
  50. "output_type": "stream",
  51. "text": [
  52. "Hello, Python!\n"
  53. ]
  54. }
  55. ],
  56. "source": [
  57. "# Practice on writing comments\n",
  58. "\n",
  59. "print('Hello, Python!') # This line prints a string\n",
  60. "# print('Hi')"
  61. ]
  62. },
  63. {
  64. "cell_type": "code",
  65. "execution_count": 7,
  66. "metadata": {},
  67. "outputs": [
  68. {
  69. "ename": "NameError",
  70. "evalue": "name 'frint' is not defined",
  71. "output_type": "error",
  72. "traceback": [
  73. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  74. "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
  75. "\u001b[0;32m<ipython-input-7-313a1769a8a5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Print string as error message\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mfrint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Hello, Python!\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
  76. "\u001b[0;31mNameError\u001b[0m: name 'frint' is not defined"
  77. ]
  78. }
  79. ],
  80. "source": [
  81. "# Print string as error message\n",
  82. "\n",
  83. "frint(\"Hello, Python!\")"
  84. ]
  85. },
  86. {
  87. "cell_type": "code",
  88. "execution_count": 8,
  89. "metadata": {},
  90. "outputs": [
  91. {
  92. "ename": "SyntaxError",
  93. "evalue": "EOL while scanning string literal (<ipython-input-8-63a21a726720>, line 3)",
  94. "output_type": "error",
  95. "traceback": [
  96. "\u001b[0;36m File \u001b[0;32m\"<ipython-input-8-63a21a726720>\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m print(\"Hello, Python!)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m EOL while scanning string literal\n"
  97. ]
  98. }
  99. ],
  100. "source": [
  101. "# Try to see build in error message\n",
  102. "\n",
  103. "print(\"Hello, Python!)"
  104. ]
  105. },
  106. {
  107. "cell_type": "code",
  108. "execution_count": 9,
  109. "metadata": {},
  110. "outputs": [
  111. {
  112. "name": "stdout",
  113. "output_type": "stream",
  114. "text": [
  115. "This will be printed\n"
  116. ]
  117. },
  118. {
  119. "ename": "NameError",
  120. "evalue": "name 'frint' is not defined",
  121. "output_type": "error",
  122. "traceback": [
  123. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  124. "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
  125. "\u001b[0;32m<ipython-input-9-af59af1b345d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"This will be printed\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mfrint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"This will cause an error\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"This will NOT be printed\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
  126. "\u001b[0;31mNameError\u001b[0m: name 'frint' is not defined"
  127. ]
  128. }
  129. ],
  130. "source": [
  131. "# Print string and error to see the running order\n",
  132. "\n",
  133. "print(\"This will be printed\")\n",
  134. "frint(\"This will cause an error\")\n",
  135. "print(\"This will NOT be printed\")"
  136. ]
  137. },
  138. {
  139. "cell_type": "markdown",
  140. "metadata": {},
  141. "source": [
  142. "#### Exercise: Your First Program4"
  143. ]
  144. },
  145. {
  146. "cell_type": "code",
  147. "execution_count": 1,
  148. "metadata": {},
  149. "outputs": [
  150. {
  151. "name": "stdout",
  152. "output_type": "stream",
  153. "text": [
  154. "Hello, World!\n"
  155. ]
  156. }
  157. ],
  158. "source": [
  159. "print('Hello, World!')"
  160. ]
  161. },
  162. {
  163. "cell_type": "markdown",
  164. "metadata": {},
  165. "source": [
  166. "#### Now, let's enhance your code with a comment. In the code cell below, print out the phrase: Hello, world! and comment it with the phrase Print the traditional hello world all in one line of code."
  167. ]
  168. },
  169. {
  170. "cell_type": "code",
  171. "execution_count": null,
  172. "metadata": {},
  173. "outputs": [],
  174. "source": [
  175. "print(\"Hello, world!\") # Print the traditional hello world"
  176. ]
  177. },
  178. {
  179. "cell_type": "markdown",
  180. "metadata": {},
  181. "source": [
  182. "#### Types of objects in Python"
  183. ]
  184. },
  185. {
  186. "cell_type": "code",
  187. "execution_count": 2,
  188. "metadata": {},
  189. "outputs": [
  190. {
  191. "data": {
  192. "text/plain": [
  193. "11"
  194. ]
  195. },
  196. "execution_count": 2,
  197. "metadata": {},
  198. "output_type": "execute_result"
  199. }
  200. ],
  201. "source": [
  202. "#integer\n",
  203. "11"
  204. ]
  205. },
  206. {
  207. "cell_type": "code",
  208. "execution_count": 3,
  209. "metadata": {},
  210. "outputs": [
  211. {
  212. "data": {
  213. "text/plain": [
  214. "12.0"
  215. ]
  216. },
  217. "execution_count": 3,
  218. "metadata": {},
  219. "output_type": "execute_result"
  220. }
  221. ],
  222. "source": [
  223. "#float \n",
  224. "12.0"
  225. ]
  226. },
  227. {
  228. "cell_type": "code",
  229. "execution_count": 4,
  230. "metadata": {},
  231. "outputs": [
  232. {
  233. "data": {
  234. "text/plain": [
  235. "'Hello, Python 101!'"
  236. ]
  237. },
  238. "execution_count": 4,
  239. "metadata": {},
  240. "output_type": "execute_result"
  241. }
  242. ],
  243. "source": [
  244. "#string\n",
  245. "\"Hello, Python 101!\""
  246. ]
  247. },
  248. {
  249. "cell_type": "code",
  250. "execution_count": 5,
  251. "metadata": {},
  252. "outputs": [
  253. {
  254. "data": {
  255. "text/plain": [
  256. "int"
  257. ]
  258. },
  259. "execution_count": 5,
  260. "metadata": {},
  261. "output_type": "execute_result"
  262. }
  263. ],
  264. "source": [
  265. "# type of 12\n",
  266. "type(11)"
  267. ]
  268. },
  269. {
  270. "cell_type": "code",
  271. "execution_count": 6,
  272. "metadata": {},
  273. "outputs": [
  274. {
  275. "data": {
  276. "text/plain": [
  277. "float"
  278. ]
  279. },
  280. "execution_count": 6,
  281. "metadata": {},
  282. "output_type": "execute_result"
  283. }
  284. ],
  285. "source": [
  286. "# type of 12.0\n",
  287. "type(12.0)"
  288. ]
  289. },
  290. {
  291. "cell_type": "code",
  292. "execution_count": 7,
  293. "metadata": {},
  294. "outputs": [
  295. {
  296. "data": {
  297. "text/plain": [
  298. "str"
  299. ]
  300. },
  301. "execution_count": 7,
  302. "metadata": {},
  303. "output_type": "execute_result"
  304. }
  305. ],
  306. "source": [
  307. "# type 0f \"Hello, Python 101!\"\n",
  308. "type(\"Hello, Python 101!\")"
  309. ]
  310. },
  311. {
  312. "cell_type": "markdown",
  313. "metadata": {},
  314. "source": [
  315. "#### Converting from one object type to a different object type\n"
  316. ]
  317. },
  318. {
  319. "cell_type": "code",
  320. "execution_count": 8,
  321. "metadata": {},
  322. "outputs": [
  323. {
  324. "data": {
  325. "text/plain": [
  326. "int"
  327. ]
  328. },
  329. "execution_count": 8,
  330. "metadata": {},
  331. "output_type": "execute_result"
  332. }
  333. ],
  334. "source": [
  335. "# Verify that this is an integer\n",
  336. "type(2)"
  337. ]
  338. },
  339. {
  340. "cell_type": "code",
  341. "execution_count": 9,
  342. "metadata": {},
  343. "outputs": [
  344. {
  345. "data": {
  346. "text/plain": [
  347. "2.0"
  348. ]
  349. },
  350. "execution_count": 9,
  351. "metadata": {},
  352. "output_type": "execute_result"
  353. }
  354. ],
  355. "source": [
  356. "# Convert 2 to a float\n",
  357. "float(2)"
  358. ]
  359. },
  360. {
  361. "cell_type": "code",
  362. "execution_count": 10,
  363. "metadata": {},
  364. "outputs": [
  365. {
  366. "data": {
  367. "text/plain": [
  368. "2"
  369. ]
  370. },
  371. "execution_count": 10,
  372. "metadata": {},
  373. "output_type": "execute_result"
  374. }
  375. ],
  376. "source": [
  377. "# convert 2.0 to int\n",
  378. "int(2.0)"
  379. ]
  380. },
  381. {
  382. "cell_type": "code",
  383. "execution_count": 11,
  384. "metadata": {},
  385. "outputs": [
  386. {
  387. "data": {
  388. "text/plain": [
  389. "1"
  390. ]
  391. },
  392. "execution_count": 11,
  393. "metadata": {},
  394. "output_type": "execute_result"
  395. }
  396. ],
  397. "source": [
  398. "# Casting 1.1 to integer will result in loss of information\n",
  399. "int(1.1)"
  400. ]
  401. },
  402. {
  403. "cell_type": "code",
  404. "execution_count": 12,
  405. "metadata": {},
  406. "outputs": [
  407. {
  408. "data": {
  409. "text/plain": [
  410. "1"
  411. ]
  412. },
  413. "execution_count": 12,
  414. "metadata": {},
  415. "output_type": "execute_result"
  416. }
  417. ],
  418. "source": [
  419. "# Convert a string into an integer\n",
  420. "int('1')"
  421. ]
  422. },
  423. {
  424. "cell_type": "code",
  425. "execution_count": 13,
  426. "metadata": {},
  427. "outputs": [
  428. {
  429. "ename": "ValueError",
  430. "evalue": "invalid literal for int() with base 10: '1 or 2 people'",
  431. "output_type": "error",
  432. "traceback": [
  433. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  434. "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
  435. "\u001b[0;32m<ipython-input-13-b78145d165c7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Convert a string into an integer with error\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'1 or 2 people'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
  436. "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: '1 or 2 people'"
  437. ]
  438. }
  439. ],
  440. "source": [
  441. "# Convert a string into an integer with error\n",
  442. "\n",
  443. "int('1 or 2 people')"
  444. ]
  445. },
  446. {
  447. "cell_type": "code",
  448. "execution_count": 14,
  449. "metadata": {},
  450. "outputs": [
  451. {
  452. "data": {
  453. "text/plain": [
  454. "1.2"
  455. ]
  456. },
  457. "execution_count": 14,
  458. "metadata": {},
  459. "output_type": "execute_result"
  460. }
  461. ],
  462. "source": [
  463. "# Convert the string \"1.2\" into a float\n",
  464. "\n",
  465. "float('1.2')"
  466. ]
  467. },
  468. {
  469. "cell_type": "markdown",
  470. "metadata": {},
  471. "source": [
  472. "#### Boolean data type"
  473. ]
  474. },
  475. {
  476. "cell_type": "code",
  477. "execution_count": 15,
  478. "metadata": {},
  479. "outputs": [
  480. {
  481. "data": {
  482. "text/plain": [
  483. "True"
  484. ]
  485. },
  486. "execution_count": 15,
  487. "metadata": {},
  488. "output_type": "execute_result"
  489. }
  490. ],
  491. "source": [
  492. "# Value True\n",
  493. "True"
  494. ]
  495. },
  496. {
  497. "cell_type": "code",
  498. "execution_count": 16,
  499. "metadata": {},
  500. "outputs": [
  501. {
  502. "data": {
  503. "text/plain": [
  504. "False"
  505. ]
  506. },
  507. "execution_count": 16,
  508. "metadata": {},
  509. "output_type": "execute_result"
  510. }
  511. ],
  512. "source": [
  513. "# Value False\n",
  514. "False"
  515. ]
  516. },
  517. {
  518. "cell_type": "code",
  519. "execution_count": 17,
  520. "metadata": {},
  521. "outputs": [
  522. {
  523. "data": {
  524. "text/plain": [
  525. "bool"
  526. ]
  527. },
  528. "execution_count": 17,
  529. "metadata": {},
  530. "output_type": "execute_result"
  531. }
  532. ],
  533. "source": [
  534. "# type of True\n",
  535. "type(True)"
  536. ]
  537. },
  538. {
  539. "cell_type": "code",
  540. "execution_count": 18,
  541. "metadata": {},
  542. "outputs": [
  543. {
  544. "data": {
  545. "text/plain": [
  546. "bool"
  547. ]
  548. },
  549. "execution_count": 18,
  550. "metadata": {},
  551. "output_type": "execute_result"
  552. }
  553. ],
  554. "source": [
  555. "# type of false\n",
  556. "type(False)"
  557. ]
  558. },
  559. {
  560. "cell_type": "markdown",
  561. "metadata": {},
  562. "source": [
  563. "#### Tuples"
  564. ]
  565. },
  566. {
  567. "cell_type": "code",
  568. "execution_count": 1,
  569. "metadata": {},
  570. "outputs": [
  571. {
  572. "data": {
  573. "text/plain": [
  574. "('disco', 10, 1.2)"
  575. ]
  576. },
  577. "execution_count": 1,
  578. "metadata": {},
  579. "output_type": "execute_result"
  580. }
  581. ],
  582. "source": [
  583. "# Create your first tuple\n",
  584. "\n",
  585. "tuple1 = (\"disco\",10,1.2 )\n",
  586. "tuple1"
  587. ]
  588. },
  589. {
  590. "cell_type": "code",
  591. "execution_count": 2,
  592. "metadata": {},
  593. "outputs": [
  594. {
  595. "data": {
  596. "text/plain": [
  597. "tuple"
  598. ]
  599. },
  600. "execution_count": 2,
  601. "metadata": {},
  602. "output_type": "execute_result"
  603. }
  604. ],
  605. "source": [
  606. "# Print the type of the tuple you created\n",
  607. "\n",
  608. "type(tuple1)"
  609. ]
  610. },
  611. {
  612. "cell_type": "code",
  613. "execution_count": 3,
  614. "metadata": {},
  615. "outputs": [
  616. {
  617. "name": "stdout",
  618. "output_type": "stream",
  619. "text": [
  620. "disco\n",
  621. "10\n",
  622. "1.2\n"
  623. ]
  624. }
  625. ],
  626. "source": [
  627. "# Print the variable on each index\n",
  628. "\n",
  629. "print(tuple1[0])\n",
  630. "print(tuple1[1])\n",
  631. "print(tuple1[2])"
  632. ]
  633. },
  634. {
  635. "cell_type": "code",
  636. "execution_count": 4,
  637. "metadata": {},
  638. "outputs": [
  639. {
  640. "name": "stdout",
  641. "output_type": "stream",
  642. "text": [
  643. "<class 'str'>\n",
  644. "<class 'int'>\n",
  645. "<class 'float'>\n"
  646. ]
  647. }
  648. ],
  649. "source": [
  650. "# Print the type of value on each index\n",
  651. "\n",
  652. "print(type(tuple1[0]))\n",
  653. "print(type(tuple1[1]))\n",
  654. "print(type(tuple1[2]))"
  655. ]
  656. },
  657. {
  658. "cell_type": "code",
  659. "execution_count": 5,
  660. "metadata": {},
  661. "outputs": [
  662. {
  663. "data": {
  664. "text/plain": [
  665. "1.2"
  666. ]
  667. },
  668. "execution_count": 5,
  669. "metadata": {},
  670. "output_type": "execute_result"
  671. }
  672. ],
  673. "source": [
  674. "# Use negative index to get the value of the last element\n",
  675. "\n",
  676. "tuple1[-1]"
  677. ]
  678. },
  679. {
  680. "cell_type": "markdown",
  681. "metadata": {},
  682. "source": [
  683. "# Concatenate Tuples"
  684. ]
  685. },
  686. {
  687. "cell_type": "code",
  688. "execution_count": 8,
  689. "metadata": {},
  690. "outputs": [
  691. {
  692. "data": {
  693. "text/plain": [
  694. "('disco', 10, 1.2, 'hard rock', 10)"
  695. ]
  696. },
  697. "execution_count": 8,
  698. "metadata": {},
  699. "output_type": "execute_result"
  700. }
  701. ],
  702. "source": [
  703. "# Concatenate two tuples\n",
  704. "\n",
  705. "tuple2 = tuple1 + (\"hard rock\", 10)\n",
  706. "tuple2"
  707. ]
  708. },
  709. {
  710. "cell_type": "markdown",
  711. "metadata": {},
  712. "source": [
  713. "# Slicing"
  714. ]
  715. },
  716. {
  717. "cell_type": "code",
  718. "execution_count": 9,
  719. "metadata": {},
  720. "outputs": [
  721. {
  722. "data": {
  723. "text/plain": [
  724. "('disco', 10, 1.2)"
  725. ]
  726. },
  727. "execution_count": 9,
  728. "metadata": {},
  729. "output_type": "execute_result"
  730. }
  731. ],
  732. "source": [
  733. "# Slice from index 0 to index 2\n",
  734. "\n",
  735. "tuple2[0:3]\n"
  736. ]
  737. },
  738. {
  739. "cell_type": "markdown",
  740. "metadata": {},
  741. "source": [
  742. "# We can obtain the length of a tuple using the length command:"
  743. ]
  744. },
  745. {
  746. "cell_type": "code",
  747. "execution_count": 10,
  748. "metadata": {},
  749. "outputs": [
  750. {
  751. "data": {
  752. "text/plain": [
  753. "5"
  754. ]
  755. },
  756. "execution_count": 10,
  757. "metadata": {},
  758. "output_type": "execute_result"
  759. }
  760. ],
  761. "source": [
  762. "# Get the length of tuple\n",
  763. "\n",
  764. "len(tuple2)"
  765. ]
  766. },
  767. {
  768. "cell_type": "markdown",
  769. "metadata": {},
  770. "source": [
  771. "# Sorting"
  772. ]
  773. },
  774. {
  775. "cell_type": "code",
  776. "execution_count": 11,
  777. "metadata": {},
  778. "outputs": [],
  779. "source": [
  780. "# A sample tuple\n",
  781. "\n",
  782. "Ratings = (0, 9, 6, 5, 10, 8, 9, 6, 2)\n"
  783. ]
  784. },
  785. {
  786. "cell_type": "markdown",
  787. "metadata": {},
  788. "source": [
  789. "# We can sort the values in a tuple and save it to a new tuple: "
  790. ]
  791. },
  792. {
  793. "cell_type": "code",
  794. "execution_count": 12,
  795. "metadata": {},
  796. "outputs": [
  797. {
  798. "data": {
  799. "text/plain": [
  800. "[0, 2, 5, 6, 6, 8, 9, 9, 10]"
  801. ]
  802. },
  803. "execution_count": 12,
  804. "metadata": {},
  805. "output_type": "execute_result"
  806. }
  807. ],
  808. "source": [
  809. "RatingsSorted = sorted(Ratings)\n",
  810. "RatingsSorted"
  811. ]
  812. },
  813. {
  814. "cell_type": "markdown",
  815. "metadata": {},
  816. "source": [
  817. "# Nested Tupled"
  818. ]
  819. },
  820. {
  821. "cell_type": "code",
  822. "execution_count": 13,
  823. "metadata": {},
  824. "outputs": [],
  825. "source": [
  826. "# Create a nest tuple\n",
  827. "\n",
  828. "NestedT =(1, 2, (\"pop\", \"rock\") ,(3,4),(\"disco\",(1,2)))"
  829. ]
  830. },
  831. {
  832. "cell_type": "code",
  833. "execution_count": 14,
  834. "metadata": {},
  835. "outputs": [
  836. {
  837. "name": "stdout",
  838. "output_type": "stream",
  839. "text": [
  840. "Element 0 of Tuple: 1\n",
  841. "Element 1 of Tuple: 2\n",
  842. "Element 2 of Tuple: ('pop', 'rock')\n",
  843. "Element 3 of Tuple: (3, 4)\n",
  844. "Element 4 of Tuple: ('disco', (1, 2))\n"
  845. ]
  846. }
  847. ],
  848. "source": [
  849. "# Print element on each index\n",
  850. "\n",
  851. "print(\"Element 0 of Tuple: \", NestedT[0])\n",
  852. "print(\"Element 1 of Tuple: \", NestedT[1])\n",
  853. "print(\"Element 2 of Tuple: \", NestedT[2])\n",
  854. "print(\"Element 3 of Tuple: \", NestedT[3])\n",
  855. "print(\"Element 4 of Tuple: \", NestedT[4])"
  856. ]
  857. },
  858. {
  859. "cell_type": "code",
  860. "execution_count": 15,
  861. "metadata": {},
  862. "outputs": [
  863. {
  864. "name": "stdout",
  865. "output_type": "stream",
  866. "text": [
  867. "Element 2, 0 of Tuple: pop\n",
  868. "Element 2, 1 of Tuple: rock\n",
  869. "Element 3, 0 of Tuple: 3\n",
  870. "Element 3, 1 of Tuple: 4\n",
  871. "Element 4, 0 of Tuple: disco\n",
  872. "Element 4, 1 of Tuple: (1, 2)\n"
  873. ]
  874. }
  875. ],
  876. "source": [
  877. "# Print element on each index, including nest indexes\n",
  878. "\n",
  879. "print(\"Element 2, 0 of Tuple: \", NestedT[2][0])\n",
  880. "print(\"Element 2, 1 of Tuple: \", NestedT[2][1])\n",
  881. "print(\"Element 3, 0 of Tuple: \", NestedT[3][0])\n",
  882. "print(\"Element 3, 1 of Tuple: \", NestedT[3][1])\n",
  883. "print(\"Element 4, 0 of Tuple: \", NestedT[4][0])\n",
  884. "print(\"Element 4, 1 of Tuple: \", NestedT[4][1])"
  885. ]
  886. },
  887. {
  888. "cell_type": "markdown",
  889. "metadata": {},
  890. "source": [
  891. "# Similarly, we can access elements nested deeper in the tree with a fourth index:"
  892. ]
  893. },
  894. {
  895. "cell_type": "markdown",
  896. "metadata": {},
  897. "source": [
  898. "# Function"
  899. ]
  900. },
  901. {
  902. "cell_type": "code",
  903. "execution_count": 18,
  904. "metadata": {},
  905. "outputs": [],
  906. "source": [
  907. "# First function example: Add 1 to a and store as b\n",
  908. "\n",
  909. "def add(a):\n",
  910. " b = a + 1\n",
  911. " print(a, \"if you add one\", b)\n",
  912. " return(b)"
  913. ]
  914. },
  915. {
  916. "cell_type": "code",
  917. "execution_count": 19,
  918. "metadata": {},
  919. "outputs": [
  920. {
  921. "name": "stdout",
  922. "output_type": "stream",
  923. "text": [
  924. "Help on function add in module __main__:\n",
  925. "\n",
  926. "add(a)\n",
  927. "\n"
  928. ]
  929. }
  930. ],
  931. "source": [
  932. "# Get a help on add function\n",
  933. "\n",
  934. "help(add)"
  935. ]
  936. },
  937. {
  938. "cell_type": "code",
  939. "execution_count": 20,
  940. "metadata": {},
  941. "outputs": [
  942. {
  943. "name": "stdout",
  944. "output_type": "stream",
  945. "text": [
  946. "1 if you add one 2\n"
  947. ]
  948. },
  949. {
  950. "data": {
  951. "text/plain": [
  952. "2"
  953. ]
  954. },
  955. "execution_count": 20,
  956. "metadata": {},
  957. "output_type": "execute_result"
  958. }
  959. ],
  960. "source": [
  961. "# Call the function add()\n",
  962. "\n",
  963. "add(1)"
  964. ]
  965. },
  966. {
  967. "cell_type": "code",
  968. "execution_count": 21,
  969. "metadata": {},
  970. "outputs": [
  971. {
  972. "name": "stdout",
  973. "output_type": "stream",
  974. "text": [
  975. "2 if you add one 3\n"
  976. ]
  977. },
  978. {
  979. "data": {
  980. "text/plain": [
  981. "3"
  982. ]
  983. },
  984. "execution_count": 21,
  985. "metadata": {},
  986. "output_type": "execute_result"
  987. }
  988. ],
  989. "source": [
  990. "# Call the function add()\n",
  991. "\n",
  992. "add(2)"
  993. ]
  994. },
  995. {
  996. "cell_type": "code",
  997. "execution_count": 24,
  998. "metadata": {},
  999. "outputs": [],
  1000. "source": [
  1001. "# Define a function for multiple two numbers\n",
  1002. "\n",
  1003. "def Mult(a, b):\n",
  1004. " c = a * b\n",
  1005. " return(c)"
  1006. ]
  1007. },
  1008. {
  1009. "cell_type": "code",
  1010. "execution_count": 25,
  1011. "metadata": {},
  1012. "outputs": [
  1013. {
  1014. "data": {
  1015. "text/plain": [
  1016. "6"
  1017. ]
  1018. },
  1019. "execution_count": 25,
  1020. "metadata": {},
  1021. "output_type": "execute_result"
  1022. }
  1023. ],
  1024. "source": [
  1025. "# Use mult() multiply two integers\n",
  1026. "\n",
  1027. "Mult(2, 3)"
  1028. ]
  1029. },
  1030. {
  1031. "cell_type": "code",
  1032. "execution_count": 26,
  1033. "metadata": {},
  1034. "outputs": [
  1035. {
  1036. "data": {
  1037. "text/plain": [
  1038. "31.400000000000002"
  1039. ]
  1040. },
  1041. "execution_count": 26,
  1042. "metadata": {},
  1043. "output_type": "execute_result"
  1044. }
  1045. ],
  1046. "source": [
  1047. "# Use mult() multiply two floats\n",
  1048. "\n",
  1049. "Mult(10.0, 3.14)"
  1050. ]
  1051. },
  1052. {
  1053. "cell_type": "code",
  1054. "execution_count": 27,
  1055. "metadata": {},
  1056. "outputs": [
  1057. {
  1058. "data": {
  1059. "text/plain": [
  1060. "'Michael Jackson Michael Jackson '"
  1061. ]
  1062. },
  1063. "execution_count": 27,
  1064. "metadata": {},
  1065. "output_type": "execute_result"
  1066. }
  1067. ],
  1068. "source": [
  1069. "# Use mult() multiply two different type values together\n",
  1070. "\n",
  1071. "Mult(2, \"Michael Jackson \")"
  1072. ]
  1073. },
  1074. {
  1075. "cell_type": "markdown",
  1076. "metadata": {},
  1077. "source": [
  1078. "# Variables"
  1079. ]
  1080. },
  1081. {
  1082. "cell_type": "code",
  1083. "execution_count": 28,
  1084. "metadata": {},
  1085. "outputs": [],
  1086. "source": [
  1087. "# Function Definition\n",
  1088. "\n",
  1089. "def square(a):\n",
  1090. " \n",
  1091. " # Local variable b\n",
  1092. " b = 1\n",
  1093. " c = a * a + b\n",
  1094. " print(a, \"if you square + 1\", c) \n",
  1095. " return(c)"
  1096. ]
  1097. },
  1098. {
  1099. "cell_type": "code",
  1100. "execution_count": 29,
  1101. "metadata": {},
  1102. "outputs": [
  1103. {
  1104. "name": "stdout",
  1105. "output_type": "stream",
  1106. "text": [
  1107. "3 if you square + 1 10\n"
  1108. ]
  1109. },
  1110. {
  1111. "data": {
  1112. "text/plain": [
  1113. "10"
  1114. ]
  1115. },
  1116. "execution_count": 29,
  1117. "metadata": {},
  1118. "output_type": "execute_result"
  1119. }
  1120. ],
  1121. "source": [
  1122. "# Initializes Global variable \n",
  1123. "\n",
  1124. "x = 3\n",
  1125. "# Makes function call and return function a y\n",
  1126. "y = square(x)\n",
  1127. "y"
  1128. ]
  1129. },
  1130. {
  1131. "cell_type": "code",
  1132. "execution_count": 30,
  1133. "metadata": {},
  1134. "outputs": [
  1135. {
  1136. "name": "stdout",
  1137. "output_type": "stream",
  1138. "text": [
  1139. "2 if you square + 1 5\n"
  1140. ]
  1141. },
  1142. {
  1143. "data": {
  1144. "text/plain": [
  1145. "5"
  1146. ]
  1147. },
  1148. "execution_count": 30,
  1149. "metadata": {},
  1150. "output_type": "execute_result"
  1151. }
  1152. ],
  1153. "source": [
  1154. "# Directly enter a number as parameter\n",
  1155. "\n",
  1156. "square(2)"
  1157. ]
  1158. },
  1159. {
  1160. "cell_type": "code",
  1161. "execution_count": 31,
  1162. "metadata": {},
  1163. "outputs": [],
  1164. "source": [
  1165. "# Define functions, one with return value None and other without return value\n",
  1166. "\n",
  1167. "def MJ():\n",
  1168. " print('Michael Jackson')\n",
  1169. " \n",
  1170. "def MJ1():\n",
  1171. " print('Michael Jackson')\n",
  1172. " return(None)"
  1173. ]
  1174. },
  1175. {
  1176. "cell_type": "code",
  1177. "execution_count": 32,
  1178. "metadata": {},
  1179. "outputs": [
  1180. {
  1181. "name": "stdout",
  1182. "output_type": "stream",
  1183. "text": [
  1184. "Michael Jackson\n"
  1185. ]
  1186. }
  1187. ],
  1188. "source": [
  1189. "# See the output\n",
  1190. "\n",
  1191. "MJ()"
  1192. ]
  1193. },
  1194. {
  1195. "cell_type": "code",
  1196. "execution_count": 33,
  1197. "metadata": {},
  1198. "outputs": [
  1199. {
  1200. "name": "stdout",
  1201. "output_type": "stream",
  1202. "text": [
  1203. "Michael Jackson\n",
  1204. "None\n",
  1205. "Michael Jackson\n",
  1206. "None\n"
  1207. ]
  1208. }
  1209. ],
  1210. "source": [
  1211. "# See what functions returns are\n",
  1212. "\n",
  1213. "print(MJ())\n",
  1214. "print(MJ1())"
  1215. ]
  1216. },
  1217. {
  1218. "cell_type": "markdown",
  1219. "metadata": {},
  1220. "source": [
  1221. "# Function Makes things simple"
  1222. ]
  1223. },
  1224. {
  1225. "cell_type": "code",
  1226. "execution_count": 35,
  1227. "metadata": {},
  1228. "outputs": [
  1229. {
  1230. "data": {
  1231. "text/plain": [
  1232. "5"
  1233. ]
  1234. },
  1235. "execution_count": 35,
  1236. "metadata": {},
  1237. "output_type": "execute_result"
  1238. }
  1239. ],
  1240. "source": [
  1241. "# a and b calculation block1\n",
  1242. "\n",
  1243. "a1 = 4\n",
  1244. "b1 = 5\n",
  1245. "c1 = a1 + b1 + 2 * a1 * b1 - 1\n",
  1246. "if(c1 < 0):\n",
  1247. " c1 = 0 \n",
  1248. "else:\n",
  1249. " c1 = 5\n",
  1250. "c1 "
  1251. ]
  1252. },
  1253. {
  1254. "cell_type": "code",
  1255. "execution_count": 36,
  1256. "metadata": {},
  1257. "outputs": [
  1258. {
  1259. "data": {
  1260. "text/plain": [
  1261. "0"
  1262. ]
  1263. },
  1264. "execution_count": 36,
  1265. "metadata": {},
  1266. "output_type": "execute_result"
  1267. }
  1268. ],
  1269. "source": [
  1270. "# a and b calculation block2\n",
  1271. "\n",
  1272. "a2 = 0\n",
  1273. "b2 = 0\n",
  1274. "c2 = a2 + b2 + 2 * a2 * b2 - 1\n",
  1275. "if(c2 < 0):\n",
  1276. " c2 = 0 \n",
  1277. "else:\n",
  1278. " c2 = 5\n",
  1279. "c2 "
  1280. ]
  1281. },
  1282. {
  1283. "cell_type": "code",
  1284. "execution_count": 37,
  1285. "metadata": {},
  1286. "outputs": [],
  1287. "source": [
  1288. "# Make a Function for the calculation above\n",
  1289. "\n",
  1290. "def Equation(a,b):\n",
  1291. " c = a + b + 2 * a * b - 1\n",
  1292. " if(c < 0):\n",
  1293. " c = 0 \n",
  1294. " else:\n",
  1295. " c = 5\n",
  1296. " return(c) "
  1297. ]
  1298. },
  1299. {
  1300. "cell_type": "code",
  1301. "execution_count": null,
  1302. "metadata": {},
  1303. "outputs": [],
  1304. "source": []
  1305. }
  1306. ],
  1307. "metadata": {
  1308. "kernelspec": {
  1309. "display_name": "Python 3",
  1310. "language": "python",
  1311. "name": "python3"
  1312. },
  1313. "language_info": {
  1314. "codemirror_mode": {
  1315. "name": "ipython",
  1316. "version": 3
  1317. },
  1318. "file_extension": ".py",
  1319. "mimetype": "text/x-python",
  1320. "name": "python",
  1321. "nbconvert_exporter": "python",
  1322. "pygments_lexer": "ipython3",
  1323. "version": "3.6.8"
  1324. }
  1325. },
  1326. "nbformat": 4,
  1327. "nbformat_minor": 2
  1328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement