Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.93 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 10,
  6. "metadata": {},
  7. "outputs": [
  8. {
  9. "name": "stdout",
  10. "output_type": "stream",
  11. "text": [
  12. "Hello, Python\n"
  13. ]
  14. }
  15. ],
  16. "source": [
  17. "# My first program in python \n",
  18. "print (\"Hello, Python\")"
  19. ]
  20. },
  21. {
  22. "cell_type": "code",
  23. "execution_count": 2,
  24. "metadata": {},
  25. "outputs": [
  26. {
  27. "name": "stdout",
  28. "output_type": "stream",
  29. "text": [
  30. "3.6.7 | packaged by conda-forge | (default, Jul 2 2019, 02:18:42) \n",
  31. "[GCC 7.3.0]\n"
  32. ]
  33. }
  34. ],
  35. "source": [
  36. "# Checking what version of python this is \n",
  37. "\n",
  38. "import sys\n",
  39. "print (sys.version)"
  40. ]
  41. },
  42. {
  43. "cell_type": "code",
  44. "execution_count": 3,
  45. "metadata": {},
  46. "outputs": [
  47. {
  48. "name": "stdout",
  49. "output_type": "stream",
  50. "text": [
  51. "Anna\n"
  52. ]
  53. }
  54. ],
  55. "source": [
  56. "# Practising writing comments \n",
  57. "\n",
  58. "print(\"Anna\") # this line of code will write \"Anna\"\n",
  59. "# print (\"Sophie\") # this line of code has been commented out "
  60. ]
  61. },
  62. {
  63. "cell_type": "code",
  64. "execution_count": 7,
  65. "metadata": {},
  66. "outputs": [
  67. {
  68. "name": "stdout",
  69. "output_type": "stream",
  70. "text": [
  71. "anna\n",
  72. "anna\n"
  73. ]
  74. }
  75. ],
  76. "source": [
  77. "# creating errors\n",
  78. "\n",
  79. "print (\"anna\")\n",
  80. "\n",
  81. "#if this said frind then an error stating that name \"frint\" is not defined would be shown\n",
  82. "\n",
  83. "print (\"anna\")\n",
  84. " \n",
  85. "# if I missed the second \" in this statement then an error stating EOL while scanning string literal would be shown"
  86. ]
  87. },
  88. {
  89. "cell_type": "code",
  90. "execution_count": 23,
  91. "metadata": {},
  92. "outputs": [
  93. {
  94. "name": "stdout",
  95. "output_type": "stream",
  96. "text": [
  97. "This will be printed\n",
  98. "This will cause an error\n",
  99. "This will NOT be printed if there is an error caused by the statement above\n"
  100. ]
  101. }
  102. ],
  103. "source": [
  104. "#if there is an error in the middle of the code then only the code up to that point will be executed, and an error message will be provided\n",
  105. "\n",
  106. "print(\"This will be printed\")\n",
  107. "print(\"This will cause an error\")\n",
  108. "print(\"This will NOT be printed if there is an error caused by the statement above\")"
  109. ]
  110. },
  111. {
  112. "cell_type": "code",
  113. "execution_count": 13,
  114. "metadata": {},
  115. "outputs": [
  116. {
  117. "name": "stdout",
  118. "output_type": "stream",
  119. "text": [
  120. "Hello World!\n"
  121. ]
  122. }
  123. ],
  124. "source": [
  125. "# Hello World\n",
  126. "\n",
  127. "print(\"Hello, World!\") #print the traditional hello world"
  128. ]
  129. },
  130. {
  131. "cell_type": "code",
  132. "execution_count": 14,
  133. "metadata": {},
  134. "outputs": [
  135. {
  136. "data": {
  137. "text/plain": [
  138. "'hello'"
  139. ]
  140. },
  141. "execution_count": 14,
  142. "metadata": {},
  143. "output_type": "execute_result"
  144. }
  145. ],
  146. "source": [
  147. "#Types of objects\n",
  148. "\n",
  149. "#integer\n",
  150. "\n",
  151. "11\n",
  152. "\n",
  153. "#float\n",
  154. "\n",
  155. "12.235\n",
  156. "\n",
  157. "#string \n",
  158. "\n",
  159. "\"hello\""
  160. ]
  161. },
  162. {
  163. "cell_type": "code",
  164. "execution_count": 24,
  165. "metadata": {},
  166. "outputs": [
  167. {
  168. "name": "stdout",
  169. "output_type": "stream",
  170. "text": [
  171. "<class 'int'>\n",
  172. "<class 'float'>\n",
  173. "<class 'str'>\n",
  174. "<class 'int'>\n",
  175. "<class 'float'>\n"
  176. ]
  177. },
  178. {
  179. "data": {
  180. "text/plain": [
  181. "sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)"
  182. ]
  183. },
  184. "execution_count": 24,
  185. "metadata": {},
  186. "output_type": "execute_result"
  187. }
  188. ],
  189. "source": [
  190. "#finding the type of an object \n",
  191. "\n",
  192. "print(type(12))\n",
  193. "\n",
  194. "print(type(17.223))\n",
  195. "\n",
  196. "print(type(\"anna\"))\n",
  197. "\n",
  198. "print(type(-1))\n",
  199. "\n",
  200. "print(type(1.0))\n",
  201. "\n",
  202. "# learn more about the specifics of floats for your runtime environment\n",
  203. "\n",
  204. "sys.float_info\n",
  205. "\n"
  206. ]
  207. },
  208. {
  209. "cell_type": "code",
  210. "execution_count": 28,
  211. "metadata": {},
  212. "outputs": [
  213. {
  214. "name": "stdout",
  215. "output_type": "stream",
  216. "text": [
  217. "<class 'int'>\n",
  218. "2.0\n",
  219. "<class 'float'>\n"
  220. ]
  221. }
  222. ],
  223. "source": [
  224. "# Verify 2 is an integer\n",
  225. "\n",
  226. "print(type(2))\n",
  227. "\n",
  228. "#converting 2 to a float\n",
  229. "\n",
  230. "print(float(2))\n",
  231. "\n",
  232. "# verify 2.0 is a float\n",
  233. "\n",
  234. "print(type(float(2)))\n",
  235. "\n"
  236. ]
  237. },
  238. {
  239. "cell_type": "code",
  240. "execution_count": 34,
  241. "metadata": {},
  242. "outputs": [
  243. {
  244. "name": "stdout",
  245. "output_type": "stream",
  246. "text": [
  247. "<class 'float'>\n",
  248. "1\n",
  249. "<class 'int'>\n"
  250. ]
  251. }
  252. ],
  253. "source": [
  254. "# verify 1.1 is a float\n",
  255. "\n",
  256. "print(type(1.1))\n",
  257. "\n",
  258. "#convert 1.1 to an integer\n",
  259. "\n",
  260. "print(int(1.1)) # doing this can cause you to lose information the 0.1\n",
  261. "\n",
  262. "# verify 1 is an integer\n",
  263. "\n",
  264. "print(type(int(1.1)))"
  265. ]
  266. },
  267. {
  268. "cell_type": "code",
  269. "execution_count": 42,
  270. "metadata": {},
  271. "outputs": [
  272. {
  273. "name": "stdout",
  274. "output_type": "stream",
  275. "text": [
  276. "<class 'str'>\n",
  277. "1\n",
  278. "<class 'int'>\n",
  279. "1.0\n"
  280. ]
  281. }
  282. ],
  283. "source": [
  284. "#converting from string to integer\n",
  285. "\n",
  286. "print(type(\"1\"))\n",
  287. "\n",
  288. "print(int(\"1\"))\n",
  289. "\n",
  290. "print(type(int(\"1\")))\n",
  291. "\n",
  292. "#this only works when the string is a number, it will produce an error if you try to do this with a word e.g\n",
  293. "\n",
  294. "#int(\"person\")\n",
  295. "\n",
  296. "#converting string to float \n",
  297. "\n",
  298. "print(float(\"1\"))"
  299. ]
  300. },
  301. {
  302. "cell_type": "code",
  303. "execution_count": 44,
  304. "metadata": {},
  305. "outputs": [
  306. {
  307. "data": {
  308. "text/plain": [
  309. "'1.27654'"
  310. ]
  311. },
  312. "execution_count": 44,
  313. "metadata": {},
  314. "output_type": "execute_result"
  315. }
  316. ],
  317. "source": [
  318. "#converting numbers to strings \n",
  319. "\n",
  320. "str(1.27654)"
  321. ]
  322. },
  323. {
  324. "cell_type": "code",
  325. "execution_count": 51,
  326. "metadata": {},
  327. "outputs": [
  328. {
  329. "name": "stdout",
  330. "output_type": "stream",
  331. "text": [
  332. "<class 'bool'>\n",
  333. "<class 'bool'>\n",
  334. "True\n",
  335. "False\n",
  336. "1\n",
  337. "0\n",
  338. "1.0\n",
  339. "0.0\n"
  340. ]
  341. }
  342. ],
  343. "source": [
  344. "#Boolean data \n",
  345. "\n",
  346. "print(type(True))\n",
  347. "print(type(False))\n",
  348. "\n",
  349. "#converting 1 and 0 into bool\n",
  350. "\n",
  351. "print(bool(1))\n",
  352. "print(bool(0))\n",
  353. "\n",
  354. "#converting True and False into int\n",
  355. "\n",
  356. "print(int(True))\n",
  357. "print(int(False))\n",
  358. "\n",
  359. "#converting True and False into floats\n",
  360. "\n",
  361. "print(float(True))\n",
  362. "print(float(False))"
  363. ]
  364. },
  365. {
  366. "cell_type": "code",
  367. "execution_count": 54,
  368. "metadata": {},
  369. "outputs": [
  370. {
  371. "name": "stdout",
  372. "output_type": "stream",
  373. "text": [
  374. "3.0\n",
  375. "<class 'float'>\n",
  376. "3\n",
  377. "<class 'int'>\n"
  378. ]
  379. }
  380. ],
  381. "source": [
  382. "#operations \n",
  383. "\n",
  384. "#division \n",
  385. "\n",
  386. "#float division \n",
  387. "\n",
  388. "print(6/2)\n",
  389. "print(type(6/2))\n",
  390. "\n",
  391. "#integer division \n",
  392. "\n",
  393. "print(6//2)\n",
  394. "print(type(6//2))\n",
  395. "\n",
  396. "#"
  397. ]
  398. },
  399. {
  400. "cell_type": "code",
  401. "execution_count": 17,
  402. "metadata": {},
  403. "outputs": [
  404. {
  405. "name": "stdout",
  406. "output_type": "stream",
  407. "text": [
  408. "x = \n",
  409. "15\n",
  410. "y = \n",
  411. "-13\n",
  412. "2.6666666666666665\n"
  413. ]
  414. }
  415. ],
  416. "source": [
  417. "#addition \n",
  418. "\n",
  419. "x=1+2+3+4+5\n",
  420. "\n",
  421. "print(\"x = \") \n",
  422. "print(x)\n",
  423. "\n",
  424. "#subtraction \n",
  425. "\n",
  426. "y=1-2-3-4-5\n",
  427. "\n",
  428. "print(\"y = \") \n",
  429. "print(y)\n",
  430. "\n",
  431. "#can do the same with multiplication and division (integer and float)\n",
  432. "\n",
  433. "#how many hours in 160 minutes\n",
  434. "\n",
  435. "mins=160\n",
  436. "number_of_hours=mins/60\n",
  437. "print(number_of_hours)\n",
  438. "\n"
  439. ]
  440. },
  441. {
  442. "cell_type": "code",
  443. "execution_count": 20,
  444. "metadata": {},
  445. "outputs": [
  446. {
  447. "data": {
  448. "text/plain": [
  449. "50.0"
  450. ]
  451. },
  452. "execution_count": 20,
  453. "metadata": {},
  454. "output_type": "execute_result"
  455. }
  456. ],
  457. "source": [
  458. "#assigning variables\n",
  459. "\n",
  460. "x=2*100*3\n",
  461. "y=x/12\n",
  462. "y"
  463. ]
  464. },
  465. {
  466. "cell_type": "code",
  467. "execution_count": 22,
  468. "metadata": {},
  469. "outputs": [
  470. {
  471. "data": {
  472. "text/plain": [
  473. "4.0"
  474. ]
  475. },
  476. "execution_count": 22,
  477. "metadata": {},
  478. "output_type": "execute_result"
  479. }
  480. ],
  481. "source": [
  482. "#overwriting variable \n",
  483. "\n",
  484. "x=12\n",
  485. "x=x/3\n",
  486. "x"
  487. ]
  488. },
  489. {
  490. "cell_type": "code",
  491. "execution_count": 25,
  492. "metadata": {},
  493. "outputs": [
  494. {
  495. "data": {
  496. "text/plain": [
  497. "21"
  498. ]
  499. },
  500. "execution_count": 25,
  501. "metadata": {},
  502. "output_type": "execute_result"
  503. }
  504. ],
  505. "source": [
  506. "# defining variables \n",
  507. "\n",
  508. "x=12\n",
  509. "y=7\n",
  510. "z=1*2\n",
  511. "m=x+y+z\n",
  512. "m"
  513. ]
  514. },
  515. {
  516. "cell_type": "code",
  517. "execution_count": null,
  518. "metadata": {},
  519. "outputs": [],
  520. "source": []
  521. }
  522. ],
  523. "metadata": {
  524. "kernelspec": {
  525. "display_name": "Python",
  526. "language": "python",
  527. "name": "conda-env-python-py"
  528. },
  529. "language_info": {
  530. "codemirror_mode": {
  531. "name": "ipython",
  532. "version": 3
  533. },
  534. "file_extension": ".py",
  535. "mimetype": "text/x-python",
  536. "name": "python",
  537. "nbconvert_exporter": "python",
  538. "pygments_lexer": "ipython3",
  539. "version": "3.6.7"
  540. }
  541. },
  542. "nbformat": 4,
  543. "nbformat_minor": 4
  544. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement