Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.07 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Python Fundamentals - част II"
  8. ]
  9. },
  10. {
  11. "cell_type": "markdown",
  12. "metadata": {},
  13. "source": [
  14. "## Числа"
  15. ]
  16. },
  17. {
  18. "cell_type": "raw",
  19. "metadata": {},
  20. "source": [
  21. "Върху числата в Python можем да упражняваме следните оператори:\n",
  22. "+\n",
  23. "-\n",
  24. "*\n",
  25. "/\n",
  26. "Скоби () се използват за групиране, подобно на други езици както напр. C И C++"
  27. ]
  28. },
  29. {
  30. "cell_type": "code",
  31. "execution_count": 1,
  32. "metadata": {},
  33. "outputs": [
  34. {
  35. "data": {
  36. "text/plain": [
  37. "7"
  38. ]
  39. },
  40. "execution_count": 1,
  41. "metadata": {},
  42. "output_type": "execute_result"
  43. }
  44. ],
  45. "source": [
  46. "3 + 4"
  47. ]
  48. },
  49. {
  50. "cell_type": "code",
  51. "execution_count": 2,
  52. "metadata": {},
  53. "outputs": [
  54. {
  55. "data": {
  56. "text/plain": [
  57. "-13"
  58. ]
  59. },
  60. "execution_count": 2,
  61. "metadata": {},
  62. "output_type": "execute_result"
  63. }
  64. ],
  65. "source": [
  66. "10 - 23"
  67. ]
  68. },
  69. {
  70. "cell_type": "code",
  71. "execution_count": 3,
  72. "metadata": {},
  73. "outputs": [
  74. {
  75. "data": {
  76. "text/plain": [
  77. "4.5"
  78. ]
  79. },
  80. "execution_count": 3,
  81. "metadata": {},
  82. "output_type": "execute_result"
  83. }
  84. ],
  85. "source": [
  86. "9 / 2"
  87. ]
  88. },
  89. {
  90. "cell_type": "code",
  91. "execution_count": 4,
  92. "metadata": {},
  93. "outputs": [
  94. {
  95. "data": {
  96. "text/plain": [
  97. "1"
  98. ]
  99. },
  100. "execution_count": 4,
  101. "metadata": {},
  102. "output_type": "execute_result"
  103. }
  104. ],
  105. "source": [
  106. "9 % 2"
  107. ]
  108. },
  109. {
  110. "cell_type": "code",
  111. "execution_count": 5,
  112. "metadata": {},
  113. "outputs": [
  114. {
  115. "data": {
  116. "text/plain": [
  117. "2"
  118. ]
  119. },
  120. "execution_count": 5,
  121. "metadata": {},
  122. "output_type": "execute_result"
  123. }
  124. ],
  125. "source": [
  126. "9 // 4"
  127. ]
  128. },
  129. {
  130. "cell_type": "code",
  131. "execution_count": 6,
  132. "metadata": {},
  133. "outputs": [
  134. {
  135. "data": {
  136. "text/plain": [
  137. "512"
  138. ]
  139. },
  140. "execution_count": 6,
  141. "metadata": {},
  142. "output_type": "execute_result"
  143. }
  144. ],
  145. "source": [
  146. "2**9"
  147. ]
  148. },
  149. {
  150. "cell_type": "markdown",
  151. "metadata": {},
  152. "source": [
  153. "## Променливи"
  154. ]
  155. },
  156. {
  157. "cell_type": "markdown",
  158. "metadata": {},
  159. "source": [
  160. "Променливете са наименовано място в паметта които служат за съхраняване на данни. Операторът '=' е опертор за присвояване подобно и на много други езици за програмиране"
  161. ]
  162. },
  163. {
  164. "cell_type": "code",
  165. "execution_count": 7,
  166. "metadata": {},
  167. "outputs": [
  168. {
  169. "data": {
  170. "text/plain": [
  171. "12"
  172. ]
  173. },
  174. "execution_count": 7,
  175. "metadata": {},
  176. "output_type": "execute_result"
  177. }
  178. ],
  179. "source": [
  180. "a = 5\n",
  181. "b = 7\n",
  182. "a + b"
  183. ]
  184. },
  185. {
  186. "cell_type": "code",
  187. "execution_count": 8,
  188. "metadata": {
  189. "scrolled": true
  190. },
  191. "outputs": [
  192. {
  193. "ename": "NameError",
  194. "evalue": "name 'var' is not defined",
  195. "output_type": "error",
  196. "traceback": [
  197. "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
  198. "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
  199. "\u001b[1;32m<ipython-input-8-fc893250f2a0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mvar\u001b[0m \u001b[1;31m#try to access undefined variable\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
  200. "\u001b[1;31mNameError\u001b[0m: name 'var' is not defined"
  201. ]
  202. }
  203. ],
  204. "source": [
  205. "var #try to access undefined variable"
  206. ]
  207. },
  208. {
  209. "cell_type": "markdown",
  210. "metadata": {},
  211. "source": [
  212. "Променливата '_' в Python e последната дефиниране. Пример:"
  213. ]
  214. },
  215. {
  216. "cell_type": "markdown",
  217. "metadata": {},
  218. "source": [
  219. "## Стрингове (strings)"
  220. ]
  221. },
  222. {
  223. "cell_type": "code",
  224. "execution_count": 9,
  225. "metadata": {},
  226. "outputs": [
  227. {
  228. "data": {
  229. "text/plain": [
  230. "'single'"
  231. ]
  232. },
  233. "execution_count": 9,
  234. "metadata": {},
  235. "output_type": "execute_result"
  236. }
  237. ],
  238. "source": [
  239. "'single'"
  240. ]
  241. },
  242. {
  243. "cell_type": "code",
  244. "execution_count": 10,
  245. "metadata": {},
  246. "outputs": [
  247. {
  248. "data": {
  249. "text/plain": [
  250. "'double'"
  251. ]
  252. },
  253. "execution_count": 10,
  254. "metadata": {},
  255. "output_type": "execute_result"
  256. }
  257. ],
  258. "source": [
  259. "\"double\""
  260. ]
  261. },
  262. {
  263. "cell_type": "code",
  264. "execution_count": 11,
  265. "metadata": {},
  266. "outputs": [
  267. {
  268. "data": {
  269. "text/plain": [
  270. "'\"multi\\nline'"
  271. ]
  272. },
  273. "execution_count": 11,
  274. "metadata": {},
  275. "output_type": "execute_result"
  276. }
  277. ],
  278. "source": [
  279. "\"\"\"\"multi\n",
  280. "line\"\"\""
  281. ]
  282. },
  283. {
  284. "cell_type": "code",
  285. "execution_count": 12,
  286. "metadata": {},
  287. "outputs": [
  288. {
  289. "data": {
  290. "text/plain": [
  291. "'another\\nmulti\\nline'"
  292. ]
  293. },
  294. "execution_count": 12,
  295. "metadata": {},
  296. "output_type": "execute_result"
  297. }
  298. ],
  299. "source": [
  300. "'''another\n",
  301. "multi\n",
  302. "line'''"
  303. ]
  304. },
  305. {
  306. "cell_type": "code",
  307. "execution_count": 13,
  308. "metadata": {},
  309. "outputs": [
  310. {
  311. "data": {
  312. "text/plain": [
  313. "'\"Ain\\'t,\" easy!.'"
  314. ]
  315. },
  316. "execution_count": 13,
  317. "metadata": {},
  318. "output_type": "execute_result"
  319. }
  320. ],
  321. "source": [
  322. "'\"Ain\\'t,\" easy!.'#escaping"
  323. ]
  324. },
  325. {
  326. "cell_type": "code",
  327. "execution_count": 14,
  328. "metadata": {},
  329. "outputs": [
  330. {
  331. "data": {
  332. "text/plain": [
  333. "'Bam Bam Bam '"
  334. ]
  335. },
  336. "execution_count": 14,
  337. "metadata": {},
  338. "output_type": "execute_result"
  339. }
  340. ],
  341. "source": [
  342. "3 * 'Bam '"
  343. ]
  344. },
  345. {
  346. "cell_type": "code",
  347. "execution_count": 15,
  348. "metadata": {},
  349. "outputs": [
  350. {
  351. "data": {
  352. "text/plain": [
  353. "'BamBoo'"
  354. ]
  355. },
  356. "execution_count": 15,
  357. "metadata": {},
  358. "output_type": "execute_result"
  359. }
  360. ],
  361. "source": [
  362. "'Bam' + 'Boo'"
  363. ]
  364. },
  365. {
  366. "cell_type": "code",
  367. "execution_count": 18,
  368. "metadata": {},
  369. "outputs": [
  370. {
  371. "data": {
  372. "text/plain": [
  373. "'P'"
  374. ]
  375. },
  376. "execution_count": 18,
  377. "metadata": {},
  378. "output_type": "execute_result"
  379. }
  380. ],
  381. "source": [
  382. "word = 'Python'\n",
  383. "word[0]"
  384. ]
  385. },
  386. {
  387. "cell_type": "code",
  388. "execution_count": 19,
  389. "metadata": {},
  390. "outputs": [
  391. {
  392. "data": {
  393. "text/plain": [
  394. "'n'"
  395. ]
  396. },
  397. "execution_count": 19,
  398. "metadata": {},
  399. "output_type": "execute_result"
  400. }
  401. ],
  402. "source": [
  403. "word[5]"
  404. ]
  405. },
  406. {
  407. "cell_type": "code",
  408. "execution_count": 20,
  409. "metadata": {},
  410. "outputs": [
  411. {
  412. "data": {
  413. "text/plain": [
  414. "'n'"
  415. ]
  416. },
  417. "execution_count": 20,
  418. "metadata": {},
  419. "output_type": "execute_result"
  420. }
  421. ],
  422. "source": [
  423. "word[-1]"
  424. ]
  425. },
  426. {
  427. "cell_type": "code",
  428. "execution_count": 21,
  429. "metadata": {},
  430. "outputs": [
  431. {
  432. "data": {
  433. "text/plain": [
  434. "'yt'"
  435. ]
  436. },
  437. "execution_count": 21,
  438. "metadata": {},
  439. "output_type": "execute_result"
  440. }
  441. ],
  442. "source": [
  443. "word[1:3]"
  444. ]
  445. },
  446. {
  447. "cell_type": "code",
  448. "execution_count": 22,
  449. "metadata": {},
  450. "outputs": [
  451. {
  452. "data": {
  453. "text/plain": [
  454. "'Py'"
  455. ]
  456. },
  457. "execution_count": 22,
  458. "metadata": {},
  459. "output_type": "execute_result"
  460. }
  461. ],
  462. "source": [
  463. "word[:2]"
  464. ]
  465. },
  466. {
  467. "cell_type": "code",
  468. "execution_count": 23,
  469. "metadata": {
  470. "scrolled": true
  471. },
  472. "outputs": [
  473. {
  474. "data": {
  475. "text/plain": [
  476. "'Pytho'"
  477. ]
  478. },
  479. "execution_count": 23,
  480. "metadata": {},
  481. "output_type": "execute_result"
  482. }
  483. ],
  484. "source": [
  485. "word[:-1]"
  486. ]
  487. },
  488. {
  489. "cell_type": "code",
  490. "execution_count": 24,
  491. "metadata": {},
  492. "outputs": [
  493. {
  494. "data": {
  495. "text/plain": [
  496. "6"
  497. ]
  498. },
  499. "execution_count": 24,
  500. "metadata": {},
  501. "output_type": "execute_result"
  502. }
  503. ],
  504. "source": [
  505. "len(word)"
  506. ]
  507. },
  508. {
  509. "cell_type": "markdown",
  510. "metadata": {},
  511. "source": [
  512. "## Lists И Tuples"
  513. ]
  514. },
  515. {
  516. "cell_type": "raw",
  517. "metadata": {},
  518. "source": [
  519. "Списъкът(List) е подобно масив, където обаче елементите могат да бъдат различни типове данни. Елементите са разделени с „,“ и списъкът е заобиколен от „[]“"
  520. ]
  521. },
  522. {
  523. "cell_type": "code",
  524. "execution_count": 26,
  525. "metadata": {},
  526. "outputs": [],
  527. "source": [
  528. "lst = [ 'Peter', 1024, 3.14, 'This is a string', 666 ]"
  529. ]
  530. },
  531. {
  532. "cell_type": "raw",
  533. "metadata": {},
  534. "source": [
  535. "Tuples са подобни на списъците, но не могат да бъдат променяни. Друга разлика е, че те са заобиколени от „()“"
  536. ]
  537. },
  538. {
  539. "cell_type": "code",
  540. "execution_count": null,
  541. "metadata": {},
  542. "outputs": [],
  543. "source": [
  544. "tup = ( 'Peter', 1024, 3.14, 'This is a string', 666 )"
  545. ]
  546. },
  547. {
  548. "cell_type": "code",
  549. "execution_count": 27,
  550. "metadata": {},
  551. "outputs": [
  552. {
  553. "data": {
  554. "text/plain": [
  555. "3.14"
  556. ]
  557. },
  558. "execution_count": 27,
  559. "metadata": {},
  560. "output_type": "execute_result"
  561. }
  562. ],
  563. "source": [
  564. "lst[2]"
  565. ]
  566. },
  567. {
  568. "cell_type": "code",
  569. "execution_count": 28,
  570. "metadata": {},
  571. "outputs": [
  572. {
  573. "data": {
  574. "text/plain": [
  575. "'This is a string'"
  576. ]
  577. },
  578. "execution_count": 28,
  579. "metadata": {},
  580. "output_type": "execute_result"
  581. }
  582. ],
  583. "source": [
  584. "lst[-2]"
  585. ]
  586. },
  587. {
  588. "cell_type": "markdown",
  589. "metadata": {},
  590. "source": [
  591. "Oсновни функции за работа с списъци: \n",
  592. "list.append(obj)\n",
  593. "list.count(obj)\n",
  594. "list.extend(seq)\n",
  595. "list.index(obj)\n",
  596. "list.insert(index, obj)\n",
  597. "list.pop(obj=list[-1])\n",
  598. "list.remove(obj)\n",
  599. "list.reverse()\n",
  600. "list.sort([func])"
  601. ]
  602. },
  603. {
  604. "cell_type": "code",
  605. "execution_count": 31,
  606. "metadata": {},
  607. "outputs": [
  608. {
  609. "name": "stdout",
  610. "output_type": "stream",
  611. "text": [
  612. "1\n",
  613. "['Peter', 1024, 3.14, 'This is a string', 666]\n",
  614. "['Peter', 1024, 3.14, 'This is a string']\n"
  615. ]
  616. }
  617. ],
  618. "source": [
  619. "print(lst.count(3.14Basic functions over Tuples: \n",
  620. "cmp(list1, list2)\n",
  621. "\n",
  622. "len(list)\n",
  623. "\n",
  624. "max(list)\n",
  625. "\n",
  626. "min(list)\n",
  627. "\n",
  628. "tuple(seq)))\n",
  629. "print(lst)\n",
  630. "lst.pop()\n",
  631. "print(lst)\n"
  632. ]
  633. },
  634. {
  635. "cell_type": "markdown",
  636. "metadata": {},
  637. "source": [
  638. "Oсновни функции за работа с Tuples: \n",
  639. "cmp(list1, list2)\n",
  640. "\n",
  641. "len(list)\n",
  642. "\n",
  643. "max(list)\n",
  644. "\n",
  645. "min(list)\n",
  646. "\n",
  647. "tuple(seq)"
  648. ]
  649. },
  650. {
  651. "cell_type": "markdown",
  652. "metadata": {},
  653. "source": [
  654. "Проверки (Conditional Statements) в Python"
  655. ]
  656. },
  657. {
  658. "cell_type": "code",
  659. "execution_count": null,
  660. "metadata": {},
  661. "outputs": [],
  662. "source": [
  663. "check = 6\n",
  664. "if check == 17:\n",
  665. " print('Success')\n",
  666. "elif check < 0:\n",
  667. " print('Negative')\n",
  668. "else:\n",
  669. " print(check)"
  670. ]
  671. },
  672. {
  673. "cell_type": "markdown",
  674. "metadata": {},
  675. "source": [
  676. "## Преобразуване на типове"
  677. ]
  678. },
  679. {
  680. "cell_type": "markdown",
  681. "metadata": {},
  682. "source": [
  683. "Както в повечето езици за програмиране преобразуването на типовете съществува и в Python. То се реализира чрез вградени функции като: int() str() list() tuple() и др."
  684. ]
  685. },
  686. {
  687. "cell_type": "code",
  688. "execution_count": 32,
  689. "metadata": {},
  690. "outputs": [
  691. {
  692. "data": {
  693. "text/plain": [
  694. "'12'"
  695. ]
  696. },
  697. "execution_count": 32,
  698. "metadata": {},
  699. "output_type": "execute_result"
  700. }
  701. ],
  702. "source": [
  703. "num = 12\n",
  704. "str(num)"
  705. ]
  706. },
  707. {
  708. "cell_type": "code",
  709. "execution_count": 33,
  710. "metadata": {},
  711. "outputs": [
  712. {
  713. "data": {
  714. "text/plain": [
  715. "123"
  716. ]
  717. },
  718. "execution_count": 33,
  719. "metadata": {},
  720. "output_type": "execute_result"
  721. }
  722. ],
  723. "source": [
  724. "s = '123'\n",
  725. "int(s)"
  726. ]
  727. },
  728. {
  729. "cell_type": "code",
  730. "execution_count": 36,
  731. "metadata": {},
  732. "outputs": [
  733. {
  734. "data": {
  735. "text/plain": [
  736. "['1', ',', 'a', '.', '4', ',', '5']"
  737. ]
  738. },
  739. "execution_count": 36,
  740. "metadata": {},
  741. "output_type": "execute_result"
  742. }
  743. ],
  744. "source": [
  745. "s = '1,a.4,5'\n",
  746. "list(s)"
  747. ]
  748. },
  749. {
  750. "cell_type": "code",
  751. "execution_count": 37,
  752. "metadata": {},
  753. "outputs": [
  754. {
  755. "data": {
  756. "text/plain": [
  757. "['1', '2', '6', '9']"
  758. ]
  759. },
  760. "execution_count": 37,
  761. "metadata": {},
  762. "output_type": "execute_result"
  763. }
  764. ],
  765. "source": [
  766. "s ='1 2 6 9'\n",
  767. "list(s.split(' '))"
  768. ]
  769. }
  770. ],
  771. "metadata": {
  772. "kernelspec": {
  773. "display_name": "Python 3",
  774. "language": "python",
  775. "name": "python3"
  776. },
  777. "language_info": {
  778. "codemirror_mode": {
  779. "name": "ipython",
  780. "version": 3
  781. },
  782. "file_extension": ".py",
  783. "mimetype": "text/x-python",
  784. "name": "python",
  785. "nbconvert_exporter": "python",
  786. "pygments_lexer": "ipython3",
  787. "version": "3.7.3"
  788. }
  789. },
  790. "nbformat": 4,
  791. "nbformat_minor": 2
  792. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement