Guest User

Untitled

a guest
Nov 17th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. # Python3 Learning Notes
  2.  
  3. ## by NCJ
  4.  
  5. <br />
  6.  
  7. <!-- `[TOC]` -->
  8. ---------------------------------------------------------------------------------------------------------------------------------------------
  9.  
  10. ### Chapter 1 Setup
  11.  
  12.  
  13. ```python
  14. import this
  15. ```
  16.  
  17. The Zen of Python, by Tim Peters
  18.  
  19. Beautiful is better than ugly.
  20. Explicit is better than implicit.
  21. Simple is better than complex.
  22. Complex is better than complicated.
  23. Flat is better than nested.
  24. Sparse is better than dense.
  25. Readability counts.
  26. Special cases aren't special enough to break the rules.
  27. Although practicality beats purity.
  28. Errors should never pass silently.
  29. Unless explicitly silenced.
  30. In the face of ambiguity, refuse the temptation to guess.
  31. There should be one-- and preferably only one --obvious way to do it.
  32. Although that way may not be obvious at first unless you're Dutch.
  33. Now is better than never.
  34. Although never is often better than *right* now.
  35. If the implementation is hard to explain, it's a bad idea.
  36. If the implementation is easy to explain, it may be a good idea.
  37. Namespaces are one honking great idea -- let's do more of those!
  38.  
  39.  
  40. <br/>
  41.  
  42.  
  43.  
  44.  
  45. ### Chapter 2 Data Types
  46.  
  47. + 001 variables
  48.  
  49.  
  50. ```python
  51. msg = "hello"
  52. print(msg)
  53. ```
  54.  
  55. hello
  56.  
  57.  
  58. + 002 string operations
  59.  
  60.  
  61. ```python
  62. name = "ada lovelace"
  63. print(name.title())
  64. print(name.upper())
  65. print(name.lower())
  66. ```
  67.  
  68. Ada Lovelace
  69. ADA LOVELACE
  70. ada lovelace
  71.  
  72.  
  73.  
  74. ```python
  75. new_msg = msg + " " + name
  76. print ('\t' + new_msg + '\n')
  77. ```
  78.  
  79. hello ada lovelace
  80.  
  81.  
  82.  
  83.  
  84. ```python
  85. msg_1 = ' hiuf yeb w hviu ywe r '
  86. print(msg_1.rstrip())
  87. ```
  88.  
  89. hiuf yeb w hviu ywe r
  90.  
  91.  
  92.  
  93. ```python
  94. msg_1 = msg_1.rstrip()
  95. print(msg_1.lstrip())
  96. ```
  97.  
  98. hiuf yeb w hviu ywe r
  99.  
  100.  
  101.  
  102. ```python
  103. msg_1 = '\t' + msg_1 + '\n'
  104. print(msg_1)
  105. msg_1 = msg_1.rstrip()
  106. print(msg_1)
  107. msg_1 = msg_1.lstrip()
  108. print(msg_1)
  109. ```
  110.  
  111. hiuf yeb w hviu ywe r
  112.  
  113. hiuf yeb w hviu ywe r
  114. hiuf yeb w hviu ywe r
  115.  
  116.  
  117. + 003 number operations
  118.  
  119.  
  120. ```python
  121. 10**6
  122. ```
  123.  
  124.  
  125.  
  126.  
  127. 1000000
  128.  
  129.  
  130.  
  131.  
  132. ```python
  133. 3/2
  134. ```
  135.  
  136.  
  137.  
  138.  
  139. 1.5
  140.  
  141.  
  142.  
  143.  
  144. ```python
  145. 3//2
  146. ```
  147.  
  148.  
  149.  
  150.  
  151. 1
  152.  
  153.  
  154.  
  155.  
  156. ```python
  157. 0.1*3
  158. ```
  159.  
  160.  
  161.  
  162.  
  163. 0.30000000000000004
  164.  
  165.  
  166.  
  167. *floats' problems still exists in python3, but it's common in any language due to how computers save these floats*
  168.  
  169. + 004 number&string
  170.  
  171.  
  172. ```python
  173. num_test_1 = 666
  174. print(num_test_1)
  175. # print('huvife' + num_test_1 + 'gbtehgbe')
  176. # error version
  177. print('huvife' + str(num_test_1) + 'gbtehgbe')
  178. # or
  179. num_test_1 = '666'
  180. print('huvife' + num_test_1 + 'gbtehgbe')
  181. ```
  182.  
  183. 666
  184. huvife666gbtehgbe
  185. huvife666gbtehgbe
  186.  
  187.  
  188. <br/>
  189.  
  190. ### Chapter 3 List
  191.  
  192. *There is no array in Python!!!*
  193.  
  194. + 001 Introduction to Lists
  195.  
  196.  
  197. ```python
  198. arr = ['vfervewr','gftewgwter','gtewgewwe','gtwgw']
  199. print(arr)
  200. print(arr[0].title())
  201. print(arr[-1]) #see? minus is not only available, but useful!
  202. print(arr[-3])
  203. message = "vfsvegeerwsgetrw " + arr[0] + " vbgfdbgfrdsbg"
  204. print(message)
  205. # message = "vfsvegeerwsgetrw " + arr + " vbgfdbgfrdsbg"
  206. # print(message)
  207. # error version
  208. ```
  209.  
  210. ['vfervewr', 'gftewgwter', 'gtewgewwe', 'gtwgw']
  211. Vfervewr
  212. gtwgw
  213. gftewgwter
  214. vfsvegeerwsgetrw vfervewr vbgfdbgfrdsbg
  215.  
  216.  
  217. &emsp;&emsp;*Again, thers is no array in Python! So don't follow my mistake! (I used 'arr' to name a list, because I thought they were the same!)*
  218.  
  219. + 002 Modifying Lists
  220.  
  221.  
  222. ```python
  223. arr[0] = 'cool'
  224. print(arr)
  225. ```
  226.  
  227. ['cool', 'gftewgwter', 'gtewgewwe', 'gtwgw']
  228.  
  229.  
  230. + 003 Add Elements
  231.  
  232.  
  233. ```python
  234. arr.append('new one')
  235. print(arr)
  236. ```
  237.  
  238. ['cool', 'gftewgwter', 'gtewgewwe', 'gtwgw', 'new one']
  239.  
  240.  
  241. &emsp;&emsp;You can even create an empty list and add elements to it later!
  242.  
  243.  
  244. ```python
  245. motor = []
  246.  
  247. motor.append('honda')
  248. motor.append('yamaha')
  249. motor.append('suzuki')
  250.  
  251. print(motor)
  252. ```
  253.  
  254. ['honda', 'yamaha', 'suzuki']
  255.  
  256.  
  257. + 004 Insert Elements
  258.  
  259.  
  260. ```python
  261. motor.insert(0,'ducati')
  262. print(motor)
  263. ```
  264.  
  265. ['ducati', 'honda', 'yamaha', 'suzuki']
  266.  
  267.  
  268. + 005 Delete Elements
  269.  
  270.  
  271. ```python
  272. print(motor)
  273.  
  274. del motor[1]
  275. print(motor)
  276. ```
  277.  
  278. ['ducati', 'honda', 'yamaha', 'suzuki']
  279. ['ducati', 'yamaha', 'suzuki']
  280.  
  281.  
  282. + 006 Pop Elements
  283.  
  284.  
  285. ```python
  286. print(motor)
  287.  
  288. poped_motor = motor.pop()
  289. print(motor)
  290. print(poped_motor)
  291. ```
  292.  
  293. ['ducati', 'yamaha', 'suzuki']
  294. ['ducati', 'yamaha']
  295. suzuki
  296.  
  297.  
  298. &emsp;&emsp;This way, you can still use the element you want to delete for the last time.
  299. &emsp;&emsp;This is useful! Watch this example!
  300.  
  301.  
  302. ```python
  303. motor = ['ducati', 'honda', 'yamaha', 'suzuki']
  304.  
  305. last_owned = motor.pop()
  306. print("The last motorcycle I owned was a " + last_owned.title() + ".")
  307. print(motor)
  308. ```
  309.  
  310. The last motorcycle I owned was a Suzuki.
  311. ['ducati', 'honda', 'yamaha']
  312.  
  313.  
  314. &emsp;&emsp;And pop can be used to pop any element!
  315.  
  316.  
  317. ```python
  318. motor = ['ducati', 'honda', 'yamaha', 'suzuki']
  319.  
  320. first_owned = motor.pop(0)
  321. print("The first motorcycle I owned was a " + first_owned.title() + ".")
  322. print(motor)
  323. ```
  324.  
  325. The first motorcycle I owned was a Ducati.
  326. ['honda', 'yamaha', 'suzuki']
  327.  
  328.  
  329. + 007 Remove Elements According to Its Value
  330.  
  331.  
  332. ```python
  333. motor = ['ducati', 'honda', 'yamaha', 'suzuki']
  334.  
  335. motor.remove('ducati')
  336. print(motor)
  337. ```
  338.  
  339. ['honda', 'yamaha', 'suzuki']
  340.  
  341.  
  342.  
  343. ```python
  344. motor = ['ducati', 'honda', 'yamaha', 'suzuki']
  345.  
  346. too_expensive = 'ducati'
  347. motor.remove(too_expensive)
  348. print(motor)
  349. print("\nA " + too_expensive.title() + " is too expensive for me.")
  350. ```
  351.  
  352. ['honda', 'yamaha', 'suzuki']
  353.  
  354. A Ducati is too expensive for me.
Add Comment
Please, Sign In to add comment