Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.63 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "## Tuples"
  8. ]
  9. },
  10. {
  11. "cell_type": "markdown",
  12. "metadata": {},
  13. "source": [
  14. "A tuple is a sequence of immutable Python objects. Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets."
  15. ]
  16. },
  17. {
  18. "cell_type": "code",
  19. "execution_count": 1,
  20. "metadata": {},
  21. "outputs": [],
  22. "source": [
  23. "ratings = (10,9,8,7,6,5,4,3,2,1)"
  24. ]
  25. },
  26. {
  27. "cell_type": "code",
  28. "execution_count": 2,
  29. "metadata": {},
  30. "outputs": [
  31. {
  32. "name": "stdout",
  33. "output_type": "stream",
  34. "text": [
  35. "(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)\n"
  36. ]
  37. }
  38. ],
  39. "source": [
  40. "print(ratings)"
  41. ]
  42. },
  43. {
  44. "cell_type": "code",
  45. "execution_count": 3,
  46. "metadata": {},
  47. "outputs": [],
  48. "source": [
  49. "rating = (10,9,8,6,3,10,6,2)"
  50. ]
  51. },
  52. {
  53. "cell_type": "code",
  54. "execution_count": 4,
  55. "metadata": {},
  56. "outputs": [
  57. {
  58. "name": "stdout",
  59. "output_type": "stream",
  60. "text": [
  61. "(10, 9, 8, 6, 3, 10, 6, 2)\n"
  62. ]
  63. }
  64. ],
  65. "source": [
  66. "print(rating)"
  67. ]
  68. },
  69. {
  70. "cell_type": "code",
  71. "execution_count": 5,
  72. "metadata": {},
  73. "outputs": [],
  74. "source": [
  75. "# tuples can store variables such as str, int, float"
  76. ]
  77. },
  78. {
  79. "cell_type": "code",
  80. "execution_count": 6,
  81. "metadata": {},
  82. "outputs": [],
  83. "source": [
  84. "tuple1 = ('disco', 2 ,1.67)"
  85. ]
  86. },
  87. {
  88. "cell_type": "code",
  89. "execution_count": 7,
  90. "metadata": {},
  91. "outputs": [
  92. {
  93. "name": "stdout",
  94. "output_type": "stream",
  95. "text": [
  96. "('disco', 2, 1.67)\n"
  97. ]
  98. }
  99. ],
  100. "source": [
  101. "print(tuple1)"
  102. ]
  103. },
  104. {
  105. "cell_type": "code",
  106. "execution_count": 8,
  107. "metadata": {},
  108. "outputs": [
  109. {
  110. "data": {
  111. "text/plain": [
  112. "tuple"
  113. ]
  114. },
  115. "execution_count": 8,
  116. "metadata": {},
  117. "output_type": "execute_result"
  118. }
  119. ],
  120. "source": [
  121. "type(tuple1)"
  122. ]
  123. },
  124. {
  125. "cell_type": "code",
  126. "execution_count": 9,
  127. "metadata": {},
  128. "outputs": [
  129. {
  130. "data": {
  131. "text/plain": [
  132. "'disco'"
  133. ]
  134. },
  135. "execution_count": 9,
  136. "metadata": {},
  137. "output_type": "execute_result"
  138. }
  139. ],
  140. "source": [
  141. "tuple1[0]"
  142. ]
  143. },
  144. {
  145. "cell_type": "code",
  146. "execution_count": 10,
  147. "metadata": {},
  148. "outputs": [
  149. {
  150. "data": {
  151. "text/plain": [
  152. "2"
  153. ]
  154. },
  155. "execution_count": 10,
  156. "metadata": {},
  157. "output_type": "execute_result"
  158. }
  159. ],
  160. "source": [
  161. "tuple1[1]"
  162. ]
  163. },
  164. {
  165. "cell_type": "code",
  166. "execution_count": 11,
  167. "metadata": {},
  168. "outputs": [
  169. {
  170. "data": {
  171. "text/plain": [
  172. "1.67"
  173. ]
  174. },
  175. "execution_count": 11,
  176. "metadata": {},
  177. "output_type": "execute_result"
  178. }
  179. ],
  180. "source": [
  181. "tuple1[2]"
  182. ]
  183. },
  184. {
  185. "cell_type": "markdown",
  186. "metadata": {},
  187. "source": [
  188. "### Variable types in a tuple"
  189. ]
  190. },
  191. {
  192. "cell_type": "code",
  193. "execution_count": 12,
  194. "metadata": {},
  195. "outputs": [
  196. {
  197. "data": {
  198. "text/plain": [
  199. "int"
  200. ]
  201. },
  202. "execution_count": 12,
  203. "metadata": {},
  204. "output_type": "execute_result"
  205. }
  206. ],
  207. "source": [
  208. "type(tuple1[1])"
  209. ]
  210. },
  211. {
  212. "cell_type": "code",
  213. "execution_count": 13,
  214. "metadata": {},
  215. "outputs": [
  216. {
  217. "data": {
  218. "text/plain": [
  219. "str"
  220. ]
  221. },
  222. "execution_count": 13,
  223. "metadata": {},
  224. "output_type": "execute_result"
  225. }
  226. ],
  227. "source": [
  228. "type(tuple1[0])"
  229. ]
  230. },
  231. {
  232. "cell_type": "code",
  233. "execution_count": 14,
  234. "metadata": {},
  235. "outputs": [
  236. {
  237. "data": {
  238. "text/plain": [
  239. "float"
  240. ]
  241. },
  242. "execution_count": 14,
  243. "metadata": {},
  244. "output_type": "execute_result"
  245. }
  246. ],
  247. "source": [
  248. "type(tuple1[2])"
  249. ]
  250. },
  251. {
  252. "cell_type": "markdown",
  253. "metadata": {},
  254. "source": [
  255. "### tuples can be listed with negative values"
  256. ]
  257. },
  258. {
  259. "cell_type": "code",
  260. "execution_count": 15,
  261. "metadata": {},
  262. "outputs": [
  263. {
  264. "data": {
  265. "text/plain": [
  266. "1.67"
  267. ]
  268. },
  269. "execution_count": 15,
  270. "metadata": {},
  271. "output_type": "execute_result"
  272. }
  273. ],
  274. "source": [
  275. "tuple1[-1]"
  276. ]
  277. },
  278. {
  279. "cell_type": "code",
  280. "execution_count": 16,
  281. "metadata": {},
  282. "outputs": [
  283. {
  284. "data": {
  285. "text/plain": [
  286. "2"
  287. ]
  288. },
  289. "execution_count": 16,
  290. "metadata": {},
  291. "output_type": "execute_result"
  292. }
  293. ],
  294. "source": [
  295. "tuple1[-2]"
  296. ]
  297. },
  298. {
  299. "cell_type": "code",
  300. "execution_count": 17,
  301. "metadata": {},
  302. "outputs": [
  303. {
  304. "data": {
  305. "text/plain": [
  306. "'disco'"
  307. ]
  308. },
  309. "execution_count": 17,
  310. "metadata": {},
  311. "output_type": "execute_result"
  312. }
  313. ],
  314. "source": [
  315. "tuple1[-3]"
  316. ]
  317. },
  318. {
  319. "cell_type": "markdown",
  320. "metadata": {},
  321. "source": [
  322. "### Concatenate two tuples"
  323. ]
  324. },
  325. {
  326. "cell_type": "code",
  327. "execution_count": 18,
  328. "metadata": {},
  329. "outputs": [
  330. {
  331. "data": {
  332. "text/plain": [
  333. "('disco', 2, 1.67)"
  334. ]
  335. },
  336. "execution_count": 18,
  337. "metadata": {},
  338. "output_type": "execute_result"
  339. }
  340. ],
  341. "source": [
  342. "tuple1"
  343. ]
  344. },
  345. {
  346. "cell_type": "code",
  347. "execution_count": 19,
  348. "metadata": {},
  349. "outputs": [],
  350. "source": [
  351. "tuple2 = tuple1 + ('hardrock', 10)"
  352. ]
  353. },
  354. {
  355. "cell_type": "code",
  356. "execution_count": 20,
  357. "metadata": {},
  358. "outputs": [
  359. {
  360. "name": "stdout",
  361. "output_type": "stream",
  362. "text": [
  363. "('disco', 2, 1.67, 'hardrock', 10)\n"
  364. ]
  365. }
  366. ],
  367. "source": [
  368. "print(tuple2)"
  369. ]
  370. },
  371. {
  372. "cell_type": "markdown",
  373. "metadata": {},
  374. "source": [
  375. "### tuple slicing"
  376. ]
  377. },
  378. {
  379. "cell_type": "code",
  380. "execution_count": 21,
  381. "metadata": {},
  382. "outputs": [
  383. {
  384. "data": {
  385. "text/plain": [
  386. "('disco', 2, 1.67)"
  387. ]
  388. },
  389. "execution_count": 21,
  390. "metadata": {},
  391. "output_type": "execute_result"
  392. }
  393. ],
  394. "source": [
  395. "tuple2[0:3]"
  396. ]
  397. },
  398. {
  399. "cell_type": "code",
  400. "execution_count": 22,
  401. "metadata": {},
  402. "outputs": [
  403. {
  404. "data": {
  405. "text/plain": [
  406. "('hardrock', 10)"
  407. ]
  408. },
  409. "execution_count": 22,
  410. "metadata": {},
  411. "output_type": "execute_result"
  412. }
  413. ],
  414. "source": [
  415. "tuple2[3:5]"
  416. ]
  417. },
  418. {
  419. "cell_type": "markdown",
  420. "metadata": {},
  421. "source": [
  422. "### length of a tuple"
  423. ]
  424. },
  425. {
  426. "cell_type": "code",
  427. "execution_count": 23,
  428. "metadata": {},
  429. "outputs": [
  430. {
  431. "data": {
  432. "text/plain": [
  433. "5"
  434. ]
  435. },
  436. "execution_count": 23,
  437. "metadata": {},
  438. "output_type": "execute_result"
  439. }
  440. ],
  441. "source": [
  442. "len(tuple2)"
  443. ]
  444. },
  445. {
  446. "cell_type": "markdown",
  447. "metadata": {},
  448. "source": [
  449. "### tuple is immutable"
  450. ]
  451. },
  452. {
  453. "cell_type": "code",
  454. "execution_count": 24,
  455. "metadata": {},
  456. "outputs": [
  457. {
  458. "data": {
  459. "text/plain": [
  460. "(10, 9, 8, 6, 3, 10, 6, 2)"
  461. ]
  462. },
  463. "execution_count": 24,
  464. "metadata": {},
  465. "output_type": "execute_result"
  466. }
  467. ],
  468. "source": [
  469. "rating"
  470. ]
  471. },
  472. {
  473. "cell_type": "code",
  474. "execution_count": 25,
  475. "metadata": {},
  476. "outputs": [],
  477. "source": [
  478. "rating1 = rating"
  479. ]
  480. },
  481. {
  482. "cell_type": "code",
  483. "execution_count": 26,
  484. "metadata": {},
  485. "outputs": [
  486. {
  487. "name": "stdout",
  488. "output_type": "stream",
  489. "text": [
  490. "(10, 9, 8, 6, 3, 10, 6, 2)\n"
  491. ]
  492. }
  493. ],
  494. "source": [
  495. "print(rating1)"
  496. ]
  497. },
  498. {
  499. "cell_type": "code",
  500. "execution_count": 27,
  501. "metadata": {},
  502. "outputs": [],
  503. "source": [
  504. "#since tuple are immutable we cannot modify tuple"
  505. ]
  506. },
  507. {
  508. "cell_type": "markdown",
  509. "metadata": {},
  510. "source": [
  511. "### sorting with tuples"
  512. ]
  513. },
  514. {
  515. "cell_type": "code",
  516. "execution_count": 28,
  517. "metadata": {},
  518. "outputs": [],
  519. "source": [
  520. "ratingsort = sorted(rating1)"
  521. ]
  522. },
  523. {
  524. "cell_type": "code",
  525. "execution_count": 29,
  526. "metadata": {},
  527. "outputs": [
  528. {
  529. "name": "stdout",
  530. "output_type": "stream",
  531. "text": [
  532. "[2, 3, 6, 6, 8, 9, 10, 10]\n"
  533. ]
  534. }
  535. ],
  536. "source": [
  537. "print(ratingsort)"
  538. ]
  539. },
  540. {
  541. "cell_type": "markdown",
  542. "metadata": {},
  543. "source": [
  544. "### Tuple nesting"
  545. ]
  546. },
  547. {
  548. "cell_type": "code",
  549. "execution_count": 30,
  550. "metadata": {},
  551. "outputs": [],
  552. "source": [
  553. "nt = (1, 2.3, ('pop', 'rock'), (3,4), ('jazz', (2,3)))"
  554. ]
  555. },
  556. {
  557. "cell_type": "code",
  558. "execution_count": 31,
  559. "metadata": {},
  560. "outputs": [
  561. {
  562. "name": "stdout",
  563. "output_type": "stream",
  564. "text": [
  565. "(1, 2.3, ('pop', 'rock'), (3, 4), ('jazz', (2, 3)))\n"
  566. ]
  567. }
  568. ],
  569. "source": [
  570. "print(nt)"
  571. ]
  572. },
  573. {
  574. "cell_type": "code",
  575. "execution_count": 32,
  576. "metadata": {},
  577. "outputs": [],
  578. "source": [
  579. "# tree is used to access the tuples in nested"
  580. ]
  581. },
  582. {
  583. "cell_type": "code",
  584. "execution_count": 33,
  585. "metadata": {},
  586. "outputs": [
  587. {
  588. "data": {
  589. "text/plain": [
  590. "1"
  591. ]
  592. },
  593. "execution_count": 33,
  594. "metadata": {},
  595. "output_type": "execute_result"
  596. }
  597. ],
  598. "source": [
  599. "nt[0]"
  600. ]
  601. },
  602. {
  603. "cell_type": "code",
  604. "execution_count": 34,
  605. "metadata": {},
  606. "outputs": [
  607. {
  608. "data": {
  609. "text/plain": [
  610. "(3, 4)"
  611. ]
  612. },
  613. "execution_count": 34,
  614. "metadata": {},
  615. "output_type": "execute_result"
  616. }
  617. ],
  618. "source": [
  619. "nt[3]"
  620. ]
  621. },
  622. {
  623. "cell_type": "code",
  624. "execution_count": 35,
  625. "metadata": {},
  626. "outputs": [
  627. {
  628. "data": {
  629. "text/plain": [
  630. "3"
  631. ]
  632. },
  633. "execution_count": 35,
  634. "metadata": {},
  635. "output_type": "execute_result"
  636. }
  637. ],
  638. "source": [
  639. "nt[3][0]"
  640. ]
  641. },
  642. {
  643. "cell_type": "code",
  644. "execution_count": 36,
  645. "metadata": {},
  646. "outputs": [
  647. {
  648. "data": {
  649. "text/plain": [
  650. "('pop', 'rock')"
  651. ]
  652. },
  653. "execution_count": 36,
  654. "metadata": {},
  655. "output_type": "execute_result"
  656. }
  657. ],
  658. "source": [
  659. "nt[2]"
  660. ]
  661. },
  662. {
  663. "cell_type": "code",
  664. "execution_count": 37,
  665. "metadata": {},
  666. "outputs": [
  667. {
  668. "data": {
  669. "text/plain": [
  670. "'rock'"
  671. ]
  672. },
  673. "execution_count": 37,
  674. "metadata": {},
  675. "output_type": "execute_result"
  676. }
  677. ],
  678. "source": [
  679. "nt[2][1]"
  680. ]
  681. },
  682. {
  683. "cell_type": "code",
  684. "execution_count": 38,
  685. "metadata": {},
  686. "outputs": [
  687. {
  688. "data": {
  689. "text/plain": [
  690. "('jazz', (2, 3))"
  691. ]
  692. },
  693. "execution_count": 38,
  694. "metadata": {},
  695. "output_type": "execute_result"
  696. }
  697. ],
  698. "source": [
  699. "nt[4]"
  700. ]
  701. },
  702. {
  703. "cell_type": "code",
  704. "execution_count": 39,
  705. "metadata": {},
  706. "outputs": [
  707. {
  708. "data": {
  709. "text/plain": [
  710. "2"
  711. ]
  712. },
  713. "execution_count": 39,
  714. "metadata": {},
  715. "output_type": "execute_result"
  716. }
  717. ],
  718. "source": [
  719. "nt[4][1][0]"
  720. ]
  721. },
  722. {
  723. "cell_type": "code",
  724. "execution_count": 40,
  725. "metadata": {},
  726. "outputs": [
  727. {
  728. "data": {
  729. "text/plain": [
  730. "'r'"
  731. ]
  732. },
  733. "execution_count": 40,
  734. "metadata": {},
  735. "output_type": "execute_result"
  736. }
  737. ],
  738. "source": [
  739. "nt[2][1][0]"
  740. ]
  741. },
  742. {
  743. "cell_type": "code",
  744. "execution_count": 42,
  745. "metadata": {},
  746. "outputs": [
  747. {
  748. "data": {
  749. "text/plain": [
  750. "1"
  751. ]
  752. },
  753. "execution_count": 42,
  754. "metadata": {},
  755. "output_type": "execute_result"
  756. }
  757. ],
  758. "source": [
  759. "nt.index(2.3)"
  760. ]
  761. },
  762. {
  763. "cell_type": "code",
  764. "execution_count": 45,
  765. "metadata": {},
  766. "outputs": [
  767. {
  768. "data": {
  769. "text/plain": [
  770. "4"
  771. ]
  772. },
  773. "execution_count": 45,
  774. "metadata": {},
  775. "output_type": "execute_result"
  776. }
  777. ],
  778. "source": [
  779. "nt.index(('jazz', (2, 3)))"
  780. ]
  781. },
  782. {
  783. "cell_type": "code",
  784. "execution_count": null,
  785. "metadata": {},
  786. "outputs": [],
  787. "source": []
  788. }
  789. ],
  790. "metadata": {
  791. "kernelspec": {
  792. "display_name": "Python",
  793. "language": "python",
  794. "name": "conda-env-python-py"
  795. },
  796. "language_info": {
  797. "codemirror_mode": {
  798. "name": "ipython",
  799. "version": 3
  800. },
  801. "file_extension": ".py",
  802. "mimetype": "text/x-python",
  803. "name": "python",
  804. "nbconvert_exporter": "python",
  805. "pygments_lexer": "ipython3",
  806. "version": "3.6.7"
  807. }
  808. },
  809. "nbformat": 4,
  810. "nbformat_minor": 4
  811. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement