Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {},
  7. "outputs": [
  8. {
  9. "data": {
  10. "text/plain": [
  11. "range(0, 3)"
  12. ]
  13. },
  14. "execution_count": 1,
  15. "metadata": {},
  16. "output_type": "execute_result"
  17. }
  18. ],
  19. "source": [
  20. "# Use the range\n",
  21. "\n",
  22. "range(3)"
  23. ]
  24. },
  25. {
  26. "cell_type": "code",
  27. "execution_count": 2,
  28. "metadata": {},
  29. "outputs": [
  30. {
  31. "name": "stdout",
  32. "output_type": "stream",
  33. "text": [
  34. "1982\n",
  35. "1980\n",
  36. "1973\n"
  37. ]
  38. }
  39. ],
  40. "source": [
  41. "# For loop example\n",
  42. "\n",
  43. "dates = [1982,1980,1973]\n",
  44. "N = len(dates)\n",
  45. "\n",
  46. "for i in range(N):\n",
  47. " print(dates[i]) "
  48. ]
  49. },
  50. {
  51. "cell_type": "code",
  52. "execution_count": 3,
  53. "metadata": {},
  54. "outputs": [
  55. {
  56. "name": "stdout",
  57. "output_type": "stream",
  58. "text": [
  59. "0\n",
  60. "1\n",
  61. "2\n",
  62. "3\n",
  63. "4\n",
  64. "5\n",
  65. "6\n",
  66. "7\n"
  67. ]
  68. }
  69. ],
  70. "source": [
  71. "# Example of for loop\n",
  72. "\n",
  73. "for i in range(0, 8):\n",
  74. " print(i)"
  75. ]
  76. },
  77. {
  78. "cell_type": "code",
  79. "execution_count": 4,
  80. "metadata": {},
  81. "outputs": [
  82. {
  83. "name": "stdout",
  84. "output_type": "stream",
  85. "text": [
  86. "1982\n",
  87. "1980\n",
  88. "1973\n"
  89. ]
  90. }
  91. ],
  92. "source": [
  93. "# Exmaple of for loop, loop through list\n",
  94. "\n",
  95. "for year in dates: \n",
  96. " print(year) "
  97. ]
  98. },
  99. {
  100. "cell_type": "code",
  101. "execution_count": 5,
  102. "metadata": {},
  103. "outputs": [
  104. {
  105. "name": "stdout",
  106. "output_type": "stream",
  107. "text": [
  108. "Before square 0 is red\n",
  109. "After square 0 is weight\n",
  110. "Before square 1 is yellow\n",
  111. "After square 1 is weight\n",
  112. "Before square 2 is green\n",
  113. "After square 2 is weight\n",
  114. "Before square 3 is purple\n",
  115. "After square 3 is weight\n",
  116. "Before square 4 is blue\n",
  117. "After square 4 is weight\n"
  118. ]
  119. }
  120. ],
  121. "source": [
  122. "# Use for loop to change the elements in list\n",
  123. "\n",
  124. "squares = ['red', 'yellow', 'green', 'purple', 'blue']\n",
  125. "\n",
  126. "for i in range(0, 5):\n",
  127. " print(\"Before square \", i, 'is', squares[i])\n",
  128. " squares[i] = 'weight'\n",
  129. " print(\"After square \", i, 'is', squares[i])"
  130. ]
  131. },
  132. {
  133. "cell_type": "code",
  134. "execution_count": 6,
  135. "metadata": {},
  136. "outputs": [
  137. {
  138. "name": "stdout",
  139. "output_type": "stream",
  140. "text": [
  141. "0 red\n",
  142. "1 yellow\n",
  143. "2 green\n",
  144. "3 purple\n",
  145. "4 blue\n"
  146. ]
  147. }
  148. ],
  149. "source": [
  150. "# Loop through the list and iterate on both index and element value\n",
  151. "\n",
  152. "squares=['red', 'yellow', 'green', 'purple', 'blue']\n",
  153. "\n",
  154. "for i, square in enumerate(squares):\n",
  155. " print(i, square)"
  156. ]
  157. },
  158. {
  159. "cell_type": "code",
  160. "execution_count": 7,
  161. "metadata": {},
  162. "outputs": [
  163. {
  164. "name": "stdout",
  165. "output_type": "stream",
  166. "text": [
  167. "1982\n",
  168. "1980\n",
  169. "1973\n",
  170. "It took 3 repetitions to get out of loop.\n"
  171. ]
  172. }
  173. ],
  174. "source": [
  175. "# While Loop Example\n",
  176. "\n",
  177. "dates = [1982, 1980, 1973, 2000]\n",
  178. "\n",
  179. "i = 0\n",
  180. "year = 0\n",
  181. "\n",
  182. "while(year != 1973):\n",
  183. " year = dates[i]\n",
  184. " i = i + 1\n",
  185. " print(year)\n",
  186. "\n",
  187. "print(\"It took \", i ,\"repetitions to get out of loop.\")"
  188. ]
  189. },
  190. {
  191. "cell_type": "code",
  192. "execution_count": 10,
  193. "metadata": {},
  194. "outputs": [
  195. {
  196. "name": "stdout",
  197. "output_type": "stream",
  198. "text": [
  199. "-5\n",
  200. "-4\n",
  201. "-3\n",
  202. "-2\n",
  203. "-1\n",
  204. "0\n",
  205. "1\n",
  206. "2\n",
  207. "3\n",
  208. "4\n",
  209. "5\n"
  210. ]
  211. }
  212. ],
  213. "source": [
  214. "#Write a for loop the prints out all the element between -5 and 5 using the range function.\n",
  215. "\n",
  216. "for i in range(-5, 6):\n",
  217. " print(i)\n"
  218. ]
  219. },
  220. {
  221. "cell_type": "code",
  222. "execution_count": 11,
  223. "metadata": {},
  224. "outputs": [
  225. {
  226. "name": "stdout",
  227. "output_type": "stream",
  228. "text": [
  229. "rock\n",
  230. "R&B\n",
  231. "Soundtrack\n",
  232. "R&B\n",
  233. "soul\n",
  234. "pop\n"
  235. ]
  236. }
  237. ],
  238. "source": [
  239. "#Print the elements of the following list: Genres=[ 'rock', 'R&B', 'Soundtrack', 'R&B', 'soul', 'pop'] Make sure you follow Python conventions.\n",
  240. "Genres = ['rock', 'R&B', 'Soundtrack', 'R&B', 'soul', 'pop']\n",
  241. "for Genre in Genres:\n",
  242. " print(Genre)\n"
  243. ]
  244. },
  245. {
  246. "cell_type": "code",
  247. "execution_count": 17,
  248. "metadata": {},
  249. "outputs": [
  250. {
  251. "name": "stdout",
  252. "output_type": "stream",
  253. "text": [
  254. "red\n",
  255. "yellow\n",
  256. "green\n",
  257. "purple\n",
  258. "blue\n"
  259. ]
  260. }
  261. ],
  262. "source": [
  263. "#Write a for loop that prints out the following list: squares=['red', 'yellow', 'green', 'purple', 'blue']\n",
  264. "\n",
  265. "squares=['red', 'yellow', 'green', 'purple', 'blue']\n",
  266. "for square in squares:\n",
  267. " print(square)\n"
  268. ]
  269. },
  270. {
  271. "cell_type": "code",
  272. "execution_count": 19,
  273. "metadata": {},
  274. "outputs": [
  275. {
  276. "name": "stdout",
  277. "output_type": "stream",
  278. "text": [
  279. "10\n",
  280. "9.5\n",
  281. "10\n",
  282. "8\n",
  283. "7.5\n"
  284. ]
  285. }
  286. ],
  287. "source": [
  288. "#Write a while loop to display the values of the Rating of an album playlist stored in the list PlayListRatings. If the score is less than 6, exit the loop. The list PlayListRatings is given by: PlayListRatings = [10, 9.5, 10, 8, 7.5, 5, 10, 10]\n",
  289. " \n",
  290. "PlayListRatings = [10, 9.5, 10, 8, 7.5, 5, 10, 10]\n",
  291. "i = 1\n",
  292. "Rating = PlayListRatings[0]\n",
  293. "while(Rating >= 6):\n",
  294. " print(Rating)\n",
  295. " Rating = PlayListRatings[i]\n",
  296. " i = i + 1 "
  297. ]
  298. },
  299. {
  300. "cell_type": "code",
  301. "execution_count": 20,
  302. "metadata": {},
  303. "outputs": [
  304. {
  305. "name": "stdout",
  306. "output_type": "stream",
  307. "text": [
  308. "['orange', 'orange']\n"
  309. ]
  310. }
  311. ],
  312. "source": [
  313. "#Write a while loop to copy the strings 'orange' of the list squares to the list new_squares. Stop and exit the loop if the value on the list is not 'orange':\n",
  314. "\n",
  315. "squares = ['orange', 'orange', 'purple', 'blue ', 'orange']\n",
  316. "new_squares = []\n",
  317. "i = 0\n",
  318. "while(squares[i] == 'orange'):\n",
  319. " new_squares.append(squares[i])\n",
  320. " i = i + 1\n",
  321. "print (new_squares)"
  322. ]
  323. },
  324. {
  325. "cell_type": "code",
  326. "execution_count": null,
  327. "metadata": {},
  328. "outputs": [],
  329. "source": []
  330. }
  331. ],
  332. "metadata": {
  333. "kernelspec": {
  334. "display_name": "Python 3",
  335. "language": "python",
  336. "name": "python3"
  337. },
  338. "language_info": {
  339. "codemirror_mode": {
  340. "name": "ipython",
  341. "version": 3
  342. },
  343. "file_extension": ".py",
  344. "mimetype": "text/x-python",
  345. "name": "python",
  346. "nbconvert_exporter": "python",
  347. "pygments_lexer": "ipython3",
  348. "version": "3.6.8"
  349. }
  350. },
  351. "nbformat": 4,
  352. "nbformat_minor": 2
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement