YouTube2Text

Python Full Course for free ๐Ÿ โ€” Transcript

by Bro Code ยท 111,769 words ยท 19,359 segments ยท language en ยท Watch on YouTube

Full transcript

  1. 0:00hey what's going on everybody it's bro
  2. 0:02hope you're doing well
  3. 0:03and in this video i'm going to help you
  4. 0:04get started with python so
  5. 0:06sit back relax and enjoy the show
  6. 0:11if you wouldn't mind please like comment
  7. 0:13and subscribe
  8. 0:14one like equals one prayer for the
  9. 0:16youtube algorithm
  10. 0:18i'm gonna tell you why you need to learn
  11. 0:20python it's the easiest programming
  12. 0:22language to learn as well as being the
  13. 0:24most popular in the world right now
  14. 0:26and according to glassdoor the salary
  15. 0:28for new python developers in the united
  16. 0:31states is
  17. 0:3264 000 if any of this sounds good to you
  18. 0:35well then
  19. 0:36let's get started if you need to
  20. 0:37download python go to
  21. 0:39python.org downloads and click this
  22. 0:42yellow download python
  23. 0:44button open when done check add
  24. 0:47python 3.9 or another version to path
  25. 0:51install now and then give it a little
  26. 0:53bit it states here that setup was
  27. 0:56successful so let's close out of this
  28. 0:58our next step is to download an ide
  29. 1:00that's an acronym for
  30. 1:02integrated development environment think
  31. 1:04of it as
  32. 1:05software that helps us write other
  33. 1:07software one ide that i recommend
  34. 1:09is pycharm you can find it at
  35. 1:11jetbrains.com
  36. 1:13pycharm go to download if you would like
  37. 1:17to pay for a professional version you
  38. 1:19can click
  39. 1:19this download button but since i don't
  40. 1:21like to pay for things i'm going to use
  41. 1:23the free community version at this point
  42. 1:26we just have to follow the standard
  43. 1:28installation procedure
  44. 1:29click next you can pick a destination
  45. 1:32folder but i'll keep the default
  46. 1:34next if you would like a desktop
  47. 1:37shortcut
  48. 1:38then check this i would also recommend
  49. 1:40updating the
  50. 1:41path variable next
  51. 1:44install and then give it a minute or two
  52. 1:48then after finishing i have a desktop
  53. 1:50shortcut so i can just click on this to
  54. 1:52run pycharm
  55. 1:53let's create a new project name this
  56. 1:56project whatever you like i'm going to
  57. 1:58name this project
  58. 1:59hello world and i recommend not creating
  59. 2:03a
  60. 2:03main dot pi welcome script right now
  61. 2:05i'll show you how to do that manually
  62. 2:07and let's create
  63. 2:09if you need a python file to work with
  64. 2:12this is how to create one go to
  65. 2:14file new python file and i'm going to
  66. 2:17name
  67. 2:18this main and then click python file
  68. 2:23all right ladies and gentlemen we now
  69. 2:25have an empty python script that we can
  70. 2:27use to begin coding
  71. 2:28now if you checked this box create a
  72. 2:31main.pi welcome script and then created
  73. 2:33a new project
  74. 2:34then your main python file will probably
  75. 2:37look a little something like this but
  76. 2:38that's completely fine we can just
  77. 2:40delete this
  78. 2:41i don't know about you guys but the font
  79. 2:43is very small for me
  80. 2:45so let's change that we can change that
  81. 2:47by going to file
  82. 2:49settings editor font and you can
  83. 2:52increase the font size
  84. 2:54let's say 25 you can also change the
  85. 2:56font too
  86. 2:57what's a good one uh how about
  87. 3:01that then click apply and
  88. 3:04okay much better we can actually see
  89. 3:06something
  90. 3:07to run a python script just click the
  91. 3:09screen play button in the corner
  92. 3:11alternatively you can go to the run tab
  93. 3:13and then go to run
  94. 3:15okay so this box that appeared at the
  95. 3:17bottom this is referred to as a terminal
  96. 3:20window this will display
  97. 3:21any output that your program has and
  98. 3:23this line at the end
  99. 3:25process finished with exit code zero
  100. 3:27that just means that there were no
  101. 3:29errors in this program
  102. 3:30so currently this program doesn't do
  103. 3:32anything but let's change that let's
  104. 3:34print something
  105. 3:35let's print a message so to print a
  106. 3:37message to the terminal window
  107. 3:39type print and then add a set of
  108. 3:42parentheses
  109. 3:42and then you can type a message but it
  110. 3:44has to be within quotes
  111. 3:46either double quotes or single quotes it
  112. 3:48doesn't matter
  113. 3:49so let's think of a message i don't know
  114. 3:52i
  115. 3:53love pizza then let me run this
  116. 3:57and in my terminal window it says i love
  117. 4:00pizza
  118. 4:01to print another line just add another
  119. 4:03print statement
  120. 4:04print and then your message within
  121. 4:06quotes
  122. 4:08it's really good
  123. 4:13and well what do you know it printed i
  124. 4:16love pizza
  125. 4:17it's really good so ladies and gentlemen
  126. 4:19you just wrote your first python program
  127. 4:21by the way you can change the font as
  128. 4:24well as the font color of your terminal
  129. 4:26window by going to
  130. 4:27file settings then go to editor
  131. 4:31color scheme console font and console
  132. 4:34colors
  133. 4:34let's change the font let's say i would
  134. 4:38like
  135. 4:39whatever this font is then click apply
  136. 4:43let's change the font color too by going
  137. 4:45to console colors
  138. 4:47for any output you can click number two
  139. 4:51and select a color let's say i would
  140. 4:54like
  141. 4:54bright green that should be decent
  142. 4:59and for anything else let's say this
  143. 5:02will be a dark color so i can barely see
  144. 5:04it
  145. 5:05so then let's click apply and okay
  146. 5:08oh yeah that's much better well
  147. 5:10everybody that was your very first
  148. 5:12python program i'll post
  149. 5:14any useful links and timestamps for this
  150. 5:16video in the comments section down below
  151. 5:18but yeah welcome to coding with python
  152. 5:22hey you yeah i'm talking to you if you
  153. 5:25learned something new
  154. 5:26then help me help you in three easy
  155. 5:28steps
  156. 5:29by smashing that like button drop a
  157. 5:31comment down below
  158. 5:32and subscribe if you'd like to become a
  159. 5:34fellow bro
  160. 5:38[Music]
  161. 5:56hey what's going on everybody it's bro
  162. 5:59hope you're doing well and in this video
  163. 6:01i'm going to explain how
  164. 6:02variables work in python so sit back
  165. 6:05relax
  166. 6:06and enjoy the show
  167. 6:09if you find this video helpful please
  168. 6:11remember to like
  169. 6:12comment and subscribe your support will
  170. 6:15help keep this channel running
  171. 6:17all right let's do this a variable is a
  172. 6:20container for a value it behaves as the
  173. 6:22value that it contains
  174. 6:24it's much like in algebra lessons back
  175. 6:26in the day where we had to solve for
  176. 6:28whatever value
  177. 6:29x was and then we can reuse x and it
  178. 6:31behaved exactly as that value
  179. 6:34well with programming we can do that
  180. 6:35plus we can assign variables of
  181. 6:37different data types
  182. 6:38so they are not limited to just numbers
  183. 6:40we can assign whole words
  184. 6:42numbers and even these things called
  185. 6:44booleans which are either true or false
  186. 6:46but i'll get more in depth into data
  187. 6:48types a little bit later on in this
  188. 6:49video
  189. 6:50so let's create a variable and this is
  190. 6:51how to do so we need a unique name for
  191. 6:54this variable
  192. 6:54let's say name name equals some value
  193. 6:57now the first data type that we'll cover
  194. 6:59is strings a string
  195. 7:01is a series of characters so to create a
  196. 7:03string
  197. 7:04we can either use single quotes or
  198. 7:06double quotes in python
  199. 7:08and we can assign this a value of
  200. 7:10whatever your own name is
  201. 7:11so i will assign this variable name a
  202. 7:14value of bro
  203. 7:15now this variable will behave exactly as
  204. 7:18if it was
  205. 7:18this value this name and then we can
  206. 7:20reuse this variable for something
  207. 7:22so let's print whatever our name is
  208. 7:25print
  209. 7:26name and this will print whatever your
  210. 7:28name is to the console window
  211. 7:30now when you print your variable make
  212. 7:32sure you don't put it within quotes
  213. 7:34because what we're doing then is
  214. 7:36literally printing
  215. 7:37the word name as you can see here so
  216. 7:41if you need to use your variable for
  217. 7:42something make sure it's not within
  218. 7:44quotes
  219. 7:45now we can combine our string variable
  220. 7:48with another string so within our print
  221. 7:50statement let's print
  222. 7:52a string literal such as hello space
  223. 7:55plus name so we are combining strings
  224. 7:58and then we're printing the results to
  225. 7:59the console window so this will print
  226. 8:02hello bro now you can actually check the
  227. 8:05data type of a variable
  228. 8:06so i'm going to turn this line into a
  229. 8:08comment so what you'll do
  230. 8:10is take the name of the variable
  231. 8:12surround this with a set of parentheses
  232. 8:14and then precede the set of parentheses
  233. 8:16with the type
  234. 8:17function and this will print the data
  235. 8:20type of this variable to the console
  236. 8:21window
  237. 8:22and you can see that it says class str
  238. 8:25short for string
  239. 8:26a string is a series of characters a
  240. 8:28name is a good example of a string
  241. 8:30a series of characters so that is how to
  242. 8:32check the data type
  243. 8:34of a variable just use the type function
  244. 8:37now you can actually combine variables
  245. 8:39together as long as they're of the same
  246. 8:41data type so let's change our name to
  247. 8:44maybe first name and we'll create a
  248. 8:45second variable called lastname
  249. 8:47now a common naming convention with
  250. 8:49variables if it has
  251. 8:51two words separate each word with an
  252. 8:53underscore
  253. 8:54it's not necessary but it's common
  254. 8:56practice for python and honestly i
  255. 8:58sometimes forget about that but don't
  256. 8:59tell anybody
  257. 9:00so let's create another variable called
  258. 9:02last name last
  259. 9:03name equals whatever your last name is
  260. 9:07and then let's create a third variable
  261. 9:09called full name
  262. 9:10full underscore name and let's combine
  263. 9:14first name plus last
  264. 9:18name and let us display full name
  265. 9:22along with hello plus full name
  266. 9:25now these two names were combined
  267. 9:26together to create a whole
  268. 9:28new variable actually what i think i'll
  269. 9:30do is add a space
  270. 9:32within my variable so i'm doing a bunch
  271. 9:35of string concatenation i'm combining
  272. 9:37the variable first name plus a space
  273. 9:39character plus
  274. 9:40last name so the result is hello
  275. 9:43whatever your first name and last name
  276. 9:44is
  277. 9:45so that is a variable of the string data
  278. 9:48type it is a series of characters
  279. 9:51now with strings we cannot normally use
  280. 9:53these for any sort of math
  281. 9:55that's where the next data type comes in
  282. 9:57for the time being i just turned all of
  283. 9:59these lines into comments and we are
  284. 10:00going to move on to the int
  285. 10:02data type short for integer this time
  286. 10:04let's create a variable called
  287. 10:06age and we will assign this a value of
  288. 10:08whatever your age is
  289. 10:09let's just say that i'm 21 now when you
  290. 10:12assign
  291. 10:12a value of the int data type make sure
  292. 10:15that it is
  293. 10:16not within quotes because then
  294. 10:18technically it would be a string then
  295. 10:19and you can see that the color scheme
  296. 10:21for this number actually changed
  297. 10:23so if this was a string we would treat
  298. 10:25it different
  299. 10:26than an int data type because with
  300. 10:28strings we cannot normally use them for
  301. 10:30any sort of math
  302. 10:31so we now have a variable called edge
  303. 10:34and we can actually
  304. 10:35increase or decrease or do whatever we
  305. 10:38want with the edge
  306. 10:38let's increase our age by one let's say
  307. 10:41it's our birthday to do so we would say
  308. 10:42age
  309. 10:43equals age plus one and then let's print
  310. 10:46our edge to the console window
  311. 10:48print edge and you can see that it says
  312. 10:51i am 22.
  313. 10:52now there's a shorthand way of writing
  314. 10:53this what you'll do is type
  315. 10:55age plus equals one so that's kind of
  316. 10:58like a shortcut and this would work the
  317. 11:00same too
  318. 11:01now let's print the data type of our
  319. 11:03inch variable because we can within a
  320. 11:05print statement i will print the edge
  321. 11:07and add my edge variable to this type
  322. 11:10function and this will print the data
  323. 11:13type of my edge variable to the console
  324. 11:15window
  325. 11:15and i will turn this line into a comment
  326. 11:17just so that it doesn't get in the way
  327. 11:19now if i were to print the data type of
  328. 11:21my age variable
  329. 11:23as you can see it is of the int data
  330. 11:25type short for integer
  331. 11:27it only stores a whole integer number
  332. 11:30now what if this was a string so what
  333. 11:33i'm going to do
  334. 11:34is surround my value within a set of
  335. 11:36quotes
  336. 11:37and i will turn this line into a comment
  337. 11:39so that it does not get in the way
  338. 11:41you can see that the data type of my age
  339. 11:43variable is now
  340. 11:44a string and with strings we cannot
  341. 11:46normally use them for any sort of math
  342. 11:48so let's attempt to increase our age
  343. 11:50variable by one
  344. 11:52and we ran into an error a type air can
  345. 11:54only concatenate string
  346. 11:56to string not end now the point being is
  347. 11:59that
  348. 12:00it's important to use the appropriate
  349. 12:02data type because
  350. 12:03with strings we cannot normally use them
  351. 12:05for any sort of math you'll want to use
  352. 12:06a data type of a numeric value
  353. 12:09an int or a float which we'll discuss a
  354. 12:11little bit later now here's a situation
  355. 12:13to consider
  356. 12:13what if we would like to display our
  357. 12:15edge variable along with a string
  358. 12:17literal much like what we did with this
  359. 12:19line involving a string literal such as
  360. 12:22hello
  361. 12:22plus the user's full name so let's
  362. 12:25attempt to do so and this is not going
  363. 12:26to work and i'll explain why
  364. 12:28so within our print statement let's say
  365. 12:30something such as
  366. 12:31your age is plus
  367. 12:35edge now we're going to run into a type
  368. 12:38error
  369. 12:38that's because we attempted to use
  370. 12:40string concatenation with a string
  371. 12:42literal
  372. 12:43and a variable of a different data type
  373. 12:46a variable of the
  374. 12:47int data type in order to display our
  375. 12:50edge along with the string we would need
  376. 12:52to convert our edge variable to the
  377. 12:54string
  378. 12:55data type and one way in which we can do
  379. 12:57that is by type casting
  380. 12:59and we'll cover this more in the next
  381. 13:00video so we're going to surround
  382. 13:02our age variable that is of the in data
  383. 13:05type with a set of parentheses
  384. 13:07and precede this with str short for
  385. 13:09string and this will convert our
  386. 13:11variable
  387. 13:12to a string that will allow us to
  388. 13:13display it with another string
  389. 13:15now if we were to run this program this
  390. 13:17way this will now display
  391. 13:19your age is and in this case 22 so if
  392. 13:22you need to display a variable of a
  393. 13:24different data type
  394. 13:26along with the string you would just
  395. 13:27need to use a stringcast to convert that
  396. 13:29data type to a string
  397. 13:31but we'll cover this more in the next
  398. 13:32video on typecasting
  399. 13:34so that is the int data type it stores a
  400. 13:37whole integer number and the next data
  401. 13:40type is the
  402. 13:41float data type and i'm going to turn
  403. 13:43these lines into comments and we can
  404. 13:44move on
  405. 13:45now the float data type is a numeric
  406. 13:47value that can store
  407. 13:48a number that includes a decimal portion
  408. 13:52an int data type cannot store a decimal
  409. 13:54portion that's why it's in
  410. 13:56short for integer it only stores a whole
  411. 13:58number this time let's say we have a
  412. 14:00height
  413. 14:00and i will assign this a perfectly
  414. 14:02normal height of maybe 250.5
  415. 14:06centimeters so this decimal portion is
  416. 14:08important
  417. 14:09that's indicating that this is a
  418. 14:11floating point number a float for short
  419. 14:13so let's print our height to the console
  420. 14:15window print
  421. 14:17height and this will print whatever my
  422. 14:19height is and now
  423. 14:20let's check the type of our height using
  424. 14:23the type function
  425. 14:25so within a print statement i'm going to
  426. 14:27add my height
  427. 14:28surround this with another set of
  428. 14:30parentheses and precede this
  429. 14:32with the type function and the data type
  430. 14:35of my variable height is a float short
  431. 14:38for a floating point number
  432. 14:39a numeric value that contains a decimal
  433. 14:42portion
  434. 14:42and now for practice let's print our
  435. 14:44height along with a string
  436. 14:46literal so let's create a string literal
  437. 14:48such as
  438. 14:50your height is
  439. 14:53plus height but guess what we're going
  440. 14:55to do some type casting
  441. 14:56so surround height with a set of
  442. 14:58parentheses and precede this with
  443. 15:00str so that we convert height to a
  444. 15:03string so that we can use
  445. 15:04string concatenation within this print
  446. 15:06statement and this will print the
  447. 15:07message
  448. 15:08your height is 250.5 actually let's add
  449. 15:12centimeters to the end so
  450. 15:14plus cm
  451. 15:17and there we go and that is what a
  452. 15:19floating point number is
  453. 15:20float for short it is a numeric value
  454. 15:22that contains a decimal portion
  455. 15:25now the last data type that we're going
  456. 15:26to cover is the boolean
  457. 15:28data type it is a variable that can only
  458. 15:30store true
  459. 15:31or false let's say we have a variable
  460. 15:34called human
  461. 15:35if we were to assign this a boolean
  462. 15:37value this would be either true or
  463. 15:40false and we can print whatever this
  464. 15:42value is let's print whatever
  465. 15:44human is and this will display false now
  466. 15:47the reason that you may want to use
  467. 15:49booleans instead of strings even though
  468. 15:51you can still store
  469. 15:52a string representation of the word
  470. 15:54false or true
  471. 15:56is that these are very useful when we
  472. 15:58get to if statements
  473. 15:59we can check to see if some statement is
  474. 16:01true so that is a good use of
  475. 16:04boolean values now let's check the data
  476. 16:07type of
  477. 16:08my human variable so let's print
  478. 16:11type human and this will print to the
  479. 16:15console window
  480. 16:16bool short for boolean now make sure
  481. 16:18you're not putting this within quotes
  482. 16:20because then it's technically a string
  483. 16:22and they behave differently
  484. 16:23now the data type is a string and you
  485. 16:26may have noticed as well that the font
  486. 16:27color has changed between
  487. 16:29strings and boolean values so that's
  488. 16:31another good indicator
  489. 16:32now what if you need to display your
  490. 16:35value along with a string using string
  491. 16:37concatenation
  492. 16:38well guess what we're going to typecast
  493. 16:39again so let's print something
  494. 16:44r u a human
  495. 16:48so we need to use string concatenation
  496. 16:50so i will convert
  497. 16:52my boolean variable of human to a string
  498. 16:55and i have been programmed to inform you
  499. 16:58that
  500. 16:59yes i am in fact a human that is true
  501. 17:01well everyone
  502. 17:02that is the basics of variables it is a
  503. 17:05container for a value and they behave
  504. 17:07as the value that they contain there are
  505. 17:09four basic data types
  506. 17:11strings which store a series of
  507. 17:13characters inc
  508. 17:14which store a whole integer floats which
  509. 17:18are floating point numbers a numeric
  510. 17:20value with a decimal
  511. 17:21and booleans which only store true or
  512. 17:24false
  513. 17:25and they're very useful with if
  514. 17:27statements so that is it for variables
  515. 17:29if you would like a copy of all this
  516. 17:31code i will copy and paste all of this
  517. 17:33to the comments section down below
  518. 17:35but yeah that is how variables work in
  519. 17:37python
  520. 17:38hey people what's going on it's bro hope
  521. 17:41you're doing well
  522. 17:41and in this video i'm going to explain
  523. 17:43multiple assignment in python so
  524. 17:45sit back relax and enjoy the show
  525. 17:50well then since you clicked on this
  526. 17:52video i should probably explain what
  527. 17:53multiple assignment is
  528. 17:55now multiple assignment allows us to
  529. 17:57assign multiple variables
  530. 17:59at the same time using one line of code
  531. 18:02here's an example of us
  532. 18:03using standard assignment let's say we
  533. 18:05have a variable
  534. 18:06name and i will set this to a value of
  535. 18:09my name but you can use your name for
  536. 18:11this example
  537. 18:12let's say edge equals 21 and how about a
  538. 18:15variable called
  539. 18:16attractive i think i'm going to set this
  540. 18:19to true
  541. 18:20okay so we have a bunch of variables and
  542. 18:22then we can print
  543. 18:23the value of these variables with some
  544. 18:25print statements
  545. 18:26so let's print name age and attractive
  546. 18:30so we have name age attractive
  547. 18:34and as you would expect this prints bro
  548. 18:3721 and true
  549. 18:38now another way in which we could write
  550. 18:40the same code is to use
  551. 18:42multiple assignment and this allows us
  552. 18:44to assign multiple variables
  553. 18:46at the same time using one line of code
  554. 18:48so i'm going to turn
  555. 18:50all of these lines into comments and
  556. 18:52this time
  557. 18:53we will only use one line of code but to
  558. 18:55do this we're going to list all of our
  559. 18:57variables
  560. 18:58separated with a comma so that would be
  561. 19:00name
  562. 19:01comma edge comma attractive
  563. 19:05and we will set them equal to those
  564. 19:08values
  565. 19:08but in the same order separated by
  566. 19:10commas so that would be
  567. 19:12bro comma 21 comma true
  568. 19:15and this would do the same thing only
  569. 19:17using one line of code
  570. 19:19here's another example let's say we have
  571. 19:21four variables
  572. 19:22spongebob patrick sandy and squidward
  573. 19:25and
  574. 19:25let's pretend that they're all around
  575. 19:27maybe 30 years old
  576. 19:28at least it seems like they're around 30
  577. 19:30years old but i could be wrong
  578. 19:31i'm not too familiar with spongebob lore
  579. 19:34so let's set them all equal to 30.
  580. 19:36now we could write this using four lines
  581. 19:39of code or
  582. 19:40we could use multiple assignment to
  583. 19:42write the same thing
  584. 19:43using one line of code so if all of
  585. 19:45these variables
  586. 19:46are all equal to the same value there's
  587. 19:49a little bit of different syntax here
  588. 19:51so let's begin with the first variable
  589. 19:53of spongebob
  590. 19:54equals patrick
  591. 19:59equals sandy equals
  592. 20:02squidward then we're going to set them
  593. 20:06all equal to 30 and
  594. 20:09this should work just fine using only
  595. 20:11one line of code
  596. 20:12well everybody that's pretty much it for
  597. 20:14multiple assignment it allows us to
  598. 20:15assign
  599. 20:16multiple variables at the same time
  600. 20:18using one line of code
  601. 20:20i will post all of this code in the
  602. 20:22comments section down below
  603. 20:23and well yeah that's how multiple
  604. 20:25assignment works in python
  605. 20:27hey what's going on everybody it's bro
  606. 20:29hope you're doing well
  607. 20:30and in this video i'm going to
  608. 20:31demonstrate some useful string methods
  609. 20:33in python so
  610. 20:34sit back relax and enjoy the show
  611. 20:39all right let's do this in this video
  612. 20:42i'm going to be explaining and
  613. 20:43demonstrating
  614. 20:44a few useful methods available to us for
  615. 20:47strings
  616. 20:47now let's create a variable called name
  617. 20:49for this example and you can use your
  618. 20:51own name if you want
  619. 20:52so we now have a variable called name
  620. 20:54and for my first example
  621. 20:56let's print the length of our name
  622. 20:59variable
  623. 21:00so to do that we can surround our name
  624. 21:03variable with the length method and this
  625. 21:05will print
  626. 21:06and return how long the length of our
  627. 21:09string is
  628. 21:09and for me the length of my name
  629. 21:11variable is three
  630. 21:13for three characters if i were to
  631. 21:15increase the size of my name
  632. 21:17well then that's going to change to a so
  633. 21:19that is
  634. 21:20the length method and i'm just going to
  635. 21:22turn this line into a comment
  636. 21:24and let's move on so next we have the
  637. 21:26find method
  638. 21:27i just copy and pasted print name so i
  639. 21:30can reuse it
  640. 21:31name dot and actually if you have
  641. 21:33intellisense setup
  642. 21:34if you add dot after your variable
  643. 21:37you'll get a suggestion or a few
  644. 21:39suggestions of
  645. 21:40different methods and other features
  646. 21:42that you have access to so
  647. 21:44let's find find which is
  648. 21:48uh screw this i'll just type find find
  649. 21:51then we can find the first index of
  650. 21:54where this character is
  651. 21:55let's say i would like to find where
  652. 21:57capital b is
  653. 21:58well that is at index zero because
  654. 22:01computers always start with zero
  655. 22:03so this first character would be zero
  656. 22:05then r
  657. 22:06would be one and then o would be
  658. 22:10two for this example all right so that
  659. 22:12is how to find a character
  660. 22:14within a string moving on so let's
  661. 22:17capitalize our name and i should
  662. 22:19probably make this lowercase
  663. 22:23so name dot capitalize
  664. 22:26and this will make my name capitalized
  665. 22:29but if there's any spaces
  666. 22:31and then additional words it's not going
  667. 22:33to capitalize that
  668. 22:34only the first letter and your string
  669. 22:37next we have upper this will make your
  670. 22:40string all uppercase
  671. 22:42name upper and now
  672. 22:45it is uppercase guess what's next
  673. 22:49lower name dot lower
  674. 22:52and i should probably make this capital
  675. 22:54again
  676. 22:57and everything is lowercase
  677. 23:00we're making some good progress today
  678. 23:02next we have
  679. 23:04is digit this will return true or false
  680. 23:07depending on if our string is a digit
  681. 23:11which it's not so this will print false
  682. 23:14if i changed my string to a bunch of
  683. 23:15numbers like 1 2
  684. 23:173 then this will return true let's
  685. 23:19change that back
  686. 23:20and move on next we have
  687. 23:25is alpha
  688. 23:28are these alphabetical characters
  689. 23:33oh make sure you add the parentheses
  690. 23:35afterwards my bad
  691. 23:37false because we added a space within
  692. 23:41here
  693. 23:42now it's true so you can use is alpha to
  694. 23:46check to see
  695. 23:46if your string contains only letters
  696. 23:49alphabetical letters
  697. 23:51next we have count
  698. 23:55we can count how many characters are
  699. 23:57within our string
  700. 23:58i would like to count how many o's are
  701. 24:00in here and this should return
  702. 24:02two that's right okay we're almost at
  703. 24:06the end
  704. 24:07we can replace characters within our
  705. 24:09string
  706. 24:10so we use the replace method
  707. 24:13and we pass in two arguments the
  708. 24:16character we would like to
  709. 24:18replace and the character we would like
  710. 24:20to replace our character with
  711. 24:21i would like to change all o's to a's
  712. 24:24and now
  713. 24:25my name is brock hod oh you guys are
  714. 24:28gonna like this next one
  715. 24:29this isn't technically a method but it's
  716. 24:31a really neat feature
  717. 24:33so one thing that we can do that's
  718. 24:34available to us with python is that we
  719. 24:36can display a string
  720. 24:38multiple times by multiplying that
  721. 24:40string by a given number
  722. 24:42so let's say i would like to display my
  723. 24:44name three times i can just take my
  724. 24:46string variable
  725. 24:47then add times three and this will print
  726. 24:49my name
  727. 24:50three times bro bro bro like i said not
  728. 24:53technically
  729. 24:54a method but it's a useful feature that
  730. 24:56you might be interested in that's
  731. 24:57related
  732. 24:58all right everybody so that is a few
  733. 25:01useful methods available to us for
  734. 25:02strings if you would like a copy of all
  735. 25:04this code
  736. 25:05i will post this in the comments section
  737. 25:07down below but yeah those are
  738. 25:09a few useful methods available to us for
  739. 25:12strings
  740. 25:13hey what's going on everybody it's you
  741. 25:14bro hope you're doing well and in this
  742. 25:16video i'm going to teach you guys all
  743. 25:18about typecasting in python so
  744. 25:19sit back relax and enjoy the show
  745. 25:24all right welcome back everybody i have
  746. 25:26a super quick video for you guys today
  747. 25:28on the subject of typecasting now
  748. 25:30typecasting is the ability to convert
  749. 25:32the data type of a value to another data
  750. 25:35type
  751. 25:35here's an example that i've written
  752. 25:36already i'm just going to paste it
  753. 25:38i have three variables for this example
  754. 25:41x y
  755. 25:42and z x contains one this is a whole
  756. 25:45integer
  757. 25:46into for short y contains two point zero
  758. 25:48this is a floating point number
  759. 25:50float four short you can tell it's a
  760. 25:52float because it ends with a decimal
  761. 25:53portion
  762. 25:54and z contains a string of three a
  763. 25:57string
  764. 25:58is a series of characters and you can
  765. 26:00tell it's a string because it's
  766. 26:01surrounded with a set of quotes
  767. 26:03and remember with strings we cannot
  768. 26:05normally perform math on strings
  769. 26:08so i have three print statements for x y
  770. 26:11and z and as you would expect they
  771. 26:13contain the values of
  772. 26:14one two point zero and three now let's
  773. 26:17begin by converting both y
  774. 26:18and z to the integer data type via
  775. 26:21typecasting in order to perform
  776. 26:23typecasting
  777. 26:24on a value or variable type that value
  778. 26:27or variable and surround this with a set
  779. 26:28of parentheses whoops i missed and then
  780. 26:32pre-save this
  781. 26:32with the data type you'd like to convert
  782. 26:34this value or variable to
  783. 26:36i would like to convert y to an integer
  784. 26:39and then you can use this for whatever
  785. 26:40you need
  786. 26:41so i'm going to replace y within my
  787. 26:43print statement
  788. 26:44with y after we typecast it as
  789. 26:47an integer and this will display 1 2
  790. 26:50not 2.0 it's the whole integer
  791. 26:54of 2 and then 3. so this is not a
  792. 26:57permanent change
  793. 26:58if you need to make this as a permanent
  794. 27:00change to y
  795. 27:02you'd have to reassign it so let's say y
  796. 27:05equals y and then we're going to
  797. 27:07typecast y
  798. 27:09so let's round this with a set of
  799. 27:10parentheses and precede this with the
  800. 27:12data type we'd like to convert
  801. 27:14y2 so now y is a whole
  802. 27:17integer and then we will print y without
  803. 27:20typecast this time and our result is
  804. 27:23one two three now z z is storing a
  805. 27:27string and with strings we cannot
  806. 27:29normally perform any math on strings
  807. 27:31for example within my print statement i
  808. 27:33will state z
  809. 27:35times three you would think that the
  810. 27:37result would be nine right
  811. 27:39well you're wrong it's going to print
  812. 27:41three three times
  813. 27:42if you need to convert a string to a int
  814. 27:46or a float you would just have to
  815. 27:47typecast it so this time let's type cast
  816. 27:50z as an integer z
  817. 27:53int and we will type cast z as an
  818. 27:56integer so then if we have z times three
  819. 28:00the result is now 9 instead of 333
  820. 28:04so that's the basic formula for type
  821. 28:06casting now let's convert these to
  822. 28:08floating point numbers but let's include
  823. 28:11x as well because
  824. 28:12x is feeling left out so let's convert x
  825. 28:14to
  826. 28:15a floating point number so float
  827. 28:17parenthesis and we will surround
  828. 28:19this with x and display x
  829. 28:22and actually let's convert y and z to
  830. 28:26floats as well because why not
  831. 28:29the results are now 1.0 2.0 and 9.0
  832. 28:33remember that we multiplied z times
  833. 28:35three
  834. 28:36and then if we need to convert these
  835. 28:38values to a string
  836. 28:41we would use the string cast
  837. 28:46so the results now are 1 2.0
  838. 28:49and 333 now you might be thinking to
  839. 28:52yourself
  840. 28:53when would you need to convert an
  841. 28:55integer or a float to a string
  842. 28:57here's a situation let's say within our
  843. 28:59print statements we need to print
  844. 29:02these variables along with another
  845. 29:04string so within our print statement
  846. 29:06let's say x is plus
  847. 29:09x we would like to display these
  848. 29:11together
  849. 29:12and let me copy this and do the same
  850. 29:14thing for y
  851. 29:15we'll say y is plus y
  852. 29:18you cannot normally display a string
  853. 29:21along with an integer
  854. 29:22or a float because we're using some
  855. 29:24string concatenation
  856. 29:26so this will result in a compilation
  857. 29:28error can only concatenate
  858. 29:30string not int to string so that's a
  859. 29:33situation
  860. 29:34in which you might need to use
  861. 29:36typecasting to convert an
  862. 29:38integer or float to a string so let's
  863. 29:40display
  864. 29:41x and y as strings via typecasting
  865. 29:45so we'll surround x and y with a set of
  866. 29:47parentheses and precede this with the
  867. 29:49data type we'd like to convert these
  868. 29:50values to
  869. 29:51we would like to convert x and y to both
  870. 29:53strings and now this will compile and
  871. 29:55run
  872. 29:56just fine well everybody that's the
  873. 29:58basics of typecasting
  874. 30:00if you would like a copy of all my code
  875. 30:02i will post everything in the comments
  876. 30:03down below
  877. 30:04if you found this video helpful don't be
  878. 30:06afraid to help me out by smashing that
  879. 30:08like button
  880. 30:09drop a comment down below and subscribe
  881. 30:11if you'd like to become a fellow bro
  882. 30:14hey what's going on everybody it's bro
  883. 30:16hope you're doing well
  884. 30:17and in this video i'm going to teach you
  885. 30:18guys how we can accept some user input
  886. 30:20in python so sit
  887. 30:22back relax and enjoy the show
  888. 30:26ladies and gentlemen this is where the
  889. 30:27fun begins in this video i'm going to
  890. 30:30explain how we can accept some user
  891. 30:31input in python
  892. 30:32and we do so by using the input function
  893. 30:35and then we type in our input to the
  894. 30:37console window
  895. 30:38but it would probably be good practice
  896. 30:40to let the user know what we want them
  897. 30:41to type in exactly
  898. 30:43so let's ask for somebody's name within
  899. 30:45quotes inside the input function i'm
  900. 30:47going to type
  901. 30:48what is your name and now
  902. 30:52our program will wait for us to enter in
  903. 30:54some user input
  904. 30:55so you can use your own name for this
  905. 30:57example i'm going to type bro
  906. 30:58now to submit some user input you just
  907. 31:00type or press
  908. 31:02enter now you can see that my process
  909. 31:04finished with exit code zero that means
  910. 31:06the program is done running
  911. 31:08so we should probably do something with
  912. 31:09this value we can actually assign it to
  913. 31:11a variable so we can use it elsewhere in
  914. 31:13our program
  915. 31:14so to do so we precede input with
  916. 31:17a variable like name works name equals
  917. 31:20input
  918. 31:20and when we accept some user input we
  919. 31:22will assign it to this variable that we
  920. 31:24called them
  921. 31:25and now we can do something with it so
  922. 31:27let's print a message
  923. 31:28involving our name variable that we have
  924. 31:31so how about
  925. 31:32hello plus name
  926. 31:35now when i run this program we can
  927. 31:37accept some user input
  928. 31:38and then do something with that input
  929. 31:40such as display message that says
  930. 31:42hello bro and then you can type in
  931. 31:44whatever you want for your name
  932. 31:45let's say i'm dude what is your name
  933. 31:48dude hello dude
  934. 31:49now this next part is very important
  935. 31:51when we accept user input it is always
  936. 31:54of the string
  937. 31:55data type a series of characters what if
  938. 31:58we need to accept a number
  939. 32:00well you can but you can't normally
  940. 32:01perform any math on it because it's of
  941. 32:04the string data type
  942. 32:05we would need to cast it as either of
  943. 32:07the integer data type
  944. 32:09or of the float data type so here's an
  945. 32:11example of why we can't perform any math
  946. 32:14on a string let's say we have a variable
  947. 32:16called
  948. 32:17edge age equals input and let's
  949. 32:20ask how old somebody is how old
  950. 32:23are you now i will add one to somebody's
  951. 32:28age let's pretend it's their birthday
  952. 32:30age equals age plus one this will add
  953. 32:32one to their age
  954. 32:33now let's try and run this what is your
  955. 32:35name bro
  956. 32:37how old are you let's say that i'm 21.
  957. 32:40okay we ran into an error a type error
  958. 32:42can only concatenate string to string
  959. 32:46with what i mentioned previously we
  960. 32:47cannot normally use
  961. 32:49strings for any sort of mathematical
  962. 32:51equation or operation
  963. 32:53we'll want to cast it to the integer or
  964. 32:55the float
  965. 32:56data type so let's cast our user input
  966. 32:59to the integer data type and we will
  967. 33:01surround our user input with a cast
  968. 33:04surround your input with a set of
  969. 33:06parenthesis and precede this with
  970. 33:08int and now we can use this user input
  971. 33:11in a mathematical operation
  972. 33:13so let's try it even though this won't
  973. 33:15display age yet so
  974. 33:16what is your name bro 21 and our program
  975. 33:20finished with
  976. 33:20an x a code of zero there were no
  977. 33:22problems now let's attempt to display
  978. 33:24this edge
  979. 33:25so i will print
  980. 33:28u r plus
  981. 33:31age plus years
  982. 33:35old we're doing some string
  983. 33:36concatenation here
  984. 33:38what is your name bro how old are you 21
  985. 33:42all right we ran into a type error again
  986. 33:44can only concatenate string to string
  987. 33:47you cannot normally display a variable
  988. 33:50of the integer data type
  989. 33:52along with strings because we're doing
  990. 33:54string concatenation we're adding
  991. 33:55strings together
  992. 33:57we would need to convert this variable
  993. 33:59back to the string
  994. 34:00data type so we can cast it we'll
  995. 34:02surround
  996. 34:03our edge variable with a cast now this
  997. 34:05is what's going to happen we will accept
  998. 34:07some user input it will be of the string
  999. 34:09data type and then we will
  1000. 34:10immediately cast it to be of the integer
  1001. 34:13data type so that we can treat it
  1002. 34:15as a number instead of a character and
  1003. 34:17then if we need to display this
  1004. 34:19edge we will need to convert it back to
  1005. 34:21a string so that we can use
  1006. 34:23some string concatenation and display
  1007. 34:25all of these strings together
  1008. 34:27and now let's try this theory so what is
  1009. 34:29your name bro
  1010. 34:30how old are you let's say i'm 21 hello
  1011. 34:33bro you
  1012. 34:34are 22 years old here's a situation what
  1013. 34:36if we enter
  1014. 34:37a number that contains a decimal portion
  1015. 34:39so let's try this again
  1016. 34:41let's say i'm 21.5 years old
  1017. 34:44well now we ran into a different issue a
  1018. 34:47value error
  1019. 34:48invalid literal for int with base 10
  1020. 34:51that means if we cast a string as an
  1021. 34:54integer
  1022. 34:54it can only be a whole number a portion
  1023. 34:57that does not contain
  1024. 34:58a decimal and that is where the float
  1025. 35:01data type would come in
  1026. 35:02it is a data type that can contain a
  1027. 35:05decimal portion
  1028. 35:06for this example let's create a third
  1029. 35:09variable called height and we will ask
  1030. 35:11for somebody's height
  1031. 35:12input we'll create a prompt that says
  1032. 35:16how tall are you
  1033. 35:21and we will cast this to be of the float
  1034. 35:24data type
  1035. 35:25so that our string our user input
  1036. 35:28can contain a decimal portion and then
  1037. 35:31we can treat it as a number instead of a
  1038. 35:33series of characters
  1039. 35:34and i think i will delete this line we
  1040. 35:36won't really need it anymore
  1041. 35:38and then let's print their height along
  1042. 35:40with a message
  1043. 35:42u r plus then
  1044. 35:45height but we need to cast this back to
  1045. 35:47being a string
  1046. 35:50so we'll surround our height with a cast
  1047. 35:54plus let's say cm
  1048. 35:57tall all right let's test this
  1049. 36:00what is your name bro how old are you
  1050. 36:03let's say i'm 21 how tall are you let's
  1051. 36:06pretend that i'm 250.5 centimeters tall
  1052. 36:12i'm adding the 0.5 just to test to see
  1053. 36:14if this will accept a floating point
  1054. 36:15number
  1055. 36:17hello bro you are 21 years old you are
  1056. 36:21250.5 centimeters tall
  1057. 36:23not really all right everybody so that
  1058. 36:25is the basics of
  1059. 36:26user input in python normally with user
  1060. 36:29input it will give you
  1061. 36:30a value of the string data type if you
  1062. 36:33need to use that value for any sort of
  1063. 36:35math
  1064. 36:36you'll probably need to cast it to be of
  1065. 36:38the integer or the float data
  1066. 36:40type so if you would like a copy of this
  1067. 36:42code i will post this in the comment
  1068. 36:44section down below
  1069. 36:45but yeah that's how to accept user input
  1070. 36:47in python
  1071. 36:49hey what's going on everybody it's bro
  1072. 36:52hope you're doing well and in this video
  1073. 36:53i'm going to demonstrate a few useful
  1074. 36:55functions related to numbers in python
  1075. 36:57so
  1076. 36:58sit back relax and enjoy the show
  1077. 37:03okay let's begin in this video i'm going
  1078. 37:05to be demonstrating a bunch of useful
  1079. 37:07functions related to numbers
  1080. 37:09and a bunch of these functions are
  1081. 37:11located within the math
  1082. 37:13module now let's create a variable for
  1083. 37:15this example let's say
  1084. 37:17pi equals 3.14 just the first few digits
  1085. 37:21of pi is fine for this example
  1086. 37:23and now here's a bunch of useful
  1087. 37:25functions that you guys would probably
  1088. 37:26be interested in
  1089. 37:28the first is round round is a built-in
  1090. 37:30function and all we do is type round
  1091. 37:32then add a set of parenthesis and then
  1092. 37:35we can pass in
  1093. 37:36a value or a variable as an argument and
  1094. 37:39the round function
  1095. 37:40will round that number for us so pi
  1096. 37:43rounded to the nearest whole integer
  1097. 37:45is you guessed it three now we can round
  1098. 37:48a number up
  1099. 37:49by using the seal function of the math
  1100. 37:51class seal
  1101. 37:52short for ceiling so let's print
  1102. 37:55our result and in order to access this
  1103. 37:58function within the math module
  1104. 38:00we would type the name of the module dot
  1105. 38:02and here's a bunch of functions that we
  1106. 38:04have access to
  1107. 38:05so we are looking for seal and it's
  1108. 38:07actually at the top here for me
  1109. 38:08and at seal we'll round a number up to
  1110. 38:11the nearest whole integer
  1111. 38:12so pi rounded up is four
  1112. 38:16then on the flip side we can round a
  1113. 38:18number down by using
  1114. 38:19floor and that is also located within
  1115. 38:21the math module
  1116. 38:22so let's type math dot floor and
  1117. 38:25pi rounded down would be three
  1118. 38:29up next i have the absolute value
  1119. 38:32function but it's abs
  1120. 38:33for short abs will give you the absolute
  1121. 38:36value of a number
  1122. 38:38so the absolute value of pi actually
  1123. 38:40let's make this negative 3.14
  1124. 38:42the absolute value will tell you how far
  1125. 38:45a number
  1126. 38:46is away from zero so if you pass in a
  1127. 38:48negative number
  1128. 38:49it will give you a positive number so
  1129. 38:52that is
  1130. 38:52abs short for absolute value next we
  1131. 38:55have
  1132. 38:56the pow function pow will raise
  1133. 38:59a base number to a power let's raise
  1134. 39:03pi to the power of two so we pass in
  1135. 39:06two arguments a base and an exponent
  1136. 39:09pi to the power of two is nine point
  1137. 39:11eight
  1138. 39:12and some change so that is the pow
  1139. 39:15function
  1140. 39:16next we have square root
  1141. 39:20so this is located within the math
  1142. 39:22module we'll type
  1143. 39:24math dot s q r
  1144. 39:27t short for square root and the square
  1145. 39:29root of pi
  1146. 39:31is let's find out oh i guess we can't
  1147. 39:34pass in pi
  1148. 39:35uh let's round a different number like
  1149. 39:38420
  1150. 39:39oh i guess that's because i made pi
  1151. 39:41negative okay so the square root of 420
  1152. 39:43is
  1153. 39:4420.4 and some change so that is the
  1154. 39:47square
  1155. 39:47root function all right we have two left
  1156. 39:50we have the max function
  1157. 39:52the max function will find the largest
  1158. 39:54of a varying amount
  1159. 39:56of values so let's create three
  1160. 39:58variables for this example
  1161. 40:00let's say x equals one y equals two
  1162. 40:03and z equals three we can use the max
  1163. 40:07function to find
  1164. 40:08the largest of these values so within a
  1165. 40:10print statement
  1166. 40:11let's use the max function and pass in
  1167. 40:15all of the different values or variables
  1168. 40:17that you would like to compare
  1169. 40:18so i would like to find the largest
  1170. 40:21value between
  1171. 40:22x y and z and the result in this example
  1172. 40:26is three now the min function will find
  1173. 40:29the lowest
  1174. 40:30so min and then pass in your values or
  1175. 40:34variables
  1176. 40:35so i would like to find the minimum
  1177. 40:36value between x
  1178. 40:38y and z and in this case it is one
  1179. 40:42well everybody those are a few useful
  1180. 40:45functions that you guys might be
  1181. 40:46interested in
  1182. 40:47if you guys would like a copy of this
  1183. 40:49code i will post this in the comments
  1184. 40:50section down below
  1185. 40:52but yeah those are a few useful
  1186. 40:54functions related to numbers
  1187. 40:56in python hey what's going on everybody
  1188. 41:00it's bro hope you're doing well and in
  1189. 41:02this video i'm going to explain string
  1190. 41:04slicing in python so sit back
  1191. 41:06relax and enjoy the show
  1192. 41:10all right then let's talk about slicing
  1193. 41:12slicing can be used to create a
  1194. 41:14substring by extracting elements from
  1195. 41:16another string
  1196. 41:17the same principle can also be applied
  1197. 41:19to different collections as well
  1198. 41:21but in this video we're going to be
  1199. 41:22focusing on slicing strings
  1200. 41:24now to slice a string we can either use
  1201. 41:27the indexing operator
  1202. 41:29which is a set of square brackets or by
  1203. 41:31use of the slice function
  1204. 41:33to create a slice object this is a
  1205. 41:35little more complex
  1206. 41:36we'll discuss the slice function near
  1207. 41:38the end of this video
  1208. 41:39so with slicing there are three optional
  1209. 41:43arguments
  1210. 41:43three fields that we can fill in
  1211. 41:45depending on where
  1212. 41:46and how we want to slice our string a
  1213. 41:49starting index
  1214. 41:50a stopping index and a step let's begin
  1215. 41:53with the starting index
  1216. 41:54now for this example let's say we have a
  1217. 41:57string
  1218. 41:58variable called name and this will store
  1219. 42:00a value of whatever your
  1220. 42:02own full name is yes i legally change my
  1221. 42:04name to bro code you can use your own
  1222. 42:06name for this if you want
  1223. 42:07so what i would like to do is to create
  1224. 42:09a substring
  1225. 42:10based off a sliced portion of my full
  1226. 42:13name
  1227. 42:14so what i would like to do is create a
  1228. 42:16substring based off of just the first
  1229. 42:18part of my name just bro
  1230. 42:20so let's begin by creating a substring
  1231. 42:23maybe i'll call this first
  1232. 42:26name equals now to slice a string
  1233. 42:30type in a string or the name of a
  1234. 42:33variable that contains your string
  1235. 42:34and follow this with the indexing
  1236. 42:36operator and like i said we'll discuss
  1237. 42:38the slice function a little bit later so
  1238. 42:41we need
  1239. 42:42a starting index and a stopping index
  1240. 42:45but we'll begin with the starting index
  1241. 42:47where do we want to begin
  1242. 42:49our slice so computers they always start
  1243. 42:51with zero
  1244. 42:52so the first character in your string
  1245. 42:54will have an index
  1246. 42:56of zero i would like my slice to begin
  1247. 42:58at index 0
  1248. 42:59so i'll list that for the starting
  1249. 43:01position the starting index
  1250. 43:03so let's test this just to be sure it
  1251. 43:05works i'll just print
  1252. 43:07first name and let's see if this prints
  1253. 43:10capital b
  1254. 43:11which it does so following that same
  1255. 43:13pattern the next character
  1256. 43:15within my string would have an index of
  1257. 43:18one that would be
  1258. 43:19r then the next index two would be
  1259. 43:22o and three should be an empty space
  1260. 43:26all right so that is the starting index
  1261. 43:29within string slicing if we would like
  1262. 43:31to slice an entire portion of our string
  1263. 43:34not just one letter we need a stopping
  1264. 43:37index as well
  1265. 43:38and both of these fields are separated
  1266. 43:40with a colon
  1267. 43:41so let's say i would like to slice the
  1268. 43:43first three characters of my string
  1269. 43:46so this would have an index of zero one
  1270. 43:50two and this isn't going to work as you
  1271. 43:52would imagine and i'll explain
  1272. 43:54why so you would think that indexes
  1273. 43:570 through 2 would give us three letters
  1274. 43:59right but it only gives us two
  1275. 44:01so this first index is inclusive
  1276. 44:05the stopping index is exclusive so if i
  1277. 44:08want the first
  1278. 44:09three characters the first three letters
  1279. 44:12in the string
  1280. 44:12i would need the starting index to be
  1281. 44:15zero
  1282. 44:16and the stopping index to be three and
  1283. 44:18now this should print
  1284. 44:19my first name and not anything else
  1285. 44:22there is a shorthand way of writing this
  1286. 44:24too
  1287. 44:24so if you were to leave this first index
  1288. 44:27blank
  1289. 44:28for start well python is going to assume
  1290. 44:30that this is zero
  1291. 44:31the beginning of your string so this
  1292. 44:34would be
  1293. 44:35no different than what we had previously
  1294. 44:37just having colon
  1295. 44:38three now let's try and create a
  1296. 44:41separate substring
  1297. 44:42for a last name so last
  1298. 44:45name name now we need a
  1299. 44:49starting index and a stopping index so
  1300. 44:52we need the starting index at least so
  1301. 44:54this would be
  1302. 44:56zero one two three four
  1303. 44:59four colon and where do we want this to
  1304. 45:01stop
  1305. 45:02zero one two three four five
  1306. 45:05six seven and remember that this is
  1307. 45:08exclusive
  1308. 45:09so we're going to place eight here and
  1309. 45:11now i would like to display
  1310. 45:13last name and this should just display
  1311. 45:16code
  1312. 45:16and a shortcut is that if you were to
  1313. 45:19leave
  1314. 45:20stop blank but you still have that colon
  1315. 45:22this is a shorthand way of writing
  1316. 45:24i would like every character starting at
  1317. 45:27index four
  1318. 45:28and everything after until the end so
  1319. 45:30there would be no change to this
  1320. 45:32as well you can write it either way okay
  1321. 45:34so we still have to discuss
  1322. 45:36step step is an optional field that we
  1323. 45:38can set a value to
  1324. 45:40so step is how much we're increasing our
  1325. 45:42index by
  1326. 45:43between starting and stopping so it is
  1327. 45:46entirely possible
  1328. 45:48to create a substring that will count
  1329. 45:50only
  1330. 45:51every second character after the first
  1331. 45:53normally step is one by default
  1332. 45:55but if we were to set that to two we're
  1333. 45:57going to count only
  1334. 45:58every second character so let's try this
  1335. 46:01maybe i'll create a new
  1336. 46:02variable called funky name because i
  1337. 46:05really don't know what else to call this
  1338. 46:06for this example
  1339. 46:07let's say funky name equals name and i'm
  1340. 46:10going to count
  1341. 46:11only every second character including
  1342. 46:14the first
  1343. 46:14so the indexes would be zero colon
  1344. 46:18eight for my example but if you're using
  1345. 46:20your own name that's probably going to
  1346. 46:21be something different
  1347. 46:23so the default first step is one and if
  1348. 46:26i were to display funky name as it
  1349. 46:28is currently this will just display my
  1350. 46:30full name
  1351. 46:31but if we set step to two this will only
  1352. 46:33display
  1353. 46:34every second character including the
  1354. 46:36first so funky name is now
  1355. 46:38for me at least b o c d now if step
  1356. 46:42was three this will print only every
  1357. 46:44third character including the first
  1358. 46:46where it's b space d a shorthand way of
  1359. 46:50writing this
  1360. 46:50is that you could leave start and stop
  1361. 46:53empty but the important thing is to have
  1362. 46:55these two colons
  1363. 46:57python is going to assume that if you
  1364. 46:59leave your starting index
  1365. 47:00empty you're referring to index 0 the
  1366. 47:03beginning
  1367. 47:04if your second index your stopping index
  1368. 47:06is empty
  1369. 47:07python will assume this will be the very
  1370. 47:10end of your string
  1371. 47:11so just having two colons and then step
  1372. 47:13three would be
  1373. 47:14no different than what we had previously
  1374. 47:16or this next part i'm going to explain
  1375. 47:19how we can reverse a string in python so
  1376. 47:22let's create a new substring variable
  1377. 47:24called
  1378. 47:24reversed name equals name i would like
  1379. 47:27to use the entire string
  1380. 47:29so i can just use two colons python will
  1381. 47:32fill in the rest
  1382. 47:33so for the step this is going to be
  1383. 47:35negative one
  1384. 47:36it's kind of like we're counting
  1385. 47:38backwards and we're going to create
  1386. 47:40a new sub string based off my name but
  1387. 47:43in reverse
  1388. 47:44so if i were to print my reversed name
  1389. 47:47this is now
  1390. 47:48edok orb i think that's how you
  1391. 47:50pronounce it
  1392. 47:51so that's how to reverse a string in
  1393. 47:53python using slicing
  1394. 47:55now let's move on to part two of this
  1395. 47:57video i'm going to be explaining the
  1396. 47:59slice function and we can use the slice
  1397. 48:01function to create a slice
  1398. 48:03object which is actually reusable so
  1399. 48:05that's somewhat convenient
  1400. 48:07let's take a different scenario this
  1401. 48:08time let's say we have a website url
  1402. 48:11website equals http colon2forwardslashes
  1403. 48:16google.com but use whatever website you
  1404. 48:18want what i would like to do for this
  1405. 48:20program
  1406. 48:20is remove and create a substring based
  1407. 48:23off the website name
  1408. 48:24and not anything else i would like to
  1409. 48:27exclude the http portion as well as the
  1410. 48:30dot-com portion
  1411. 48:31of this website url so let's break it
  1412. 48:34down step by step
  1413. 48:35our first step is to create a slice
  1414. 48:37object so i will name the slice object
  1415. 48:40as slice
  1416. 48:40slice equals and i will invoke the slice
  1417. 48:43function
  1418. 48:44so within the parentheses of the slice
  1419. 48:46function we can add up to
  1420. 48:48three values a start a stop and a step
  1421. 48:52exactly the same as what we did with
  1422. 48:53indexing however with the slice function
  1423. 48:56we separate each of these values with a
  1424. 48:59comma
  1425. 48:59instead of a colon but it works the same
  1426. 49:01way so we would like
  1427. 49:03our substring to begin where the name of
  1428. 49:05the website begins
  1429. 49:06so we would like our start to be zero
  1430. 49:10one two three four five six seven
  1431. 49:13so i will pass in seven for where my
  1432. 49:15substring will begin
  1433. 49:17and now each value within the slice
  1434. 49:19function will be separated with
  1435. 49:21a comma instead of a colon like what we
  1436. 49:23did with the indexing operator
  1437. 49:25now for the stop index that's going to
  1438. 49:28be a little bit tricky
  1439. 49:29and let me explain why one issue that
  1440. 49:31we're going to run into
  1441. 49:32is that not all website names are
  1442. 49:34consistent with their length they can
  1443. 49:36really vary
  1444. 49:37so it is not possible for us to use the
  1445. 49:40same stopping index
  1446. 49:41for each of these website urls so one
  1447. 49:44way in which we can account for that
  1448. 49:46is to use what is called a negative
  1449. 49:48index each character within a string
  1450. 49:51has a positive index as well as a
  1451. 49:53negative index
  1452. 49:54and a negative index works almost
  1453. 49:57exactly the same way
  1454. 49:58except the character most on the right
  1455. 50:01begins with a negative index
  1456. 50:03of minus one and the character on the
  1457. 50:06left
  1458. 50:06of that would be negative two negative
  1459. 50:09three and negative four
  1460. 50:10it's as if you're counting backwards so
  1461. 50:12with indexing we can use some
  1462. 50:14combination of
  1463. 50:15positive and negative indexing so i
  1464. 50:18would like
  1465. 50:19my substring to end where this dot is so
  1466. 50:22this would have
  1467. 50:23a index of minus one minus two
  1468. 50:26minus three minus four and remember that
  1469. 50:28this is exclusive
  1470. 50:30so i would like my slice to begin at
  1471. 50:32index seven
  1472. 50:34and a negative index of four for the
  1473. 50:37stopping position
  1474. 50:38so that will give me just the website
  1475. 50:40name and remove
  1476. 50:41the http portion as well as the dot com
  1477. 50:44portion of my website url
  1478. 50:47and now we have a slice object and we
  1479. 50:49can reuse this
  1480. 50:50so to apply your slice object type in
  1481. 50:53the name your string
  1482. 50:54index operator and place your slice
  1483. 50:57right within here
  1484. 50:58and let's print this so print
  1485. 51:01our website and apply the slice to it
  1486. 51:03and now this should give us
  1487. 51:05just the website name and now let's test
  1488. 51:07this with maybe another website
  1489. 51:09i'll rename this as website one and
  1490. 51:11let's create website2
  1491. 51:14and let's try a different website name
  1492. 51:17for goodmeasure
  1493. 51:18so let's try wikipedia so we can reuse
  1494. 51:22our slice object
  1495. 51:23and we will apply our slice to website
  1496. 51:26two this time
  1497. 51:27and this should remove the url portions
  1498. 51:30http as well as
  1499. 51:32com all right everybody so that is
  1500. 51:34slicing strings
  1501. 51:36in python you can either use the
  1502. 51:38indexing operator
  1503. 51:39or the slice function to create a slice
  1504. 51:42object
  1505. 51:43if you would like a copy of all this
  1506. 51:44code i will post all of this in the
  1507. 51:46comments section down below
  1508. 51:48but yeah that is how string slicing
  1509. 51:50works in python
  1510. 51:52hey what's going on people it's you bro
  1511. 51:54hope you're doing well and in this video
  1512. 51:56i'm going to explain
  1513. 51:57if statements else if statements and
  1514. 51:59else statements and python so
  1515. 52:01sit back relax and enjoy the show
  1516. 52:06all right people let's talk about if
  1517. 52:08statements an if statement is a block of
  1518. 52:10code
  1519. 52:11that will execute only if its condition
  1520. 52:13is true
  1521. 52:14it's a very basic form of decision
  1522. 52:16making with programming
  1523. 52:18for this example let's create a prompt
  1524. 52:21where we will ask a user
  1525. 52:22for their age and depending on what
  1526. 52:24their age is we will print a response
  1527. 52:26so let's say age equals input and then
  1528. 52:29we will add a prompt
  1529. 52:30how old are you now this will return a
  1530. 52:33string
  1531. 52:33so we should probably cast this as an
  1532. 52:36integer data type
  1533. 52:37so we now have a variable called age
  1534. 52:39where we will accept some user input
  1535. 52:42let's check somebody's age with a
  1536. 52:43variety of if statements
  1537. 52:45so let's check to see if somebody's age
  1538. 52:47is greater than or equal to 18
  1539. 52:49so to create an if statement type if and
  1540. 52:51then some sort of condition
  1541. 52:53if age is greater than or equal to 18
  1542. 52:56to finish this if statement add a colon
  1543. 52:58at the end
  1544. 52:59now pay attention to this if i move down
  1545. 53:01to the next line you'll notice that my
  1546. 53:03cursor is now
  1547. 53:04indented any indented code underneath an
  1548. 53:07if statement is the block of code for
  1549. 53:10that if statement
  1550. 53:11so if this condition is true we will
  1551. 53:13execute some
  1552. 53:14block of code if it's false we'll skip
  1553. 53:17over it so if somebody's age is greater
  1554. 53:19than or equal to 18
  1555. 53:20let's print you are an
  1556. 53:24adult and let's try this program how old
  1557. 53:27are you
  1558. 53:28let's say i'm 21 so my condition when we
  1559. 53:31get to the if statement
  1560. 53:32is going to check to see if this
  1561. 53:33condition is true if it is true we're
  1562. 53:35going to
  1563. 53:36execute this block of code if it's not
  1564. 53:38we skip over it
  1565. 53:39so how old are you let's say i'm 21 i'm
  1566. 53:42going to hit enter
  1567. 53:43and my if statement is true so we
  1568. 53:45execute this block of code which prints
  1569. 53:48you are an adult so what if this
  1570. 53:49condition is false
  1571. 53:51let's say i'm 12. well then we're just
  1572. 53:53going to skip over this block of code
  1573. 53:54and continue on with the rest of the
  1574. 53:56program
  1575. 53:56if you would like your program to take
  1576. 53:58some other course of action
  1577. 54:00if this condition is false we can add an
  1578. 54:02else
  1579. 54:03statement so after the if statement add
  1580. 54:06else
  1581. 54:06make sure you get the indentation right
  1582. 54:08you don't want this within the block of
  1583. 54:10code for the if statement
  1584. 54:11so if this condition is false we can
  1585. 54:14take
  1586. 54:15some other course of action so if
  1587. 54:17somebody's age
  1588. 54:18is less than 18 let's print
  1589. 54:22you are a child
  1590. 54:26so let's try this again i'm going to
  1591. 54:28enter that i
  1592. 54:29am 12 years old not really but let's
  1593. 54:31just pretend i am
  1594. 54:33so this will now print you are a child
  1595. 54:35if
  1596. 54:36this condition is false perform this
  1597. 54:38block of code for the else statement
  1598. 54:40so with if statements we can check more
  1599. 54:43than one condition before reaching the
  1600. 54:45else statement by using an
  1601. 54:46else if statement and we add that after
  1602. 54:50an if statement so it's shortened to e l
  1603. 54:53i f for else if so let's check to see if
  1604. 54:56somebody's age
  1605. 54:57is less than zero for some reason
  1606. 55:00so let's print
  1607. 55:03you haven't been born yet
  1608. 55:08so with if statements we start at the
  1609. 55:11top we first check
  1610. 55:12the if statement if this is false we
  1611. 55:14move down to the next
  1612. 55:15statement to check then we check our
  1613. 55:18else if statement
  1614. 55:19if this is also false then we resort to
  1615. 55:21the else statement
  1616. 55:23the else statement is sort of like a
  1617. 55:25last resort if all conditions
  1618. 55:27evaluate to be false then we execute the
  1619. 55:30else statement
  1620. 55:31so let's try this how old are you let's
  1621. 55:33say i'm negative
  1622. 55:341 years old you haven't been born yet
  1623. 55:38so with if statements we always begin
  1624. 55:40with an if statement and we check
  1625. 55:42that condition if that condition
  1626. 55:44evaluates to be false
  1627. 55:46then we will skip this block of code and
  1628. 55:48move down to the else if statement
  1629. 55:50if there is one it's optional if this
  1630. 55:53condition
  1631. 55:54is also false then we will use our else
  1632. 55:56statement as a last resort
  1633. 55:58if there is one if there is no else
  1634. 56:00statement then we just skip over all of
  1635. 56:02this then and continue on with the rest
  1636. 56:04of the program
  1637. 56:05so you can add more than one else if
  1638. 56:07statement so let's check something else
  1639. 56:10let's check to see else if age
  1640. 56:14is equal to 100 so if you need to check
  1641. 56:17to see
  1642. 56:18if a value is equal to a particular
  1643. 56:21value
  1644. 56:21make sure you use double equal signs
  1645. 56:24this is the comparison
  1646. 56:25operator for equality if you use just
  1647. 56:29one equal sign that's the assignment
  1648. 56:31operator and
  1649. 56:32python thinks you're attempting to set
  1650. 56:34age equal to 100
  1651. 56:35so if you want to check to see if age is
  1652. 56:38equal to 100
  1653. 56:39then use double equals so let's check to
  1654. 56:42see if somebody's age is
  1655. 56:44equal to 100 so let's print
  1656. 56:48you are a century
  1657. 56:52old now pay attention to this if i were
  1658. 56:54to run this program
  1659. 56:56how old are you let's say i'm 100. so
  1660. 56:59this is printing you are an adult that's
  1661. 57:02because we first check
  1662. 57:03our if statement and well we're 100
  1663. 57:06years old and 100
  1664. 57:07is greater than or equal to 18. so we
  1665. 57:10print
  1666. 57:10this block of code we execute this block
  1667. 57:12of code and then we skip
  1668. 57:14everything else even though age is equal
  1669. 57:17to 100 within
  1670. 57:19our else if statement so the order of
  1671. 57:22your if statements
  1672. 57:23does matter so let's change this around
  1673. 57:25let's add this
  1674. 57:26to the very beginning and this will be
  1675. 57:30our
  1676. 57:30if statement and we will set age
  1677. 57:33is greater than or equal to 18 to be an
  1678. 57:35else if statement
  1679. 57:36so we're first going to check to see if
  1680. 57:38age is equal to 100
  1681. 57:40if not we will check to see if age is
  1682. 57:43greater than or equal to 18
  1683. 57:45and we just follow that order so let's
  1684. 57:47try this again how old are you
  1685. 57:48i am 100 years old you are
  1686. 57:52a century old so we first check our if
  1687. 57:54statement
  1688. 57:55if this condition is true we will
  1689. 57:57execute this block of code
  1690. 57:59if it's false we just go down the order
  1691. 58:01till we reach our else statement
  1692. 58:03well everybody that is the basics of if
  1693. 58:06statements we have if statements
  1694. 58:08else if statements and else statements
  1695. 58:10so if you would like a copy of this code
  1696. 58:12i will post this in the comments down
  1697. 58:14below
  1698. 58:15but yeah that is how if statements work
  1699. 58:17in python
  1700. 58:19hey what's going on everybody it's bro
  1701. 58:21hope you're doing well in this video i'm
  1702. 58:23going to explain
  1703. 58:24logical operators in python so sit back
  1704. 58:26relax
  1705. 58:27and enjoy the show
  1706. 58:30all right everybody let's talk about
  1707. 58:32logical operators
  1708. 58:33these are used to check if two or more
  1709. 58:36conditional statements
  1710. 58:37are true so we'll be discussing the and
  1711. 58:40as well as the or
  1712. 58:41logical operators there's also a third
  1713. 58:44one called
  1714. 58:44not but that works a little bit
  1715. 58:46different and i'll explain why
  1716. 58:48so let's create a program to demonstrate
  1717. 58:50this let's create a program
  1718. 58:51where we will ask somebody for the
  1719. 58:53temperature outside and then depending
  1720. 58:55on the temperature if it falls within a
  1721. 58:56certain range
  1722. 58:57we will print a message so we'll need to
  1723. 58:59use and as well as or logical operators
  1724. 59:01to do this
  1725. 59:02so here i have a prompt temp for
  1726. 59:04temperature equals input what is the
  1727. 59:06temperature outside
  1728. 59:08so i'm going to cast this as an integer
  1729. 59:10data type
  1730. 59:11so what if we want to check to see if
  1731. 59:13our temperature is within a certain
  1732. 59:15range
  1733. 59:16we can use the and logical operator to
  1734. 59:19check
  1735. 59:19two or more conditional statements this
  1736. 59:21is how we might do that
  1737. 59:23let's check if temp
  1738. 59:26is greater than or equal to zero zero
  1739. 59:28degrees celsius
  1740. 59:30and we can check another condition too
  1741. 59:32if temp
  1742. 59:34is less than or equal to 30 degrees
  1743. 59:36celsius
  1744. 59:38so we need to add a colon at the end to
  1745. 59:40finish this if statement
  1746. 59:42and let me just fix some of the spacing
  1747. 59:44okay so
  1748. 59:46with the and logical operator in order
  1749. 59:48for this entire condition to be true
  1750. 59:51both conditions must be true this needs
  1751. 59:53to be true
  1752. 59:54and this needs to be true so if our
  1753. 59:57temperature
  1754. 59:57is equal to or above 0 as well
  1755. 1:00:01as being less than or equal to 30 that
  1756. 1:00:04means our temperature
  1757. 1:00:05falls within a certain range so let's
  1758. 1:00:07print a message
  1759. 1:00:08such as the temperature
  1760. 1:00:12is good today
  1761. 1:00:16print go
  1762. 1:00:19outside all right let's try it
  1763. 1:00:23so what is the temperature outside let's
  1764. 1:00:25say it's 20 degrees celsius
  1765. 1:00:27well the temperature is good today go
  1766. 1:00:30outside
  1767. 1:00:30this condition is true 20 is greater
  1768. 1:00:33than or equal to zero
  1769. 1:00:34and temperature is less than or equal to
  1770. 1:00:3730.
  1771. 1:00:38let's try a different temperature like
  1772. 1:00:40negative 20 degrees celsius so it's
  1773. 1:00:42really freaking cold outside right now
  1774. 1:00:44as you can see nothing happened because
  1775. 1:00:46our first condition
  1776. 1:00:48was false our temperature is not greater
  1777. 1:00:50than or equal to zero
  1778. 1:00:52but our temperature is less than or
  1779. 1:00:54equal to 30 so
  1780. 1:00:56our second condition is true but our
  1781. 1:00:58first condition was false
  1782. 1:00:59and in order for this entire statement
  1783. 1:01:01to be true since we're using the and
  1784. 1:01:03logical operator
  1785. 1:01:04both conditions must be true in order
  1786. 1:01:06for this statement
  1787. 1:01:07to be true now let's discuss the or
  1788. 1:01:10logical operator
  1789. 1:01:11we can check to see if our temperature
  1790. 1:01:13is below
  1791. 1:01:14or above a certain range so let's write
  1792. 1:01:16else if
  1793. 1:01:17temp is less than zero or
  1794. 1:01:21temp is greater than 30. so that means
  1795. 1:01:25it's going to be either
  1796. 1:01:26very cold outside or very hot outside so
  1797. 1:01:29let's print a message
  1798. 1:01:31the temperature
  1799. 1:01:35is bad today
  1800. 1:01:39print stay
  1801. 1:01:44inside all right let's try this again
  1802. 1:01:49what is the temperature outside it is
  1803. 1:01:50negative 20 degrees outside
  1804. 1:01:53the temperature is bad today stay inside
  1805. 1:01:55so with the
  1806. 1:01:56orological operator as long as one of
  1807. 1:01:59these conditions
  1808. 1:02:00is true then the entire statement is
  1809. 1:02:02true it doesn't matter if one of them is
  1810. 1:02:04false as long as one of them is true
  1811. 1:02:05then the entire statement
  1812. 1:02:07is true last but not least we have the
  1813. 1:02:09not logical operator this works a little
  1814. 1:02:11bit different from the
  1815. 1:02:12and as well as the or logical operator
  1816. 1:02:14because we can check
  1817. 1:02:16one or more conditional statements
  1818. 1:02:18instead of two or more conditional
  1819. 1:02:20statements
  1820. 1:02:21so what the not logical operator will do
  1821. 1:02:23is that it'll take
  1822. 1:02:24a conditional statement if it's true
  1823. 1:02:26it's going to flip it to false
  1824. 1:02:28if it's normally false it's going to
  1825. 1:02:29flip it to true so for this example we
  1826. 1:02:31have another way in which we could write
  1827. 1:02:33this
  1828. 1:02:34although it's a little more complex so
  1829. 1:02:36if our statement
  1830. 1:02:37is true what we can do is that we can
  1831. 1:02:39flip it to false using the
  1832. 1:02:41not logical operator if it's normally
  1833. 1:02:43false we can change it to true
  1834. 1:02:45so what we'll do is surround one or more
  1835. 1:02:47conditional statements with a set of
  1836. 1:02:48parentheses
  1837. 1:02:49and precede everything within the
  1838. 1:02:51parentheses with the not
  1839. 1:02:52logical operator so if the statement is
  1840. 1:02:55normally true
  1841. 1:02:56it's going to become false using the not
  1842. 1:02:58logical operator
  1843. 1:02:59if it's normally false it's going to be
  1844. 1:03:01true then let's do the same with the
  1845. 1:03:03else if statement
  1846. 1:03:04so surround one or more conditional
  1847. 1:03:06statements with the not
  1848. 1:03:08logical operator so now these roles are
  1849. 1:03:10going to be kind of reversed so let's
  1850. 1:03:12try this again what is the temperature
  1851. 1:03:13outside it is negative 20 degrees
  1852. 1:03:15outside
  1853. 1:03:16the temperature is good today go outside
  1854. 1:03:18so what we should probably do
  1855. 1:03:19is kind of reverse these rolls so i'm
  1856. 1:03:22just going to swap
  1857. 1:03:23everything within these if statements
  1858. 1:03:25with each other
  1859. 1:03:27and now this should work the same as it
  1860. 1:03:29did before but we just wrote it a little
  1861. 1:03:30bit different
  1862. 1:03:31what is the temperature outside it is 15
  1863. 1:03:34degrees outside
  1864. 1:03:3515 degrees celsius the temperature is
  1865. 1:03:37good today go outside
  1866. 1:03:38so with the not logical operator you can
  1867. 1:03:41surround one
  1868. 1:03:42or more conditional statements with the
  1869. 1:03:44not logical operator and what this will
  1870. 1:03:46do
  1871. 1:03:47is flip it from being false to true or
  1872. 1:03:49from true to false
  1873. 1:03:50well everybody that's the basics of
  1874. 1:03:52logical operators in python if you would
  1875. 1:03:55like a copy of all this code i will post
  1876. 1:03:57this in the comments
  1877. 1:03:58down below but yeah that's the basics of
  1878. 1:04:00logical operators
  1879. 1:04:02in python hey what's going on everybody
  1880. 1:04:05it's bro hope you're doing well and in
  1881. 1:04:07this video i'm going to explain while
  1882. 1:04:08loops in python so sit back
  1883. 1:04:10relax and enjoy the show
  1884. 1:04:15so while loops a while loop is a
  1885. 1:04:17statement that will execute its block of
  1886. 1:04:19code
  1887. 1:04:20as long as its condition remains true
  1888. 1:04:23later on in this video for this example
  1889. 1:04:25we're going to create
  1890. 1:04:26a program where we will prompt a user to
  1891. 1:04:28enter their name
  1892. 1:04:29if they attempt to skip that prompt then
  1893. 1:04:31we will continually ask the user to
  1894. 1:04:33enter their name
  1895. 1:04:34and they can't continue on with the rest
  1896. 1:04:36of the program until they do so the key
  1897. 1:04:37thing to keep in mind with while loops
  1898. 1:04:39is that you'll want some way to
  1899. 1:04:41eventually escape the while loop if you
  1900. 1:04:42don't have any way to escape the while
  1901. 1:04:44loop that's referred to as an
  1902. 1:04:46infinite loop so let's create an example
  1903. 1:04:48of an infinite loop because it's fun
  1904. 1:04:50so to create a while loop type while and
  1905. 1:04:52then some sort of condition
  1906. 1:04:53so for our condition let's say while one
  1907. 1:04:55is equal to one
  1908. 1:04:56so this will always be true now what do
  1909. 1:04:59we want to do
  1910. 1:05:00when we execute this block of code let's
  1911. 1:05:02print something
  1912. 1:05:03print help i'm
  1913. 1:05:06stuck in a loop all right so let's try
  1914. 1:05:10this
  1915. 1:05:10while one is equal to one print this
  1916. 1:05:13statement
  1917. 1:05:15and we will just continue on and on
  1918. 1:05:17within this while loop because we have
  1919. 1:05:19no way to escape this while loop
  1920. 1:05:21so now let's use this concept and write
  1921. 1:05:24a program where we will prompt a user to
  1922. 1:05:26type in their name
  1923. 1:05:27if they don't type in anything then we
  1924. 1:05:29will continue to prompt them to type in
  1925. 1:05:31at least something
  1926. 1:05:32so let's say name is equal to
  1927. 1:05:35and then to just keep this blank we'll
  1928. 1:05:36just set this to a set of quotes
  1929. 1:05:39and then let's write a while loop here
  1930. 1:05:41while name
  1931. 1:05:43and let's check the length of name if
  1932. 1:05:45the length
  1933. 1:05:46of our name is equal
  1934. 1:05:49to zero then we will ask the user to
  1935. 1:05:52type in their name
  1936. 1:05:53name equals input
  1937. 1:05:57enter your name
  1938. 1:06:00so then once we escape the while loop
  1939. 1:06:02let's print something
  1940. 1:06:04print hello plus
  1941. 1:06:07name whoops hello plus name
  1942. 1:06:10alright so when i run this we are giving
  1943. 1:06:12the user a way to escape the while loop
  1944. 1:06:14they have to type in something for their
  1945. 1:06:16name
  1946. 1:06:16as long as the length of my name is
  1947. 1:06:19equal to zero
  1948. 1:06:20keep on printing this prompt enter your
  1949. 1:06:23name
  1950. 1:06:23so i'm just going to hit enter a bunch
  1951. 1:06:25of times enter your name
  1952. 1:06:26no enter your name no enter your name no
  1953. 1:06:30so we are stuck within this loop until
  1954. 1:06:32we enter something for our name
  1955. 1:06:34so this time let me type in a name then
  1956. 1:06:36hit enter
  1957. 1:06:37so therefore our condition is now false
  1958. 1:06:39the length
  1959. 1:06:40of my name is now one two three so
  1960. 1:06:43three is not equal to zero therefore our
  1961. 1:06:46while statement our while loop is false
  1962. 1:06:48and we continue on with the rest of the
  1963. 1:06:49program there are a few variations in
  1964. 1:06:51which you could write the same program
  1965. 1:06:53so
  1966. 1:06:54let's write this a different way what we
  1967. 1:06:55could do is say
  1968. 1:06:57name is equal to none while
  1969. 1:07:00not name and this will pretty much do
  1970. 1:07:03the same thing it's just another way of
  1971. 1:07:05writing this
  1972. 1:07:06well everybody that's the basics of
  1973. 1:07:08while loops a while loop is a statement
  1974. 1:07:10that will execute
  1975. 1:07:11its block of code as long as its
  1976. 1:07:13condition remains true
  1977. 1:07:15for our example we asked the user to
  1978. 1:07:17type in their name
  1979. 1:07:18if they were to leave it empty or blank
  1980. 1:07:20we would keep on asking the user to type
  1981. 1:07:22in their name
  1982. 1:07:23so if you would like a copy of this code
  1983. 1:07:25i will post this in the comments section
  1984. 1:07:26down below
  1985. 1:07:27but yeah that's the basics of while
  1986. 1:07:29loops in python
  1987. 1:07:31hey what's going on everybody it's bro
  1988. 1:07:33hope you're doing well and in this video
  1989. 1:07:35i'm going to explain for loops in python
  1990. 1:07:37and at the end of this video we're going
  1991. 1:07:38to create a countdown timer so
  1992. 1:07:40sit back relax and enjoy the show
  1993. 1:07:45all right guys and gals let's talk about
  1994. 1:07:47for loops a for loop is a statement that
  1995. 1:07:49will execute its block of code a
  1996. 1:07:52limited amount of times it's similar but
  1997. 1:07:54different from a while loop because a
  1998. 1:07:56while loop
  1999. 1:07:57could iterate an unlimited or infinite
  2000. 1:08:00amount of times depending on the
  2001. 1:08:02condition a for loop
  2002. 1:08:04will only iterate a limited amount of
  2003. 1:08:06times and before we start executing
  2004. 1:08:08its block of code we already know how
  2005. 1:08:10many times we're going to repeat this
  2006. 1:08:11block of code
  2007. 1:08:12so for this example let's create a for
  2008. 1:08:14loop that will simply count it to 10
  2009. 1:08:16and then we'll create a few more
  2010. 1:08:17sophisticated examples
  2011. 1:08:19so to create a for loop that will count
  2012. 1:08:20up to 10 this is what we'll type
  2013. 1:08:22four and then we need some sort of
  2014. 1:08:24counter people usually either write
  2015. 1:08:26index or they shorten it to just i i for
  2016. 1:08:29index
  2017. 1:08:30four i in and we can set a range range
  2018. 1:08:34ten so we will execute this for loop ten
  2019. 1:08:36times
  2020. 1:08:37and to finish this for loop just add a
  2021. 1:08:39colon at the end
  2022. 1:08:40so the next line or lines will be
  2023. 1:08:43indented so that's the block of code for
  2024. 1:08:45the for loop
  2025. 1:08:46what do we want to do during each
  2026. 1:08:47iteration let's just print what
  2027. 1:08:49i is and let's take a look to see what
  2028. 1:08:51would happen
  2029. 1:08:52so we executed this for loop ten times
  2030. 1:08:55except with computers computers always
  2031. 1:08:57start with zero
  2032. 1:08:58so when we said four i in range ten
  2033. 1:09:00we're counting numbers
  2034. 1:09:02zero through nine so it's as if this
  2035. 1:09:04number
  2036. 1:09:05is exclusive so there's two ways we can
  2037. 1:09:07change this we could say range ten
  2038. 1:09:09plus one or what i would do is say print
  2039. 1:09:12i
  2040. 1:09:12plus one so this will give us numbers
  2041. 1:09:15one
  2042. 1:09:15through ten so that's a for loop it will
  2043. 1:09:18execute a limited amount of times
  2044. 1:09:20this time let's count a range between
  2045. 1:09:22two numbers
  2046. 1:09:23not necessarily 0 to 10 but something
  2047. 1:09:25else so let's say 4i
  2048. 1:09:27in range maybe the numbers 50 through
  2049. 1:09:31100
  2050. 1:09:31so within my range function i will pass
  2051. 1:09:34in two numbers
  2052. 1:09:35the first number will be the starting
  2053. 1:09:37point and the second number is the
  2054. 1:09:38ending point
  2055. 1:09:39this first number is inclusive and the
  2056. 1:09:41second number is exclusive
  2057. 1:09:43so let's print this print whatever i is
  2058. 1:09:46so this will count
  2059. 1:09:4850 through 99 actually if you wanted to
  2060. 1:09:51include this last number since it's
  2061. 1:09:53exclusive what we could do is just add
  2062. 1:09:55one to the end so this would give
  2063. 1:09:56us 50 through 100 except that
  2064. 1:10:00this will iterate 51 times since we're
  2065. 1:10:02counting 50.
  2066. 1:10:04so you just have to pay attention to
  2067. 1:10:05your range and the numbers that it's
  2068. 1:10:06counting now one thing that you can do
  2069. 1:10:08with the range function is that you can
  2070. 1:10:10add a third argument and this will
  2071. 1:10:12function as the step
  2072. 1:10:13how much you want to count up or down by
  2073. 1:10:15so this time let's count up by two
  2074. 1:10:17so i'll add comma two we're passing in a
  2075. 1:10:20third argument this time
  2076. 1:10:22so this program will now count up by two
  2077. 1:10:24starting at fifty and go all the way
  2078. 1:10:27to one hundred a benefit of for loops is
  2079. 1:10:30that we can iterate through anything
  2080. 1:10:32that is considered
  2081. 1:10:33iterable this could include a string the
  2082. 1:10:35letters in a string
  2083. 1:10:36or any sort of collection so this time
  2084. 1:10:39let's create a for loop that will
  2085. 1:10:40iterate
  2086. 1:10:41once through each letter in maybe a name
  2087. 1:10:44so for
  2088. 1:10:44i in and let's type a name here type in
  2089. 1:10:47whatever your first name and last name
  2090. 1:10:49is
  2091. 1:10:50and then we will print whatever i is
  2092. 1:10:53for i in whatever your name is print
  2093. 1:10:56each letter
  2094. 1:10:57in the string so when i run this each
  2095. 1:11:00letter
  2096. 1:11:01within my name will be printed to a new
  2097. 1:11:03line
  2098. 1:11:04all right people for my last trick we're
  2099. 1:11:06going to create a program
  2100. 1:11:07where we will simulate a countdown
  2101. 1:11:09starting at 10 and count down to zero
  2102. 1:11:11and then maybe once we reach zero we can
  2103. 1:11:13print something such as
  2104. 1:11:15happy new year so to create this program
  2105. 1:11:17we'll need an import so at the top
  2106. 1:11:19we're going to import the time module
  2107. 1:11:21because we'll be waiting
  2108. 1:11:22one second after each iteration of this
  2109. 1:11:24for loop
  2110. 1:11:25so to create this program let's say four
  2111. 1:11:27you could say i but you can really write
  2112. 1:11:29anything here and
  2113. 1:11:30to demonstrate that let's say four
  2114. 1:11:32seconds four seconds
  2115. 1:11:35in range so the starting point is going
  2116. 1:11:38to be ten
  2117. 1:11:38the ending point will be zero and we'll
  2118. 1:11:41add a step
  2119. 1:11:42what do we want to count up or down by
  2120. 1:11:44let's set this to negative one so this
  2121. 1:11:46will be a countdown
  2122. 1:11:47starting at ten and ending at zero so
  2123. 1:11:49after each iteration
  2124. 1:11:51let's print whatever i is or in this
  2125. 1:11:54case
  2126. 1:11:55seconds and then we can sleep have our
  2127. 1:11:58thread sleep
  2128. 1:11:58for a number of seconds too so after we
  2129. 1:12:01print seconds
  2130. 1:12:03type time dot sleep and pass in
  2131. 1:12:06how long you want to sleep for i would
  2132. 1:12:08like to sleep for one second
  2133. 1:12:10and then at the end let's print happy
  2134. 1:12:12new year make sure this isn't within the
  2135. 1:12:14for loop
  2136. 1:12:15print happy new year and well
  2137. 1:12:18that's it let's test this so we'll begin
  2138. 1:12:21at 10 and count down to zero and you can
  2139. 1:12:23see that my thread that is running this
  2140. 1:12:25program
  2141. 1:12:25is sleeping for one second after each
  2142. 1:12:28iteration of this for loop
  2143. 1:12:29then once we reach zero it will display
  2144. 1:12:32happy new year
  2145. 1:12:33well everybody that's the basics of for
  2146. 1:12:35loops a for loop is a statement
  2147. 1:12:37that will execute its block of code a
  2148. 1:12:40limited amount of times
  2149. 1:12:41it's different from a while loop because
  2150. 1:12:43a while loop could execute
  2151. 1:12:45an infinite or unlimited amount of times
  2152. 1:12:47with for loops they execute limited
  2153. 1:12:50amounts of times
  2154. 1:12:51and before we enter the for loop we
  2155. 1:12:53already know how many times we're going
  2156. 1:12:54to iterate this for loop
  2157. 1:12:56if you'd like a copy of all this code i
  2158. 1:12:58will post this in the comments down
  2159. 1:12:59below
  2160. 1:13:00but yeah that's how for loops work in
  2161. 1:13:02python
  2162. 1:13:04hey what's going on everybody it's you
  2163. 1:13:06bro hope you're doing well and in this
  2164. 1:13:07video i'm going to explain nested loops
  2165. 1:13:09in python so
  2166. 1:13:10sit back relax and enjoy the show
  2167. 1:13:15hey all you people let's talk about
  2168. 1:13:17nested loops a nested loop is a general
  2169. 1:13:19concept of having
  2170. 1:13:20one loop inside of another loop and
  2171. 1:13:22that's really it it doesn't matter if
  2172. 1:13:24it's a for loop or a while loop
  2173. 1:13:26so the inner loop will finish all of its
  2174. 1:13:28iterations
  2175. 1:13:29before we finish one iteration of the
  2176. 1:13:31outer loop
  2177. 1:13:32and to best demonstrate this we're going
  2178. 1:13:34to create a program where we will draw a
  2179. 1:13:36rectangle made out of a certain symbol
  2180. 1:13:38that we choose
  2181. 1:13:39we need to set a width and a height and
  2182. 1:13:41to best do this we'll need to use a
  2183. 1:13:43nested loop we'll create a few prompts
  2184. 1:13:45one for rows
  2185. 1:13:46columns and a symbol that we want to
  2186. 1:13:48make a rectangle of
  2187. 1:13:49so let's begin with rows rows equals
  2188. 1:13:52input how many
  2189. 1:13:56rows and we'll need to cast this as an
  2190. 1:14:00integer data type
  2191. 1:14:01because we're working with numbers okay
  2192. 1:14:04let's do the same thing for columns
  2193. 1:14:08so replace rows with columns for this
  2194. 1:14:10next line
  2195. 1:14:12input how many columns
  2196. 1:14:15and we'll create a symbol variable
  2197. 1:14:18symbol
  2198. 1:14:18equals input and this we don't have to
  2199. 1:14:21cast
  2200. 1:14:23enter a symbol to
  2201. 1:14:26use now it's time for the nested loops
  2202. 1:14:29we're going to create an outer for loop
  2203. 1:14:31as well as an inner for loop
  2204. 1:14:33the outer for loop will be in charge of
  2205. 1:14:35the rows the inner for loop will be in
  2206. 1:14:37charge of the columns
  2207. 1:14:38so let's begin by creating our outer
  2208. 1:14:41loop so
  2209. 1:14:42we'll write 4 i in range
  2210. 1:14:46rows we would like our outer for loop to
  2211. 1:14:49iterate as many times
  2212. 1:14:50as we have rows and we're going to
  2213. 1:14:52create an inner for loop that will
  2214. 1:14:54iterate as many times as we have columns
  2215. 1:14:56so within the block of code for the
  2216. 1:14:58outer for loop we're going to create
  2217. 1:15:00another for loop
  2218. 1:15:01this time it'll be four and a common
  2219. 1:15:04convention
  2220. 1:15:05for inner loops is to write j as an
  2221. 1:15:07index because
  2222. 1:15:09j comes after i i guess so for j
  2223. 1:15:12in range columns
  2224. 1:15:18so our inner for loop will iterate for
  2225. 1:15:21as many times
  2226. 1:15:22as we have columns all we'll do within
  2227. 1:15:24the
  2228. 1:15:25inner for loop is print our symbol that
  2229. 1:15:27we have
  2230. 1:15:28except we'll have one issue using a
  2231. 1:15:31print statement
  2232. 1:15:32after we use the print statement we will
  2233. 1:15:34enter a new line character and move down
  2234. 1:15:36to the next line
  2235. 1:15:37we can actually prevent that by adding
  2236. 1:15:39comma
  2237. 1:15:40and equals quotes so after using a print
  2238. 1:15:45statement
  2239. 1:15:45this will prevent our cursor from moving
  2240. 1:15:47down to the next line
  2241. 1:15:49so pay attention to the indentation too
  2242. 1:15:52so there's kind of two levels of
  2243. 1:15:53indentation
  2244. 1:15:54we're currently within the inner for
  2245. 1:15:56loop so that's pretty much it for the
  2246. 1:15:58inner for loop now we're going to
  2247. 1:16:00print a new line once we exit the inner
  2248. 1:16:02for loop
  2249. 1:16:03and you can see that the indentation is
  2250. 1:16:05on
  2251. 1:16:07this line right here we're within the
  2252. 1:16:08outer for loop now
  2253. 1:16:10and that's pretty much it so let's try
  2254. 1:16:12this program
  2255. 1:16:13how many rows let's say five rows how
  2256. 1:16:16many columns perhaps six
  2257. 1:16:19what symbol do we want to use let's say
  2258. 1:16:21the dollar sign
  2259. 1:16:22and let's try it there we go there's our
  2260. 1:16:25rectangle
  2261. 1:16:26so we have let's see six columns one
  2262. 1:16:30two three four five six and five rows
  2263. 1:16:32one
  2264. 1:16:33two three four five in summary
  2265. 1:16:36a nested loop is really just this
  2266. 1:16:38concept of having
  2267. 1:16:39one loop inside of another loop it
  2268. 1:16:41doesn't matter if it's a while loop or a
  2269. 1:16:43for a loop it's really situational
  2270. 1:16:45the inner loop will finish all of its
  2271. 1:16:47iterations before finishing
  2272. 1:16:49one iteration of the outer loop so what
  2273. 1:16:51we did to demonstrate that is that we
  2274. 1:16:53had
  2275. 1:16:54the outer loop in charge the rows and an
  2276. 1:16:56inner loop in charge of the columns for
  2277. 1:16:58our rectangle program
  2278. 1:16:59so if you would like a copy of this
  2279. 1:17:01program i will post this in the comment
  2280. 1:17:03section down below
  2281. 1:17:04but yeah that's the basics of nested
  2282. 1:17:06loops in python
  2283. 1:17:09hey what's going on everybody it's bro
  2284. 1:17:11hope you're doing well
  2285. 1:17:12and in this video i'm going to explain
  2286. 1:17:14loop control statements in python
  2287. 1:17:16so sit back relax and enjoy the show
  2288. 1:17:21all right ladies and gentlemen let's
  2289. 1:17:23talk about loop control statements
  2290. 1:17:24these are used to change a loop's
  2291. 1:17:26execution from its normal sequence
  2292. 1:17:29and there are three we're going to
  2293. 1:17:30discuss break continue
  2294. 1:17:32and pass let's begin with break break is
  2295. 1:17:35used to terminate the loop entirely
  2296. 1:17:37now here's a good example of where a
  2297. 1:17:39break would be useful
  2298. 1:17:40let's say while true we're going to
  2299. 1:17:43continually ask
  2300. 1:17:44for somebody's name if they don't type
  2301. 1:17:46in anything then we'll continue the
  2302. 1:17:48while loop over and over again
  2303. 1:17:49so let's say name equals input
  2304. 1:17:55enter your name now
  2305. 1:17:58if name does not equal
  2306. 1:18:02a set of quotes that means they don't
  2307. 1:18:04type in anything then
  2308. 1:18:05we will break and break out of this
  2309. 1:18:08while loop and let's test it
  2310. 1:18:10enter your name nah i don't think so i'm
  2311. 1:18:12too lazy to enter my name
  2312. 1:18:14all right i can see that i'm not going
  2313. 1:18:15to escape this while loop so i'm going
  2314. 1:18:17to type in my name because i give up
  2315. 1:18:19and hit enter therefore i will break and
  2316. 1:18:22exit out of this while loop
  2317. 1:18:23so a break is used to terminate the loop
  2318. 1:18:26entirely when it's encountered
  2319. 1:18:28next we have continue continue skips to
  2320. 1:18:30the next iteration of the loop
  2321. 1:18:32now let's say we have a phone number
  2322. 1:18:35phone
  2323. 1:18:35underscore number and i will set the
  2324. 1:18:38sequel to
  2325. 1:18:39let's say a random phone number
  2326. 1:18:40including dashes 123-456-7890
  2327. 1:18:45what i would like to do is to display
  2328. 1:18:47this number within the console window
  2329. 1:18:49without these dashes
  2330. 1:18:51this is how to do that using a continue
  2331. 1:18:53control statement
  2332. 1:18:54for i in phone number
  2333. 1:19:00so for each character within our string
  2334. 1:19:03of phone number
  2335. 1:19:04what we'll do is check to see if we
  2336. 1:19:06encounter a dash character
  2337. 1:19:08if i i is our index is
  2338. 1:19:11equal to a dash then we want to
  2339. 1:19:15continue and skip over this iteration of
  2340. 1:19:18the loop
  2341. 1:19:19so continue
  2342. 1:19:23then i would like to print
  2343. 1:19:26whatever our indexes whatever character
  2344. 1:19:29we're working on
  2345. 1:19:30so if i were to run this as it is it's
  2346. 1:19:32going to print each digit
  2347. 1:19:34on a new line so with print statements
  2348. 1:19:37they will add a new line character to
  2349. 1:19:39the end of your string
  2350. 1:19:40and you can actually change that by
  2351. 1:19:42adding to the end comma
  2352. 1:19:44end equals and then a set of quotes so
  2353. 1:19:47this will print my phone number without
  2354. 1:19:49any dashes
  2355. 1:19:50one two three four five six seven eight
  2356. 1:19:51nine zero so that's a use of
  2357. 1:19:54the continue control statement it skips
  2358. 1:19:56to the next iteration of the loop
  2359. 1:19:58last but not least we have pass pass
  2360. 1:20:01does nothing
  2361. 1:20:01it acts as a placeholder let's say that
  2362. 1:20:04i would like to print the numbers
  2363. 1:20:051 through 20 using a for loop for i
  2364. 1:20:09in range 1 through 21 and remember that
  2365. 1:20:13the second digit is
  2366. 1:20:14exclusive so this will iterate once
  2367. 1:20:16through the digits of 1
  2368. 1:20:18through 20. let's pretend that i'm
  2369. 1:20:20highly superstitious and i don't want to
  2370. 1:20:22print
  2371. 1:20:22the number 13 because 13 is considered
  2372. 1:20:24an unlucky number
  2373. 1:20:26if i is equal to 13
  2374. 1:20:29what we'll do is pass and pass acts as a
  2375. 1:20:31placeholder and it won't do anything
  2376. 1:20:34else print
  2377. 1:20:37i that's one i there we go
  2378. 1:20:40this will print the numbers 1 through 20
  2379. 1:20:42and skip 13 because we used
  2380. 1:20:44the past control statement which does
  2381. 1:20:47nothing it acts as a placeholder
  2382. 1:20:49so everybody those are loop control
  2383. 1:20:51statements they change
  2384. 1:20:52a loop's execution from its normal
  2385. 1:20:54sequence we have break
  2386. 1:20:56continue and pass so if you would like a
  2387. 1:20:59copy of all this code
  2388. 1:21:00i will post all of this in the comments
  2389. 1:21:01section down below but yeah those are a
  2390. 1:21:03few
  2391. 1:21:04loop control statements in python what's
  2392. 1:21:07going on everybody
  2393. 1:21:08it's bro hope you're doing well and in
  2394. 1:21:10this video i'm going to explain
  2395. 1:21:12lists in python so sit back relax
  2396. 1:21:15and enjoy the show
  2397. 1:21:19all right ladies and gentlemen let's get
  2398. 1:21:21down to business i got to explain
  2399. 1:21:23lists to you all now a list is used to
  2400. 1:21:25store
  2401. 1:21:26multiple items within a single variable
  2402. 1:21:29for example let's say we have a variable
  2403. 1:21:32called food
  2404. 1:21:33and i will store a string value of pizza
  2405. 1:21:36because i like pizza
  2406. 1:21:37what we could do is that we can store
  2407. 1:21:40multiple items within this
  2408. 1:21:42variable by turning this variable into a
  2409. 1:21:44list
  2410. 1:21:45and in order to do so we're going to
  2411. 1:21:47surround all of the values that we would
  2412. 1:21:49like to add
  2413. 1:21:50to our list with a set of square
  2414. 1:21:52brackets
  2415. 1:21:53and boom there you have it we now have a
  2416. 1:21:56list called food it is no longer
  2417. 1:21:58a variable so what we could do is add
  2418. 1:22:01multiple items to this list of food so
  2419. 1:22:04let's add some other food items that we
  2420. 1:22:06might like
  2421. 1:22:07let's see i also like maybe some
  2422. 1:22:09hamburgers
  2423. 1:22:11and a hot dog
  2424. 1:22:15and maybe some spaghetti i think i
  2425. 1:22:18smelled spaghetti right
  2426. 1:22:19okay so we now have a list called food
  2427. 1:22:22now what happens if we attempt to print
  2428. 1:22:25this list of food
  2429. 1:22:26so print food so this will
  2430. 1:22:30print all of the elements found in this
  2431. 1:22:34list
  2432. 1:22:34each item in a list is referred to as an
  2433. 1:22:37element
  2434. 1:22:38if we need to access a certain element
  2435. 1:22:41of
  2436. 1:22:41this list we have to list the index so
  2437. 1:22:44next to our list we'll add a set of
  2438. 1:22:46square brackets again
  2439. 1:22:47and we need to list the numbered index
  2440. 1:22:49of the element that we're trying to
  2441. 1:22:51access
  2442. 1:22:52now computers they always start with
  2443. 1:22:53zero so the first
  2444. 1:22:55element in our list would be element
  2445. 1:22:58number zero
  2446. 1:22:59the next one would be one two and three
  2447. 1:23:02so if we need to access element zero
  2448. 1:23:06which is in the first position we would
  2449. 1:23:07say food square brackets
  2450. 1:23:10and then within here list the index so
  2451. 1:23:12the first position
  2452. 1:23:13is zero and this will now print the
  2453. 1:23:15first element of our list
  2454. 1:23:17which is pizza let's attempt to access
  2455. 1:23:19some of these other elements
  2456. 1:23:20so the element at index one would be
  2457. 1:23:24you guessed it hamburger two
  2458. 1:23:28would be hot dog and three would be
  2459. 1:23:30spaghetti
  2460. 1:23:31now what happens if we attempt to access
  2461. 1:23:34the element at index four
  2462. 1:23:36well currently that's out of range
  2463. 1:23:38because we did not assign
  2464. 1:23:40a value so we're experiencing an error
  2465. 1:23:42an index
  2466. 1:23:43out of range error but if i were to add
  2467. 1:23:46another element
  2468. 1:23:47let's say pudding alright well then
  2469. 1:23:51the element at index 4 would be putting
  2470. 1:23:54then
  2471. 1:23:55one important concept with lists is that
  2472. 1:23:57you can always
  2473. 1:23:58update and change the elements found
  2474. 1:24:00within a list
  2475. 1:24:01later on in the program after you
  2476. 1:24:03declare one so let's say we would like
  2477. 1:24:05to
  2478. 1:24:05immediately change one of these elements
  2479. 1:24:08let's say
  2480. 1:24:09food at index zero i want to
  2481. 1:24:12replace with maybe sushi because sushi
  2482. 1:24:15is great
  2483. 1:24:16so if i were to print the element at
  2484. 1:24:18index
  2485. 1:24:190 this would no longer be pizza because
  2486. 1:24:22we updated element 0 to be
  2487. 1:24:25sushi then and this prints sushi now if
  2488. 1:24:28you need to display
  2489. 1:24:29all of the elements found within a list
  2490. 1:24:31you can easily do so with a standard for
  2491. 1:24:34loop
  2492. 1:24:34what we'll do is say for x in
  2493. 1:24:38the name of our list which is food
  2494. 1:24:41print x and let me turn this line into a
  2495. 1:24:45comment
  2496. 1:24:46all right so this for loop will print
  2497. 1:24:49all of the elements found within our
  2498. 1:24:51list
  2499. 1:24:51of food and the results are sushi
  2500. 1:24:55hamburger hot dog spaghetti and pudding
  2501. 1:24:57all right people for this next portion
  2502. 1:24:59i'm going to demonstrate a few useful
  2503. 1:25:01functions of
  2504. 1:25:02lists and to access some of these
  2505. 1:25:04functions type the name of your list
  2506. 1:25:06dot and there's a bunch to choose from
  2507. 1:25:08here let's begin with
  2508. 1:25:10event we can add an element to this list
  2509. 1:25:12let's say at the end i would like to add
  2510. 1:25:14ice cream so then if i were to display
  2511. 1:25:17my list we now have ice cream at the end
  2512. 1:25:20because we appended
  2513. 1:25:22this value to my list of food so let's
  2514. 1:25:24go over a few others
  2515. 1:25:26so we can also remove a value food
  2516. 1:25:29dot remove let's say i would like to
  2517. 1:25:32remove
  2518. 1:25:32hotdog so i'm going to type that within
  2519. 1:25:36my function and this will remove hotdog
  2520. 1:25:39and that is no longer here
  2521. 1:25:42next we can pop food.pop
  2522. 1:25:46pop will remove the last element so this
  2523. 1:25:50will remove
  2524. 1:25:52pudding
  2525. 1:25:54we can insert a value at a given index
  2526. 1:25:57food dot insert we need to list an index
  2527. 1:26:01let's say zero that would be the first
  2528. 1:26:03position technically that's where pizza
  2529. 1:26:05is currently
  2530. 1:26:06and i would like to add cake
  2531. 1:26:10and at index 0 we now have cake
  2532. 1:26:14so next up we have sort
  2533. 1:26:17food dot sort this will sort a list
  2534. 1:26:20alphabetically
  2535. 1:26:22so in the first position we have
  2536. 1:26:24hamburger hot dog pudding spaghetti
  2537. 1:26:26then sushi we can also clear a list
  2538. 1:26:31food dot clear and this will remove
  2539. 1:26:34all of the elements of a list so this
  2540. 1:26:37will not print anything because we
  2541. 1:26:39cleared the list
  2542. 1:26:40well everyone in conclusion a list is
  2543. 1:26:42really just a variable that can store
  2544. 1:26:45multiple values think of it that way so
  2545. 1:26:48if you would like a copy
  2546. 1:26:50of all the code that we have written
  2547. 1:26:51here today i will post all of this in
  2548. 1:26:53the comments down below
  2549. 1:26:54but yeah that's how lists work in python
  2550. 1:26:58how's it going everybody it's bro hope
  2551. 1:27:00you're doing well
  2552. 1:27:01and in this video i'm going to explain
  2553. 1:27:022d lists in python so sit back
  2554. 1:27:05relax and enjoy the show
  2555. 1:27:10i have a super quick video for you all
  2556. 1:27:12today on
  2557. 1:27:132d lists also referred to as
  2558. 1:27:15multi-dimensional lists
  2559. 1:27:17all it is is a list of separate lists
  2560. 1:27:20so let's begin by creating a few
  2561. 1:27:22separate lists let's say we have a list
  2562. 1:27:24called drinks and let's assign a few
  2563. 1:27:27values
  2564. 1:27:28let's say we have coffee as well
  2565. 1:27:32as soda and maybe some tea
  2566. 1:27:35all right so we currently have one list
  2567. 1:27:37let's make maybe two more let's make a
  2568. 1:27:39list called
  2569. 1:27:40dinner and maybe in here we'll place
  2570. 1:27:44pizza then hamburger
  2571. 1:27:50and hot dog okay
  2572. 1:27:53then let's create one more for this
  2573. 1:27:55example let's say dessert
  2574. 1:27:59and maybe we'll add two items
  2575. 1:28:02they all don't need the same amount of
  2576. 1:28:03items or elements so we have cake
  2577. 1:28:06and ice cream
  2578. 1:28:09all right so we have three lists one
  2579. 1:28:12called drinks
  2580. 1:28:13dinner and dessert so what we can do is
  2581. 1:28:16add
  2582. 1:28:16all of these lists to one list
  2583. 1:28:19and let's create a list called food food
  2584. 1:28:22equals and for each of these elements
  2585. 1:28:24we're going to place
  2586. 1:28:25our lists so we have drinks dinner
  2587. 1:28:29and dessert check this out what if i
  2588. 1:28:32were to
  2589. 1:28:33print my 2d list of food
  2590. 1:28:36so what ends up happening is that this
  2591. 1:28:38will print all of the elements
  2592. 1:28:40found within each individual list and
  2593. 1:28:42they're all grouped together
  2594. 1:28:44so this first portion is my drinks list
  2595. 1:28:48which contains coffee soda tea this next
  2596. 1:28:51element
  2597. 1:28:51is my dinner list which contains pizza
  2598. 1:28:54hamburger hot dog and the last element
  2599. 1:28:57dessert contains cake and ice cream if i
  2600. 1:29:00need to access
  2601. 1:29:02just one of these lists i will add an
  2602. 1:29:04index
  2603. 1:29:05after my food 2d list and
  2604. 1:29:08set an index number so index zero is
  2605. 1:29:11referring to my first
  2606. 1:29:13list of drinks and this will display all
  2607. 1:29:15of the elements found within my first
  2608. 1:29:17list
  2609. 1:29:18and if i need just one of these elements
  2610. 1:29:21i will add
  2611. 1:29:22a second set of square brackets and list
  2612. 1:29:24the index of the item that i'm trying to
  2613. 1:29:27access
  2614. 1:29:27so this very first item coffee would be
  2615. 1:29:31index zero and then index zero again so
  2616. 1:29:33we need
  2617. 1:29:34two sets of square brackets and if i
  2618. 1:29:36were to print this
  2619. 1:29:37this will display coffee so if i go down
  2620. 1:29:40the list
  2621. 1:29:40the next element would be soda
  2622. 1:29:44and then t if i were to change the index
  2623. 1:29:47within
  2624. 1:29:47this first set of square brackets well
  2625. 1:29:50now we're working with
  2626. 1:29:51a different list this time we're working
  2627. 1:29:53with our dinner list
  2628. 1:29:54so the element at index 1
  2629. 1:29:58index 2 that would be our dinner list
  2630. 1:30:01and this will display the element of hot
  2631. 1:30:03dog
  2632. 1:30:04if i change this to the next list which
  2633. 1:30:06would be dessert
  2634. 1:30:08well we're going to receive an index out
  2635. 1:30:10of range error because
  2636. 1:30:11we only have two values within this list
  2637. 1:30:14cake and ice cream there is no element
  2638. 1:30:18at index 2 because we only added two
  2639. 1:30:21elements
  2640. 1:30:22to this list of dessert all right
  2641. 1:30:24everybody so that
  2642. 1:30:26is 2d lists also referred to as
  2643. 1:30:29multi-dimensional lists
  2644. 1:30:30it's a list of separate lists if you
  2645. 1:30:33need to access
  2646. 1:30:34one of the elements within your 2d list
  2647. 1:30:36you need
  2648. 1:30:37two sets of square brackets so if you
  2649. 1:30:40would like a copy of this code
  2650. 1:30:41i will post this in the comments down
  2651. 1:30:43below but yeah that's how
  2652. 1:30:442d lists work in python hey what's going
  2653. 1:30:48on people
  2654. 1:30:48it's you bro hope you're doing well and
  2655. 1:30:50in this video i'm going to explain
  2656. 1:30:51tuples in python so
  2657. 1:30:53sit back relax and enjoy the show
  2658. 1:30:58so tuples they are collections which are
  2659. 1:31:01ordered
  2660. 1:31:01and unchangeable they're very similar to
  2661. 1:31:03lists but they're ordered
  2662. 1:31:05and we can't change them they're useful
  2663. 1:31:07for grouping together related data
  2664. 1:31:09let's say we would like to create some
  2665. 1:31:10sort of student record
  2666. 1:31:12so we can create a tuple to take care of
  2667. 1:31:14that for us so let's say we would like
  2668. 1:31:16to create a tuple called student
  2669. 1:31:18the process of creating a tuple is very
  2670. 1:31:20similar to lists
  2671. 1:31:22but instead of using a set of square
  2672. 1:31:24brackets to place all of the values
  2673. 1:31:26within
  2674. 1:31:26we're going to use a set of parentheses
  2675. 1:31:29and now we can add a bunch of values
  2676. 1:31:31related to this collection of student
  2677. 1:31:33so let's add some student information
  2678. 1:31:35let's say that this student's name is
  2679. 1:31:37bro
  2680. 1:31:38let's give him an age as well as a
  2681. 1:31:40gender
  2682. 1:31:41all right so there you have it we now
  2683. 1:31:43have a tuple called student
  2684. 1:31:45and let's take a look at some of the
  2685. 1:31:46functions related to tuples
  2686. 1:31:48and you can access them by typing
  2687. 1:31:49student dot
  2688. 1:31:51and then there are a few here not as
  2689. 1:31:53many as lists we have count and we have
  2690. 1:31:55index
  2691. 1:31:56so we can find the count of how many
  2692. 1:31:58times a value appears
  2693. 1:32:00let's count how many times the value of
  2694. 1:32:03bro appears
  2695. 1:32:04so we'll need to put this within a print
  2696. 1:32:06statement so we can actually see it
  2697. 1:32:08so bro appears
  2698. 1:32:12one time within this tuple there is also
  2699. 1:32:14an index method as well
  2700. 1:32:16so type the name of your tuple dot
  2701. 1:32:19and we will use the index method we can
  2702. 1:32:22find the index of a certain value
  2703. 1:32:24let's say we would like to find the
  2704. 1:32:26index of mail
  2705. 1:32:27and then i will just print this to the
  2706. 1:32:29console window
  2707. 1:32:31so the index of the value mail would be
  2708. 1:32:34at two zero one
  2709. 1:32:37two that's it for the methods available
  2710. 1:32:40to tuples
  2711. 1:32:41a few tricks that you can do with tuples
  2712. 1:32:42is that you can display
  2713. 1:32:44all of the contents within a tuple using
  2714. 1:32:47a for loop
  2715. 1:32:48let's say 4x in student
  2716. 1:32:51we will print x so
  2717. 1:32:55this will iterate once through all the
  2718. 1:32:57values found within our tuple
  2719. 1:32:59of student and we can also check to see
  2720. 1:33:02if a certain value
  2721. 1:33:03exists within our tuple using an if
  2722. 1:33:05statement let's say if
  2723. 1:33:07bro in student
  2724. 1:33:11if this is true then what do we want to
  2725. 1:33:13do let's say
  2726. 1:33:14print bro is here
  2727. 1:33:17all right so since bro is within here
  2728. 1:33:21this will be true and this will print
  2729. 1:33:23our
  2730. 1:33:24statement of bro is here all right
  2731. 1:33:27everybody like i said this is a super
  2732. 1:33:29quick video on tuples
  2733. 1:33:31they're very similar to lists except
  2734. 1:33:34they are ordered and unchangeable if
  2735. 1:33:36you'd like a copy of all this code i
  2736. 1:33:38will post all of this in the comments
  2737. 1:33:39down below
  2738. 1:33:40and don't forget to smash that like
  2739. 1:33:42button drop a comment down below
  2740. 1:33:44and subscribe if you'd like to become a
  2741. 1:33:46fellow bro
  2742. 1:33:47hey what's going on everybody it's you
  2743. 1:33:49bro hope you're doing well
  2744. 1:33:51and in this video i'm going to explain
  2745. 1:33:52what a set is in python so
  2746. 1:33:55sit back relax and enjoy the show
  2747. 1:34:00a set is a collection which is unordered
  2748. 1:34:03as well as being
  2749. 1:34:04unindexed they do not allow any
  2750. 1:34:06duplicate values
  2751. 1:34:07for example let's create a set of
  2752. 1:34:10silverware
  2753. 1:34:10so in order to create a set we need a
  2754. 1:34:12name for this set
  2755. 1:34:14let's call it utensils equals
  2756. 1:34:17and in order to create a set we need to
  2757. 1:34:19surround all values with a set
  2758. 1:34:21of curly braces now let's add a few
  2759. 1:34:23utensils
  2760. 1:34:24to the set called utensils let's add a
  2761. 1:34:27fork
  2762. 1:34:29a spoon and maybe a knife and that
  2763. 1:34:32should be good
  2764. 1:34:33and let's display all of the values for
  2765. 1:34:36x in utensils
  2766. 1:34:41print x now with sets
  2767. 1:34:44they're different from lists because
  2768. 1:34:46they're unordered as well as being
  2769. 1:34:48unindexed
  2770. 1:34:49if i were to print all of the elements
  2771. 1:34:51in utensils
  2772. 1:34:52they might not necessarily be in the
  2773. 1:34:54same order in which we placed them
  2774. 1:34:56for example when we printed all of the
  2775. 1:34:59elements found within utensils
  2776. 1:35:00first we had a knife a fork and then a
  2777. 1:35:03spoon if i were to run this again
  2778. 1:35:05they might be in a different order kind
  2779. 1:35:07of like that
  2780. 1:35:08so a set is actually faster than a list
  2781. 1:35:11if you need to check to see if something
  2782. 1:35:14is within a set compared to a list and
  2783. 1:35:16they do not allow any duplicate values
  2784. 1:35:19let me add a bunch of knives and let's
  2785. 1:35:21see what happens
  2786. 1:35:22so i'll have a total of three knives but
  2787. 1:35:26when we print all of the elements
  2788. 1:35:28of the set only one knife appears so
  2789. 1:35:30here's a few useful methods of sets
  2790. 1:35:34the first method is that we can add an
  2791. 1:35:36item to our set
  2792. 1:35:37so type in the name of the set in this
  2793. 1:35:40case it's utensils dot and here's a
  2794. 1:35:42bunch of methods that you can use
  2795. 1:35:44i am looking for add and we can add an
  2796. 1:35:47element to the set
  2797. 1:35:48i would like to add a napkin
  2798. 1:35:52and then if we were to print the set we
  2799. 1:35:54do have a napkin
  2800. 1:35:56within the set we can also remove an
  2801. 1:35:58element
  2802. 1:35:59utensils dot remove
  2803. 1:36:02and i would like to remove the fork
  2804. 1:36:08and now our fork is gone we can also
  2805. 1:36:11clear
  2806. 1:36:12utensils dot clear
  2807. 1:36:16and all the elements within our set
  2808. 1:36:18should be gone
  2809. 1:36:20so let me turn these lines into a
  2810. 1:36:22comment
  2811. 1:36:23all right for this next example we'll
  2812. 1:36:25need a second set
  2813. 1:36:26let's create a second set called dishes
  2814. 1:36:30and then we need a set of curly braces
  2815. 1:36:33and
  2816. 1:36:33let's add a bowl
  2817. 1:36:36a plate and a cup
  2818. 1:36:40our next method is that we're going to
  2819. 1:36:43add one set to another by using the
  2820. 1:36:45update method
  2821. 1:36:46let's say we would like to add our
  2822. 1:36:48dishes set to our utensil set
  2823. 1:36:51so let's type utensils dot and use the
  2824. 1:36:54update method
  2825. 1:36:54and within this method we will pass in
  2826. 1:36:57the set that we would like to add
  2827. 1:36:59so utensils update dishes this will add
  2828. 1:37:02all of the elements found within dishes
  2829. 1:37:04to our utensil set and you can see that
  2830. 1:37:07when we print our utensils
  2831. 1:37:08we have a bowl a plate and a cup in here
  2832. 1:37:12as well
  2833. 1:37:12and then if i were to switch these
  2834. 1:37:14around let's say dishes
  2835. 1:37:16update utensils and then i will
  2836. 1:37:20print everything found within dishes now
  2837. 1:37:23there's a fork a spoon
  2838. 1:37:25and a knife in here as well we can also
  2839. 1:37:28join
  2840. 1:37:28two sets together and create a new set
  2841. 1:37:31entirely
  2842. 1:37:32so let's create a set called dinner
  2843. 1:37:34table like we're setting up a dinner
  2844. 1:37:36table and we need a fork a spoon a knife
  2845. 1:37:39a bowl a plate and a cup
  2846. 1:37:40so we have a dinner table set
  2847. 1:37:44we're going to set the sequel to either
  2848. 1:37:46utensils
  2849. 1:37:48dot union
  2850. 1:37:51dishes or we could do dishes
  2851. 1:37:54union utensils either way would work and
  2852. 1:37:57let's display all the elements found
  2853. 1:37:59within dinner table
  2854. 1:38:00and we should now have elements from
  2855. 1:38:02both sets utensils
  2856. 1:38:04and dishes as well there are also some
  2857. 1:38:07methods in which we can compare
  2858. 1:38:09the similarities as well as the
  2859. 1:38:11differences between
  2860. 1:38:12the elements found within two sets let's
  2861. 1:38:14say i would like to check to see
  2862. 1:38:16what utensils has that dishes doesn't
  2863. 1:38:19and for this example i think i'll add a
  2864. 1:38:21knife
  2865. 1:38:22to my dishes set just so that they have
  2866. 1:38:24at least one thing in common
  2867. 1:38:26so i would like to see what utensils has
  2868. 1:38:29that dishes doesn't so we can do so
  2869. 1:38:31using the
  2870. 1:38:32difference method and i will print the
  2871. 1:38:35results with a print statement
  2872. 1:38:36so i'm going to compare utensils against
  2873. 1:38:40dishes utensils
  2874. 1:38:45dishes and this will print what utensils
  2875. 1:38:48has
  2876. 1:38:49that dishes doesn't which is a fork as
  2877. 1:38:52well as a spoon
  2878. 1:38:54and we can reverse the rolls too we
  2879. 1:38:56could say dishes difference
  2880. 1:38:58utensils what do dishes have that
  2881. 1:39:01utensils doesn't
  2882. 1:39:02and dishes has a cup a bowl and a plate
  2883. 1:39:06they both have knives so that's why
  2884. 1:39:08knife isn't appearing
  2885. 1:39:10we can also check to see if there's
  2886. 1:39:12anything that they have in common
  2887. 1:39:13using the intersection method so i'll
  2888. 1:39:16turn this line into a comment
  2889. 1:39:20we'll print utensils
  2890. 1:39:23dot intersection
  2891. 1:39:27dishes and this will return whatever
  2892. 1:39:30element that they have in common
  2893. 1:39:32which is a knife all right everybody in
  2894. 1:39:35conclusion a set
  2895. 1:39:36is a collection which is unordered as
  2896. 1:39:39well as being
  2897. 1:39:40unindexed they do not allow any
  2898. 1:39:42duplicate values
  2899. 1:39:43and you can do things like compare two
  2900. 1:39:45sets see if there's anything that they
  2901. 1:39:47have in common any differences
  2902. 1:39:49and we can add or remove elements to a
  2903. 1:39:51set if you'd like a copy of all this
  2904. 1:39:53code i will post all of this in the
  2905. 1:39:55comments down below
  2906. 1:39:56don't be afraid to smash that like
  2907. 1:39:58button drop a comment down below
  2908. 1:40:00and subscribe if you'd like to become a
  2909. 1:40:02fellow bro hey what's going on everybody
  2910. 1:40:05it's you bro hope you're doing well and
  2911. 1:40:06in this video i'm going to explain
  2912. 1:40:08how dictionaries work in python so sit
  2913. 1:40:11back
  2914. 1:40:11relax and enjoy the show
  2915. 1:40:15all right let's get down to business a
  2916. 1:40:17dictionary is a
  2917. 1:40:19changeable unordered collection of
  2918. 1:40:22unique key value pairs they're fast
  2919. 1:40:25because they use hashing
  2920. 1:40:27and they allow us to access a value
  2921. 1:40:29quickly
  2922. 1:40:30now to create a dictionary it's very
  2923. 1:40:32similar to creating a
  2924. 1:40:33set based on the last video except we're
  2925. 1:40:36going to store
  2926. 1:40:36unique key value pairs let's create a
  2927. 1:40:39dictionary
  2928. 1:40:40of countries and their capitals we can
  2929. 1:40:43store those
  2930. 1:40:44as key value pairs so let's call this
  2931. 1:40:46dictionary
  2932. 1:40:48capitals capitals equals then we need a
  2933. 1:40:51set of curly braces much like what we
  2934. 1:40:53use
  2935. 1:40:53with sets now we need a key
  2936. 1:40:56and a value let's say we have the usa as
  2937. 1:40:59a key
  2938. 1:41:00and the capital of the usa would be
  2939. 1:41:03washington dc
  2940. 1:41:04so in order to associate a value with
  2941. 1:41:06the key we'll follow the key with a
  2942. 1:41:08colon
  2943. 1:41:09and then add some value and the data
  2944. 1:41:11type really doesn't matter but
  2945. 1:41:12in this case we're just using strings so
  2946. 1:41:14the capital of the usa
  2947. 1:41:16would be washington dc
  2948. 1:41:19and to add another key value pair we'll
  2949. 1:41:21separate each with a comma
  2950. 1:41:23and let's add a few others for good
  2951. 1:41:25measure so let's say we have india next
  2952. 1:41:28as a key and the value would be
  2953. 1:41:31new delhi and we'll add two more
  2954. 1:41:35let's say we have china
  2955. 1:41:38and the capital of china is beijing
  2956. 1:41:41and lastly let's add russia
  2957. 1:41:46and the capital of russia is moscow
  2958. 1:41:49and there you have it ladies and
  2959. 1:41:51gentlemen we now have a dictionary
  2960. 1:41:53called capitals
  2961. 1:41:54that has unique key value pairs so
  2962. 1:41:57remember with
  2963. 1:41:58dictionaries they are unordered in order
  2964. 1:42:00to access one of these values
  2965. 1:42:02instead of using a numbered index we're
  2966. 1:42:04going to use the associated key with
  2967. 1:42:06that value
  2968. 1:42:07here's an example let's say i would like
  2969. 1:42:09to print the capital
  2970. 1:42:10of russia so i need to use this key of
  2971. 1:42:14russia
  2972. 1:42:14so in order to do this type in the name
  2973. 1:42:16of the dictionary
  2974. 1:42:18followed by a set of straight brackets
  2975. 1:42:20and then instead of an
  2976. 1:42:21index number like 0 or 1 2 3
  2977. 1:42:25i'm going to use the key so i would like
  2978. 1:42:27to print
  2979. 1:42:28the value that's stored or associated
  2980. 1:42:31with the key
  2981. 1:42:31of russia so when i run this this will
  2982. 1:42:35print
  2983. 1:42:36that value which is moscow so this isn't
  2984. 1:42:39always safe
  2985. 1:42:40let's say we have a key that doesn't
  2986. 1:42:41exist like germany
  2987. 1:42:43germany is not currently within my
  2988. 1:42:44dictionary so if i were to run this
  2989. 1:42:47well then my program is going to
  2990. 1:42:49encounter an error
  2991. 1:42:51and this will interrupt the normal flow
  2992. 1:42:52of my program
  2993. 1:42:54a much safer way to access a key
  2994. 1:42:57to check to see if it's there or not is
  2995. 1:42:59to use the get method
  2996. 1:43:01of dictionaries so i'm going to turn
  2997. 1:43:03this line into a comment and let's try
  2998. 1:43:04this again but this time we'll use the
  2999. 1:43:06get method
  3000. 1:43:07so type in the name of the dictionary
  3001. 1:43:09dot get
  3002. 1:43:12and list the key that you want to use
  3003. 1:43:15so i would like to see if germany
  3004. 1:43:18is within my dictionary currently there
  3005. 1:43:21isn't so this will return
  3006. 1:43:23none and we will not encounter an error
  3007. 1:43:25so this is a much
  3008. 1:43:26safer way of checking to see if there is
  3009. 1:43:28a key within your dictionary
  3010. 1:43:30here's a few other useful methods there
  3011. 1:43:32is a method to print only the keys
  3012. 1:43:35so we type in the name of the dictionary
  3013. 1:43:38capitals
  3014. 1:43:39and here's a listing of all of the
  3015. 1:43:41methods that we have access to
  3016. 1:43:42i'm looking for the keys method and this
  3017. 1:43:45will print
  3018. 1:43:46only the keys and not the values we can
  3019. 1:43:49also print
  3020. 1:43:50just the values and not the keys so type
  3021. 1:43:53in the name of the dictionary again
  3022. 1:43:55capitals dot values
  3023. 1:43:59and this will print only the values or
  3024. 1:44:02you could print everything
  3025. 1:44:03both the keys and the values and you do
  3026. 1:44:06so by using
  3027. 1:44:07the items method capitals
  3028. 1:44:10dot items
  3029. 1:44:15and this will print your entire
  3030. 1:44:16dictionary one other way in which we can
  3031. 1:44:18display
  3032. 1:44:19all of the key value pairs in a
  3033. 1:44:21dictionary is to use a for loop
  3034. 1:44:23so at the end let's create a for loop
  3035. 1:44:25for
  3036. 1:44:26key comma value in
  3037. 1:44:30capitals dot items
  3038. 1:44:33this is going to iterate once for each
  3039. 1:44:36key value pair
  3040. 1:44:37in my dictionary for each key value in
  3041. 1:44:40capitals
  3042. 1:44:40dot items print
  3043. 1:44:44key comma value
  3044. 1:44:47and this will print my entire dictionary
  3045. 1:44:50a feature of dictionaries
  3046. 1:44:51is that they are mutable that means we
  3047. 1:44:54can change them or alter them
  3048. 1:44:55after the program is already running so
  3049. 1:44:58one way in which we can do that
  3050. 1:44:59is to use the update method of
  3051. 1:45:01dictionaries
  3052. 1:45:02so let's add germany as a key and give
  3053. 1:45:06it a value
  3054. 1:45:06of berlin so type in the name of the
  3055. 1:45:09dictionary capitals
  3056. 1:45:11and we'll use the update method and
  3057. 1:45:13within the parentheses of this method
  3058. 1:45:15we'll add a set of curly braces and now
  3059. 1:45:18we can add a new
  3060. 1:45:19key value pair so let's add germany
  3061. 1:45:23and a value of berlin so if i were to
  3062. 1:45:27run this and display
  3063. 1:45:28all of the key value pairs found within
  3064. 1:45:30my dictionary we now have
  3065. 1:45:32germany as a key and berlin as its value
  3066. 1:45:35not only can you use the update method
  3067. 1:45:38to add a new
  3068. 1:45:39key value pair but you can update an
  3069. 1:45:41existing one let's say we would like to
  3070. 1:45:43change the
  3071. 1:45:44capital of usa so capitals
  3072. 1:45:47dot update parentheses curly braces
  3073. 1:45:51list the key usa colon
  3074. 1:45:54and then we can give the usa a new value
  3075. 1:45:57let's pretend that the united states
  3076. 1:45:59government has decided to relocate
  3077. 1:46:01their capital to las vegas
  3078. 1:46:05so this will now update our key of usa
  3079. 1:46:08with a new value and if i were to print
  3080. 1:46:11all of the key value pairs found within
  3081. 1:46:14my dictionary of capitals the usa has a
  3082. 1:46:16new value
  3083. 1:46:17of las vegas so the last two methods i'm
  3084. 1:46:21going to cover are both pop
  3085. 1:46:22and clear you can use the pop method to
  3086. 1:46:25remove
  3087. 1:46:25a key value pair so capitals
  3088. 1:46:29dot pop parentheses and list the key
  3089. 1:46:32of the key value pair that you would
  3090. 1:46:34like to remove so let's say we would
  3091. 1:46:36like to remove china
  3092. 1:46:37so we'll list that key and then when we
  3093. 1:46:39use the pop method
  3094. 1:46:41that will remove this key value pair
  3095. 1:46:44from my dictionary
  3096. 1:46:45and then of course we have clear as well
  3097. 1:46:47which will just remove everything
  3098. 1:46:49capitals dot clear
  3099. 1:46:52and this will clear my dictionary all
  3100. 1:46:55right everybody
  3101. 1:46:56so in conclusion a dictionary is a
  3102. 1:46:58changeable
  3103. 1:46:59unordered collection of unique key value
  3104. 1:47:02pairs
  3105. 1:47:03they're fast because they use hashing
  3106. 1:47:05and they allow us to access a value
  3107. 1:47:07quickly
  3108. 1:47:08well that's the basics of dictionaries
  3109. 1:47:10in python
  3110. 1:47:11if you would like a copy of all this
  3111. 1:47:13code i will post all of this in the
  3112. 1:47:15comments down below
  3113. 1:47:16but yeah that's how dictionaries work in
  3114. 1:47:18python
  3115. 1:47:20hey what's going on everybody it's bro
  3116. 1:47:22hope you're doing well and in this video
  3117. 1:47:24i'm going to teach you guys all about
  3118. 1:47:25the index
  3119. 1:47:26operator in python so sit back relax
  3120. 1:47:29and enjoy the show
  3121. 1:47:32all right people let's begin i'm going
  3122. 1:47:34to be explaining the index operator
  3123. 1:47:36which is represented by
  3124. 1:47:38a set of square brackets now they give
  3125. 1:47:41access
  3126. 1:47:41to a sequences elements they include but
  3127. 1:47:44are not limited to
  3128. 1:47:46strings lists and tuples for this
  3129. 1:47:48example we're going to be working with
  3130. 1:47:50strings because strings are easy to work
  3131. 1:47:52with
  3132. 1:47:52let's say we have a name and name equals
  3133. 1:47:55whatever your first
  3134. 1:47:56and last name is we can use the index
  3135. 1:47:58operator to access
  3136. 1:48:00an element of the sequence our string
  3137. 1:48:03so let's check to see if the first
  3138. 1:48:05letter in our name
  3139. 1:48:07is lowercase and i should probably
  3140. 1:48:09change that to lowercase for this
  3141. 1:48:11example
  3142. 1:48:11so within an if statement if name and we
  3143. 1:48:14can add a set
  3144. 1:48:16of square brackets after our sequence
  3145. 1:48:19which is a string and we can check
  3146. 1:48:22a given element of our sequence let's
  3147. 1:48:25check to see
  3148. 1:48:26if the first letter is lowercase
  3149. 1:48:29now with computers they always start at
  3150. 1:48:31zero if you need to access the first
  3151. 1:48:33element
  3152. 1:48:33in a sequence that would be index zero
  3153. 1:48:36and then the next sequence well the next
  3154. 1:48:38element in the sequence would be one
  3155. 1:48:40then two then you followed that pattern
  3156. 1:48:42so i would like to check to see if
  3157. 1:48:44name at index zero that would be the
  3158. 1:48:47first character
  3159. 1:48:48is lowercase so there is a method to
  3160. 1:48:51check that
  3161. 1:48:52dot is lower this will return
  3162. 1:48:55true or false if the first letter is
  3163. 1:48:58lowercase
  3164. 1:48:59then our if statement is going to be
  3165. 1:49:00resolved to true
  3166. 1:49:03and what i could do is that i can
  3167. 1:49:06perhaps change it
  3168. 1:49:07to uppercase then so let's reassign
  3169. 1:49:10name to equal name dot
  3170. 1:49:14and there is a capitalize function here
  3171. 1:49:17and then let's print our name print
  3172. 1:49:21name so when we run this
  3173. 1:49:24the first letter in my name is now
  3174. 1:49:27capitalized
  3175. 1:49:28so if you need to access an element
  3176. 1:49:30within a sequence a string or
  3177. 1:49:32list or a tuple you would add a set
  3178. 1:49:36of square brackets afterwards and then
  3179. 1:49:38you list an integer
  3180. 1:49:39of the element that you're trying to
  3181. 1:49:41access so here's a few other examples
  3182. 1:49:43i'm going to turn
  3183. 1:49:44this as well as this into a comment
  3184. 1:49:47and let's say we would like to create
  3185. 1:49:49some substrings and we can do so
  3186. 1:49:51using our index operator so i would like
  3187. 1:49:54to
  3188. 1:49:54create a substring from the first part
  3189. 1:49:57of my name
  3190. 1:49:57so what i'll do is create a new variable
  3191. 1:50:00first
  3192. 1:50:01name equals name index operator
  3193. 1:50:05so you can specify a range for an index
  3194. 1:50:08operator
  3195. 1:50:09so you would set the starting position
  3196. 1:50:12on the left hand side followed by a
  3197. 1:50:14colon and then your ending position
  3198. 1:50:16so i would like the substring to begin
  3199. 1:50:18at index 0 and
  3200. 1:50:19end at 3 so that would be 0 colon
  3201. 1:50:22then three all right and then i would
  3202. 1:50:25like to make this all
  3203. 1:50:26uppercase for some reason and then let's
  3204. 1:50:29print my first name
  3205. 1:50:33so with the index operator we specified
  3206. 1:50:35a range
  3207. 1:50:36we would like to access elements 0
  3208. 1:50:39through
  3209. 1:50:393. turn them up your case and assign
  3210. 1:50:42them to a new
  3211. 1:50:42variable of first name here's a shortcut
  3212. 1:50:45that you might find useful
  3213. 1:50:46if the index for your range begins with
  3214. 1:50:49zero you can actually delete that you
  3215. 1:50:50don't even necessarily need that zero
  3216. 1:50:52now we just have a colon three and this
  3217. 1:50:55would work the same as it did before
  3218. 1:50:57so that's a useful shortcut and we're
  3219. 1:50:59going to bring it up later if we need to
  3220. 1:51:00access the last element in a sequence
  3221. 1:51:03this time let's create a
  3222. 1:51:05substring of last name so last
  3223. 1:51:09name equals name index operator
  3224. 1:51:13this time i would like to access the
  3225. 1:51:16element at index let's see 0 1
  3226. 1:51:202 3 4. all right so i'm going to say
  3227. 1:51:24for colon and we're not really sure
  3228. 1:51:27where our name our last name is going to
  3229. 1:51:29end
  3230. 1:51:30so what you could do is just say colon
  3231. 1:51:32and then leave
  3232. 1:51:33the next index blank so that will be
  3233. 1:51:36the index beginning at four and
  3234. 1:51:38everything afterwards
  3235. 1:51:40and let's say two lower to make this all
  3236. 1:51:42lowercase for some reason
  3237. 1:51:44so let's print our last name print
  3238. 1:51:48last name so this should
  3239. 1:51:52take everything starting at index 4
  3240. 1:51:56and after that and turn it into its own
  3241. 1:51:58substring of last name
  3242. 1:52:01now you can access the last element in a
  3243. 1:52:03sequence by using what's called
  3244. 1:52:05negative indexing so let's add
  3245. 1:52:08a exclamation point to the end of our
  3246. 1:52:11name so let's say
  3247. 1:52:13we have a new variable called last
  3248. 1:52:16character and i would like to store
  3249. 1:52:21the last character in my name to this
  3250. 1:52:23new
  3251. 1:52:24variable this new substring so to access
  3252. 1:52:27the last
  3253. 1:52:27element in a sequence within the index
  3254. 1:52:31operator we can use
  3255. 1:52:32negative indexing so the last element
  3256. 1:52:35would be
  3257. 1:52:36negative one all right and then let's
  3258. 1:52:38print
  3259. 1:52:39whatever our last character is so this
  3260. 1:52:42should print an exclamation point
  3261. 1:52:45yep which did so then if this was
  3262. 1:52:48negative 2
  3263. 1:52:48that would be the second to last element
  3264. 1:52:51in a sequence
  3265. 1:52:52so now that would be e because e is in
  3266. 1:52:55the second to last
  3267. 1:52:56element in my sequence all right
  3268. 1:52:59everybody so that's the basics of the
  3269. 1:53:01index operator
  3270. 1:53:02you can add a set of square brackets
  3271. 1:53:04after a string
  3272. 1:53:05a list or a tuple and then list an
  3273. 1:53:08integer or a range
  3274. 1:53:10of the elements that you're trying to
  3275. 1:53:11access so if you would like a copy of
  3276. 1:53:13all this code i will post everything in
  3277. 1:53:15the comments down below
  3278. 1:53:16don't be afraid to help me out and smash
  3279. 1:53:18that like button drop a comment down
  3280. 1:53:20below
  3281. 1:53:20and subscribe if you'd like to become a
  3282. 1:53:22fellow bro
  3283. 1:53:24hey what's going on everybody it's you
  3284. 1:53:26bro hope you're doing well
  3285. 1:53:27and in this video i'm going to explain
  3286. 1:53:28how functions work in python so
  3287. 1:53:31sit back relax and enjoy the show
  3288. 1:53:36if you find this video helpful please
  3289. 1:53:37remember to like
  3290. 1:53:39comment and subscribe your support will
  3291. 1:53:41help keep this channel running
  3292. 1:53:43okay let's do this thing a function
  3293. 1:53:46is a block of code which is executed
  3294. 1:53:49only when it is called
  3295. 1:53:50this process is also known as invoking a
  3296. 1:53:53function
  3297. 1:53:54with programming we try not to repeat
  3298. 1:53:56code if we don't have to
  3299. 1:53:57that's why functions are useful because
  3300. 1:53:59our function can perform
  3301. 1:54:01some specific task for us some block of
  3302. 1:54:03code for us
  3303. 1:54:04whenever it is called so we only need to
  3304. 1:54:06write that block of code
  3305. 1:54:08once and if we need to repeat it we just
  3306. 1:54:10call this function one more time
  3307. 1:54:12so let's define and create a function
  3308. 1:54:13we'll create a function called hello
  3309. 1:54:15and in order to define a function we'll
  3310. 1:54:17type def
  3311. 1:54:19and then a unique function name so let's
  3312. 1:54:21call this function hello
  3313. 1:54:23and with functions they always end with
  3314. 1:54:25a set of parentheses
  3315. 1:54:26and lastly to finish defining this
  3316. 1:54:29function we'll add
  3317. 1:54:30a colon at the end so if you go down to
  3318. 1:54:33the next line
  3319. 1:54:33take notice that my cursor is now
  3320. 1:54:36indented
  3321. 1:54:37any code underneath a function that is
  3322. 1:54:39indented
  3323. 1:54:40belongs to that specific function and
  3324. 1:54:42will only execute
  3325. 1:54:44whenever that function is called so for
  3326. 1:54:47now we need to type in something
  3327. 1:54:48if you don't know what you want your
  3328. 1:54:50function to do you can always type pass
  3329. 1:54:52for now
  3330. 1:54:52but let's print something print the word
  3331. 1:54:56hello and in order to call this function
  3332. 1:55:00all we need to do within our program is
  3333. 1:55:02type the name of the function which is
  3334. 1:55:04hello
  3335. 1:55:05and then add a set of parentheses
  3336. 1:55:07because functions always end with a set
  3337. 1:55:09of parentheses
  3338. 1:55:10so what this will do is execute my
  3339. 1:55:13function
  3340. 1:55:13once and perform this block of code
  3341. 1:55:15whenever it is called
  3342. 1:55:17if i were to remove this well our
  3343. 1:55:19program currently isn't going to do
  3344. 1:55:21anything
  3345. 1:55:21because functions only execute their
  3346. 1:55:24block of code
  3347. 1:55:25only when it is called so this time
  3348. 1:55:27let's print the word
  3349. 1:55:28hello three times so i will call this
  3350. 1:55:31function
  3351. 1:55:32three separate times hello hello hello
  3352. 1:55:35so when i run this our hello function
  3353. 1:55:38will be called
  3354. 1:55:39three separate times now with functions
  3355. 1:55:41they are not limited to just one line
  3356. 1:55:44this is an entire block of code so maybe
  3357. 1:55:46we can do something else too
  3358. 1:55:48let's print have a nice
  3359. 1:55:52day now if i were to run this my hello
  3360. 1:55:55function is going to execute
  3361. 1:55:56this entire block of code three separate
  3362. 1:55:59times hello
  3363. 1:56:00have a nice day now an important feature
  3364. 1:56:03of functions
  3365. 1:56:04is that we can send our function some
  3366. 1:56:06information
  3367. 1:56:07and our function can do something with
  3368. 1:56:08that information that it receives
  3369. 1:56:10let's say this time i would like to
  3370. 1:56:11print hello plus somebody's name
  3371. 1:56:14currently name is an unresolved
  3372. 1:56:16reference what we can do
  3373. 1:56:17when we call this function is send our
  3374. 1:56:19function some information
  3375. 1:56:21this can be a value a variable a
  3376. 1:56:23collection
  3377. 1:56:24all sorts of different things so if you
  3378. 1:56:26need to send your function some
  3379. 1:56:27information
  3380. 1:56:28within the parentheses when you call
  3381. 1:56:30that function just list the data that
  3382. 1:56:32you want to send your function
  3383. 1:56:34so let's say i would like to send my
  3384. 1:56:36function a string
  3385. 1:56:37value of bro whatever your first name is
  3386. 1:56:40when you send information
  3387. 1:56:41to a function these are called arguments
  3388. 1:56:45they are the information that you're
  3389. 1:56:46sending to a function
  3390. 1:56:48and when you define that function you
  3391. 1:56:50need a matching
  3392. 1:56:52set of what is known as parameters
  3393. 1:56:55so our hello function is going to
  3394. 1:56:57receive one argument a string value
  3395. 1:57:00we need a matching number of parameters
  3396. 1:57:03currently this isn't going to work you
  3397. 1:57:05can see here that hello
  3398. 1:57:06takes zero positional arguments but one
  3399. 1:57:09was given
  3400. 1:57:10our function needs a matching number of
  3401. 1:57:13parameters
  3402. 1:57:14so think of it this way when we receive
  3403. 1:57:17this value
  3404. 1:57:17we're going to give a nickname to this
  3405. 1:57:20value
  3406. 1:57:21so when we receive the string value what
  3407. 1:57:24do we want to call it
  3408. 1:57:25well let's call it name so in order to
  3409. 1:57:27add parameters
  3410. 1:57:29to your function just list the
  3411. 1:57:31parameters within
  3412. 1:57:32the parentheses of that function and
  3413. 1:57:35well this should work now
  3414. 1:57:36we have a matching set of arguments and
  3415. 1:57:40parameters when we call the hello
  3416. 1:57:42function this time we are sending
  3417. 1:57:44one argument over and when our hello
  3418. 1:57:46function
  3419. 1:57:47receives this argument we're going to
  3420. 1:57:49give it a temporary nickname of
  3421. 1:57:52name or you can call this whatever you
  3422. 1:57:53want to and then we can use
  3423. 1:57:55this value for whatever we want within
  3424. 1:57:58our function
  3425. 1:57:58now if we were to run this this will
  3426. 1:58:00print hello bro
  3427. 1:58:02have a nice day now i can send maybe a
  3428. 1:58:04different value
  3429. 1:58:05let's say hello and then i will send a
  3430. 1:58:09string value of dude
  3431. 1:58:11hello dude have a nice day so this isn't
  3432. 1:58:14limited to
  3433. 1:58:15just values we can send variables to
  3434. 1:58:18let's say we have a
  3435. 1:58:19variable called name equals
  3436. 1:58:23bro and this time i'm going to send
  3437. 1:58:26this variable over hello and then send
  3438. 1:58:30name so these don't need to be the
  3439. 1:58:34exact same let's say this is
  3440. 1:58:37my name and i'm going to send
  3441. 1:58:40my name over and i will temporarily give
  3442. 1:58:45this value a nickname of just name
  3443. 1:58:48and then i can use this variable for
  3444. 1:58:50something hello bro
  3445. 1:58:52have a nice day now with arguments you
  3446. 1:58:55can send
  3447. 1:58:55more than one value over let's say this
  3448. 1:58:58time i would like to send
  3449. 1:58:59two values over so let's send pro
  3450. 1:59:02maybe for a first name and code for a
  3451. 1:59:05second name but
  3452. 1:59:06you can use your first name and last
  3453. 1:59:08name so this isn't going to work because
  3454. 1:59:10we do not have a matching set
  3455. 1:59:13of arguments and parameters we're
  3456. 1:59:15sending
  3457. 1:59:16two arguments over but our function is
  3458. 1:59:19only designed
  3459. 1:59:20to accept one argument because we have
  3460. 1:59:23one parameter so let's rename
  3461. 1:59:26name as first name and we need
  3462. 1:59:30two parameters this time so what are we
  3463. 1:59:32going to call
  3464. 1:59:33this value when we receive it let's call
  3465. 1:59:35this last name
  3466. 1:59:38so this time we are going to receive two
  3467. 1:59:40values
  3468. 1:59:42they will be nicknamed first name and
  3469. 1:59:44last name so this time
  3470. 1:59:45let's print hello first name
  3471. 1:59:48maybe i'll add a space plus
  3472. 1:59:52last name and this is now valid
  3473. 1:59:56hello bro code have a nice day with
  3474. 1:59:59arguments
  3475. 1:59:59you can mix and match the data types
  3476. 2:00:02that you're sending
  3477. 2:00:03as arguments so currently we're sending
  3478. 2:00:05two
  3479. 2:00:06string values as arguments this time
  3480. 2:00:08let's send an integer value
  3481. 2:00:10let's say we would like to send an edge
  3482. 2:00:13over to our hello function
  3483. 2:00:15so i'll just send maybe the value of 21.
  3484. 2:00:18so currently this isn't going to work
  3485. 2:00:20because the hello function takes two
  3486. 2:00:22positional arguments
  3487. 2:00:23but three were given when we call this
  3488. 2:00:26hello function we're sending
  3489. 2:00:27this argument over but our function
  3490. 2:00:30doesn't know what to do with this
  3491. 2:00:31argument
  3492. 2:00:32so we need a matching parameter for this
  3493. 2:00:34value that it receives
  3494. 2:00:36so let's call this value maybe edge as a
  3495. 2:00:39temporary nickname
  3496. 2:00:40and then we can do something with this
  3497. 2:00:42value when we receive it
  3498. 2:00:43so let's print an additional message
  3499. 2:00:47perhaps u r plus
  3500. 2:00:50age plus years
  3501. 2:00:54old so if you need to display an
  3502. 2:00:57integer or number along with the string
  3503. 2:00:59you do need to convert it to a string
  3504. 2:01:01so let's just cast our edge as a string
  3505. 2:01:04when we display this
  3506. 2:01:06so this should work now we now have a
  3507. 2:01:08matching number
  3508. 2:01:09of arguments and parameters so this will
  3509. 2:01:13display
  3510. 2:01:14hello bro code you are 21 years old have
  3511. 2:01:16a nice day
  3512. 2:01:17so ladies and gentlemen in conclusion a
  3513. 2:01:20function
  3514. 2:01:20is a block of code which is executed
  3515. 2:01:23only when it is called
  3516. 2:01:24if i were to remove this function call
  3517. 2:01:27well then
  3518. 2:01:28our program is just going to completely
  3519. 2:01:30ignore this function and
  3520. 2:01:31this block of code would not be executed
  3521. 2:01:34when we call this function we can pass
  3522. 2:01:36information known as arguments but we
  3523. 2:01:39need a
  3524. 2:01:40matching number of parameters set up to
  3525. 2:01:42receive
  3526. 2:01:43these arguments there are a few advanced
  3527. 2:01:46features of parameters but
  3528. 2:01:48i'll hopefully get to that in future
  3529. 2:01:50videos we'll just stick with the basics
  3530. 2:01:51for now of functions
  3531. 2:01:53so that's the basics of functions if you
  3532. 2:01:55would like a copy of this code
  3533. 2:01:57i will post this in the comments down
  3534. 2:01:59below but yeah that is how functions
  3535. 2:02:01work in python hey what's going on
  3536. 2:02:04everybody it's you bro hope you're doing
  3537. 2:02:06well
  3538. 2:02:06and in this video i'm going to explain
  3539. 2:02:08the return statement in python so
  3540. 2:02:10sit back relax and enjoy the show
  3541. 2:02:15all right people let's begin the return
  3542. 2:02:18statement is used within functions
  3543. 2:02:19to send python values or objects back to
  3544. 2:02:23the caller
  3545. 2:02:23these values or objects are known as the
  3546. 2:02:26function's
  3547. 2:02:26return value so do you know how with
  3548. 2:02:29functions we can pass
  3549. 2:02:30arguments to a function and then our
  3550. 2:02:32function can do something with those
  3551. 2:02:34arguments
  3552. 2:02:34well not only that but our function can
  3553. 2:02:36pass some value or
  3554. 2:02:38object back to the caller so what we'll
  3555. 2:02:41do for this example is create
  3556. 2:02:43a function that will simply multiply two
  3557. 2:02:45numbers together
  3558. 2:02:46and return the result back to the caller
  3559. 2:02:49so let's define
  3560. 2:02:50a function called multiply and we'll set
  3561. 2:02:52up two parameters for this function
  3562. 2:02:54let's say number one and number two
  3563. 2:02:58and what we'll do is say result
  3564. 2:03:01equals number one times number two
  3565. 2:03:05and then at the end we can return
  3566. 2:03:07something what do we want to return
  3567. 2:03:08let's return whatever the result is now
  3568. 2:03:11when we call this function we will pass
  3569. 2:03:13in two values as arguments let's say we
  3570. 2:03:16would like to multiply
  3571. 2:03:17six times eight so when we run this well
  3572. 2:03:20we can't actually see anything
  3573. 2:03:22when we return the result after
  3574. 2:03:24multiplying numbers
  3575. 2:03:25one and two it's going to return the
  3576. 2:03:27result back to the caller
  3577. 2:03:29in order to see the value that is
  3578. 2:03:31returned back to the caller
  3579. 2:03:32one way in which we can do this is to
  3580. 2:03:35print
  3581. 2:03:35the value that is returned within a
  3582. 2:03:38print statement
  3583. 2:03:39so what we'll do is print the value that
  3584. 2:03:41is returned after calling the multiply
  3585. 2:03:44function
  3586. 2:03:44so 6 times 8 is really great 6 times 8
  3587. 2:03:47is 48
  3588. 2:03:48now one other thing that we can do is
  3589. 2:03:51store the returned value within a
  3590. 2:03:53variable
  3591. 2:03:54so let's say x equals multiply
  3592. 2:03:586 times 8 and then we will print
  3593. 2:04:00whatever x is
  3594. 2:04:02so the result is still the same of 48
  3595. 2:04:05now with the return value there is
  3596. 2:04:07another way in which we could write this
  3597. 2:04:09that uses
  3598. 2:04:10less lines of code so we'll delete this
  3599. 2:04:12first line and instead of returning
  3600. 2:04:14a variable called result what we'll do
  3601. 2:04:16is just return
  3602. 2:04:18number one times number two and this
  3603. 2:04:20would work the same as it does before
  3604. 2:04:23but it's written in less lines of code
  3605. 2:04:25so that's another way in which you could
  3606. 2:04:26write this
  3607. 2:04:27all right everybody so that is the
  3608. 2:04:29return statement functions can send
  3609. 2:04:31python values or objects back to the
  3610. 2:04:34caller
  3611. 2:04:34these values or objects are known as the
  3612. 2:04:36functions return
  3613. 2:04:38value you can either print this value
  3614. 2:04:40directly to the console window
  3615. 2:04:42or you can store it within a variable or
  3616. 2:04:44some other location
  3617. 2:04:46and well ladies and gentlemen that is
  3618. 2:04:48how the return statement works in python
  3619. 2:04:51what's going on everybody it's bro hope
  3620. 2:04:53you're doing well
  3621. 2:04:54and in this video i'm going to explain
  3622. 2:04:56keyword arguments in python so
  3623. 2:04:58sit back relax and enjoy the show
  3624. 2:05:02all right everybody let's discuss
  3625. 2:05:05keyword arguments
  3626. 2:05:06these are arguments that are preceded by
  3627. 2:05:09an identifier when we pass them to a
  3628. 2:05:11function
  3629. 2:05:12the order of the arguments doesn't
  3630. 2:05:14matter unlike positional arguments
  3631. 2:05:16which do matter and we've been working
  3632. 2:05:18with positional arguments already
  3633. 2:05:20and python knows the names of the
  3634. 2:05:21arguments that our function receives
  3635. 2:05:24when we use keyword arguments here's an
  3636. 2:05:26example of us
  3637. 2:05:27using positional arguments and then i'll
  3638. 2:05:29demonstrate the benefits of
  3639. 2:05:30keyword arguments let's say we have a
  3640. 2:05:32function called hello
  3641. 2:05:34that accepts three arguments a first
  3642. 2:05:36name a middle name and a last name
  3643. 2:05:38and the output will be hello first name
  3644. 2:05:40middle name last name
  3645. 2:05:41so if i were to run this this will
  3646. 2:05:43display hello bro
  3647. 2:05:45dude code so we're using positional
  3648. 2:05:47arguments
  3649. 2:05:48the order of the arguments does matter
  3650. 2:05:50what do you think's gonna happen if i
  3651. 2:05:52were to replace some of these values
  3652. 2:05:54let's say i will swap the first argument
  3653. 2:05:56with the
  3654. 2:05:57third argument so let's try it again
  3655. 2:06:01hello code dude bro so obviously the
  3656. 2:06:04order of the arguments matters
  3657. 2:06:05and now if we were to use keyword
  3658. 2:06:07arguments then the order of the
  3659. 2:06:09arguments
  3660. 2:06:10doesn't matter but with each argument we
  3661. 2:06:12need to precede each argument with
  3662. 2:06:15a unique identifier and that identifier
  3663. 2:06:18is the name of the parameter we want to
  3664. 2:06:20associate each argument with
  3665. 2:06:22so for my intended last name i will use
  3666. 2:06:25an identifier of
  3667. 2:06:26last to match this parameter so
  3668. 2:06:29dude will be middle middle equals dude
  3669. 2:06:32and bro
  3670. 2:06:33will be first even though they're not in
  3671. 2:06:35the correct order so let's try this
  3672. 2:06:36again
  3673. 2:06:37and my output is hello bro dude code
  3674. 2:06:40so that's all about keyword arguments
  3675. 2:06:43they are arguments
  3676. 2:06:44preceded with an identifier when we pass
  3677. 2:06:46them to a function
  3678. 2:06:48the order of the arguments doesn't
  3679. 2:06:49matter unlike positional arguments
  3680. 2:06:51which do matter and then python knows
  3681. 2:06:53the names of the arguments
  3682. 2:06:54that our function receives when we use
  3683. 2:06:56keyword arguments
  3684. 2:06:58so that's it for keyword arguments if
  3685. 2:07:00you would like a copy of this code
  3686. 2:07:02i will post this in the comment section
  3687. 2:07:03down below but yeah that's how keyword
  3688. 2:07:06arguments
  3689. 2:07:06work in python hey what's going on
  3690. 2:07:10everybody it's you bro hope you're doing
  3691. 2:07:12well and in this video i'm going to
  3692. 2:07:13explain
  3693. 2:07:14nested function calls in python so sit
  3694. 2:07:16back
  3695. 2:07:17relax and enjoy the show
  3696. 2:07:21all right people nested function calls
  3697. 2:07:24these are function calls
  3698. 2:07:25inside of other function calls and this
  3699. 2:07:27is possible because
  3700. 2:07:29certain functions will return a value
  3701. 2:07:31and we can immediately use that value as
  3702. 2:07:33an argument for the next function
  3703. 2:07:35so here's an example let's say that we
  3704. 2:07:37have the small program
  3705. 2:07:39where we will ask a user to type in a
  3706. 2:07:41whole positive number
  3707. 2:07:43and let's say i don't let's say i type
  3708. 2:07:45in negative 3.14
  3709. 2:07:47and then i'm storing that value within a
  3710. 2:07:50variable
  3711. 2:07:50named num what i'm going to do is
  3712. 2:07:52convert that number
  3713. 2:07:54to a floating point number because
  3714. 2:07:55currently it's a string when you accept
  3715. 2:07:57user input
  3716. 2:07:58i will find the absolute value of num
  3717. 2:08:01and then round it to the nearest whole
  3718. 2:08:03number
  3719. 2:08:03and then print the value so we can
  3720. 2:08:05actually do this using less lines of
  3721. 2:08:07code with nested function calls
  3722. 2:08:09so let's start with the first function
  3723. 2:08:11which will be accepting some user input
  3724. 2:08:13the input function so this will be in
  3725. 2:08:16the very center
  3726. 2:08:17of our nested function calls and then
  3727. 2:08:19the next function we would like to apply
  3728. 2:08:21to
  3729. 2:08:22the variable that is returned the value
  3730. 2:08:24that is returned
  3731. 2:08:25we will pass in as an argument to our
  3732. 2:08:27next function of casting
  3733. 2:08:29this data type to a float so we'll
  3734. 2:08:31surround
  3735. 2:08:32our function with another function and
  3736. 2:08:34use
  3737. 2:08:35the float function and then we'll just
  3738. 2:08:38keep on adding layers to this
  3739. 2:08:39so we have the absolute value function
  3740. 2:08:42next
  3741. 2:08:42so we'll add another layer and we have
  3742. 2:08:45the round function
  3743. 2:08:47so we'll add a layer to that and
  3744. 2:08:50lastly we're going to print and that'll
  3745. 2:08:52be the outermost layer
  3746. 2:08:55so this program will do the exact same
  3747. 2:08:57thing
  3748. 2:08:58let's say negative 3.14 except we've
  3749. 2:09:01written this with
  3750. 2:09:02one line of code so basically we start
  3751. 2:09:05with the innermost function and resolve
  3752. 2:09:07that first
  3753. 2:09:08whatever value is returned we use as an
  3754. 2:09:10argument to the next outermost function
  3755. 2:09:13and in this case it would be float then
  3756. 2:09:15we resolve that then move on to absolute
  3757. 2:09:18value then round and then print
  3758. 2:09:20so it's just another way of writing code
  3759. 2:09:22that takes up less
  3760. 2:09:23lines but this is more of an extreme
  3761. 2:09:25example you probably won't see
  3762. 2:09:27like four or five nested functions well
  3763. 2:09:29everybody those are nested function
  3764. 2:09:31calls
  3765. 2:09:32if you would like a copy of this code i
  3766. 2:09:34will post all of this to the comment
  3767. 2:09:35section down below
  3768. 2:09:36but yeah those are nested function calls
  3769. 2:09:39in python
  3770. 2:09:40hey what's going on people it's your bro
  3771. 2:09:42hope you're doing well and in this video
  3772. 2:09:44i'm going to show you how variable scope
  3773. 2:09:46works in python so
  3774. 2:09:47sit back relax and enjoy the show
  3775. 2:09:52all right let's do a thing people let's
  3776. 2:09:54discuss the scope of a variable
  3777. 2:09:56now the scope of a variable is the
  3778. 2:09:58region that a variable
  3779. 2:10:00is recognized a variable is only
  3780. 2:10:02available from inside the region
  3781. 2:10:05that it is created let's say that we
  3782. 2:10:06have a function and i just gave this
  3783. 2:10:08function a name of display name
  3784. 2:10:10i created a variable inside of this
  3785. 2:10:12function named name
  3786. 2:10:14and you can assign this a value of
  3787. 2:10:16whatever your name is
  3788. 2:10:17preferably a last name for this example
  3789. 2:10:19and what i'll do is just print
  3790. 2:10:21whatever my name is so this variable is
  3791. 2:10:24known to have
  3792. 2:10:25a local scope because it's declared
  3793. 2:10:28inside of a function
  3794. 2:10:29and a variable is only recognized inside
  3795. 2:10:32of the region
  3796. 2:10:33that it is created this variable of name
  3797. 2:10:36since it's created inside
  3798. 2:10:37of this function it is known as a local
  3799. 2:10:40variable
  3800. 2:10:41that has a local scope it's only
  3801. 2:10:44available
  3802. 2:10:44inside of this function that it's
  3803. 2:10:46created now let's attempt to access this
  3804. 2:10:49variable from
  3805. 2:10:50outside of our function so i'm going to
  3806. 2:10:52print my
  3807. 2:10:53name variable but we'll have an issue
  3808. 2:10:55though
  3809. 2:10:56so name name is not defined so
  3810. 2:10:59local variables are declared inside of a
  3811. 2:11:02function
  3812. 2:11:03and they have a local scope they are
  3813. 2:11:05only
  3814. 2:11:06available inside of this function in
  3815. 2:11:07which they're created
  3816. 2:11:09on the other hand a global variable is a
  3817. 2:11:12variable that is declared
  3818. 2:11:14outside of any function but within your
  3819. 2:11:16module that you're working with
  3820. 2:11:17a global variable is known to have a
  3821. 2:11:20global scope
  3822. 2:11:21they are available inside and outside of
  3823. 2:11:23any functions
  3824. 2:11:24now if i was to print my name variable
  3825. 2:11:27this is going to print
  3826. 2:11:29bro or whatever your first name is and
  3827. 2:11:31it is entirely possible to have both
  3828. 2:11:33a global and local version of the same
  3829. 2:11:35named variable
  3830. 2:11:37so we have both a global version of name
  3831. 2:11:39as well as a local version of name
  3832. 2:11:42if i was to print my name variable from
  3833. 2:11:45outside of this function
  3834. 2:11:46it's going to print my global version
  3835. 2:11:48however if i print
  3836. 2:11:50my version of name inside of that
  3837. 2:11:52function in which it's created
  3838. 2:11:54we're going to first use the local
  3839. 2:11:56version before the
  3840. 2:11:57global version of name and let's try
  3841. 2:11:59that so i'm going to call
  3842. 2:12:01my display name function
  3843. 2:12:04and this will print my last name
  3844. 2:12:08code and then my first name of bro so it
  3845. 2:12:11is possible to have a global
  3846. 2:12:12and local version of the same named
  3847. 2:12:15variable
  3848. 2:12:16now if i was to remove this line where
  3849. 2:12:18we declare a local version
  3850. 2:12:20of name well then the next best
  3851. 2:12:22available name to us would be
  3852. 2:12:24the global version of name so if you use
  3853. 2:12:26a variable inside of a function
  3854. 2:12:28you'll first use a local version of that
  3855. 2:12:31variable
  3856. 2:12:32if it's available if not you'll use a
  3857. 2:12:34global version instead
  3858. 2:12:36and python will follow this rule known
  3859. 2:12:38as the legb
  3860. 2:12:40rule you use any local variables first
  3861. 2:12:42then
  3862. 2:12:43enclosed variables then global and then
  3863. 2:12:45any built-in variables in that order
  3864. 2:12:48so that's the main difference between
  3865. 2:12:50local scope and
  3866. 2:12:51global scope the scope of a variable is
  3867. 2:12:54the region in which it's available
  3868. 2:12:56and it's limited to the region in which
  3869. 2:12:58it's created a local scope
  3870. 2:13:00is available only inside of the function
  3871. 2:13:02in which that variable is created
  3872. 2:13:04a global scoped variable is a variable
  3873. 2:13:07that is available
  3874. 2:13:08inside and outside of any functions but
  3875. 2:13:10within your module that you're working
  3876. 2:13:12with
  3877. 2:13:13so that's variable scope in python if
  3878. 2:13:15you would like a copy of this code i
  3879. 2:13:17will post this code to the comment
  3880. 2:13:18section down below
  3881. 2:13:19and well yeah that's how variable scope
  3882. 2:13:22works in python
  3883. 2:13:24hey what's going on everybody it's bro
  3884. 2:13:26hope you're doing well
  3885. 2:13:27and in this video i'm going to explain
  3886. 2:13:29the arcs parameter in python so sit back
  3887. 2:13:32relax and enjoy the show
  3888. 2:13:36all right guys and gals let's talk about
  3889. 2:13:38args
  3890. 2:13:39now args is a parameter that will pack
  3891. 2:13:42all arguments into a tuple
  3892. 2:13:44it's useful so that a function can
  3893. 2:13:45accept a varying amount of arguments
  3894. 2:13:48here's an example of why it would be
  3895. 2:13:50useful let's say we have a function
  3896. 2:13:52called add that will accept two numbers
  3897. 2:13:54as arguments
  3898. 2:13:55and add them together and return the sum
  3899. 2:13:57so this works as intended right as long
  3900. 2:13:59as we pass in
  3901. 2:14:00only two numbers but what if we need to
  3902. 2:14:02pass in three numbers as arguments
  3903. 2:14:04well we can no longer use this add
  3904. 2:14:06function because we have two parameters
  3905. 2:14:08set up
  3906. 2:14:08but we're passing in three arguments you
  3907. 2:14:10can see here within my console window
  3908. 2:14:12that we have a type error
  3909. 2:14:14add takes two positional arguments but
  3910. 2:14:16three were given
  3911. 2:14:17and one way in which we can solve this
  3912. 2:14:19issue is to use this
  3913. 2:14:20args parameter so what we're going to do
  3914. 2:14:22is replace all of our parameters
  3915. 2:14:24with asterisk args and actually
  3916. 2:14:27the args portion isn't as important as
  3917. 2:14:30the asterisk
  3918. 2:14:31args is short for arguments and you can
  3919. 2:14:32really name this whatever you want just
  3920. 2:14:34be sure that you have this asterisk
  3921. 2:14:36because with this asterisk we're doing a
  3922. 2:14:38form of packing
  3923. 2:14:39what we're doing is passing all of these
  3924. 2:14:41arguments and packing them
  3925. 2:14:43into a tuple so let's rewrite this
  3926. 2:14:45program so let's say maybe sum
  3927. 2:14:47equals zero and then we will need to
  3928. 2:14:51iterate through all of the items within
  3929. 2:14:53our tuple because
  3930. 2:14:54tuples are iterable so let's say for i
  3931. 2:14:57in args what we'll do is say sum
  3932. 2:15:00plus equals whatever i is and at the end
  3933. 2:15:03when we escape the for loop we will
  3934. 2:15:05return
  3935. 2:15:06our sum so let's try it again and see if
  3936. 2:15:08it works
  3937. 2:15:09well would you look at that it works
  3938. 2:15:11let's pass in a few other arguments for
  3939. 2:15:13good measure
  3940. 2:15:14let's pass in six arguments this time
  3941. 2:15:16all right look at that
  3942. 2:15:17this does in fact work so you can
  3943. 2:15:19actually name this whatever you want the
  3944. 2:15:20important thing is to have this asterisk
  3945. 2:15:23so let's rename this as i don't know
  3946. 2:15:25stuff
  3947. 2:15:26and let's try this again yep
  3948. 2:15:29it still works with tuples they are
  3949. 2:15:32ordered
  3950. 2:15:32and unchangeable let's attempt to edit
  3951. 2:15:34one of these values
  3952. 2:15:36so let's say stuff at
  3953. 2:15:39index zero is equal to
  3954. 2:15:42zero and let's try and change it so you
  3955. 2:15:45can see that tuple object does not
  3956. 2:15:47support
  3957. 2:15:48item assignment if you need to change
  3958. 2:15:50one of the values
  3959. 2:15:51within your tuple after you pack all of
  3960. 2:15:54these arguments
  3961. 2:15:55you might need to convert this to a
  3962. 2:15:56different collection one way in which we
  3963. 2:15:58can do that is to cast it
  3964. 2:16:00let's say stuff equals and we'll cast
  3965. 2:16:03our tuple as a list because a list
  3966. 2:16:06can be changeable it's mutable so let's
  3967. 2:16:09attempt this again we'll cast
  3968. 2:16:11our tuple as a list and then edit one of
  3969. 2:16:13the values
  3970. 2:16:14and then add all the values together so
  3971. 2:16:16to change one of the values
  3972. 2:16:18say the name your collection stuff in
  3973. 2:16:20this case let's say at index zero
  3974. 2:16:22this now equals zero so now if we were
  3975. 2:16:25to add all these items together
  3976. 2:16:26they should equal 20. there we go so
  3977. 2:16:29yeah that's basically the args parameter
  3978. 2:16:32the name isn't as important as the
  3979. 2:16:34asterisk because you can really name it
  3980. 2:16:36whatever you want
  3981. 2:16:37a common convention is to just name this
  3982. 2:16:39args it's a parameter that will pack
  3983. 2:16:41all arguments into a tuple it's useful
  3984. 2:16:44so that a function can accept
  3985. 2:16:46a varying amount of arguments well that
  3986. 2:16:48is all about args
  3987. 2:16:50if you would like a copy of this code i
  3988. 2:16:51will post this in the comments section
  3989. 2:16:53down below
  3990. 2:16:54but yeah that is how the args parameter
  3991. 2:16:56works in python
  3992. 2:16:58hey what's going on everybody it's bro
  3993. 2:17:00hope you're doing well and in this video
  3994. 2:17:02i'm going to be explaining quarks
  3995. 2:17:04in python so sit back relax
  3996. 2:17:07and enjoy the show
  3997. 2:17:10all right everybody let's talk about
  3998. 2:17:12quarkx now this is a parameter that will
  3999. 2:17:14pack
  4000. 2:17:15all arguments into a dictionary it's
  4001. 2:17:17useful so that a function can accept a
  4002. 2:17:20varying amount of keyword arguments it's
  4003. 2:17:22identical
  4004. 2:17:23to args except with args args will
  4005. 2:17:26accept a varying amount of positional
  4006. 2:17:28arguments
  4007. 2:17:29and pack them into a tuple with quarks
  4008. 2:17:31this will accept a varying amount of
  4009. 2:17:33keyword arguments
  4010. 2:17:34and pack them into a dictionary so
  4011. 2:17:36here's an example of why this would be
  4012. 2:17:38useful
  4013. 2:17:38let's say we have a function called
  4014. 2:17:40hello that accepts two keyword arguments
  4015. 2:17:43a first name
  4016. 2:17:44and a last name all this will do is
  4017. 2:17:46output hello
  4018. 2:17:47your first name and last name but what
  4019. 2:17:49if somebody has more than two names
  4020. 2:17:51let's say somebody has a middle name
  4021. 2:17:53and for this example i'll just say my
  4022. 2:17:54middle name is dude
  4023. 2:17:56so if i were to run this well this hello
  4024. 2:17:59function is no longer going to work
  4025. 2:18:01as you can see in the console window we
  4026. 2:18:03have a type error
  4027. 2:18:04the hello function got an unexpected
  4028. 2:18:06keyword argument middle
  4029. 2:18:08one way in which we can solve this we
  4030. 2:18:10can use this quark's
  4031. 2:18:11parameter so we can accept a varying
  4032. 2:18:14amount
  4033. 2:18:15of keyword arguments so what we'll do is
  4034. 2:18:17replace
  4035. 2:18:18our parameters with two asterisks then
  4036. 2:18:21quarks now if i would like the same
  4037. 2:18:23output as i had before i'm going to need
  4038. 2:18:25to change a few things around
  4039. 2:18:26in order to access a value within a
  4040. 2:18:29dictionary you type the name of the
  4041. 2:18:30dictionary
  4042. 2:18:31square brackets and then within quotes
  4043. 2:18:34list your key
  4044. 2:18:36so i would like to access the value at
  4045. 2:18:38this key of first
  4046. 2:18:40and let's repeat the process for last
  4047. 2:18:42name
  4048. 2:18:44so the key is going to be last we'll
  4049. 2:18:46access this value
  4050. 2:18:48at this key and this should work the
  4051. 2:18:50same as it did before
  4052. 2:18:52now let's take this a step further let's
  4053. 2:18:54say we would like to display
  4054. 2:18:55somebody's full name based on the amount
  4055. 2:18:58of keyword arguments that they pass into
  4056. 2:19:00this function
  4057. 2:19:01well there's a few different ways in
  4058. 2:19:02which we could do this one easy way is
  4059. 2:19:05that we could write our program like
  4060. 2:19:06this
  4061. 2:19:08let's print the word hello
  4062. 2:19:11and we will need to iterate once through
  4063. 2:19:14each key value pair within this
  4064. 2:19:16dictionary
  4065. 2:19:17and we can use a for loop to do that so
  4066. 2:19:19to iterate
  4067. 2:19:20once through each key value pair in your
  4068. 2:19:22dictionary
  4069. 2:19:23we'll type for key value
  4070. 2:19:26in the name of our dictionary quarks in
  4071. 2:19:28this case
  4072. 2:19:29dot items and then we will print
  4073. 2:19:33each value
  4074. 2:19:37so there's one more thing that we need
  4075. 2:19:38to do this kind of works as intended but
  4076. 2:19:40it's going to
  4077. 2:19:41print each value including our word
  4078. 2:19:44hello
  4079. 2:19:44on a new line so with a standard print
  4080. 2:19:48statement
  4081. 2:19:48this will print a new line character
  4082. 2:19:50after each print statement as if we're
  4083. 2:19:52hitting enter
  4084. 2:19:53we can actually change that so after
  4085. 2:19:56your string
  4086. 2:19:56add and equals and we can replace that
  4087. 2:19:59new line character with something else
  4088. 2:20:01maybe a space
  4089. 2:20:02and let's do the same thing for our for
  4090. 2:20:04loop so value
  4091. 2:20:06comma end and then just add space now
  4092. 2:20:08this will print
  4093. 2:20:09all of your names on the same line hello
  4094. 2:20:12bro
  4095. 2:20:13dude code and now we can pass in a
  4096. 2:20:15varying amount of keyword arguments
  4097. 2:20:17based on how long our name is
  4098. 2:20:19perhaps we have like a title title
  4099. 2:20:22equals
  4100. 2:20:23mr and we can print that as well
  4101. 2:20:26hello mr bro dude code here's a fun
  4102. 2:20:30fact of the day you don't necessarily
  4103. 2:20:31need to name this parameter as quarks
  4104. 2:20:34the important thing is to have
  4105. 2:20:35these double asterisks that precede it
  4106. 2:20:37quarks is just a
  4107. 2:20:39common naming convention it's short for
  4108. 2:20:41keyword arguments
  4109. 2:20:42you could name this as something else
  4110. 2:20:44perhaps something more descriptive like
  4111. 2:20:45names since we're passing in a bunch of
  4112. 2:20:47names so the name of our dictionary
  4113. 2:20:49is now names and this would work the
  4114. 2:20:52same as it did before
  4115. 2:20:53the important thing is to have these
  4116. 2:20:55double asterisks that precede the name
  4117. 2:20:57of your dictionary
  4118. 2:20:58but quarks is the common naming
  4119. 2:21:00convention
  4120. 2:21:01well everybody that's all about quarkx
  4121. 2:21:03it's a parameter that will pack
  4122. 2:21:05a varying amount of keyword arguments
  4123. 2:21:07into a
  4124. 2:21:08dictionary if you would like a copy of
  4125. 2:21:10all this code i will post
  4126. 2:21:12this in the comments section down below
  4127. 2:21:13but yeah that is how quarkx
  4128. 2:21:15works in python hey what's going on
  4129. 2:21:18everybody
  4130. 2:21:19it's your bro hope you're doing well and
  4131. 2:21:20in this video i'm going to teach you
  4132. 2:21:22guys all about the format method
  4133. 2:21:23in python so sit back relax and enjoy
  4134. 2:21:27the show
  4135. 2:21:29okay ladies and gentlemen let's begin
  4136. 2:21:32the format method
  4137. 2:21:33is a method available to strings it's
  4138. 2:21:35optional and it gives users
  4139. 2:21:37more control when displaying output to
  4140. 2:21:40best demonstrate this i'm going to use a
  4141. 2:21:42line
  4142. 2:21:42from a popular nursery rhyme titled hey
  4143. 2:21:45diddle diddle because well
  4144. 2:21:47you know i'm a mature adult like that
  4145. 2:21:49i'll be printing
  4146. 2:21:50this line to my console window the cow
  4147. 2:21:53jumped over
  4148. 2:21:54the moon let's do so using a standard
  4149. 2:21:56print statement
  4150. 2:21:57and then i'll show you the benefits of
  4151. 2:21:59using the format method later
  4152. 2:22:01for this example i have two variables a
  4153. 2:22:04variable called animal and i'm going to
  4154. 2:22:05store
  4155. 2:22:06a string of cow and item and i'll be
  4156. 2:22:09storing an item name
  4157. 2:22:10which is moon when we compile and run
  4158. 2:22:12this as you would expect
  4159. 2:22:14the output is the cow jumped over the
  4160. 2:22:16moon
  4161. 2:22:17there's a more elegant way of writing
  4162. 2:22:19this line of code
  4163. 2:22:20and that is by using the format method
  4164. 2:22:22available to strings
  4165. 2:22:24i'm going to turn this line into a
  4166. 2:22:26comment and we're instead
  4167. 2:22:27going to produce the same output but use
  4168. 2:22:29the format method
  4169. 2:22:31instead so we need a standard print
  4170. 2:22:33statement like usual
  4171. 2:22:34and then a string that we would like to
  4172. 2:22:36format so
  4173. 2:22:37we're going to write the same thing but
  4174. 2:22:39in place of the variables we're going to
  4175. 2:22:41add a placeholder
  4176. 2:22:42the instead of the variable name or
  4177. 2:22:45value name i'm going to add
  4178. 2:22:47a set of curly braces so that functions
  4179. 2:22:50as a placeholder
  4180. 2:22:51for a value or a variable the
  4181. 2:22:55this should be cow jumped over
  4182. 2:22:59the and i'm going to insert another
  4183. 2:23:01placeholder
  4184. 2:23:02now this is a string we can format the
  4185. 2:23:04string by following
  4186. 2:23:06this string with dot format parenthesis
  4187. 2:23:10and within the parentheses of the format
  4188. 2:23:12method we can insert
  4189. 2:23:14some values or variables let's begin
  4190. 2:23:16with some
  4191. 2:23:17values i would like to insert cow
  4192. 2:23:20as well as moon so this will produce
  4193. 2:23:23the same output but it's written a
  4194. 2:23:26little more elegantly
  4195. 2:23:27we can also replace these with variables
  4196. 2:23:30because
  4197. 2:23:31they store these values so i'm going to
  4198. 2:23:33replace cow
  4199. 2:23:34with animal and moon with item
  4200. 2:23:37and this will create the same output as
  4201. 2:23:40it did before
  4202. 2:23:41these curly braces are what's known as
  4203. 2:23:43format fields
  4204. 2:23:44they function as a placeholder for a
  4205. 2:23:46value or
  4206. 2:23:47a variable and they work in order the
  4207. 2:23:50first format field will insert the first
  4208. 2:23:52value
  4209. 2:23:53at this location if you have another set
  4210. 2:23:56or more sets of this format field you'll
  4211. 2:23:59insert
  4212. 2:24:00the next value that's listed within the
  4213. 2:24:02format method
  4214. 2:24:03so if we were to switch these around
  4215. 2:24:06we'll switch the position of animal with
  4216. 2:24:08item
  4217. 2:24:09well then the first format field is
  4218. 2:24:12going to insert
  4219. 2:24:13our item and the second format field
  4220. 2:24:15will insert our animal
  4221. 2:24:17this time the moon jumped over the cow
  4222. 2:24:20an additional way of inserting values at
  4223. 2:24:22a given place holder would be to use
  4224. 2:24:24what's referred to as a
  4225. 2:24:26positional argument
  4226. 2:24:29within these format fields we will list
  4227. 2:24:32the index of the value that we would
  4228. 2:24:33like to insert at this location
  4229. 2:24:36if you need to insert the first value
  4230. 2:24:38well that has an index
  4231. 2:24:40of zero because computers always start
  4232. 2:24:41with zero and following that pattern
  4233. 2:24:44the next index would be one if you had
  4234. 2:24:47let's say three values that you need to
  4235. 2:24:49insert if you had another placeholder
  4236. 2:24:51that would be two then so you would just
  4237. 2:24:53follow that pattern
  4238. 2:24:54so there's going to be no visible change
  4239. 2:24:57but what we could do
  4240. 2:24:58is reverse the positional arguments of
  4241. 2:25:01these placeholders
  4242. 2:25:02so what do you think would happen if i
  4243. 2:25:04switched one with zero at these
  4244. 2:25:06locations
  4245. 2:25:07well this format field is going to
  4246. 2:25:09insert the value found at index
  4247. 2:25:11one this first value is index zero and
  4248. 2:25:14the second one
  4249. 2:25:15is index one so now the roles are
  4250. 2:25:17reversed this time the moon
  4251. 2:25:19jumped over the cow our last way of
  4252. 2:25:22inserting some values at a given format
  4253. 2:25:25field would be to use what's referred to
  4254. 2:25:27as a
  4255. 2:25:27keyword argument within
  4256. 2:25:31the format method we're going to list
  4257. 2:25:33some keyword argument pairs
  4258. 2:25:35we need a keyword name followed by a
  4259. 2:25:37value
  4260. 2:25:38so let's say animal is going to be the
  4261. 2:25:40keyword we're going to say equals
  4262. 2:25:42some value animal equals cow and
  4263. 2:25:45item equals moon and you may have
  4264. 2:25:48noticed that the color of your text
  4265. 2:25:50might have changed
  4266. 2:25:51so we no longer need these variables
  4267. 2:25:53actually um but i'm going to comment out
  4268. 2:25:55these two print statements because we
  4269. 2:25:57will get an error then
  4270. 2:25:58so these are in keyword argument pairs
  4271. 2:26:02and within our format field instead of
  4272. 2:26:04keeping these empty
  4273. 2:26:06or using an index we can use the keyword
  4274. 2:26:08name at my first format field i'm going
  4275. 2:26:10to insert my keyword
  4276. 2:26:12of animal and at my second format field
  4277. 2:26:15i would like to insert
  4278. 2:26:17the item keyword so the result is going
  4279. 2:26:20to be
  4280. 2:26:20the cow jumped over the moon so let's
  4281. 2:26:22reverse these now
  4282. 2:26:23we'll place our item keyword here and
  4283. 2:26:26then at the second format field we will
  4284. 2:26:28insert
  4285. 2:26:28our animal and this time the moon
  4286. 2:26:32jumped over the cow here's an important
  4287. 2:26:33concept that i'd like to bring to your
  4288. 2:26:35attention
  4289. 2:26:35with these values found within the
  4290. 2:26:37format method you can actually reuse
  4291. 2:26:39some of these
  4292. 2:26:40more than once so let's say we would
  4293. 2:26:42like to state
  4294. 2:26:43the animal jumped over the animal we're
  4295. 2:26:46going to use
  4296. 2:26:46this animal keyword twice and our output
  4297. 2:26:49is going to be
  4298. 2:26:50the cow jumped over the cow if we're
  4299. 2:26:52doing the same thing with positional
  4300. 2:26:54arguments
  4301. 2:26:55we can reuse the same index this time
  4302. 2:26:58let's say
  4303. 2:26:59that the moon jumped over the moon so
  4304. 2:27:02our item has an
  4305. 2:27:03index of one so within our first format
  4306. 2:27:06field we'll state
  4307. 2:27:08the index will be one and our second
  4308. 2:27:10format field will also be one
  4309. 2:27:12so our output's going to be the moon
  4310. 2:27:14jumped over the moon
  4311. 2:27:16all right let's take things up a level
  4312. 2:27:17there's an even more elegant way in
  4313. 2:27:19which we could write all of this
  4314. 2:27:21what we'll do is store our string that
  4315. 2:27:23we would like to format
  4316. 2:27:25within a variable let's turn this line
  4317. 2:27:27into a comment and let's create a
  4318. 2:27:29variable called
  4319. 2:27:30text text equals the string that we
  4320. 2:27:32would like to format
  4321. 2:27:33which is the format field
  4322. 2:27:37jumped over the format field
  4323. 2:27:41and if we need to format this variable
  4324. 2:27:44we can just call the format method on it
  4325. 2:27:47so within a print statement let's say
  4326. 2:27:50text dot and then use the format method
  4327. 2:27:54and pass
  4328. 2:27:54in the values that we would like to
  4329. 2:27:56insert at these placeholders
  4330. 2:27:58so we're going to insert animal as well
  4331. 2:28:02as item so our result as you would
  4332. 2:28:05imagine
  4333. 2:28:05is the cow jumped over the moon this
  4334. 2:28:08next section i'm going to explain how we
  4335. 2:28:10can add some padding
  4336. 2:28:11to a string when we display it using the
  4337. 2:28:13format method
  4338. 2:28:14let's say we have a name variable and
  4339. 2:28:16assign this a value of whatever your
  4340. 2:28:18name is
  4341. 2:28:19so i would like to display my name along
  4342. 2:28:21with the custom message
  4343. 2:28:22let's say hello my name
  4344. 2:28:25is and then i'll use a format field here
  4345. 2:28:28so let's follow this string by using the
  4346. 2:28:30format method
  4347. 2:28:31and i would like to insert my name at
  4348. 2:28:33this location
  4349. 2:28:34so currently this will display hello my
  4350. 2:28:37name is bro
  4351. 2:28:38or whatever else your name is so we can
  4352. 2:28:40add some padding
  4353. 2:28:41either before our name after or we can
  4354. 2:28:44add some padding to the left and the
  4355. 2:28:46right hand side
  4356. 2:28:47of our value that we have so what we'll
  4357. 2:28:49do to begin
  4358. 2:28:51is let's add some padding to the right
  4359. 2:28:53hand side of
  4360. 2:28:54our name that we have so within our
  4361. 2:28:56format field if we need to add some
  4362. 2:28:58padding
  4363. 2:28:59within the format field add a colon and
  4364. 2:29:01then the amount of space you would like
  4365. 2:29:03to allocate
  4366. 2:29:04to displaying your value so let's say 10
  4367. 2:29:07so i'm going to allocate 10 spaces worth
  4368. 2:29:10of room
  4369. 2:29:11to display my name you can't really see
  4370. 2:29:13it right now but let's take a look
  4371. 2:29:15so i can't move my cursor past this line
  4372. 2:29:18at the end of my name
  4373. 2:29:20but with my second line i have all of
  4374. 2:29:22this room to work with
  4375. 2:29:23so if i were to change my string that i
  4376. 2:29:25have let's say
  4377. 2:29:28nice to meet you well then
  4378. 2:29:31all of this padding that i have is going
  4379. 2:29:32to be more evident
  4380. 2:29:34so we can add a number preceding with a
  4381. 2:29:38colon to add some padding
  4382. 2:29:40to a value and then you can left align
  4383. 2:29:42this right align it or center align it
  4384. 2:29:45so what i'll do is copy this line that i
  4385. 2:29:47have
  4386. 2:29:48if you need to left align it you use the
  4387. 2:29:51less than sign
  4388. 2:29:52but there's going to be no visible
  4389. 2:29:53change because that's already the
  4390. 2:29:55default
  4391. 2:29:56if you need to write a line precede your
  4392. 2:29:59number with a greater than sign
  4393. 2:30:01so your value is going to be right
  4394. 2:30:04aligned
  4395. 2:30:05if you need to center this value then
  4396. 2:30:07use
  4397. 2:30:08the carrot so this will center align
  4398. 2:30:11your value within the padding that you
  4399. 2:30:14have allotted
  4400. 2:30:15one question that you might have for me
  4401. 2:30:16and i'll try and answer that now because
  4402. 2:30:18it's relevant what if we need to add a
  4403. 2:30:20positional argument or a
  4404. 2:30:22keyword argument to our format field if
  4405. 2:30:24there's already some text within here
  4406. 2:30:26so what you would do before the colon
  4407. 2:30:28you would just precede the colon
  4408. 2:30:30with either your positional argument or
  4409. 2:30:32your keyword argument
  4410. 2:30:34followed by colon and then whatever you
  4411. 2:30:36want to add afterwards to format
  4412. 2:30:38your value that you have alright ladies
  4413. 2:30:40and gentlemen we are near the end and i
  4414. 2:30:42promise that this is the last section
  4415. 2:30:44how can we format some numbers let's say
  4416. 2:30:46we have a
  4417. 2:30:47number variable and this will equal the
  4418. 2:30:50first few digits of pi
  4419. 2:30:523.14159 let's pretend that we would like
  4420. 2:30:55to
  4421. 2:30:56display only the first two digits after
  4422. 2:30:59the decimal so we can do so using the
  4423. 2:31:02format method
  4424. 2:31:03let's print the number
  4425. 2:31:06pi is and then i'll use a format field
  4426. 2:31:09here
  4427. 2:31:10and we will call the format method and
  4428. 2:31:12insert our
  4429. 2:31:13number that we have so if i need to
  4430. 2:31:17display only the first two digits after
  4431. 2:31:20the decimal
  4432. 2:31:21within the format field i will add
  4433. 2:31:24colon dot to f
  4434. 2:31:28f is for floating point numbers that's
  4435. 2:31:30anything with a decimal portion
  4436. 2:31:32so the result is that this will display
  4437. 2:31:35only the first
  4438. 2:31:36two digits after my decimal because we
  4439. 2:31:39placed two within
  4440. 2:31:41the format field so if this was three
  4441. 2:31:44point three f this would display three
  4442. 2:31:47digits
  4443. 2:31:47after the decimal portion oh and
  4444. 2:31:50apparently this will round your number
  4445. 2:31:51so that's something that you should keep
  4446. 2:31:53in mind
  4447. 2:31:53so this time let's change our number to
  4448. 2:31:55something larger like
  4449. 2:31:561000 and i will copy this line and make
  4450. 2:31:59a few changes
  4451. 2:32:00let's say the number is
  4452. 2:32:04and i would like to add a comma at the
  4453. 2:32:06thousands place
  4454. 2:32:07so after the colon within the format
  4455. 2:32:09field i will add a comma
  4456. 2:32:11so this will automatically add a comma
  4457. 2:32:13to all 1000s places
  4458. 2:32:15another cool thing that you can do is
  4459. 2:32:17that you can display your number as
  4460. 2:32:19binary so within the format field add
  4461. 2:32:21colon b
  4462. 2:32:22this will display a binary
  4463. 2:32:25representation of your number
  4464. 2:32:27you can do the same thing with octal by
  4465. 2:32:30adding colon
  4466. 2:32:31o so our number is now being displayed
  4467. 2:32:34as an octal number
  4468. 2:32:35and you can do the same thing with
  4469. 2:32:37hexadecimal lowercase x
  4470. 2:32:39for lowercase or uppercase x for all
  4471. 2:32:42uppercase
  4472. 2:32:42so my number 1000 is now in hexadecimal
  4473. 2:32:46and lastly we can display this number
  4474. 2:32:49in scientific notation using either
  4475. 2:32:53lowercase e for lowercase or capital e
  4476. 2:32:55for uppercase
  4477. 2:32:56and mynumber 1000 is now in scientific
  4478. 2:33:00notation
  4479. 2:33:01all right everybody so that's a few
  4480. 2:33:03things that you can do with the format
  4481. 2:33:05method it's an optional method that
  4482. 2:33:07gives users
  4483. 2:33:08more control when displaying output if
  4484. 2:33:10you would like a copy of all the code
  4485. 2:33:12that we've written here today i will
  4486. 2:33:13post everything in the comments down
  4487. 2:33:15below
  4488. 2:33:16don't be afraid to smash that like
  4489. 2:33:17button drop a comment down below and
  4490. 2:33:20subscribe if you'd like to become a
  4491. 2:33:21fellow bro
  4492. 2:33:22hey what's going on everybody it's bro
  4493. 2:33:24hope you're doing well and in this video
  4494. 2:33:26i'm going to explain a few useful
  4495. 2:33:27methods of the random module
  4496. 2:33:29so sit back relax and enjoy the show
  4497. 2:33:34okay people in this video i'm going to
  4498. 2:33:36explain how we can generate some random
  4499. 2:33:38numbers
  4500. 2:33:39as well as a few other useful methods of
  4501. 2:33:41the random module
  4502. 2:33:43now we're not going to be creating true
  4503. 2:33:45random numbers but something called
  4504. 2:33:47pseudorandom numbers which are fairly
  4505. 2:33:49darn close so the first step
  4506. 2:33:51is to import the random module at the
  4507. 2:33:53top import
  4508. 2:33:54random we now have access to everything
  4509. 2:33:57that the random module has to offer
  4510. 2:33:59let's begin by generating a random
  4511. 2:34:01number between one and six
  4512. 2:34:02like we're rolling a dice so what i'm
  4513. 2:34:04going to do is say x
  4514. 2:34:06equals random dot rand
  4515. 2:34:10and we can generate a random integer
  4516. 2:34:12between a certain range
  4517. 2:34:13if i want one through six i will state
  4518. 2:34:16one
  4519. 2:34:17comma six and then we can generate a
  4520. 2:34:19random number between one and six like
  4521. 2:34:21we're rolling a dice
  4522. 2:34:22so if i were to print the value of x
  4523. 2:34:26then we will get a random number between
  4524. 2:34:27one and six for example i just got a one
  4525. 2:34:30a two a three and a six all right
  4526. 2:34:33we can also generate a random floating
  4527. 2:34:35point number two
  4528. 2:34:37let's say y equals random dot
  4529. 2:34:40random so this will give us a random
  4530. 2:34:43number between
  4531. 2:34:440 and 1. this time i'm going to print
  4532. 2:34:47y so we have 0.49 and some change
  4533. 2:34:520.145 and 0.858
  4534. 2:34:56we can also generate a random choice
  4535. 2:34:58from a list or other collection
  4536. 2:35:00let's say we're playing a game of rock
  4537. 2:35:02paper scissors so let's create a list
  4538. 2:35:04i'll just call this my list equals
  4539. 2:35:08square brackets and we will have three
  4540. 2:35:10choices
  4541. 2:35:11rock paper and
  4542. 2:35:14scissors
  4543. 2:35:18so let's say z
  4544. 2:35:21will be the random string that we
  4545. 2:35:24generate
  4546. 2:35:25z equals random dot
  4547. 2:35:28choice and we will pass in my list
  4548. 2:35:33and let's print z so this will generate
  4549. 2:35:36a
  4550. 2:35:37random choice from my list called my
  4551. 2:35:40list
  4552. 2:35:40so we got scissors paper and
  4553. 2:35:43rock we can also use the shuffle method
  4554. 2:35:46of the random module to
  4555. 2:35:47shuffle a list or other collection let's
  4556. 2:35:50say we're working with a deck of cards
  4557. 2:35:52cards equals let's say one
  4558. 2:35:56through nine
  4559. 2:36:00and we have a jack a queen
  4560. 2:36:04a king and an ace this won't be a full
  4561. 2:36:08deck of cards it'll just be one suit
  4562. 2:36:10so we can actually shuffle this list by
  4563. 2:36:13using the shuffle method
  4564. 2:36:14random dot shuffle what do we want to
  4565. 2:36:18shuffle
  4566. 2:36:18we want to shuffle our cards and i will
  4567. 2:36:22display
  4568. 2:36:23my cards with a print statement so the
  4569. 2:36:26shuffle method will shuffle a list or
  4570. 2:36:28other collection for you
  4571. 2:36:29well everybody that's a few useful
  4572. 2:36:31methods of the random module
  4573. 2:36:33if you would like a copy of all this
  4574. 2:36:34code i will post this in the comments
  4575. 2:36:36down below
  4576. 2:36:37don't be afraid to smash that like
  4577. 2:36:39button drop a comment down below
  4578. 2:36:40and subscribe if you'd like to become a
  4579. 2:36:42fellow bro hey what's going on everybody
  4580. 2:36:45it's
  4581. 2:36:45bro hope you're doing well and in this
  4582. 2:36:47video i'm going to explain exception
  4583. 2:36:49handling in python
  4584. 2:36:50so sit back relax and enjoy the show
  4585. 2:36:55well everyone let's talk about
  4586. 2:36:57exceptions an exception is an event
  4587. 2:36:59detected
  4588. 2:37:00during execution that interrupts the
  4589. 2:37:02normal flow of a program
  4590. 2:37:04here's a program that i've written and
  4591. 2:37:06we're intentionally going to cause an
  4592. 2:37:07exception
  4593. 2:37:08so this program that i've written we
  4594. 2:37:10will accept a numerator and a
  4595. 2:37:12denominator from the user
  4596. 2:37:13we'll divide the numerator by the
  4597. 2:37:15denominator we'll get a result
  4598. 2:37:17and print the result so what if i were
  4599. 2:37:19to divide a number such as 5
  4600. 2:37:21divided by 0 which we cannot
  4601. 2:37:23mathematically do
  4602. 2:37:24well this will cause an exception an
  4603. 2:37:26event detected
  4604. 2:37:27during execution that interrupts the
  4605. 2:37:30flow of a program
  4606. 2:37:31so our program was interrupted and
  4607. 2:37:33within our console window we have this
  4608. 2:37:35traceback message
  4609. 2:37:36which states what went wrong and a name
  4610. 2:37:39for this exception that occurred
  4611. 2:37:41this specific exception that occurred is
  4612. 2:37:43a zero division error
  4613. 2:37:44so this video is all about handling
  4614. 2:37:47these exceptions so they do not
  4615. 2:37:48interrupt the normal flow of a program
  4616. 2:37:51now a very basic form of exception
  4617. 2:37:53handling
  4618. 2:37:53is to surround any code that is
  4619. 2:37:55considered dangerous as in it might
  4620. 2:37:57cause an exception
  4621. 2:37:58within a try block anytime you accept
  4622. 2:38:01user input would be a good indicator too
  4623. 2:38:03so this code is considered dangerous
  4624. 2:38:05because we don't know what the user is
  4625. 2:38:06going to type in
  4626. 2:38:07so we will try all of this code if an
  4627. 2:38:10exception occurs
  4628. 2:38:11we can catch it and handle it so to do
  4629. 2:38:13that
  4630. 2:38:14we'll add an accept block accept and
  4631. 2:38:16then we will catch
  4632. 2:38:18exception normally having this block by
  4633. 2:38:21itself
  4634. 2:38:21isn't considered good practice but i'll
  4635. 2:38:23explain why later so
  4636. 2:38:25this accept exception block will catch
  4637. 2:38:27all sorts of different exceptions
  4638. 2:38:29and it will prevent our program from
  4639. 2:38:31being interrupted and we can do
  4640. 2:38:32something else entirely such as print a
  4641. 2:38:34message
  4642. 2:38:35something went wrong so let's try this
  4643. 2:38:39again
  4644. 2:38:40we will divide five by zero which we
  4645. 2:38:42cannot do
  4646. 2:38:43we no longer have that traceback message
  4647. 2:38:45within our console window
  4648. 2:38:46we caught the exception and we did
  4649. 2:38:48something else we printed
  4650. 2:38:50something went wrong like i said before
  4651. 2:38:53it's not considered good practice to
  4652. 2:38:55have a single
  4653. 2:38:56accept block that will handle all
  4654. 2:38:58exceptions it's much better to first
  4655. 2:39:00handle specific exceptions when they
  4656. 2:39:02occur
  4657. 2:39:02and we can do so by writing additional
  4658. 2:39:05accept blocks
  4659. 2:39:06so let's create an additional accept
  4660. 2:39:08block that will catch
  4661. 2:39:10any zero division error exceptions
  4662. 2:39:13that occurs when somebody attempts to
  4663. 2:39:14divide by zero so if
  4664. 2:39:16this exception occurs how do we want to
  4665. 2:39:18handle it let's just print the message
  4666. 2:39:20let's print you can't divide
  4667. 2:39:24by zero idiot
  4668. 2:39:27and let's try this again so five divided
  4669. 2:39:30by zero
  4670. 2:39:31you can't divide by zero idiot here's
  4671. 2:39:33another one i'm going to turn
  4672. 2:39:35this accept block into a comment so we
  4673. 2:39:38can take a look at something
  4674. 2:39:39what if i divide by something that's not
  4675. 2:39:41a number like we take
  4676. 2:39:425 and divide it by the word pizza well
  4677. 2:39:45pizza is not a number
  4678. 2:39:46and we ran into a value error exception
  4679. 2:39:50so we should probably handle this too so
  4680. 2:39:52let's create another accept block that
  4681. 2:39:54will handle that
  4682. 2:39:55accept value error
  4683. 2:39:59and how do we want to handle this let's
  4684. 2:40:02print something
  4685. 2:40:03enter only numbers
  4686. 2:40:06please okay let's try this again and see
  4687. 2:40:10if
  4688. 2:40:10this exception is caught five divided by
  4689. 2:40:13the word
  4690. 2:40:14pizza enter only numbers please
  4691. 2:40:17not only that but you can also add the
  4692. 2:40:20accept exception block at the very end
  4693. 2:40:23just in case there's something you won't
  4694. 2:40:24anticipate
  4695. 2:40:25but it's always considered good practice
  4696. 2:40:27to first catch any specific exceptions
  4697. 2:40:30and let the user know exactly what went
  4698. 2:40:31wrong an additional way in which we can
  4699. 2:40:34handle these exceptions
  4700. 2:40:35is that we could display the exception
  4701. 2:40:38that occurs although it is completely
  4702. 2:40:39optional
  4703. 2:40:40so to do that what we'll do is that
  4704. 2:40:42after each of these accept block
  4705. 2:40:44definitions
  4706. 2:40:45let's add as e so we're calling the
  4707. 2:40:48exception as
  4708. 2:40:49e that's just standard practice although
  4709. 2:40:50it's not necessary
  4710. 2:40:52so let's do that for each of these
  4711. 2:40:54except blocks
  4712. 2:40:55and then we can print whatever e is so
  4713. 2:40:58let's add that
  4714. 2:40:59to each of these blocks so
  4715. 2:41:02it should now look something like this
  4716. 2:41:04so when we encounter an exception
  4717. 2:41:06we can display what exception occurred
  4718. 2:41:09too as well
  4719. 2:41:10so let's divide five by pizza invalid
  4720. 2:41:13literal for int with base 10 pizza
  4721. 2:41:16enter only numbers please so like i said
  4722. 2:41:19this
  4723. 2:41:20is an additional way in which you could
  4724. 2:41:21handle an exception although it's
  4725. 2:41:23entirely optional
  4726. 2:41:24another thing that you can do is that
  4727. 2:41:26you can add an else statement
  4728. 2:41:28to the end of your accept blocks so
  4729. 2:41:30what's happening here is that we're
  4730. 2:41:32going to
  4731. 2:41:32try all of this code if an exception
  4732. 2:41:35happens
  4733. 2:41:36then catch it and handle it if not then
  4734. 2:41:38do something else
  4735. 2:41:40so why don't we print our result only if
  4736. 2:41:42there are no
  4737. 2:41:43exceptions that occur so if there are no
  4738. 2:41:46exceptions we will execute
  4739. 2:41:48this else statement if not then we won't
  4740. 2:41:51so
  4741. 2:41:51let's divide five by an actual number
  4742. 2:41:53this time like
  4743. 2:41:54two so there are no exceptions that
  4744. 2:41:57occurred so
  4745. 2:41:58we executed this else statement whatever
  4746. 2:42:00code is within
  4747. 2:42:01our else block and this printed the
  4748. 2:42:03result of 2.5
  4749. 2:42:05but if an exception were to occur like
  4750. 2:42:07we divide 5 by
  4751. 2:42:080 then we will catch our exception and
  4752. 2:42:11handle it
  4753. 2:42:12and we do not execute whatever block of
  4754. 2:42:15code is within
  4755. 2:42:16our else statement there is one other
  4756. 2:42:18clause that you should be aware of that
  4757. 2:42:20is the
  4758. 2:42:20final clause this is always at the end
  4759. 2:42:22how this works
  4760. 2:42:23is that whether or not we catch an
  4761. 2:42:25exception we will always execute any
  4762. 2:42:28code
  4763. 2:42:28that is within the block of code for our
  4764. 2:42:30final clause
  4765. 2:42:32so this is a good opportunity if you
  4766. 2:42:34open files
  4767. 2:42:35to close them within the finally block
  4768. 2:42:38but
  4769. 2:42:38we haven't dealt with opening files yet
  4770. 2:42:40so we won't do that
  4771. 2:42:41all we'll do is just print something
  4772. 2:42:43print this
  4773. 2:42:45will always execute so
  4774. 2:42:48whether or not we encounter an exception
  4775. 2:42:50this finally block will always execute
  4776. 2:42:52at the end
  4777. 2:42:53so let's try this again so this time
  4778. 2:42:55let's cause an exception
  4779. 2:42:56let's divide five by zero you can't
  4780. 2:42:59divide by
  4781. 2:43:00zero this will always execute and this
  4782. 2:43:02time let's take this seriously and
  4783. 2:43:04divide
  4784. 2:43:04five by another number such as three so
  4785. 2:43:07this will give us a result
  4786. 2:43:09and like i said the finally clause the
  4787. 2:43:11code within the finally clause will
  4788. 2:43:13always execute
  4789. 2:43:14but we won't really be working with this
  4790. 2:43:16until we get to the section on file
  4791. 2:43:18handling
  4792. 2:43:19well everybody those are exceptions in
  4793. 2:43:21python they are events detected
  4794. 2:43:24during execution that interrupt the flow
  4795. 2:43:26of a program
  4796. 2:43:27and we can handle these exceptions by
  4797. 2:43:29using try and accept blocks
  4798. 2:43:31so if you would like a copy of all this
  4799. 2:43:33code i will post all of this in the
  4800. 2:43:34comments down below
  4801. 2:43:36but yeah that is how exceptions work in
  4802. 2:43:38python
  4803. 2:43:40hey what's going on everybody it's bro
  4804. 2:43:42hope you're doing well
  4805. 2:43:43and in this video we're going to be
  4806. 2:43:45covering some basic file detection using
  4807. 2:43:47python so
  4808. 2:43:48sit back relax and enjoy the show
  4809. 2:43:53welcome ladies and gentlemen to the
  4810. 2:43:55beginning of a mini series involving
  4811. 2:43:57doing stuff with files using python and
  4812. 2:43:59in this video we're going to be doing
  4813. 2:44:00some basic
  4814. 2:44:01file detection and in order to do so i
  4815. 2:44:04recommend
  4816. 2:44:04importing the os module it's already
  4817. 2:44:07included with the standard python
  4818. 2:44:08library
  4819. 2:44:09all you need to do is import it all
  4820. 2:44:11we'll be doing is checking to see if a
  4821. 2:44:13file
  4822. 2:44:14exists someplace on our computer so
  4823. 2:44:16we'll probably need a file to work with
  4824. 2:44:17what i'm going to do is go to my desktop
  4825. 2:44:20for convenience
  4826. 2:44:21and just create a new text document i
  4827. 2:44:23will call this test.txt
  4828. 2:44:25now i probably need the file location so
  4829. 2:44:28i'm going to copy that
  4830. 2:44:30and then we can use this so let's create
  4831. 2:44:32a variable called path
  4832. 2:44:34that will include the path to the file
  4833. 2:44:36that i would like to detect
  4834. 2:44:38so that was test.txt now if you have
  4835. 2:44:41backslashes in your file path you'll
  4836. 2:44:44probably need
  4837. 2:44:45double backslashes because that's the
  4838. 2:44:47escape sequence for a backslash within a
  4839. 2:44:49string
  4840. 2:44:50so we now have this variable path that
  4841. 2:44:53we can work with
  4842. 2:44:54now i would like to check to see if this
  4843. 2:44:56location
  4844. 2:44:57exists on my computer so if os
  4845. 2:45:00dot path dot exists and you can see that
  4846. 2:45:04we can pass in
  4847. 2:45:05our path as an argument so i will pass
  4848. 2:45:07in my path
  4849. 2:45:08and if this returns true if that
  4850. 2:45:10location exists i will print
  4851. 2:45:14that location exists
  4852. 2:45:18so this will not tell me if this is a
  4853. 2:45:21file or not
  4854. 2:45:22we'll be covering that later else
  4855. 2:45:25let's print that location
  4856. 2:45:29doesn't exist
  4857. 2:45:32all right so if this location exists
  4858. 2:45:35this will return true
  4859. 2:45:37if not this will execute our else
  4860. 2:45:39statement
  4861. 2:45:40that location exists so what if i were
  4862. 2:45:43to delete this file
  4863. 2:45:44and we run this program again that
  4864. 2:45:46location doesn't exist
  4865. 2:45:48now this won't tell you if the location
  4866. 2:45:51you're dealing with
  4867. 2:45:52is a file or not there is a separate
  4868. 2:45:55function for that
  4869. 2:45:56it is if os
  4870. 2:45:59dot path dot is file
  4871. 2:46:05what we'll do is pass in our path and
  4872. 2:46:07print
  4873. 2:46:09that is a file
  4874. 2:46:12all right so let's create another file
  4875. 2:46:16because i deleted the previous one
  4876. 2:46:18test.txt it's in the same location let's
  4877. 2:46:21run this again
  4878. 2:46:22that location exists that is a file now
  4879. 2:46:25what if this was a folder
  4880. 2:46:27so i'm going to create a new folder i
  4881. 2:46:29will call this folder
  4882. 2:46:30i will delete my test document
  4883. 2:46:34and let's change the location to folder
  4884. 2:46:39that location exists but it is not a
  4885. 2:46:41file you can check to see if a location
  4886. 2:46:43is a directory
  4887. 2:46:44so let's write an else if statement os
  4888. 2:46:48dot path dot is directory and we will
  4889. 2:46:52pass in our path
  4890. 2:46:54and let's print that
  4891. 2:46:57is a directory
  4892. 2:47:01all right so we have our folder we're
  4893. 2:47:03going to check to see
  4894. 2:47:05if this location exists and if it's a
  4895. 2:47:08file or
  4896. 2:47:09a directory that location exists that is
  4897. 2:47:12a directory
  4898. 2:47:13well everyone that is the basics of file
  4899. 2:47:15detection
  4900. 2:47:16we'll be building upon this concept in
  4901. 2:47:18future videos
  4902. 2:47:19so if you would like a copy of this code
  4903. 2:47:21i will post this in the comment section
  4904. 2:47:23down below
  4905. 2:47:24but yeah that is the basics of simple
  4906. 2:47:26file detection using python
  4907. 2:47:28hey what's going on everybody it's bro
  4908. 2:47:31hope you're doing well and in this video
  4909. 2:47:32we're going to be reading a file in
  4910. 2:47:34python so
  4911. 2:47:35sit back relax and enjoy the show
  4912. 2:47:40welcome back guys and gals in this video
  4913. 2:47:42i'm going to explain how we can read the
  4914. 2:47:44contents of a file using python
  4915. 2:47:46so we'll probably need some sort of file
  4916. 2:47:48to work with i created a plain text file
  4917. 2:47:50called
  4918. 2:47:51test.txt it's a plain text file and all
  4919. 2:47:54this says
  4920. 2:47:55is omg you can read this have a nice day
  4921. 2:47:58like comment and subscribe so what i
  4922. 2:48:01would like to do is to read
  4923. 2:48:02the contents of that file line by line
  4924. 2:48:04and print it to the console window
  4925. 2:48:06and here's how and honestly we only need
  4926. 2:48:08two lines of code to do this
  4927. 2:48:10so what we'll do is type with open
  4928. 2:48:13then within the open function list the
  4929. 2:48:15name of your file
  4930. 2:48:17or the file path this file of mine is
  4931. 2:48:20within my project folder
  4932. 2:48:21so i only need to list the file name if
  4933. 2:48:24this was someplace else you'll probably
  4934. 2:48:26need
  4935. 2:48:27the file path so let's say this was on
  4936. 2:48:30my desktop
  4937. 2:48:31well i'll probably need to list the file
  4938. 2:48:33path of where that file is located
  4939. 2:48:35and i'll need to use double backslashes
  4940. 2:48:38because that's the escape sequence for a
  4941. 2:48:40backslash
  4942. 2:48:41but since this file is within my project
  4943. 2:48:43folder i only need to list the file name
  4944. 2:48:45so with open then the name your file or
  4945. 2:48:49the file path
  4946. 2:48:50as file then what we're going to do
  4947. 2:48:54is print file dot
  4948. 2:48:58read and that is it so let's test it
  4949. 2:49:02and it works omg you can read this have
  4950. 2:49:05a nice day
  4951. 2:49:06like comment and subscribe so this will
  4952. 2:49:09actually
  4953. 2:49:09close files automatically after opening
  4954. 2:49:12them
  4955. 2:49:12so that's kind of convenient and let's
  4956. 2:49:14just test that theory
  4957. 2:49:15so i'm going to print file dot
  4958. 2:49:19closed if my file is in fact closed this
  4959. 2:49:22will print
  4960. 2:49:22true if it's still open this will print
  4961. 2:49:24false
  4962. 2:49:26so normally when you read a file it's
  4963. 2:49:28normally open and you need to
  4964. 2:49:29close it manually but if you write your
  4965. 2:49:31program this way
  4966. 2:49:32with open this will close the file
  4967. 2:49:35automatically for you
  4968. 2:49:36so using with open will close any files
  4969. 2:49:39automatically for you
  4970. 2:49:41after they've been opened however this
  4971. 2:49:43does not catch
  4972. 2:49:44and handle any exceptions that might
  4973. 2:49:46occur like if we cannot locate this file
  4974. 2:49:49so let's say that i mistyped the
  4975. 2:49:51extension for this file
  4976. 2:49:53let's say that i type in test.tx
  4977. 2:49:56that's kind of funny because tx is the
  4978. 2:49:58abbreviation for
  4979. 2:49:59texas it's a texas file so this will
  4980. 2:50:02cause
  4981. 2:50:03a file not found error and this will
  4982. 2:50:05interrupt the flow of our program and we
  4983. 2:50:08would probably like to prevent that
  4984. 2:50:09so what we could do is write a try and
  4985. 2:50:12an accept block
  4986. 2:50:13so we will try all of this code if we
  4987. 2:50:15cannot find
  4988. 2:50:16this file we can handle this exception
  4989. 2:50:19file not found error
  4990. 2:50:20so we'll place all of this code within
  4991. 2:50:22the try block then
  4992. 2:50:23if an exception happens we will catch
  4993. 2:50:25that exception
  4994. 2:50:26except we would like to catch any file
  4995. 2:50:29not found air
  4996. 2:50:30exceptions and let's handle this by
  4997. 2:50:33printing
  4998. 2:50:35that file was not found
  4999. 2:50:40so if i were to run this again we will
  5000. 2:50:42catch and handle this exception and this
  5001. 2:50:44will not
  5002. 2:50:45interrupt the normal flow of our program
  5003. 2:50:48well everybody that's how to open and
  5004. 2:50:50read
  5005. 2:50:50a file in python if you'd like a copy of
  5006. 2:50:53all this code i will post this in the
  5007. 2:50:55comment section down below
  5008. 2:50:56but yeah that's how to open and read a
  5009. 2:50:58file using python
  5010. 2:51:00hey what's going on everybody it's bro
  5011. 2:51:03hope you're doing well and in this video
  5012. 2:51:04we're going to be writing files in
  5013. 2:51:06python so sit back
  5014. 2:51:07relax and enjoy the show
  5015. 2:51:11so to begin it's kind of similar to the
  5016. 2:51:13last video on reading files
  5017. 2:51:15with open within the open function list
  5018. 2:51:18the file name or the file path so what
  5019. 2:51:21do you want to call this file
  5020. 2:51:22i will call this file test dot txt
  5021. 2:51:26now normally with the open function
  5022. 2:51:28there is a mode
  5023. 2:51:29and you can actually change that there's
  5024. 2:51:31a second argument that you can pass in
  5025. 2:51:33by default this is r for read but if you
  5026. 2:51:36want to write a file this would be
  5027. 2:51:38w so now we're going to write a file
  5028. 2:51:41called
  5029. 2:51:42test.txt so to finish with open
  5030. 2:51:46as file what are we going to do file
  5031. 2:51:49dot write and within the write function
  5032. 2:51:52we can write some text
  5033. 2:51:54so let's create a text variable and we
  5034. 2:51:57will add a string
  5035. 2:51:58so let's say text equals
  5036. 2:52:03yo and if you need to go down to a new
  5037. 2:52:06line
  5038. 2:52:07be sure to use the new line character so
  5039. 2:52:09after this new line character
  5040. 2:52:11the text that comes after is going to be
  5041. 2:52:13on the next line as if we're hitting
  5042. 2:52:15enter
  5043. 2:52:16so yo this is some
  5044. 2:52:19text have
  5045. 2:52:22a good one
  5046. 2:52:26all right so after i run this program we
  5047. 2:52:29should have
  5048. 2:52:29a file a text file within our project
  5049. 2:52:32folder that appears
  5050. 2:52:34and it is right here and it says yo this
  5051. 2:52:37is some text
  5052. 2:52:38have a good one now this is in write
  5053. 2:52:41mode
  5054. 2:52:42if this text was something else it will
  5055. 2:52:44actually
  5056. 2:52:45overwrite your current file so if i were
  5057. 2:52:47to change this to
  5058. 2:52:49uh oh this text has
  5059. 2:52:53been over written well then
  5060. 2:52:56this will overwrite the text that we
  5061. 2:52:59have
  5062. 2:52:59uh oh this text has been overwritten so
  5063. 2:53:02you can actually append a file by
  5064. 2:53:04changing the mode
  5065. 2:53:05to a for append so let me change this
  5066. 2:53:08back
  5067. 2:53:09we will rewrite this this will save over
  5068. 2:53:12our current file
  5069. 2:53:13now we can append some text so let's add
  5070. 2:53:15maybe a new line
  5071. 2:53:17uh have a nice
  5072. 2:53:20day see ya so i would like to append
  5073. 2:53:25some text to this file i'm going to
  5074. 2:53:26change the mode to a
  5075. 2:53:28for append and we can now append some
  5076. 2:53:31text onto the end of this file
  5077. 2:53:33well everybody that's how to write to a
  5078. 2:53:35file and or append a file if you would
  5079. 2:53:37like a copy of this code
  5080. 2:53:38i will post this in the comment section
  5081. 2:53:40down below but yeah that's how to
  5082. 2:53:42write to and or append a file in python
  5083. 2:53:46hey what's going on everybody it's bro
  5084. 2:53:48hope you're doing well
  5085. 2:53:49and in this video we're going to be
  5086. 2:53:50copying files in python so
  5087. 2:53:52sit back relax and enjoy the show
  5088. 2:53:57welcome back to another video regarding
  5089. 2:53:59doing stuff with files using python and
  5090. 2:54:01in this video we're going to be
  5091. 2:54:02copying some files and in order to do so
  5092. 2:54:05i recommend
  5093. 2:54:06importing the shuttle module there's
  5094. 2:54:08other ways of doing this too
  5095. 2:54:10but i tend to like using the shuttle
  5096. 2:54:11module now
  5097. 2:54:13with this module there are three basic
  5098. 2:54:15functions
  5099. 2:54:16to copy a file starting at the top they
  5100. 2:54:19are copy file
  5101. 2:54:20copy and copy to so copy file is what
  5102. 2:54:24we'll be working with
  5103. 2:54:25this will copy the contents of a file
  5104. 2:54:27copy
  5105. 2:54:28will do everything that copy file does
  5106. 2:54:31plus copy
  5107. 2:54:32the permissions mode and the destination
  5108. 2:54:34can be a directory
  5109. 2:54:36and lastly copy 2 this will do
  5110. 2:54:38everything
  5111. 2:54:39that copy does plus it copies the
  5112. 2:54:41metadata
  5113. 2:54:42of that file including the files
  5114. 2:54:44creation and modification times
  5115. 2:54:46so depending on the project or the
  5116. 2:54:48program you're working with
  5117. 2:54:49you may need to use copy or copy too but
  5118. 2:54:52in this video we're going to be using
  5119. 2:54:54copy file just to copy the contents of a
  5120. 2:54:56file
  5121. 2:54:57now after you import the shuttle module
  5122. 2:55:00this is all you need to do to copy a
  5123. 2:55:01file
  5124. 2:55:02shuttle dot then use the appropriate
  5125. 2:55:05function that you need to copy your file
  5126. 2:55:07we're going to be using copy file copy
  5127. 2:55:10file and within this function there are
  5128. 2:55:12two arguments
  5129. 2:55:13a source and a destination so within my
  5130. 2:55:17project folder i have a plain
  5131. 2:55:19text file called test test.txt
  5132. 2:55:22all this says is yo this is some text
  5133. 2:55:25see ya what i would like to do is copy
  5134. 2:55:27this file because it's cool
  5135. 2:55:29now since this file is within my project
  5136. 2:55:32folder i only have to list the file name
  5137. 2:55:34otherwise if this is someplace else i
  5138. 2:55:36would list the file path here
  5139. 2:55:38so this file is test.txt
  5140. 2:55:42this is the source file and now i need a
  5141. 2:55:45destination
  5142. 2:55:46so the second argument will be separated
  5143. 2:55:48with the comma i'm going to list
  5144. 2:55:50a destination so you can actually rename
  5145. 2:55:53this to
  5146. 2:55:53i would like to name this as copy.txt
  5147. 2:55:57so the destination is my project folder
  5148. 2:56:00so when i create a copy of this file
  5149. 2:56:02test.txt
  5150. 2:56:04this copy will appear in my project
  5151. 2:56:06folder and say the same thing
  5152. 2:56:07so after running this we now have a
  5153. 2:56:11file named copy.txt that says the same
  5154. 2:56:13thing
  5155. 2:56:14as my test file and remember what i said
  5156. 2:56:16you can copy
  5157. 2:56:17this file to a different location on
  5158. 2:56:19your computer so you could list the file
  5159. 2:56:22path
  5160. 2:56:22so this file path would go to my desktop
  5161. 2:56:25and then when i run this
  5162. 2:56:26this will copy my original file my
  5163. 2:56:28source and then copy it to
  5164. 2:56:30wherever my destination is oh and i
  5165. 2:56:32almost forgot so if you need to use copy
  5166. 2:56:34or copy 2
  5167. 2:56:35the arguments are exactly the same so if
  5168. 2:56:38you needed to use copy
  5169. 2:56:40this would be a shuttle dot copy instead
  5170. 2:56:42of copy file
  5171. 2:56:43then copy 2 is shuttle dot copy 2.
  5172. 2:56:46the arguments are exactly the same but
  5173. 2:56:48each of these functions will copy
  5174. 2:56:50different things depending on what you
  5175. 2:56:52need for your own program or project
  5176. 2:56:54all right everybody so that's the basics
  5177. 2:56:56of copying files and
  5178. 2:56:58python i will post this in the comment
  5179. 2:57:00section down below
  5180. 2:57:01but yeah that is how to copy files using
  5181. 2:57:04python
  5182. 2:57:05hey what's going on everybody it's bro
  5183. 2:57:08hope you're doing well and in this video
  5184. 2:57:09i'm going to explain how we can move
  5185. 2:57:11files using python so sit back relax
  5186. 2:57:15and enjoy the show
  5187. 2:57:18welcome back everybody to another video
  5188. 2:57:20regarding that thing that we do called
  5189. 2:57:22programming and in this video we're
  5190. 2:57:23going to be moving files using python
  5191. 2:57:26and in order to do so i recommend
  5192. 2:57:28importing the os module
  5193. 2:57:30this is included with the standard
  5194. 2:57:32python library all you need to do is
  5195. 2:57:34import it
  5196. 2:57:35and now let's create two variables a
  5197. 2:57:37variable called source
  5198. 2:57:38that will hold the location of where our
  5199. 2:57:41source file is located
  5200. 2:57:42as well as a variable called destination
  5201. 2:57:45to function as well
  5202. 2:57:46the destination now for this video i'm
  5203. 2:57:49going
  5204. 2:57:50to move a file within my project folder
  5205. 2:57:52to my desktop
  5206. 2:57:53so to do so i'm going to create a new
  5207. 2:57:56file
  5208. 2:57:57i will name this test.txt but take the
  5209. 2:58:00liberty to name this whatever you want
  5210. 2:58:02so i'm going to list the location of
  5211. 2:58:05where
  5212. 2:58:05my test file is located since this is
  5213. 2:58:08within my project folder i only need
  5214. 2:58:10the file name if this was someplace else
  5215. 2:58:12on your computer you would probably need
  5216. 2:58:14the file path
  5217. 2:58:15and now where would i like to move this
  5218. 2:58:16file to i would like to move this file
  5219. 2:58:19to my desktop
  5220. 2:58:20so i just need the file path so i'm
  5221. 2:58:22going to go to my desktop
  5222. 2:58:24right click go to properties i'm going
  5223. 2:58:27to copy
  5224. 2:58:28this location and paste it within my
  5225. 2:58:30destination
  5226. 2:58:31and i will add slash desktop
  5227. 2:58:34slash test.txt you can also rename this
  5228. 2:58:38too if you want i will just keep it the
  5229. 2:58:40same
  5230. 2:58:40now if you have single backslashes
  5231. 2:58:43you'll likely need to change these
  5232. 2:58:45to double backslashes because that is
  5233. 2:58:47the escape sequence
  5234. 2:58:49to print a backslash within a string so
  5235. 2:58:52we have a
  5236. 2:58:52source location as well as a destination
  5237. 2:58:55location
  5238. 2:58:56now we can begin i would recommend
  5239. 2:58:58writing our code within a try and accept
  5240. 2:59:00block to
  5241. 2:59:01handle any exceptions so i would like to
  5242. 2:59:03handle any file
  5243. 2:59:05not found error exceptions just in case
  5244. 2:59:08our program cannot locate our source
  5245. 2:59:10file so let's print a message if that is
  5246. 2:59:12the case
  5247. 2:59:13perhaps source plus was not
  5248. 2:59:17found now within our try block let's
  5249. 2:59:20check to see if there is already a file
  5250. 2:59:22at this destination
  5251. 2:59:23because i would not like to save over it
  5252. 2:59:25if there's already a file there
  5253. 2:59:27so let's do some basic file detection if
  5254. 2:59:30os
  5255. 2:59:31dot path dot exists
  5256. 2:59:35and i will pass in my destination as an
  5257. 2:59:37argument to the exists
  5258. 2:59:39function so if there is already a file
  5259. 2:59:42here i should probably let the user know
  5260. 2:59:44print there is already a
  5261. 2:59:47file there else
  5262. 2:59:51we can replace our files honestly if you
  5263. 2:59:53do not care about saving over any files
  5264. 2:59:56you can omit this part so else os
  5265. 2:59:59dot replace and we will pass
  5266. 3:00:02in our source and our destination
  5267. 3:00:06as arguments and let's print a message
  5268. 3:00:09to the console window to let the user
  5269. 3:00:11know
  5270. 3:00:11that source plus was
  5271. 3:00:16moved all right let's try this oh i
  5272. 3:00:19accidentally deleted my
  5273. 3:00:21file test.txt all right
  5274. 3:00:24now this should disappear in three
  5275. 3:00:27two one and it's gone
  5276. 3:00:31test.txt was moved and it should now be
  5277. 3:00:33on my desktop
  5278. 3:00:35which it is now you can also use this to
  5279. 3:00:37move a directory as well
  5280. 3:00:39i'm going to create a new directory i
  5281. 3:00:42will name this folder
  5282. 3:00:44and i will change source to folder
  5283. 3:00:47and the destination from test.txt to
  5284. 3:00:50folder and now this should move my
  5285. 3:00:53folder
  5286. 3:00:55there is already a file there
  5287. 3:00:59oh let's delete that okay
  5288. 3:01:02folder was moved all right everybody so
  5289. 3:01:05that is how
  5290. 3:01:06to move a file and or directory using
  5291. 3:01:08python
  5292. 3:01:09if you would like a copy of this code i
  5293. 3:01:11will post this in the comment section
  5294. 3:01:13down below
  5295. 3:01:14but yeah that is how to move a file and
  5296. 3:01:17or directory
  5297. 3:01:18using python hey what's going on
  5298. 3:01:21everybody
  5299. 3:01:21it's bro hope you're doing well and in
  5300. 3:01:23this video i'm going to teach you guys
  5301. 3:01:25how we can delete files using python so
  5302. 3:01:27sit back relax and enjoy the show
  5303. 3:01:32all right everybody so in this video
  5304. 3:01:35we're going to be deleting files using
  5305. 3:01:37python and in order to do so i recommend
  5306. 3:01:39importing the os module
  5307. 3:01:41this is already included with the
  5308. 3:01:42standard python library all you need to
  5309. 3:01:44do
  5310. 3:01:45is import it and now we need a file to
  5311. 3:01:47work with
  5312. 3:01:48so let's right click on our projects
  5313. 3:01:49folder and go to new
  5314. 3:01:51file i will name this test.txt
  5315. 3:01:54and we now have a file that we can work
  5316. 3:01:56with but you know what i would like to
  5317. 3:01:58delete this file because it's lame
  5318. 3:02:00so let's do that in order to delete this
  5319. 3:02:02file let's type os
  5320. 3:02:03dot remove and we're going to list the
  5321. 3:02:05file path here
  5322. 3:02:07since this file is within my project
  5323. 3:02:09folder i only need to list
  5324. 3:02:10the file path test.txt if this was
  5325. 3:02:14someplace else on my computer i would
  5326. 3:02:16probably need the file path
  5327. 3:02:17like that but since this file is within
  5328. 3:02:19my project folder i only need
  5329. 3:02:21the file name another thing that you can
  5330. 3:02:23do too at least what i like to do
  5331. 3:02:25is that i like to pass in a variable
  5332. 3:02:27called path i will make this a variable
  5333. 3:02:29and assign it a
  5334. 3:02:31string value of whatever the path to my
  5335. 3:02:33file is
  5336. 3:02:34so i will assign path the name to my
  5337. 3:02:37file
  5338. 3:02:38test.txt now when i run this this will
  5339. 3:02:40delete my file
  5340. 3:02:42test.txt and you can see that it's no
  5341. 3:02:44longer there
  5342. 3:02:45now what if we attempt to delete a file
  5343. 3:02:47that doesn't exist
  5344. 3:02:49well we'll encounter an exception so if
  5345. 3:02:51you want
  5346. 3:02:52this is optional we can do some
  5347. 3:02:53exception handling so let's write
  5348. 3:02:56the remove function within a try block
  5349. 3:02:58and we will catch
  5350. 3:02:59this exception file not found error and
  5351. 3:03:02let's print something
  5352. 3:03:04print that file was not found
  5353. 3:03:08now if you attempt to delete a file that
  5354. 3:03:10doesn't exist
  5355. 3:03:11your program will not be interrupted and
  5356. 3:03:13instead you'll print something
  5357. 3:03:15that file was not found now this
  5358. 3:03:18function
  5359. 3:03:18does not remove empty folders this is
  5360. 3:03:21how to do so
  5361. 3:03:22and actually let's test that so let's
  5362. 3:03:23create a new directory
  5363. 3:03:25i'll call this empty underscore folder
  5364. 3:03:29now i will attempt to delete this empty
  5365. 3:03:31folder
  5366. 3:03:32empty folder
  5367. 3:03:36we encountered a permission error access
  5368. 3:03:38is denied so let's create an accept
  5369. 3:03:40block for that
  5370. 3:03:41accept permission error
  5371. 3:03:44print i don't know you do
  5372. 3:03:48not have permission to
  5373. 3:03:51delete that right to delete
  5374. 3:03:55an empty folder there is a different
  5375. 3:03:57function
  5376. 3:03:58os dot rmdir
  5377. 3:04:02short for remove directory we're going
  5378. 3:04:04to list the file path
  5379. 3:04:05and pass that in as an argument and
  5380. 3:04:07let's create an else block as well
  5381. 3:04:09because we can
  5382. 3:04:10else if there are no exceptions let's
  5383. 3:04:12print
  5384. 3:04:14path plus was
  5385. 3:04:17deleted all right so rmdir
  5386. 3:04:20remove directory will delete an empty
  5387. 3:04:23directory
  5388. 3:04:24empty folder was deleted now this
  5389. 3:04:27function
  5390. 3:04:27will not delete a folder that contains
  5391. 3:04:30files and let's try that
  5392. 3:04:31so let's create a new directory i'll
  5393. 3:04:34just call this folder
  5394. 3:04:36and within this folder i will create a
  5395. 3:04:38new file
  5396. 3:04:40test.txt
  5397. 3:04:43all right let's attempt to delete this
  5398. 3:04:45folder i will probably need to change
  5399. 3:04:46the file name
  5400. 3:04:48let's change that to folder and we
  5401. 3:04:51encountered an
  5402. 3:04:52os error that directory is not empty so
  5403. 3:04:55let's handle this exception we don't
  5404. 3:04:56need to but i like to
  5405. 3:04:58accept os air print
  5406. 3:05:03you cannot delete that
  5407. 3:05:06using that function
  5408. 3:05:10okay to delete a folder that contains
  5409. 3:05:13files
  5410. 3:05:13we'll need the shuttle module import
  5411. 3:05:17shuttle and the function that we're
  5412. 3:05:19looking for is
  5413. 3:05:20shuttle dot rm tree
  5414. 3:05:24short for remove tree and we will pass
  5415. 3:05:26in our path
  5416. 3:05:27now be careful with this function it is
  5417. 3:05:29considered dangerous
  5418. 3:05:30because it will delete a directory and
  5419. 3:05:32all files contained within
  5420. 3:05:34so now we can delete a folder that
  5421. 3:05:36contains files
  5422. 3:05:38folder was deleted in conclusion these
  5423. 3:05:41are
  5424. 3:05:41three basic functions to delete a file
  5425. 3:05:44or directory they
  5426. 3:05:45are remove which deletes a file rmdir
  5427. 3:05:49which is an abbreviation for remove
  5428. 3:05:52directory
  5429. 3:05:52which will delete an empty directory and
  5430. 3:05:55lastly
  5431. 3:05:56rmtree which is an abbreviation for
  5432. 3:05:58remove tree
  5433. 3:05:59and this is of the shuttle module and
  5434. 3:06:01this will delete a directory containing
  5435. 3:06:04files
  5436. 3:06:04so if you would like a copy of all this
  5437. 3:06:06code i will post this in the comments
  5438. 3:06:08section down below
  5439. 3:06:10but yeah that is how to delete files and
  5440. 3:06:12or directories
  5441. 3:06:13using python yo what's going on
  5442. 3:06:16everybody it's you bro hope you're doing
  5443. 3:06:18well
  5444. 3:06:18and in today's video i'm going to be
  5445. 3:06:20discussing modules in python so
  5446. 3:06:22sit back relax and enjoy the show
  5447. 3:06:27all right well welcome back we're
  5448. 3:06:29talking about modules today
  5449. 3:06:31a module is a file containing python
  5450. 3:06:33code
  5451. 3:06:34it may contain functions classes etc
  5452. 3:06:37it's used with modular programming which
  5453. 3:06:39is this concept of separating a program
  5454. 3:06:42into useful different parts we're
  5455. 3:06:44currently working within our main module
  5456. 3:06:46and mine is named hello youtube this is
  5457. 3:06:48how to create a separate module
  5458. 3:06:50right click on your project folder go to
  5459. 3:06:52new python file
  5460. 3:06:54i think i'll create a module filled with
  5461. 3:06:56maybe message functions
  5462. 3:06:58so i'll call this messages and click
  5463. 3:07:00python file
  5464. 3:07:01all right we have two tabs one for our
  5465. 3:07:03main module mine is called hello youtube
  5466. 3:07:05and a separate module
  5467. 3:07:06for messages and i can write whatever i
  5468. 3:07:09want within here since we're using
  5469. 3:07:10modular programming i'll place a bunch
  5470. 3:07:12of useful functions within this module
  5471. 3:07:15and i'll import them to the main module
  5472. 3:07:17that we're currently working with
  5473. 3:07:18let's define a function called hello all
  5474. 3:07:21this will do is print
  5475. 3:07:24hello have a nice
  5476. 3:07:27day and i'll create a second function
  5477. 3:07:30for good measure
  5478. 3:07:30let's call this pie
  5479. 3:07:35let's say bye have
  5480. 3:07:38a wonderful time
  5481. 3:07:42now if we would like access to this
  5482. 3:07:44module we need to import it
  5483. 3:07:46i will head back to my main module and
  5484. 3:07:47at the top write this
  5485. 3:07:49import then the name of the module and
  5486. 3:07:52for this example we used the name of
  5487. 3:07:54messages
  5488. 3:07:54for our separate module and to use a
  5489. 3:07:57function from that module
  5490. 3:07:58type the name of the module dot and then
  5491. 3:08:01the name of the function
  5492. 3:08:02so messages.hello will call the hello
  5493. 3:08:05function
  5494. 3:08:06found within the messages module and i
  5495. 3:08:08can also call the
  5496. 3:08:09buy function as well messages dot bye
  5497. 3:08:13and that will print bye have a wonderful
  5498. 3:08:16time
  5499. 3:08:16no it can be somewhat tedious to have to
  5500. 3:08:18write the name the module followed by
  5501. 3:08:20the function name what we could do to
  5502. 3:08:22shorten things up
  5503. 3:08:22is to give our messages module an alias
  5504. 3:08:25also known as a nickname
  5505. 3:08:27so at the top write import messages
  5506. 3:08:30as and what sort of nickname should we
  5507. 3:08:32give messages
  5508. 3:08:33let's say msg for short so we'll replace
  5509. 3:08:37messages with
  5510. 3:08:38msg msg hello and msg buy
  5511. 3:08:41and this would work the same and it
  5512. 3:08:42involves less typing there's also an
  5513. 3:08:45additional way of
  5514. 3:08:46importing a module and it goes a little
  5515. 3:08:47something like this from
  5516. 3:08:49the name of the module import and then
  5517. 3:08:52list all of the functions or classes you
  5518. 3:08:54would like to import
  5519. 3:08:55i would like to import hello comma buy
  5520. 3:08:58and we no longer need the module name
  5521. 3:09:00before calling one of these functions
  5522. 3:09:02i can simply just call hello and vi
  5523. 3:09:04directly another way in which this could
  5524. 3:09:06be written is
  5525. 3:09:06from messages import asterisk that means
  5526. 3:09:09to
  5527. 3:09:10import all and i actually would not
  5528. 3:09:11recommend using this if you're working
  5529. 3:09:13on a large program or something that
  5530. 3:09:15contains
  5531. 3:09:16many modules because you can run into a
  5532. 3:09:18naming conflict
  5533. 3:09:19because some of these modules may have
  5534. 3:09:21the same named function names or
  5535. 3:09:22variables
  5536. 3:09:24so if you're working on a small program
  5537. 3:09:25i really don't think it's a big deal but
  5538. 3:09:27if you have multiple modules i would
  5539. 3:09:28actually refrain from using import alt
  5540. 3:09:31it's much safer to write the name of the
  5541. 3:09:33module followed by the name of the
  5542. 3:09:35function that you want to use
  5543. 3:09:36now another cool thing is that python
  5544. 3:09:38has a bunch of pre-written modules that
  5545. 3:09:40you have access to if you would like a
  5546. 3:09:41comprehensive list of them type help
  5547. 3:09:44and pass in the word modules and within
  5548. 3:09:46my console window this will populate a
  5549. 3:09:48listing of all the modules available to
  5550. 3:09:50us
  5551. 3:09:51here they are let's see if math is in
  5552. 3:09:53here yep
  5553. 3:09:54there it is so yeah feel free to take a
  5554. 3:09:56look through some of these modules
  5555. 3:09:58because there are some fairly useful
  5556. 3:09:59tools within some of these modules
  5557. 3:10:01i suppose you can also go to python's
  5558. 3:10:03official documentation and search for
  5559. 3:10:05the python module index and here's a
  5560. 3:10:08listing of all the different modules too
  5561. 3:10:09they have access to
  5562. 3:10:10well everyone those are modules they are
  5563. 3:10:13files containing python code and they
  5564. 3:10:15may contain functions classes etc
  5565. 3:10:17if you would like a copy of all this
  5566. 3:10:18code i will post all of this to the
  5567. 3:10:20comments section down below
  5568. 3:10:22but yeah those are modules in python i
  5569. 3:10:25guess there's a module called pickle
  5570. 3:10:26hey what's going on everybody it's bro
  5571. 3:10:28hope you're doing well
  5572. 3:10:29and in this video we're going to be
  5573. 3:10:31creating a basic game of rock paper
  5574. 3:10:33scissors in python so
  5575. 3:10:34sit back relax and enjoy the show
  5576. 3:10:39let's begin by importing the random
  5577. 3:10:42module
  5578. 3:10:42because we're going to need the computer
  5579. 3:10:44to pick a random choice and we'll create
  5580. 3:10:46a
  5581. 3:10:46list of possible choices so we have
  5582. 3:10:49three strings within here
  5583. 3:10:51rock paper and scissors
  5584. 3:10:57i think i spelled scissors wrong there
  5585. 3:11:00we go
  5586. 3:11:01okay and we will have our computer pick
  5587. 3:11:04a
  5588. 3:11:04random dot choice from our list
  5589. 3:11:08of choices and let's test it by printing
  5590. 3:11:12whatever the computer picks
  5591. 3:11:16so the computer picked paper paper
  5592. 3:11:19scissors
  5593. 3:11:20and scissors okay what about the player
  5594. 3:11:23choice
  5595. 3:11:23let's say player equals
  5596. 3:11:27input rock
  5597. 3:11:31paper or
  5598. 3:11:34scissors
  5599. 3:11:38and let's print what the player has
  5600. 3:11:40print
  5601. 3:11:41player and let's print the computer too
  5602. 3:11:47maybe i'll add a string before these so
  5603. 3:11:49let's say
  5604. 3:11:50computer call in space
  5605. 3:11:54whatever the computer chooses and same
  5606. 3:11:57thing with the player
  5607. 3:11:59player all right
  5608. 3:12:02let's see if this works rock paper or
  5609. 3:12:06scissors let's say rock
  5610. 3:12:07the computer picks paper and i pick rock
  5611. 3:12:10so i would technically lose this round
  5612. 3:12:12now what if the player doesn't pick rock
  5613. 3:12:14paper or scissors what if they type in
  5614. 3:12:16gun so we would like some way to prevent
  5615. 3:12:19that
  5616. 3:12:19so let's put our input for our player
  5617. 3:12:22within a while loop
  5618. 3:12:24while player
  5619. 3:12:27not in choices
  5620. 3:12:32so we're going to need to initialize
  5621. 3:12:34player because right now it's an
  5622. 3:12:36unresolved reference
  5623. 3:12:38so let's say player equals none
  5624. 3:12:42for the time being okay now let's test
  5625. 3:12:45our choices
  5626. 3:12:46our player choices rock paper or
  5627. 3:12:48scissors i pick
  5628. 3:12:49the gun and it's going to keep on asking
  5629. 3:12:52me until i pick
  5630. 3:12:53something from my list of choices so
  5631. 3:12:56if i type in that still won't accept it
  5632. 3:12:58now let's type in
  5633. 3:12:59rock again there we go now what if
  5634. 3:13:02somebody types in one of these choices
  5635. 3:13:04either all caps or
  5636. 3:13:05capitalized well that technically
  5637. 3:13:07wouldn't be in choices because
  5638. 3:13:09these strings are case sensitive so why
  5639. 3:13:12don't we take our user input and make it
  5640. 3:13:14lowercase
  5641. 3:13:14and i'll just add on dot lower and use
  5642. 3:13:17the lower method
  5643. 3:13:18so therefore we can now type in any
  5644. 3:13:22input that is either all caps or
  5645. 3:13:24uppercase
  5646. 3:13:25and then that will still be accepted
  5647. 3:13:27because we're going to take our player
  5648. 3:13:28input
  5649. 3:13:29and then make it lower case so that it
  5650. 3:13:30matches one of these choices
  5651. 3:13:33okay let's work on the win conditions
  5652. 3:13:34next so first
  5653. 3:13:36let's check to see if we encountered a
  5654. 3:13:39tie
  5655. 3:13:39if player is equal to computer that
  5656. 3:13:42means that
  5657. 3:13:43we both chose the same thing so let's
  5658. 3:13:46print whatever the computer picked
  5659. 3:13:48what we picked and we will print
  5660. 3:13:52tie now let's write an else
  5661. 3:13:56if statement e l i
  5662. 3:13:59f player equals
  5663. 3:14:03rock so if we pick rock and then we will
  5664. 3:14:06create
  5665. 3:14:07a nested if statement if computer
  5666. 3:14:11equals paper
  5667. 3:14:15that means we lose so let's print
  5668. 3:14:18everything we have here except we're
  5669. 3:14:20going to change
  5670. 3:14:21tie to you
  5671. 3:14:25lose now if the computer picks
  5672. 3:14:29let's say scissors then we win
  5673. 3:14:33so scissors and you
  5674. 3:14:37win okay then let's just repeat this
  5675. 3:14:40process
  5676. 3:14:41for scissors and paper
  5677. 3:14:45so this will be another else if
  5678. 3:14:47statement
  5679. 3:14:48if we pick
  5680. 3:14:53scissors and the computer picks
  5681. 3:14:56rock then we lose if the computer picks
  5682. 3:15:00paper then you win
  5683. 3:15:04and lastly we have paper as the player
  5684. 3:15:07choice
  5685. 3:15:12so else if player equals paper
  5686. 3:15:16and the computer picks scissors then we
  5687. 3:15:20lose
  5688. 3:15:21if the computer picks rock well then
  5689. 3:15:23paper covers rock
  5690. 3:15:25and we win now let's test it
  5691. 3:15:29rock paper or scissors again i'll try
  5692. 3:15:32and pick the gun
  5693. 3:15:33which i can't let's take this seriously
  5694. 3:15:35i will pick rock
  5695. 3:15:36the computer picked rock so it's a tie
  5696. 3:15:40i'm feeling like paper today oh it's
  5697. 3:15:43another tie i guess
  5698. 3:15:44and how about scissors okay
  5699. 3:15:48so i win this round i picked uh scissors
  5700. 3:15:50and the computer picked paper
  5701. 3:15:51therefore i win let's try and lose this
  5702. 3:15:54time
  5703. 3:15:55just to verify it so i won again let's
  5704. 3:15:58try rock again
  5705. 3:16:00okay so i lost that round so we know
  5706. 3:16:02that the win conditions are working now
  5707. 3:16:04how about we add a feature where we will
  5708. 3:16:06ask the player if they would like to
  5709. 3:16:08play again
  5710. 3:16:08play another round i think the best way
  5711. 3:16:10to do that would be to write
  5712. 3:16:12all of this code within a while loop so
  5713. 3:16:14at the top
  5714. 3:16:15let's write while true and we'll need to
  5715. 3:16:18indent everything so that it's within
  5716. 3:16:20the while loop so let's just indent
  5717. 3:16:22everything here
  5718. 3:16:39now at the bottom we will ask the user
  5719. 3:16:41if they would like to play again and i
  5720. 3:16:43will store this within a variable
  5721. 3:16:45named play again play again equals input
  5722. 3:16:49and the prompt will be play again
  5723. 3:16:54yes slash no and depending on their
  5724. 3:16:58input
  5725. 3:16:58let's just make it all lower case
  5726. 3:17:02if play again
  5727. 3:17:06does not equal yes
  5728. 3:17:09that means they would like to quit so
  5729. 3:17:11let's use a break statement to break out
  5730. 3:17:14of our while loop that we're in
  5731. 3:17:17and at the end let's print by
  5732. 3:17:20and pay attention to the indentation
  5733. 3:17:23this input and
  5734. 3:17:24this if statement should be within the
  5735. 3:17:25while loop so they should have at least
  5736. 3:17:27one indentation and our print statement
  5737. 3:17:30here we'll just say bye
  5738. 3:17:32and you can see that there is no
  5739. 3:17:33indentation so that is outside of the
  5740. 3:17:35while loop
  5741. 3:17:36okay let's test this one last time
  5742. 3:17:40rock paper scissors i'm going to pick
  5743. 3:17:42the
  5744. 3:17:43cannon okay that's not valid input how
  5745. 3:17:46about rock
  5746. 3:17:47okay the computer picks rock i pick rock
  5747. 3:17:49so it's a tie do i want to play again
  5748. 3:17:52i'm going to select actually yes all
  5749. 3:17:54caps
  5750. 3:17:55that should still be acceptable elbow
  5751. 3:17:57paper this time
  5752. 3:17:59it's a tie again man the computer is
  5753. 3:18:01really good at this game
  5754. 3:18:02i'd like to play again i'll pick
  5755. 3:18:04scissors
  5756. 3:18:06okay so i lose i would like to play
  5757. 3:18:08again and i want to win this time
  5758. 3:18:11okay computer pick scissors i pick rock
  5759. 3:18:14you win do you want to play again
  5760. 3:18:16nah and it will print
  5761. 3:18:19bye well everybody that's a simple game
  5762. 3:18:22of rock paper scissors i'll post all of
  5763. 3:18:24this code to the comment section down
  5764. 3:18:26below
  5765. 3:18:26and well yeah that's a basic game of
  5766. 3:18:29rock paper scissors in python
  5767. 3:18:32hey what's going on everybody it's you
  5768. 3:18:34bro hope you're doing well
  5769. 3:18:36and in this video i'm going to show you
  5770. 3:18:37guys how we can build a basic quiz game
  5771. 3:18:39in python
  5772. 3:18:40so sit back relax and enjoy the show
  5773. 3:18:45to begin this project i think it would
  5774. 3:18:47be wise for us to create a skeletal
  5775. 3:18:48structure for how this program is going
  5776. 3:18:50to operate
  5777. 3:18:51and then we'll fill in the gaps later as
  5778. 3:18:52we go along so to begin
  5779. 3:18:54let's define all of the functions that
  5780. 3:18:56we'll need we'll need four
  5781. 3:18:58we'll create a function that is named
  5782. 3:19:00new game
  5783. 3:19:01whenever we call this function it will
  5784. 3:19:02create a new quiz game for us
  5785. 3:19:05and for the time being let's write pass
  5786. 3:19:07just as a placeholder
  5787. 3:19:08we'll have a total of four functions new
  5788. 3:19:10game
  5789. 3:19:12a function to check our answer
  5790. 3:19:16a function to display our score
  5791. 3:19:21and lastly a function to
  5792. 3:19:24play again
  5793. 3:19:30all right now i'm just going to add some
  5794. 3:19:33comments here
  5795. 3:19:34just to separate each of these functions
  5796. 3:19:36that we have just so that it's a little
  5797. 3:19:37bit easier to read
  5798. 3:19:38although this part is not really
  5799. 3:19:40necessary so i just want to separate
  5800. 3:19:42each of these functions for some
  5801. 3:19:44readability
  5802. 3:19:47okay those are all the functions that
  5803. 3:19:49we'll need what we'll need now
  5804. 3:19:51is some sort of collection to hold all
  5805. 3:19:53of the questions and answers that we
  5806. 3:19:55have and i think a dictionary would be
  5807. 3:19:57perfect for this
  5808. 3:19:58so what i did is that i created a
  5809. 3:20:00dictionary named questions
  5810. 3:20:02a dictionary has key value pairs
  5811. 3:20:05each key is a question that i would like
  5812. 3:20:07to ask and each question
  5813. 3:20:08has an associated value we'll have the
  5814. 3:20:11user guess between
  5815. 3:20:12answers of a b c or d so these would be
  5816. 3:20:16all of the correct answers all of the
  5817. 3:20:17values within this dictionary
  5818. 3:20:19so here are some of the questions that i
  5819. 3:20:20want to ask feel free to come up with
  5820. 3:20:22your own questions
  5821. 3:20:23if you would like you can copy the
  5822. 3:20:25questions that i have they should be
  5823. 3:20:26posted in the comment section
  5824. 3:20:28down below so the questions that i'm
  5825. 3:20:30going to ask are
  5826. 3:20:31who created python what year was python
  5827. 3:20:33created
  5828. 3:20:34python is attributed to which comedy
  5829. 3:20:36group and lastly is the earth round
  5830. 3:20:38i couldn't think of a fourth question so
  5831. 3:20:40i just threw in a random stupid question
  5832. 3:20:43and the correct answers to all of these
  5833. 3:20:45questions in order would be
  5834. 3:20:46a b c and a but feel free to come up
  5835. 3:20:50with your own
  5836. 3:20:51well we have our questions but we'll
  5837. 3:20:53need some sort of collection to hold all
  5838. 3:20:55of the different possible answers
  5839. 3:20:57to each of these questions and i think a
  5840. 3:20:592d list would work perfect for this
  5841. 3:21:02so i have a list of lists a list of
  5842. 3:21:05tuples could work too i suppose
  5843. 3:21:07so here are all of the answers for the
  5844. 3:21:09first question
  5845. 3:21:10and the correct answer is a i believe
  5846. 3:21:13his name is
  5847. 3:21:14guido van rossum hey if you're listening
  5848. 3:21:16man i'm sorry if i'm mispronouncing your
  5849. 3:21:18name i'm really trying here
  5850. 3:21:20the second list corresponds to the
  5851. 3:21:21second question
  5852. 3:21:23what year was python created it was
  5853. 3:21:24created in 1991
  5854. 3:21:26at least according to wikipedia python
  5855. 3:21:29is attributed to which comedy group
  5856. 3:21:31the answer is c monty python like monty
  5857. 3:21:34python and the holy grail
  5858. 3:21:35good movie by the way and lastly is the
  5859. 3:21:38earth round
  5860. 3:21:39this is a highly debated topic but the
  5861. 3:21:41answer is hey
  5862. 3:21:42the earth is in fact round so i have a
  5863. 3:21:45list of lists
  5864. 3:21:47each list corresponds to a key value
  5865. 3:21:49pair within my dictionary of questions
  5866. 3:21:52and this is a lot to type if you want
  5867. 3:21:54you can just copy the code that i posted
  5868. 3:21:56in the comments down below
  5869. 3:21:57just copy and paste and you'll have all
  5870. 3:21:59of this or if you want you can pause the
  5871. 3:22:01video and type it
  5872. 3:22:02i don't care so now that you have your
  5873. 3:22:04questions and your options we're ready
  5874. 3:22:05to begin
  5875. 3:22:06so the first thing that we'll do within
  5876. 3:22:07our program is to call the
  5877. 3:22:09new game function to begin a new game so
  5878. 3:22:12after
  5879. 3:22:12we have our questions and options
  5880. 3:22:14created let's create a
  5881. 3:22:16new game by calling the new game
  5882. 3:22:19function
  5883. 3:22:20now when we run this program we'll
  5884. 3:22:22generate our dictionary of questions
  5885. 3:22:24and our 2d list of different options for
  5886. 3:22:27each question
  5887. 3:22:28and then we will call the new game
  5888. 3:22:29function to begin a new game
  5889. 3:22:31so let's head to the new game function
  5890. 3:22:33and fill in everything within our new
  5891. 3:22:35game function
  5892. 3:22:36so at the top let's declare a few things
  5893. 3:22:39let's say we have
  5894. 3:22:40a list named guesses and guesses will be
  5895. 3:22:43an empty list for now
  5896. 3:22:45i will declare a variable called correct
  5897. 3:22:48guesses set it equal to zero because we
  5898. 3:22:51haven't guessed anything yet
  5899. 3:22:52and we will set a current question
  5900. 3:22:55number
  5901. 3:22:56and set the sequel to one to represent
  5902. 3:22:58the first question
  5903. 3:23:00okay now we need to display all of the
  5904. 3:23:03questions within our dictionary of
  5905. 3:23:05questions
  5906. 3:23:06and we can use a for loop for that so
  5907. 3:23:09for
  5908. 3:23:10key in questions
  5909. 3:23:14i'm going to print my key
  5910. 3:23:20and let's just test this okay yeah
  5911. 3:23:23here's all my questions
  5912. 3:23:25i think what i'll do is actually print
  5913. 3:23:28something to separate each question
  5914. 3:23:30let's say one of these lines i think it
  5915. 3:23:34would look better with them
  5916. 3:23:36okay i'll print this line and then we'll
  5917. 3:23:38move on to a question
  5918. 3:23:40all right now after re-running this we
  5919. 3:23:42have each question and i added just a
  5920. 3:23:44line break between each of these to make
  5921. 3:23:46it look kind of fancy
  5922. 3:23:47now i need to display all of the
  5923. 3:23:49different options for each question
  5924. 3:23:51and we can do that with a nested for
  5925. 3:23:54loop
  5926. 3:23:55so if i were to write i in
  5927. 3:23:59options print i
  5928. 3:24:02let me show you what happens now with
  5929. 3:24:05the way this for loop is written now
  5930. 3:24:07it's going to display
  5931. 3:24:08all of the different options available
  5932. 3:24:10to us for the entire quiz game
  5933. 3:24:12what i would like instead is to display
  5934. 3:24:14only the first
  5935. 3:24:16list for the first question and then the
  5936. 3:24:18second list
  5937. 3:24:19for the second question and follow that
  5938. 3:24:21pattern so we're going to change our for
  5939. 3:24:23loop around
  5940. 3:24:244i in options and we'll set an index for
  5941. 3:24:27options
  5942. 3:24:28the index is going to be our question
  5943. 3:24:30number
  5944. 3:24:31minus one now remember what i said in
  5945. 3:24:34previous videos where different
  5946. 3:24:36collections including
  5947. 3:24:37lists tuples etc the first element in a
  5948. 3:24:40collection
  5949. 3:24:41has an index of zero because computers
  5950. 3:24:43always start with zero
  5951. 3:24:44then the next element would therefore
  5952. 3:24:46have an index of one
  5953. 3:24:48then two three then you follow that
  5954. 3:24:50pattern we're using this question number
  5955. 3:24:52variable
  5956. 3:24:53as some sort of counter and since we
  5957. 3:24:55initially set this to one
  5958. 3:24:57i'm just going to subtract one so that
  5959. 3:24:59we effectively
  5960. 3:25:00receive zero as the index and now we
  5961. 3:25:03just need to increment our question
  5962. 3:25:04number
  5963. 3:25:05after each iteration so let's do that
  5964. 3:25:08i will add that to the end here after we
  5965. 3:25:11finish displaying all of the options
  5966. 3:25:13let's increment question number by one
  5967. 3:25:16by typing question num plus equals one
  5968. 3:25:20and if i were to run this currently
  5969. 3:25:23we'll display all of the different
  5970. 3:25:24options for each question
  5971. 3:25:26who created python here's all of the
  5972. 3:25:28associated options
  5973. 3:25:30what year is python created python is
  5974. 3:25:32attributed to which comedy group
  5975. 3:25:34and lastly is the earth round now it's
  5976. 3:25:37time for some user input
  5977. 3:25:38so i'm going to create a variable called
  5978. 3:25:41guess
  5979. 3:25:42and make sure you don't put it within
  5980. 3:25:44your inner for loop it should be within
  5981. 3:25:46the outer for loop
  5982. 3:25:48guess equals input and i'll create a
  5983. 3:25:51prompt
  5984. 3:25:53enter a b
  5985. 3:25:56c or d
  5986. 3:26:00here's something to consider we would
  5987. 3:26:02like the user to type in either capital
  5988. 3:26:04a
  5989. 3:26:04b c or d with strings they're case
  5990. 3:26:07sensitive what if the user typed in
  5991. 3:26:09one of these letters but lowercase if
  5992. 3:26:12they're correct we would still like to
  5993. 3:26:13give them their point
  5994. 3:26:14why don't we take our guess and make it
  5995. 3:26:16uppercase
  5996. 3:26:17so guess equals guess and we can make
  5997. 3:26:20this uppercase
  5998. 3:26:21using the upper method of strings at the
  5999. 3:26:24end of this game i'm going to compare
  6000. 3:26:26our guesses to the correct answers so we
  6001. 3:26:29have an empty
  6002. 3:26:30list named guesses and i'm going to
  6003. 3:26:33append our current guest that we're
  6004. 3:26:34working on
  6005. 3:26:35to our list of guesses so guesses
  6006. 3:26:39dot append and we will append our
  6007. 3:26:42guess this round now that we have our
  6008. 3:26:45guess
  6009. 3:26:46and we've appended our guess to our list
  6010. 3:26:48of guesses
  6011. 3:26:49let's check to see if it's the correct
  6012. 3:26:51answer or not so we're just about to
  6013. 3:26:53fill in
  6014. 3:26:53this check answer function next
  6015. 3:26:57so we are going to call this function
  6016. 3:26:59and pass in a few items as arguments
  6017. 3:27:02so let's use the check answer function
  6018. 3:27:07and we'll pass in the key for the
  6019. 3:27:10current question that we're on
  6020. 3:27:12so the key is the correct answer so that
  6021. 3:27:14would be
  6022. 3:27:15our questions
  6023. 3:27:18dot get key
  6024. 3:27:23this would be the answer and we'll also
  6025. 3:27:25pass in
  6026. 3:27:26our guess and we are going to fill in
  6027. 3:27:30the check answer function but we need to
  6028. 3:27:32set up the parameters
  6029. 3:27:34we're receiving effectively our answer
  6030. 3:27:37as well as our guess so i'm going to
  6031. 3:27:41name these as
  6032. 3:27:42answer and guess and we are going to
  6033. 3:27:45check to see if
  6034. 3:27:46our answer is equal to our guess
  6035. 3:27:50if answer is equal
  6036. 3:27:53to guess let's print something
  6037. 3:27:57print correct
  6038. 3:28:03and i think we should give the user a
  6039. 3:28:04point we'll have our check answer
  6040. 3:28:06function return
  6041. 3:28:08one for one point return one
  6042. 3:28:12else if this is not the correct answer
  6043. 3:28:15let's print
  6044. 3:28:17wrong and we will return
  6045. 3:28:20zero they do not get a point
  6046. 3:28:24and since this is returning a value we
  6047. 3:28:26should assign that
  6048. 3:28:27so let's assign the point we may or may
  6049. 3:28:30not receive
  6050. 3:28:31to our variable of correct guesses which
  6051. 3:28:33is initially set to zero
  6052. 3:28:35so we'll type correct guesses plus
  6053. 3:28:38equals check answer and check answer
  6054. 3:28:41will return
  6055. 3:28:42one if we scored a point or zero if we
  6056. 3:28:44did not score a point and make sure you
  6057. 3:28:46have
  6058. 3:28:47plus equals because if you just set this
  6059. 3:28:49to equals
  6060. 3:28:50then we cannot score more than one point
  6061. 3:28:52it will either be
  6062. 3:28:53zero or one so we are effectively
  6063. 3:28:56incrementing our correct
  6064. 3:28:57guesses by one for each point that we
  6065. 3:29:00score
  6066. 3:29:01and now we're going to work on the
  6067. 3:29:03display score
  6068. 3:29:04function and we'll call that at the end
  6069. 3:29:06after we finish all of our questions
  6070. 3:29:08that we have
  6071. 3:29:09so make sure that you do not write this
  6072. 3:29:10within the for loop it should be outside
  6073. 3:29:13of it
  6074. 3:29:13because once we finish iterating through
  6075. 3:29:16all of our questions we're going to
  6076. 3:29:17display a final score
  6077. 3:29:19so let's call the display score
  6078. 3:29:22function and we'll need to pass in some
  6079. 3:29:25arguments
  6080. 3:29:26our correct guesses as well
  6081. 3:29:29as our list of guesses
  6082. 3:29:33okay let's head to the display score
  6083. 3:29:36function
  6084. 3:29:37so we have as parameters
  6085. 3:29:40correct guesses as well as
  6086. 3:29:45guesses i need to remove this pass i'm
  6087. 3:29:48going to add
  6088. 3:29:49one of these fancy lines in just to
  6089. 3:29:51separate the questions
  6090. 3:29:52from the results and i will print
  6091. 3:29:56results
  6092. 3:29:59and maybe another one of these lines
  6093. 3:30:02okay
  6094. 3:30:02so i need to print all of the answers
  6095. 3:30:08answers
  6096. 3:30:11and i do not want to end on a new line
  6097. 3:30:14so i'm going to set
  6098. 3:30:15end equal to nothing and i need to
  6099. 3:30:19display all of the values within
  6100. 3:30:22our dictionary all of the answers i will
  6101. 3:30:25do that using
  6102. 3:30:26a for loop for i
  6103. 3:30:29in questions
  6104. 3:30:33print questions
  6105. 3:30:37dot get i
  6106. 3:30:41and i do not want to end on a new line
  6107. 3:30:44so i'm going to set
  6108. 3:30:45end equal to nothing effectively
  6109. 3:30:48and then i'll print a new line okay
  6110. 3:30:50let's work on the guesses i think i'm
  6111. 3:30:52just going to copy all this
  6112. 3:30:53and make some changes i'll replace
  6113. 3:30:56answers
  6114. 3:30:57with guesses for
  6115. 3:31:00i in guesses
  6116. 3:31:04print i
  6117. 3:31:07okay let's just be sure that
  6118. 3:31:09everything's working we're not
  6119. 3:31:10calculating a score
  6120. 3:31:12quite yet so i'm just going to answer a
  6121. 3:31:16then b c and d
  6122. 3:31:19on second thought after printing each of
  6123. 3:31:22these answers and guesses
  6124. 3:31:23i'm going to add a space after each of
  6125. 3:31:25these so
  6126. 3:31:26within each of these four loops at the
  6127. 3:31:28end i will add a space
  6128. 3:31:30let me try that again a b
  6129. 3:31:33c d okay not too bad
  6130. 3:31:37now let's calculate the final score and
  6131. 3:31:39we're still within our display score
  6132. 3:31:41function
  6133. 3:31:43let's set score equal to
  6134. 3:31:46correct
  6135. 3:31:49guesses divided by
  6136. 3:31:53the length of our questions
  6137. 3:31:58and let me add a set of parentheses
  6138. 3:32:00around here
  6139. 3:32:03then i'm going to multiply our score by
  6140. 3:32:06100
  6141. 3:32:08and if you don't want a decimal portion
  6142. 3:32:12because we'll display a percentage we
  6143. 3:32:14can cast this as an end
  6144. 3:32:16okay that all looks good and we'll print
  6145. 3:32:19the final score
  6146. 3:32:21print your score
  6147. 3:32:25is plus
  6148. 3:32:29we need to cast our score to a string
  6149. 3:32:31because we're using string concatenation
  6150. 3:32:34plus i'll add a percent sign
  6151. 3:32:38all right let's try it i'm intentionally
  6152. 3:32:40going to get the last question wrong
  6153. 3:32:42so we should have a 75 if we have four
  6154. 3:32:45questions
  6155. 3:32:46so that would be a b c
  6156. 3:32:50is the earth round what's earth i'm
  6157. 3:32:52gonna guess d
  6158. 3:32:54answers a b c a your guess is a b c
  6159. 3:32:57d your score is 75 okay let's test it
  6160. 3:33:01but getting all wrong answers d d d
  6161. 3:33:04d your score is zero percent
  6162. 3:33:07congratulations
  6163. 3:33:08and we'll test it one more time but get
  6164. 3:33:10all the right answers
  6165. 3:33:12we just want to be sure that we have 100
  6166. 3:33:14that's a
  6167. 3:33:15b c a your score is 100
  6168. 3:33:20now the last thing that you can add is
  6169. 3:33:22that we can play again if we want
  6170. 3:33:24and i have a separate function for that
  6171. 3:33:26if you would like to add this option
  6172. 3:33:27here's how to do so
  6173. 3:33:28let's remove that pass
  6174. 3:33:32let's set a variable named response
  6175. 3:33:34equal to
  6176. 3:33:36some input do you
  6177. 3:33:40want to play again
  6178. 3:33:45let's ask for yes or no
  6179. 3:33:50actually let me remove that okay
  6180. 3:33:55and depending on the user's response it
  6181. 3:33:56might be a lowercase so let's make it
  6182. 3:33:58uppercase
  6183. 3:33:59response equals response
  6184. 3:34:02and use the upper method
  6185. 3:34:07if response is equal to
  6186. 3:34:10yes all caps if response is equal to yes
  6187. 3:34:14then return true
  6188. 3:34:18else we will return
  6189. 3:34:22false and that is it for this function
  6190. 3:34:25the last thing we need to do
  6191. 3:34:26is to create a while loop that will
  6192. 3:34:28continue to ask the player if they want
  6193. 3:34:30to play again
  6194. 3:34:31this will be after we call the new game
  6195. 3:34:33function
  6196. 3:34:34while play again
  6197. 3:34:38and remember this will return either
  6198. 3:34:40true or
  6199. 3:34:41false depending on the user's response
  6200. 3:34:44while
  6201. 3:34:44play again new game
  6202. 3:34:48and call the new game function to create
  6203. 3:34:50a new game for us
  6204. 3:34:52if we escape the while loop that means
  6205. 3:34:54the user doesn't want to play again
  6206. 3:34:56so let's print the message
  6207. 3:34:59such as by e
  6208. 3:35:02all right let's test it i'm going to get
  6209. 3:35:06all of the correct answers this time a b
  6210. 3:35:10c a all right your score is 100
  6211. 3:35:14do you want to play again i'm going to
  6212. 3:35:16type
  6213. 3:35:17yes all right then we can play again
  6214. 3:35:22d d d d your score is zero percent do
  6215. 3:35:26you want to play again
  6216. 3:35:27no i think i mastered this game
  6217. 3:35:30bye all right everybody so that's how
  6218. 3:35:33you can use python to create a simple
  6219. 3:35:36quiz game
  6220. 3:35:37i will post all of this code to the
  6221. 3:35:38comment section down below
  6222. 3:35:40but well yeah that's a basic quiz game
  6223. 3:35:43in python for you all
  6224. 3:35:46hey what's going on everybody it's bro
  6225. 3:35:48hope you're doing well
  6226. 3:35:49and in this video i'm going to explain
  6227. 3:35:51object-oriented programming in python so
  6228. 3:35:53sit back relax and enjoy the show
  6229. 3:35:58welcome to today's topic on python
  6230. 3:36:01object oriented programming
  6231. 3:36:03also known as pop and in today's video
  6232. 3:36:06we're going to be creating objects
  6233. 3:36:07an object is an instance of a class by
  6234. 3:36:10using programming we can create
  6235. 3:36:12representations of real-life objects so
  6236. 3:36:15look around you wherever you're sitting
  6237. 3:36:16or standing right now you are surrounded
  6238. 3:36:19by objects next to me i have a phone
  6239. 3:36:21a television some snacks and i'm talking
  6240. 3:36:24into a microphone
  6241. 3:36:25point being we can use programming to
  6242. 3:36:27mimic real world objects by assigning a
  6243. 3:36:30combination of attributes
  6244. 3:36:32what an object is or has and methods
  6245. 3:36:35what an object can do now in order to
  6246. 3:36:37create an object we'll need to create a
  6247. 3:36:39class
  6248. 3:36:40a class can function as a blueprint that
  6249. 3:36:41will describe what attributes and
  6250. 3:36:43methods
  6251. 3:36:44that our distinct type of object will
  6252. 3:36:46have you can either create your class
  6253. 3:36:48within your main module
  6254. 3:36:49or you could create a separate file
  6255. 3:36:51dedicated solely for your class
  6256. 3:36:53now to create a class we would type
  6257. 3:36:55class and then the name of the object
  6258. 3:36:57that we would like to create so today i
  6259. 3:36:58would like to create some car objects
  6260. 3:37:00because i like cars
  6261. 3:37:02so we would type car and a common naming
  6262. 3:37:04convention
  6263. 3:37:05with class names is that they should be
  6264. 3:37:07capital so car is going to have a
  6265. 3:37:09capital c
  6266. 3:37:10and for the time being we need to type
  6267. 3:37:11in something i'll just type in pass as a
  6268. 3:37:13placeholder
  6269. 3:37:14so if you have a small program it may be
  6270. 3:37:16better to write your class within your
  6271. 3:37:18main
  6272. 3:37:19module but if your class is fairly large
  6273. 3:37:22you may want to consider placing your
  6274. 3:37:24class within a separate module
  6275. 3:37:26so if you were to take that route we
  6276. 3:37:28would go to file
  6277. 3:37:29new python file and we would name this
  6278. 3:37:32car
  6279. 3:37:32click python file and we would declare
  6280. 3:37:35our class within
  6281. 3:37:36the separate module so class car and for
  6282. 3:37:39now i'll type in pass
  6283. 3:37:40then we just need to import this class
  6284. 3:37:42so within our main module we would type
  6285. 3:37:44from the name of the module import the
  6286. 3:37:47name of the class
  6287. 3:37:48from car import car now objects can have
  6288. 3:37:51some combination of attributes and
  6289. 3:37:53methods attributes describe what an
  6290. 3:37:55object is or has
  6291. 3:37:56so what are a few attributes that cars
  6292. 3:37:58might have they might have a make so
  6293. 3:38:00let's create a few variables
  6294. 3:38:02for the time being i'm just going to use
  6295. 3:38:04none as a placeholder
  6296. 3:38:06cars can have a model a year
  6297. 3:38:10and a color
  6298. 3:38:14now objects can also have methods what
  6299. 3:38:16kinds of methods could cars perform
  6300. 3:38:18perhaps a drive method and a stop method
  6301. 3:38:20so let's define those
  6302. 3:38:22def let's say drive and we'll have one
  6303. 3:38:25argument
  6304. 3:38:26self self refers to the object that is
  6305. 3:38:28using this method
  6306. 3:38:30now what do we want to do when we call
  6307. 3:38:31this method
  6308. 3:38:33let's say this car
  6309. 3:38:36is driving let's create a stop method as
  6310. 3:38:39well
  6311. 3:38:40def stop
  6312. 3:38:43and we will print this
  6313. 3:38:46car is stopped we now have all of the
  6314. 3:38:49different attributes and methods we
  6315. 3:38:51would like our car objects to have but
  6316. 3:38:53there is one more thing that we need
  6317. 3:38:54it is a special method called the init
  6318. 3:38:57method that will construct
  6319. 3:38:58objects for us in other programming
  6320. 3:39:00languages this is known as the
  6321. 3:39:02constructor
  6322. 3:39:03so we need a special method that will
  6323. 3:39:05create objects for us
  6324. 3:39:06so we need to define this method def and
  6325. 3:39:08the syntax on this is somewhat strange
  6326. 3:39:10it is
  6327. 3:39:11two underscores init it's short for
  6328. 3:39:14initialize
  6329. 3:39:14200 scores again and then we need at
  6330. 3:39:16least self as an argument
  6331. 3:39:18and then we can actually assign our car
  6332. 3:39:21objects unique variables so take all of
  6333. 3:39:24these attributes that we have
  6334. 3:39:26and we're going to place them within
  6335. 3:39:27this init method now we can receive
  6336. 3:39:30arguments
  6337. 3:39:30when we create car objects but we need
  6338. 3:39:33to pass them in
  6339. 3:39:34as arguments to our init method so we
  6340. 3:39:36need to set up some parameters
  6341. 3:39:38let's say in order to create a car
  6342. 3:39:39object we need a make
  6343. 3:39:42a model a year and a color
  6344. 3:39:45then when we receive these arguments we
  6345. 3:39:47can actually assign them to
  6346. 3:39:49each car's specific attributes but we
  6347. 3:39:52need to precede each of these with self
  6348. 3:39:54self is referring to the object that
  6349. 3:39:56we're currently working on
  6350. 3:39:57or creating so self.make self.model
  6351. 3:40:01self.ear and self.color
  6352. 3:40:05and then when we assign these we're
  6353. 3:40:07going to say self.make
  6354. 3:40:08equals whatever make that we receive
  6355. 3:40:11when it's passed in as an argument
  6356. 3:40:12self.model equals model and continue
  6357. 3:40:15that pattern for year
  6358. 3:40:17and color all right that is all we need
  6359. 3:40:20for our class car we have a constructor
  6360. 3:40:23we're assigning
  6361. 3:40:24arguments that we receive to the
  6362. 3:40:26attributes of our car object
  6363. 3:40:28and we also have two methods one for
  6364. 3:40:30drive and one for stop
  6365. 3:40:32now we can start creating some car
  6366. 3:40:33objects so going back to my main file
  6367. 3:40:36here
  6368. 3:40:36to create an object we need a unique
  6369. 3:40:38name for it let's call this first car
  6370. 3:40:40just car one
  6371. 3:40:41equals the name of the class car then a
  6372. 3:40:44set of parentheses
  6373. 3:40:46now in order to construct a car object
  6374. 3:40:48we need to pass in a matching
  6375. 3:40:50set of arguments you can see here that
  6376. 3:40:52we need to pass in a make a model a year
  6377. 3:40:54and a color
  6378. 3:40:55so what kind of car should we make let's
  6379. 3:40:57say that the make is going to be
  6380. 3:40:59chevy we need a model
  6381. 3:41:03perhaps corvette a year
  6382. 3:41:072021 is good and a color let's say blue
  6383. 3:41:10alright now let's actually access some
  6384. 3:41:13of these cars attributes
  6385. 3:41:15and i should probably put this one in a
  6386. 3:41:16print statement so print car ones
  6387. 3:41:19make and this should print chevy
  6388. 3:41:23then we can do the same thing for model
  6389. 3:41:25year and color
  6390. 3:41:26so let's change some of these around
  6391. 3:41:29model
  6392. 3:41:30year and color
  6393. 3:41:35yep this object is a chevy corvette 2021
  6394. 3:41:38and the color is blue
  6395. 3:41:39and we should have two methods as well
  6396. 3:41:41so car one
  6397. 3:41:42i would like this car to use its drive
  6398. 3:41:44method
  6399. 3:41:46this car is driving and let's have this
  6400. 3:41:48car use it stop method
  6401. 3:41:50car one dot stop
  6402. 3:41:55this car is stopped there's one thing
  6403. 3:41:56that i should mention real quick because
  6404. 3:41:58i forgot to mention it earlier
  6405. 3:42:00so within our init method you can see
  6406. 3:42:02that we need five
  6407. 3:42:03arguments in order to construct a car
  6408. 3:42:05object self
  6409. 3:42:06make model year in color but when we
  6410. 3:42:09pass in
  6411. 3:42:10our arguments we're not passing in
  6412. 3:42:12anything for self we only have
  6413. 3:42:13four arguments here that's because with
  6414. 3:42:16python we do not need to pass itself
  6415. 3:42:18that's done automatically for us we're
  6416. 3:42:20referring to the object that we're
  6417. 3:42:22dealing with
  6418. 3:42:23as you can see with our drive and stop
  6419. 3:42:25method we need
  6420. 3:42:26to pass in self in order to execute our
  6421. 3:42:29method
  6422. 3:42:30but when we call this method we do not
  6423. 3:42:31need to do so so just remember with
  6424. 3:42:34python with self we do not need to pass
  6425. 3:42:36in anything for this argument that is
  6426. 3:42:38all
  6427. 3:42:38now the nice thing about this is that we
  6428. 3:42:40can reuse this class
  6429. 3:42:42as a blueprint to create more car
  6430. 3:42:43objects we just call that
  6431. 3:42:45init method that constructor so this
  6432. 3:42:47time let's create a second car called
  6433. 3:42:49car
  6434. 3:42:492 and this will be a different kind of
  6435. 3:42:51car
  6436. 3:42:53what kinds of arguments should we pass
  6437. 3:42:55it let's say this is a ford
  6438. 3:42:58mustang the year will be 2022 and the
  6439. 3:43:01color will be red
  6440. 3:43:03all right let's check card to use
  6441. 3:43:05attributes
  6442. 3:43:08and let's have car 2 use its drive and
  6443. 3:43:10stop method
  6444. 3:43:12this is a ford mustang the year is 2022
  6445. 3:43:15the color is red
  6446. 3:43:15this car is driving and this car is
  6447. 3:43:17stopped one other thing that we can do
  6448. 3:43:19too
  6449. 3:43:20is that within our methods here let's
  6450. 3:43:22replace car with
  6451. 3:43:23the name of the model that we're working
  6452. 3:43:25with so this
  6453. 3:43:27plus self dot model
  6454. 3:43:31plus is driving now this self keyword
  6455. 3:43:34think of it as you're replacing self
  6456. 3:43:37with the name of the object that we're
  6457. 3:43:39working on if car one
  6458. 3:43:41is using its drive method replace itself
  6459. 3:43:44with car one
  6460. 3:43:45if this is car two that called this
  6461. 3:43:47method then replace itself with car 2.
  6462. 3:43:50think of it that way and let's do the
  6463. 3:43:51same thing for stop
  6464. 3:43:54this plus self dot model
  6465. 3:43:58plus is stopped all right
  6466. 3:44:01probably don't need these anymore so
  6467. 3:44:03let's have car one
  6468. 3:44:04use its drive and stop method this
  6469. 3:44:07corvette is driving this corvette is
  6470. 3:44:09stopped
  6471. 3:44:09let's try this with car two this mustang
  6472. 3:44:13is driving
  6473. 3:44:13this mustang is stopped now you can have
  6474. 3:44:16them do things independently too let's
  6475. 3:44:18have car 1 use its drive method and car
  6476. 3:44:202 uses stop method
  6477. 3:44:22this corvette is driving and this
  6478. 3:44:24mustang is stopped in conclusion
  6479. 3:44:26a class can function as a blueprint to
  6480. 3:44:28create objects
  6481. 3:44:29we can assign attributes what describe
  6482. 3:44:32an object is or
  6483. 3:44:33has and methods what each object
  6484. 3:44:36can do and then within our class we have
  6485. 3:44:38a special method called the init method
  6486. 3:44:41we can pass in some arguments and assign
  6487. 3:44:44these arguments to each
  6488. 3:44:45object's attributes and then we can
  6489. 3:44:47reuse this class as if it was a
  6490. 3:44:49blueprint
  6491. 3:44:50so we can create more objects out of it
  6492. 3:44:53so
  6493. 3:44:53that is the basics of object oriented
  6494. 3:44:56programming in python if you would like
  6495. 3:44:58a copy of all this code i will post this
  6496. 3:45:00in the comment section down below
  6497. 3:45:01but yeah that's the basics of object
  6498. 3:45:04oriented programming
  6499. 3:45:05in python hey what's going on everybody
  6500. 3:45:08it's bro hope you're doing well and in
  6501. 3:45:10this video i'm going to explain
  6502. 3:45:12the basics of class variables in python
  6503. 3:45:14so sit back
  6504. 3:45:15relax and enjoy the show
  6505. 3:45:19here's a super quick video on the
  6506. 3:45:21differences between class and instance
  6507. 3:45:23variables
  6508. 3:45:24for this example let's say we have two
  6509. 3:45:26car objects
  6510. 3:45:27named car one and car two and i'm just
  6511. 3:45:29passing in a bunch of values to each of
  6512. 3:45:31these car constructors
  6513. 3:45:33now within my class car i have my init
  6514. 3:45:36method
  6515. 3:45:36also known as the constructor and what
  6516. 3:45:38i'm doing is assigning all of these
  6517. 3:45:40values
  6518. 3:45:40to the variables found within my car
  6519. 3:45:43constructor
  6520. 3:45:44these variables declared inside the
  6521. 3:45:46constructor
  6522. 3:45:47are known as instance variables and each
  6523. 3:45:50object
  6524. 3:45:50can have their own unique values
  6525. 3:45:53assigned to each
  6526. 3:45:54of these variables now where a class
  6527. 3:45:56variable is different
  6528. 3:45:57is that a class variable is declared
  6529. 3:46:00inside the class but outside of the
  6530. 3:46:02constructor and what we can do
  6531. 3:46:04is set some default values for some
  6532. 3:46:06variables
  6533. 3:46:07let's say we would like to add an
  6534. 3:46:08additional variable let's say wheels
  6535. 3:46:10wheels equals and we can set a default
  6536. 3:46:13value
  6537. 3:46:14for all instances of this class for each
  6538. 3:46:16car object that we create
  6539. 3:46:18so we set wheels to equal four this is
  6540. 3:46:20an example of a class
  6541. 3:46:22variable whenever we create a car object
  6542. 3:46:25we can pass in a unique
  6543. 3:46:26make model year in color but by default
  6544. 3:46:29they will all have
  6545. 3:46:30four wheels now if i was to print the
  6546. 3:46:33amount of wheels
  6547. 3:46:34that car 1 and car 2 have they will both
  6548. 3:46:37have
  6549. 3:46:38four wheels it's as if we're setting a
  6550. 3:46:40default value
  6551. 3:46:41if i change this to two wheels well then
  6552. 3:46:43these cars have two wheels
  6553. 3:46:45maybe their motorcycles or something now
  6554. 3:46:47you can actually
  6555. 3:46:48change these values each object will
  6556. 3:46:50have their own
  6557. 3:46:51copy of this variable but you can set it
  6558. 3:46:53to a default value
  6559. 3:46:54so let's say car one
  6560. 3:46:58is a motorcycle car one dot wheels
  6561. 3:47:01equals two so if i were to print car
  6562. 3:47:04one's wheels along with card two
  6563. 3:47:06car one will have two wheels but car two
  6564. 3:47:09is still using that default amount of
  6565. 3:47:11wheels which is four
  6566. 3:47:12now there's another way in which you can
  6567. 3:47:14access a class variable
  6568. 3:47:16you don't necessarily need to create an
  6569. 3:47:18object to do so you can just use the
  6570. 3:47:19name of the class
  6571. 3:47:20so what i'm going to do is turn all of
  6572. 3:47:23these lines into comments
  6573. 3:47:25and i will print the amount of wheels
  6574. 3:47:28that our class has so type in the name
  6575. 3:47:31of the class car with a capital c
  6576. 3:47:33it should be the same name as your class
  6577. 3:47:35name followed by the name of the class
  6578. 3:47:37variable
  6579. 3:47:38car dot wheels but make sure you spell
  6580. 3:47:41it right
  6581. 3:47:42all right so this will display four
  6582. 3:47:44wheels
  6583. 3:47:45now what if we decide to change the
  6584. 3:47:47class variable
  6585. 3:47:48through our class well that will affect
  6586. 3:47:51all
  6587. 3:47:51instances of our class so let's say car
  6588. 3:47:55dot wheels equals two it's as if we
  6589. 3:47:59change our mind and we have decided to
  6590. 3:48:01create motorcycles instead of cars
  6591. 3:48:03well if we were to change the wheels
  6592. 3:48:06class variable through the use of our
  6593. 3:48:08car class
  6594. 3:48:09well then it's going to affect it for
  6595. 3:48:11all instances of this class
  6596. 3:48:13you can see that both car 1 and car 2
  6597. 3:48:15now have two wheels
  6598. 3:48:16when we just changed car 1's wheels it
  6599. 3:48:19did not affect
  6600. 3:48:20car 2 at all so that's the main
  6601. 3:48:22difference between a class variable and
  6602. 3:48:24an instance variable
  6603. 3:48:25an instance variable is declared inside
  6604. 3:48:27of constructor
  6605. 3:48:28and they can be given unique values with
  6606. 3:48:31class variables
  6607. 3:48:32they are declared within a class but
  6608. 3:48:34outside of the constructor
  6609. 3:48:35and you can set a default value for all
  6610. 3:48:38instances of this class
  6611. 3:48:40for all unique objects that are created
  6612. 3:48:42and then you can change those values
  6613. 3:48:43later too
  6614. 3:48:44so if you would like a copy of all this
  6615. 3:48:46code i will post all of this in the
  6616. 3:48:48comment section down below
  6617. 3:48:49but yeah that is the basics of class
  6618. 3:48:51variables
  6619. 3:48:52in python hey what's going on everybody
  6620. 3:48:56it's you bro hope you're doing well and
  6621. 3:48:57in this video i'm going to explain
  6622. 3:48:59inheritance in python so
  6623. 3:49:01sit back relax and enjoy the show
  6624. 3:49:06let's talk about irritants the term
  6625. 3:49:08inherit means to receive derive or
  6626. 3:49:10be left with and we can apply this
  6627. 3:49:11concept to programming classes can
  6628. 3:49:13inherit something
  6629. 3:49:14usually attributes and methods from
  6630. 3:49:16another class these classes can form
  6631. 3:49:18parent-child relationships where a child
  6632. 3:49:21will receive everything
  6633. 3:49:22that the parent class has much like you
  6634. 3:49:24inherit jeans from your parents
  6635. 3:49:26and classes can have children and give
  6636. 3:49:28whatever they own to their children
  6637. 3:49:30and in today's video we'll be creating a
  6638. 3:49:32parent class called animal
  6639. 3:49:33and children of the animal class will
  6640. 3:49:35inherit the common attributes and
  6641. 3:49:37methods
  6642. 3:49:37that all animals might have to kick off
  6643. 3:49:39this video i think what we'll do
  6644. 3:49:41is keep all of our classes within the
  6645. 3:49:42same file just so that it's easier for
  6646. 3:49:44us to read and understand
  6647. 3:49:45let's begin by creating a class called
  6648. 3:49:47animal so at the top
  6649. 3:49:49type class animal and what are some
  6650. 3:49:51attributes and methods that all animals
  6651. 3:49:53should have
  6652. 3:49:53let's say we have a class variable
  6653. 3:49:55called alive and we'll set this to true
  6654. 3:49:58if you're an animal you begin by being
  6655. 3:50:00alive and what are some methods
  6656. 3:50:02how about an eat and sleep method so
  6657. 3:50:04let's define those
  6658. 3:50:05def eat what are we gonna do when we
  6659. 3:50:08call this method
  6660. 3:50:09let's print something let's print this
  6661. 3:50:12animal
  6662. 3:50:12is eating let's also create a sleep
  6663. 3:50:15method def
  6664. 3:50:16sleep what are we gonna do when we call
  6665. 3:50:18this method let's print something as
  6666. 3:50:19well
  6667. 3:50:20this animal is sleeping and we now have
  6668. 3:50:24our animal class
  6669. 3:50:25now let's create separate classes for
  6670. 3:50:27specific types of animals let's say
  6671. 3:50:30a class for rabbit a fish and a hawk so
  6672. 3:50:32we can make
  6673. 3:50:33objects of those classes so let's begin
  6674. 3:50:36with a
  6675. 3:50:36rabbit class class rabbit now to use
  6676. 3:50:40inheritance
  6677. 3:50:41with the class that you intend to be the
  6678. 3:50:43child class after the class name add a
  6679. 3:50:45set of parentheses
  6680. 3:50:46and then pass in the name of the parent
  6681. 3:50:48class in this case it's going to be
  6682. 3:50:49animal
  6683. 3:50:50so we will add that to the parentheses
  6684. 3:50:53so now
  6685. 3:50:54rabbit is the child class and animal is
  6686. 3:50:56the parent class
  6687. 3:50:57so the child class is going to inherit
  6688. 3:51:00everything that the animal class has
  6689. 3:51:02so for the time being i'm going to type
  6690. 3:51:04pass so this rabbit class
  6691. 3:51:05will have access to a class variable
  6692. 3:51:07called alive
  6693. 3:51:08and an eat and sleep method so let's
  6694. 3:51:11define
  6695. 3:51:12a fish and hawk class and i'm just going
  6696. 3:51:14to copy what i have here and just change
  6697. 3:51:16rabbit to fish and then do so again and
  6698. 3:51:19change rabbit to
  6699. 3:51:21hawk this is our family tree animal
  6700. 3:51:24is the parent class and it has three
  6701. 3:51:26children a rabbit class
  6702. 3:51:27a fish class and a hot class and for the
  6703. 3:51:29time being we're just going to write
  6704. 3:51:31pass as a placeholder
  6705. 3:51:32so i bet we can create objects from
  6706. 3:51:35these classes
  6707. 3:51:36so let's do so let's say we have an
  6708. 3:51:37object called rabbit
  6709. 3:51:39rabbit equals rabbit let's do the same
  6710. 3:51:41thing for
  6711. 3:51:42fish and hawk
  6712. 3:51:47all right we now have three objects
  6713. 3:51:50rabbit
  6714. 3:51:50fish and hawk and let's take a look at
  6715. 3:51:53their
  6716. 3:51:53class variables i bet they have an alive
  6717. 3:51:56variable let's check that so i would
  6718. 3:51:58like to print
  6719. 3:51:59rabbit dot oh would you look at that
  6720. 3:52:02this rabbit class
  6721. 3:52:03has a class variable called alive even
  6722. 3:52:05though there's nothing within this class
  6723. 3:52:07well that's because we used inheritance
  6724. 3:52:09so each of these
  6725. 3:52:10three children classes inherit
  6726. 3:52:12everything from their parents
  6727. 3:52:14all of these attributes and methods i
  6728. 3:52:16bet they have eaten sleep functions as
  6729. 3:52:18well so let's check that
  6730. 3:52:19with fish let's have fish use its eat
  6731. 3:52:22method
  6732. 3:52:24and hawk will use its sleep method hawk
  6733. 3:52:26dot sleep
  6734. 3:52:29so the rabbit is alive the fish is
  6735. 3:52:31eating and
  6736. 3:52:32the hawk is sleeping now even though we
  6737. 3:52:34have nothing written within each of
  6738. 3:52:36these
  6739. 3:52:36specific types of animal classes all of
  6740. 3:52:39these animals have access to these
  6741. 3:52:41attributes and methods because of
  6742. 3:52:43inheritance
  6743. 3:52:44and another benefit of inheritance is
  6744. 3:52:46that we don't need to keep on
  6745. 3:52:48copying and pasting this code for
  6746. 3:52:50example let's say we were not using
  6747. 3:52:51inheritance so we would not need an
  6748. 3:52:53animal class
  6749. 3:52:54we could write our code this way where
  6750. 3:52:56we would just copy and paste everything
  6751. 3:52:59underneath each of our classes so what
  6752. 3:53:01if we need to make a change to one of
  6753. 3:53:03these methods
  6754. 3:53:04as in your boss says you know what let's
  6755. 3:53:06change sleep to
  6756. 3:53:07slumber okay well we would have to go to
  6757. 3:53:10each of these classes and make the
  6758. 3:53:11change manually
  6759. 3:53:12it's not too bad if you have like three
  6760. 3:53:14classes but what if you have hundreds
  6761. 3:53:16that's going to take a lot of effort so
  6762. 3:53:18wouldn't it be nice if we just
  6763. 3:53:20list everything that each of these
  6764. 3:53:21classes has in common
  6765. 3:53:23and then have all of these classes
  6766. 3:53:25inherit from
  6767. 3:53:26one common class and then if you need to
  6768. 3:53:28make any changes
  6769. 3:53:29you can do so just within this class and
  6770. 3:53:31this change will apply to
  6771. 3:53:33all of the different child classes so
  6772. 3:53:35that's another benefit of inheritance
  6773. 3:53:37and not only does it make any changes
  6774. 3:53:39easier but each class
  6775. 3:53:40can have their own unique attributes and
  6776. 3:53:42methods as well
  6777. 3:53:43along with the attributes and methods
  6778. 3:53:45that they inherit from their parents
  6779. 3:53:47so for rabbit fish and hawk let's have
  6780. 3:53:49each of these classes have
  6781. 3:53:50their own unique method as well so for
  6782. 3:53:53rabbit let's define
  6783. 3:53:54a run method if you're a rabbit then you
  6784. 3:53:57can run
  6785. 3:53:57so let's print this rabbit
  6786. 3:54:01is running and i'm just going to fix
  6787. 3:54:04some of the spacing here
  6788. 3:54:05all right if you're a fish then you can
  6789. 3:54:08swim
  6790. 3:54:09def swim let's print
  6791. 3:54:13this fish is swimming
  6792. 3:54:17and lastly we have hawk if you're a hawk
  6793. 3:54:21then you can fly def fly
  6794. 3:54:24let's print this hawk
  6795. 3:54:28is flying like i said before not only
  6796. 3:54:32do these specific types of animals have
  6797. 3:54:35access to
  6798. 3:54:35all of the attributes and methods that
  6799. 3:54:37they inherit from their parents
  6800. 3:54:39but they can have their own attributes
  6801. 3:54:40and methods as well so let's test some
  6802. 3:54:42of these methods
  6803. 3:54:43so we have our rabbit fish and hawk
  6804. 3:54:46objects
  6805. 3:54:46let's have rabbit use its run method
  6806. 3:54:51fish we'll use its swim method and hawk
  6807. 3:54:54will use its fly method
  6808. 3:54:59and here we go this rabbit is running
  6809. 3:55:02this fish is swimming
  6810. 3:55:03and this hawk is flying so in conclusion
  6811. 3:55:06classes can have children the children
  6812. 3:55:09classes will inherit everything that
  6813. 3:55:11their parent class has
  6814. 3:55:12all of the parents attributes and
  6815. 3:55:14methods but not only that the children
  6816. 3:55:16classes can implement
  6817. 3:55:17their own unique attributes and methods
  6818. 3:55:19as well
  6819. 3:55:20so that is inheritance if you would like
  6820. 3:55:22a copy of this code
  6821. 3:55:23i will post all of this in the comment
  6822. 3:55:25section down below but yeah that's the
  6823. 3:55:27basics of inheritance using python
  6824. 3:55:31yo what's going on everybody it's you
  6825. 3:55:33bro hope you're doing well
  6826. 3:55:34and in this video i'm going to explain
  6827. 3:55:36how multi-level inheritance works in
  6828. 3:55:38python so
  6829. 3:55:39sit back relax and enjoy the show
  6830. 3:55:43all right welcome back everybody let's
  6831. 3:55:45talk about multi-level inheritance
  6832. 3:55:47this is a concept where a derived class
  6833. 3:55:50also known as a child class
  6834. 3:55:52inherits from another derived class
  6835. 3:55:54here's an example let's say
  6836. 3:55:56we have a hierarchy of classes there
  6837. 3:55:58will be three levels to this
  6838. 3:56:00we'll create a family tree of living
  6839. 3:56:02organisms
  6840. 3:56:03so at the very top we're going to have a
  6841. 3:56:05class called
  6842. 3:56:06organism any class that inherits from
  6843. 3:56:09the organism parent class
  6844. 3:56:10will receive one attribute one variable
  6845. 3:56:13named alive
  6846. 3:56:14and this will be set to true so if you
  6847. 3:56:16are an organism you begin by being alive
  6848. 3:56:19now let's say we have a child class that
  6849. 3:56:21will inherit from the organism parent
  6850. 3:56:23class
  6851. 3:56:24let's say animal class animal
  6852. 3:56:27inherits from organism so any animal
  6853. 3:56:30that we create
  6854. 3:56:31will have this alive attribute set the
  6855. 3:56:33true
  6856. 3:56:34and all animals eat so let's define a
  6857. 3:56:37function that does that
  6858. 3:56:39at least i believe all animals eat let's
  6859. 3:56:40pretend that they do
  6860. 3:56:42so this animal is eating
  6861. 3:56:46and now why stop there let's create a
  6862. 3:56:48class for a specific type of animal
  6863. 3:56:50i like dogs so let's create a dog class
  6864. 3:56:53class dog
  6865. 3:56:54inherits from the animal class
  6866. 3:56:57and all dogs should be able to bark so
  6867. 3:56:59let's create a function to do that
  6868. 3:57:04and we'll print this dog
  6869. 3:57:07is barking okay so what we just did here
  6870. 3:57:10is
  6871. 3:57:11multi-level inheritance we had a derived
  6872. 3:57:15child class inherit from another child
  6873. 3:57:17class
  6874. 3:57:18so it's kind of like this dog class is
  6875. 3:57:20the child animal class is the parent
  6876. 3:57:22and then the parent of the parent class
  6877. 3:57:25would be like
  6878. 3:57:25a grandparent right and then if you
  6879. 3:57:27added another layer on top of that to
  6880. 3:57:29this hierarchy
  6881. 3:57:30well then the parent of the grandparent
  6882. 3:57:31would be like a great grandparent
  6883. 3:57:33that's how i think of it at least now
  6884. 3:57:35let's create a dog object just to verify
  6885. 3:57:37that everything's working so let's call
  6886. 3:57:38our dog
  6887. 3:57:39dog and our dog object should have
  6888. 3:57:42access to an attribute named alive
  6889. 3:57:44and our dog object receives that
  6890. 3:57:47attribute from the organism class
  6891. 3:57:49and this will print true if i were to
  6892. 3:57:51print whatever this attribute contains
  6893. 3:57:53and our dog should be able to eat
  6894. 3:57:55because it receives this method from the
  6895. 3:57:57animal parent class
  6896. 3:58:00and lastly our dog should be able to
  6897. 3:58:02bark and that method is defined within
  6898. 3:58:04its own class
  6899. 3:58:07so that's how multi-level inheritance
  6900. 3:58:09works it's a concept where a child class
  6901. 3:58:12will inherit from another child class
  6902. 3:58:14i like to think of it like a family tree
  6903. 3:58:17where a child class will have a parent
  6904. 3:58:20and the parent of that parent would be
  6905. 3:58:22like a grandparent basically
  6906. 3:58:24so if you would like a copy of this code
  6907. 3:58:26i will post all of this to the comment
  6908. 3:58:28section down below
  6909. 3:58:29but yeah that is how multi-level
  6910. 3:58:31inheritance works in python
  6911. 3:58:33hey what's going on everybody it's you
  6912. 3:58:35bro hope you're doing well and in this
  6913. 3:58:36video i'm going to show you all how
  6914. 3:58:38multiple inheritance works in python so
  6915. 3:58:40sit back
  6916. 3:58:41relax and enjoy the show
  6917. 3:58:45all right people multiple inheritance
  6918. 3:58:48multiple inheritance is the concept
  6919. 3:58:50where a child class
  6920. 3:58:52is derived from more than one parent
  6921. 3:58:54class
  6922. 3:58:55here's an example let's say that all of
  6923. 3:58:57our children classes are going to be
  6924. 3:58:58certain types of animals and we would
  6925. 3:59:01like our animal classes
  6926. 3:59:03to inherit from either the prepared
  6927. 3:59:05class
  6928. 3:59:06the predator parrot class or both
  6929. 3:59:08depending on what type of animal it is
  6930. 3:59:10because some animals in the animal
  6931. 3:59:12kingdom can be both prey
  6932. 3:59:14and predators and one example that comes
  6933. 3:59:16to mind would be fish
  6934. 3:59:17fish will eat smaller fish but also can
  6935. 3:59:20get eaten by
  6936. 3:59:21larger fish so here we have two classes
  6937. 3:59:23prey and predator
  6938. 3:59:25each has a dedicated method if a class
  6939. 3:59:27inherits from
  6940. 3:59:28the prey parent class they will have
  6941. 3:59:30access to a flea method
  6942. 3:59:32which will print this animal please if a
  6943. 3:59:35class inherits from the predator parent
  6944. 3:59:37class
  6945. 3:59:37that class will have access to a hunt
  6946. 3:59:39method that will print
  6947. 3:59:41this animal is hunting and it's possible
  6948. 3:59:43that some animals can do both
  6949. 3:59:45they can both flee and hunt but they'll
  6950. 3:59:47need to inherit
  6951. 3:59:48both of these classes both prey and
  6952. 3:59:50predator and that's where multiple
  6953. 3:59:52inheritance comes in
  6954. 3:59:53now let's create some classes based on
  6955. 3:59:55certain types of animals
  6956. 3:59:56let's say we have class rabbit and
  6957. 3:59:59rabbit we'll inherit from the prey class
  6958. 4:00:01because
  6959. 4:00:02rabbits are typically prey and not
  6960. 4:00:03predators but i don't know there could
  6961. 4:00:05be some killer rabbits out there
  6962. 4:00:07haven't run into any yet let's have
  6963. 4:00:09class hawk inherit from the
  6964. 4:00:12predator class and lastly fish
  6965. 4:00:16and fish can be both prey and predators
  6966. 4:00:18so they will inherit from both classes
  6967. 4:00:20and we just separate each class with a
  6968. 4:00:22comma so prey
  6969. 4:00:24comma predator and it's as simple as
  6970. 4:00:26that
  6971. 4:00:27now let's create an object from each of
  6972. 4:00:29these classes we have rabbit
  6973. 4:00:31equals rabbit hawk
  6974. 4:00:36equals hawk and fish
  6975. 4:00:39equals fish now let's just verify
  6976. 4:00:42all of the different methods that each
  6977. 4:00:44of these objects inherited
  6978. 4:00:46so our rabbit should have access to a
  6979. 4:00:48flea method
  6980. 4:00:49but no hunt method and let's just test
  6981. 4:00:52that
  6982. 4:00:53yep this animal flees and my hawk object
  6983. 4:00:57should have access to a hunt method but
  6984. 4:00:59no flea method
  6985. 4:01:01this animal is hunting and lastly
  6986. 4:01:04my fish object has access to both a flea
  6987. 4:01:08and a hunt method and if you take a look
  6988. 4:01:10on the right hand side of this pop-up
  6989. 4:01:11window it shows the class that it
  6990. 4:01:13inherited this method from
  6991. 4:01:15so fleet comes from the parent class of
  6992. 4:01:17prey and hunt comes from the apparent
  6993. 4:01:19class of predator
  6994. 4:01:21so my fish can both flee and hunt
  6995. 4:01:24because it's typically considered both
  6996. 4:01:26prey and predators
  6997. 4:01:29this animal flees this animal is hunting
  6998. 4:01:31so that's basically what multiple
  6999. 4:01:33inheritance is
  7000. 4:01:34it's a concept where a child class is
  7001. 4:01:37derived from more than one parent class
  7002. 4:01:40so that's basically multiple inheritance
  7003. 4:01:42i'll post all of this code to the
  7004. 4:01:43comment section down below
  7005. 4:01:45and well yeah that's how multiple
  7006. 4:01:47inheritance works
  7007. 4:01:48in python hey what's going on everybody
  7008. 4:01:51it's bro hope you're doing well
  7009. 4:01:53and in this video i'm going to teach you
  7010. 4:01:54guys about method overriding in python
  7011. 4:01:56so
  7012. 4:01:57sit back relax and enjoy the show
  7013. 4:02:01here's a super quick video on method
  7014. 4:02:03overriding i have two classes a class
  7015. 4:02:06called animal and a class called rabbit
  7016. 4:02:08rabbit inherits from the animal class
  7017. 4:02:09therefore rabbit is the child class
  7018. 4:02:11an animal is the parent class within the
  7019. 4:02:13animal class there is one method a
  7020. 4:02:15method called eat
  7021. 4:02:16and when you call this method all this
  7022. 4:02:18will do is print this animal is eating
  7023. 4:02:21the rabbit class inherits from the
  7024. 4:02:22animal class therefore
  7025. 4:02:24rabbit has access to this eat method and
  7026. 4:02:26if i were to create a rabbit object
  7027. 4:02:28and this rabbit used its eat method well
  7028. 4:02:31then this is going to print
  7029. 4:02:32this animal is eating now method of
  7030. 4:02:35writing is the ability of an object
  7031. 4:02:37oriented programming language
  7032. 4:02:38to allow a subclass also known as a
  7033. 4:02:41child class
  7034. 4:02:42to provide a specific implementation of
  7035. 4:02:45a method
  7036. 4:02:46that is already provided by one of its
  7037. 4:02:48parents in this case we're going to
  7038. 4:02:50override
  7039. 4:02:50the eat method and what we can do is
  7040. 4:02:52provide a more specific
  7041. 4:02:54implementation for the rabbit class and
  7042. 4:02:57now to override a method what we'll do
  7043. 4:02:59is that within
  7044. 4:02:59the child class we need to define a
  7045. 4:03:02method with the same
  7046. 4:03:03matching method signature that is the
  7047. 4:03:05combination of a method's name
  7048. 4:03:07plus its parameters both of these
  7049. 4:03:09together are known as a
  7050. 4:03:10method signature within the child class
  7051. 4:03:13we will define
  7052. 4:03:14and eat method with the same parameters
  7053. 4:03:16def eat
  7054. 4:03:17and in this case the only parameter is
  7055. 4:03:19self just to keep this simple
  7056. 4:03:21and what we'll do is that will provide a
  7057. 4:03:23more specific
  7058. 4:03:24implementation of the eat method
  7059. 4:03:26specifically for rabbits
  7060. 4:03:28so let's print something more closely
  7061. 4:03:29associated with rabbits such as
  7062. 4:03:32this rabbit is eating a
  7063. 4:03:35carrot now if i were to run this program
  7064. 4:03:37again
  7065. 4:03:38we will instead use this implementation
  7066. 4:03:40of the
  7067. 4:03:41method instead of the one that it
  7068. 4:03:42inherits from its parent class
  7069. 4:03:44of animal basically speaking an object
  7070. 4:03:47will use a method that is more closely
  7071. 4:03:49associated with itself first before
  7072. 4:03:51relying on a method that it may inherit
  7073. 4:03:54from a parent class
  7074. 4:03:55in this example we're using this version
  7075. 4:03:57of the eat method
  7076. 4:03:58specifically for rabbits instead of the
  7077. 4:04:00version that we inherit from the parent
  7078. 4:04:02class
  7079. 4:04:03of animal that is the basics of method
  7080. 4:04:05overriding i will post this code in the
  7081. 4:04:07comment section down below
  7082. 4:04:09and well yeah that's how to override a
  7083. 4:04:11method using python
  7084. 4:04:14hey what's going on everybody it's you
  7085. 4:04:16bro hope you're doing well
  7086. 4:04:17and in this video i'm going to teach you
  7087. 4:04:18guys all about method chaining in python
  7088. 4:04:21so
  7089. 4:04:21sit back relax and enjoy the show
  7090. 4:04:26all right everybody let's talk about
  7091. 4:04:28method training method chaining is used
  7092. 4:04:30to call
  7093. 4:04:30multiple methods sequentially and each
  7094. 4:04:33call performs an action on the same
  7095. 4:04:35object
  7096. 4:04:36and return self here's an example of how
  7097. 4:04:38method chaining would be useful
  7098. 4:04:40let's say we have a class named car car
  7099. 4:04:43has
  7100. 4:04:43four methods turn on drive break and
  7101. 4:04:46turn off
  7102. 4:04:47they will all just print a simple
  7103. 4:04:48message beginning with turn on
  7104. 4:04:50turn on what print you start the engine
  7105. 4:04:53drive will print you drive the car
  7106. 4:04:55brake will print you step on the brakes
  7107. 4:04:56and turn off will print
  7108. 4:04:58you turn off the engine let's say we
  7109. 4:05:00have a car object
  7110. 4:05:01car equals car and i would like my car
  7111. 4:05:04object
  7112. 4:05:04to use its turn on method immediately
  7113. 4:05:07followed by
  7114. 4:05:08the drive method with how we've been
  7115. 4:05:10coding things previously we may write
  7116. 4:05:12something such as
  7117. 4:05:13the name of the object in this case car
  7118. 4:05:15dot and then the method to use
  7119. 4:05:17i would like my card to use the turn on
  7120. 4:05:19method followed by
  7121. 4:05:21car dot drive so currently this will
  7122. 4:05:23take two lines of code
  7123. 4:05:25but this will print you start the engine
  7124. 4:05:27and you drive the car
  7125. 4:05:29now with method training we can call
  7126. 4:05:31multiple methods
  7127. 4:05:32sequentially now the format on this is
  7128. 4:05:34going to be a little bit different
  7129. 4:05:36and if we were to write the same code
  7130. 4:05:38using method chaining it might look a
  7131. 4:05:39little something like this
  7132. 4:05:40after the first method call we would add
  7133. 4:05:43dot
  7134. 4:05:44and then an additional method that we
  7135. 4:05:46would like to call so for example
  7136. 4:05:48after the turn on method we'll
  7137. 4:05:50immediately call
  7138. 4:05:51the drive method but there's one more
  7139. 4:05:53thing that we need to add when we're
  7140. 4:05:54using python to do method training
  7141. 4:05:56so normally this by itself will not work
  7142. 4:05:59when we call a method in python if there
  7143. 4:06:01is nothing that is returned python will
  7144. 4:06:03return
  7145. 4:06:03none so as you can see here attribute
  7146. 4:06:06error none type
  7147. 4:06:07object has no attribute of drive so we
  7148. 4:06:10need to return itself under each method
  7149. 4:06:12that we're using method training with
  7150. 4:06:14so let's add return self to each of
  7151. 4:06:16these methods
  7152. 4:06:17so let's do that for drive
  7153. 4:06:20break and the turn off method and this
  7154. 4:06:24should work now you start the engine
  7155. 4:06:26you drive the car so think of it like
  7156. 4:06:28this after we finish
  7157. 4:06:30calling this method python is going to
  7158. 4:06:32return self
  7159. 4:06:33so it's going to return car so then
  7160. 4:06:35we'll immediately use
  7161. 4:06:37car.drive now here's another example
  7162. 4:06:39let's say i would like to
  7163. 4:06:41call the break method followed by the
  7164. 4:06:43turn off method
  7165. 4:06:44so that would be car dot break
  7166. 4:06:47parentheses
  7167. 4:06:48followed by dot turn
  7168. 4:06:51off so this will print you step on the
  7169. 4:06:55brakes
  7170. 4:06:55you turn off the engine now here's an
  7171. 4:06:57extreme example let's say we would like
  7172. 4:06:59to call all four methods in order
  7173. 4:07:01so that would be car dot
  7174. 4:07:04turn on followed by dot drive
  7175. 4:07:08dot break dot
  7176. 4:07:11turn off so this will call
  7177. 4:07:15all four methods in order starting with
  7178. 4:07:17the leftmost method
  7179. 4:07:19you start the engine you drive the car
  7180. 4:07:21you step on the brakes
  7181. 4:07:22you turn off the engine now if you have
  7182. 4:07:25one long method chain like this
  7183. 4:07:27it might become difficult to read after
  7184. 4:07:28a while so what i would recommend
  7185. 4:07:30if you're doing a lot of method chaining
  7186. 4:07:32is that after each method call
  7187. 4:07:34just hit enter to move each method call
  7188. 4:07:36down to a new line
  7189. 4:07:38so it'll look something like that and
  7190. 4:07:40this backslash
  7191. 4:07:41might be inserted this is a line
  7192. 4:07:43continuation character
  7193. 4:07:44so this is a little more readable but
  7194. 4:07:47you know it still does the same thing
  7195. 4:07:48basically
  7196. 4:07:49all right everybody so that is method
  7197. 4:07:51training it's used to call
  7198. 4:07:53multiple methods sequentially and each
  7199. 4:07:55call performs an
  7200. 4:07:56action on the same object and returns
  7201. 4:07:58self if you would like a copy of this
  7202. 4:08:00code i will post all of this in the
  7203. 4:08:02comments section down below but yeah
  7204. 4:08:04that is how to do a method chaining in
  7205. 4:08:06python
  7206. 4:08:08hey what's going on everybody it's bro
  7207. 4:08:10hope you're doing well
  7208. 4:08:11and in this video i'm going to explain
  7209. 4:08:13the super function in python so
  7210. 4:08:15sit back relax and enjoy the show
  7211. 4:08:20well then since you clicked on this
  7212. 4:08:21video i should probably discuss
  7213. 4:08:23the super function now the super
  7214. 4:08:25function is used to give access
  7215. 4:08:26to the methods of a parent class it
  7216. 4:08:29returns a temporary object
  7217. 4:08:32of a parent class when used here's an
  7218. 4:08:34example of why the super function would
  7219. 4:08:36be useful
  7220. 4:08:37let's take this program that i've
  7221. 4:08:38written there's three classes
  7222. 4:08:40a class called rectangle which is the
  7223. 4:08:43parent class
  7224. 4:08:43to two children classes square and cube
  7225. 4:08:47and all we're doing is creating a square
  7226. 4:08:49object and a cube object
  7227. 4:08:50but we need to pass in a length and a
  7228. 4:08:53width for squares
  7229. 4:08:54and a length width and height for cubes
  7230. 4:08:57now with programming we don't like to
  7231. 4:08:59repeat code we like to reuse code and if
  7232. 4:09:01you take a look within my knit methods
  7233. 4:09:04for both square and cube we're repeating
  7234. 4:09:06self.length equals length
  7235. 4:09:08and self.width equals width twice for
  7236. 4:09:11our init methods
  7237. 4:09:12so one thing that we can do any
  7238. 4:09:14similarities between
  7239. 4:09:16the square and cube class we can place
  7240. 4:09:18within
  7241. 4:09:19the rectangle class and then reuse this
  7242. 4:09:21code so what i'm going to do
  7243. 4:09:23is copy this indent method within our
  7244. 4:09:26square class
  7245. 4:09:27and paste it within our rectangle parent
  7246. 4:09:29class
  7247. 4:09:30and then in order to access this init
  7248. 4:09:33method
  7249. 4:09:33we can use the super function so we no
  7250. 4:09:36longer need
  7251. 4:09:37these two lines of code what i'm going
  7252. 4:09:39to do is ask
  7253. 4:09:40the rectangle class to use its init
  7254. 4:09:42method so we're going to type
  7255. 4:09:44super parentheses dot and then the name
  7256. 4:09:48or the function of the parent class
  7257. 4:09:49that we would like to use and i would
  7258. 4:09:51like to use the init method
  7259. 4:09:53but i need to send a length and a width
  7260. 4:09:55because it requires that
  7261. 4:09:56so let's send these so we're going to
  7262. 4:09:58pass in our length and
  7263. 4:10:00our width and now we can do the same
  7264. 4:10:01thing for our cube init method
  7265. 4:10:03so i will copy this line of code we no
  7266. 4:10:06longer need these two lines of code
  7267. 4:10:08because we will be reusing our init
  7268. 4:10:10method within the parent class
  7269. 4:10:12of rectangle but we'll still need to
  7270. 4:10:14keep this line in to assign the height
  7271. 4:10:16because that's a difference between our
  7272. 4:10:19square and cube class
  7273. 4:10:20and now we can just reuse this init
  7274. 4:10:23method within
  7275. 4:10:24the rectangle parent class now i'm
  7276. 4:10:26thinking that we should probably test to
  7277. 4:10:27see if
  7278. 4:10:28length and width are assigned to these
  7279. 4:10:30attributes and one way
  7280. 4:10:31that i think would be fun would be to
  7281. 4:10:33create an area method
  7282. 4:10:35for squares and a volume method for
  7283. 4:10:37cubes so let's define
  7284. 4:10:38an area method within the square class
  7285. 4:10:41def
  7286. 4:10:42area and i would like to return self
  7287. 4:10:46dot length times self
  7288. 4:10:49dot width and we can create a volume
  7289. 4:10:52method within cubes
  7290. 4:10:54so let's do that def let's change this
  7291. 4:10:57to volume
  7292. 4:10:58we'll multiply it length times width
  7293. 4:11:00times height
  7294. 4:11:02but we need to type self dot pipe
  7295. 4:11:05and we can call these methods to get the
  7296. 4:11:07area
  7297. 4:11:08of a square or the volume of a cube
  7298. 4:11:11let's print
  7299. 4:11:13square dot area
  7300. 4:11:17and then cube dot
  7301. 4:11:21volume so if our init method
  7302. 4:11:25is actually being used we should have a
  7303. 4:11:27length and the width
  7304. 4:11:28assigned to these attributes and it
  7305. 4:11:31looks like we do
  7306. 4:11:33in conclusion the super function is used
  7307. 4:11:35to give access to the methods of a
  7308. 4:11:37parent class
  7309. 4:11:38it returns a temporary object of a
  7310. 4:11:40parent class when used in order to
  7311. 4:11:43access
  7312. 4:11:43the methods of the parent class also
  7313. 4:11:46known as
  7314. 4:11:46a super class and in this example within
  7315. 4:11:49the init methods of both square and cube
  7316. 4:11:52we immediately called the init method of
  7317. 4:11:55the parent class
  7318. 4:11:56to pass in some arguments that both of
  7319. 4:11:58these classes
  7320. 4:11:59have in common so if you would like a
  7321. 4:12:01copy of this code i will post all of
  7322. 4:12:03this in the comment section down below
  7323. 4:12:05but yeah that is how the super function
  7324. 4:12:07works in python
  7325. 4:12:09hey what's going on everybody it's bro
  7326. 4:12:11hope you're doing well
  7327. 4:12:12and in this video i'm going to explain
  7328. 4:12:14how abstract classes work in python so
  7329. 4:12:17sit back relax and enjoy the show
  7330. 4:12:21hey what's good everybody let's talk
  7331. 4:12:23about abstract classes
  7332. 4:12:25now abstract classes prevent a user from
  7333. 4:12:27creating an object of that class
  7334. 4:12:30think of an abstract class more as a
  7335. 4:12:32template
  7336. 4:12:33it's an idea it's not real it's like a
  7337. 4:12:35ghost class basically
  7338. 4:12:37plus an abstract class compels a user to
  7339. 4:12:40override any abstract methods within a
  7340. 4:12:42child class
  7341. 4:12:44so it's also a form of checks and
  7342. 4:12:45balances as well which we'll discuss
  7343. 4:12:47later
  7344. 4:12:48now here's a small program that i've
  7345. 4:12:49written let's say we have
  7346. 4:12:51three classes class vehicle car and
  7347. 4:12:53motorcycle
  7348. 4:12:54car and motorcycle are the children
  7349. 4:12:57classes of the vehicle class
  7350. 4:12:59now within this vehicle class we have a
  7351. 4:13:01go method
  7352. 4:13:02but i'm currently only defining it not
  7353. 4:13:04actually implementing it so i'm just
  7354. 4:13:05writing pass
  7355. 4:13:06now within car and motorcycle we're
  7356. 4:13:09overriding this method and creating our
  7357. 4:13:10own implementation of the skull method
  7358. 4:13:12that we inherit
  7359. 4:13:13the go method for the car will print you
  7360. 4:13:16drive the car
  7361. 4:13:16and the go method for the motorcycle
  7362. 4:13:18class will print you ride the motorcycle
  7363. 4:13:20now currently i'm creating an object of
  7364. 4:13:22each of these classes
  7365. 4:13:24i have a generic vehicle object car
  7366. 4:13:26object
  7367. 4:13:27and motorcycle object now after running
  7368. 4:13:29this program
  7369. 4:13:30currently the go method within our
  7370. 4:13:32vehicle class is not being implemented
  7371. 4:13:34so this won't print anything but the go
  7372. 4:13:36method within our car class will print
  7373. 4:13:38you drive the car and the go method
  7374. 4:13:41within our motorcycle class will print
  7375. 4:13:43you ride the motorcycle with that being
  7376. 4:13:45said let's pretend that we're coding the
  7377. 4:13:47next
  7378. 4:13:47need for speed game and we would like a
  7379. 4:13:49user to create an object from a specific
  7380. 4:13:52kind of vehicle whether it's a car from
  7381. 4:13:54the car class or a motorcycle from the
  7382. 4:13:56motorcycle class
  7383. 4:13:57we would like to prevent a user from
  7384. 4:13:59creating an object of the vehicle class
  7385. 4:14:02because the vehicle class is too generic
  7386. 4:14:04we do not have all of the
  7387. 4:14:05implementations
  7388. 4:14:06set up for a vehicle it would be like
  7389. 4:14:09somebody unlocked the ghost car or the
  7390. 4:14:11invisible car
  7391. 4:14:12which doesn't actually exist and is
  7392. 4:14:13missing a lot of features we need a user
  7393. 4:14:16to create an object
  7394. 4:14:17from a child class because these are
  7395. 4:14:19fully fleshed out and one way in which
  7396. 4:14:21we can prevent a user from creating an
  7397. 4:14:23object of this class
  7398. 4:14:25as well to turn this class into an
  7399. 4:14:27abstract class
  7400. 4:14:28which is what this video is all about go
  7401. 4:14:30figure now the first step to creating an
  7402. 4:14:32abstract class is that we'll need some
  7403. 4:14:34imports
  7404. 4:14:34so at the top of your program write this
  7405. 4:14:37from
  7406. 4:14:37abc abc is an acronym for abstract
  7407. 4:14:41based class abc from abc
  7408. 4:14:44import abc all capital comma
  7409. 4:14:48abstract method all right
  7410. 4:14:51now with our vehicle class the class
  7411. 4:14:53that you intend to be the abstract class
  7412. 4:14:56add us out of parentheses and our
  7413. 4:14:58vehicle class
  7414. 4:14:59will inherit from the abc class
  7415. 4:15:02and now with any methods within your
  7416. 4:15:04vehicle class you're going to add
  7417. 4:15:05this decorator at the top at abstract
  7418. 4:15:09method all right we should now be
  7419. 4:15:12prevented from creating a
  7420. 4:15:13vehicle object our vehicle class is now
  7421. 4:15:16an abstract class
  7422. 4:15:17and we cannot give it a physical form a
  7423. 4:15:19physical manifestation
  7424. 4:15:21and if we attempt to you can see here
  7425. 4:15:22that we have a type error
  7426. 4:15:24can't instantiate abstract class from
  7427. 4:15:26vehicle with abstract methods
  7428. 4:15:28go so an abstract class is a class which
  7429. 4:15:31contains
  7430. 4:15:32one or more abstract methods and an
  7431. 4:15:35abstract method
  7432. 4:15:36is a method that has a declaration but
  7433. 4:15:38does not have an implementation
  7434. 4:15:40now by definition an abstract class
  7435. 4:15:43contains
  7436. 4:15:44one or more abstract methods if i remove
  7437. 4:15:47this one abstract method within our
  7438. 4:15:49vehicle class
  7439. 4:15:50well we could still create a vehicle
  7440. 4:15:52object and i'm just going to turn this
  7441. 4:15:54vehicle.go line into a comment because
  7442. 4:15:56we don't have a go method anymore
  7443. 4:15:58as you can see we can still create a
  7444. 4:16:00vehicle object which we would like to
  7445. 4:16:02prevent a user from doing
  7446. 4:16:04so we need at least one abstract method
  7447. 4:16:06within our vehicle class
  7448. 4:16:08so i'm going to go ahead and add this go
  7449. 4:16:10method back in
  7450. 4:16:11an additional feature of abstract
  7451. 4:16:13classes is that they compel
  7452. 4:16:15a user to override any abstract methods
  7453. 4:16:17within a child class
  7454. 4:16:19a method that is abstract has a
  7455. 4:16:21declaration but does not have
  7456. 4:16:22an implementation so let's say that we
  7457. 4:16:25create our vehicle class
  7458. 4:16:26our abstract vehicle class and now we're
  7459. 4:16:29creating
  7460. 4:16:29the children classes of car and
  7461. 4:16:31motorcycle and let's say i'm not paying
  7462. 4:16:33attention and i forget to add a go
  7463. 4:16:35method
  7464. 4:16:36so if i just write pass within my
  7465. 4:16:38motorcycle class i'm currently missing a
  7466. 4:16:40go method within my motorcycle class
  7467. 4:16:43well python is going to prevent me from
  7468. 4:16:45running this because we have a type air
  7469. 4:16:47can't instantiate abstract class vehicle
  7470. 4:16:50with abstract methods of go
  7471. 4:16:52so by inheriting from an abstract class
  7472. 4:16:55it's a good form of checks and balances
  7473. 4:16:57to be sure that your children classes
  7474. 4:16:59are not missing
  7475. 4:17:00any implementations of any methods that
  7476. 4:17:02they inherit
  7477. 4:17:04here's an additional way of thinking of
  7478. 4:17:05this our vehicle class
  7479. 4:17:07is telling its children if you're going
  7480. 4:17:08to inherit from me then you need to
  7481. 4:17:10override
  7482. 4:17:11this abstract method of mine and if you
  7483. 4:17:13don't well i'm not going to let you be
  7484. 4:17:14instantiated
  7485. 4:17:16so in order for us to create a cart and
  7486. 4:17:18motorcycle class
  7487. 4:17:19we need to override the go method that
  7488. 4:17:22they inherit from its parent class of
  7489. 4:17:23vehicle
  7490. 4:17:24and provide its own implementation now
  7491. 4:17:26let's say we create an additional method
  7492. 4:17:29let's say we have
  7493. 4:17:30a stop method so let's define that def
  7494. 4:17:32stop
  7495. 4:17:33for the time being i'll write pass and
  7496. 4:17:35to make this an abstract method
  7497. 4:17:37add this decorator at abstract
  7498. 4:17:40method now car and motorcycle both need
  7499. 4:17:44to
  7500. 4:17:44implement that method of stop it's
  7501. 4:17:46currently missing it
  7502. 4:17:47as you can see here class car must
  7503. 4:17:49implement all abstract methods
  7504. 4:17:51and the same thing applies for a
  7505. 4:17:52motorcycle as well so we need to
  7506. 4:17:54override this method and provide its own
  7507. 4:17:56implementation
  7508. 4:17:58so let's remove that decorator and let's
  7509. 4:18:01print something
  7510. 4:18:02let's print this car is stopped
  7511. 4:18:06and i'll do the same thing for
  7512. 4:18:08motorcycle
  7513. 4:18:10so def stop let's say
  7514. 4:18:14this motorcycle
  7515. 4:18:18is stopped and we'll call the stop
  7516. 4:18:21method for each of these
  7517. 4:18:22types of vehicles car dot stop
  7518. 4:18:26and motorcycle dot stop
  7519. 4:18:29all right and we can now create a car
  7520. 4:18:32and motorcycle object
  7521. 4:18:33because we are overriding both of these
  7522. 4:18:36abstract methods found within the parent
  7523. 4:18:38class of vehicle
  7524. 4:18:39all right everybody so that's the basics
  7525. 4:18:41of abstract classes
  7526. 4:18:42it's a class which contains one or more
  7527. 4:18:45abstract methods
  7528. 4:18:46and an abstract method is a method that
  7529. 4:18:48has a declaration
  7530. 4:18:50but does not have an implementation and
  7531. 4:18:52the benefits of using an abstract class
  7532. 4:18:54is that they prevent a user from
  7533. 4:18:56creating an object of that class
  7534. 4:18:58plus it compels a user to override any
  7535. 4:19:01abstract methods found within a child
  7536. 4:19:02class
  7537. 4:19:03so if you would like a copy of this code
  7538. 4:19:05i will post all of this in the comments
  7539. 4:19:07section down below
  7540. 4:19:08but yeah that is how abstract classes
  7541. 4:19:10work in python
  7542. 4:19:12hey what's going on everybody it's you
  7543. 4:19:14bro hope you're doing well
  7544. 4:19:15and in this video i'm going to explain
  7545. 4:19:17how we can pass objects as
  7546. 4:19:18arguments so sit back relax and enjoy
  7547. 4:19:22the show
  7548. 4:19:24ladies and gentlemen in today's video
  7549. 4:19:26i'm going to be demonstrating how we can
  7550. 4:19:28pass
  7551. 4:19:28objects as arguments for this example
  7552. 4:19:31let's say we have a class
  7553. 4:19:32car and there is one class variable
  7554. 4:19:35called color
  7555. 4:19:36color equals none what we'll be doing is
  7556. 4:19:38calling a function that accepts an
  7557. 4:19:40object as an argument as well as a color
  7558. 4:19:43so let's create a function named change
  7559. 4:19:46color now make sure when you define this
  7560. 4:19:48function it's not within the cart class
  7561. 4:19:50then technically this would be a method
  7562. 4:19:52of the car class
  7563. 4:19:53what we would like is a separate
  7564. 4:19:55function outside of the car class
  7565. 4:19:57now we need to set up some parameters we
  7566. 4:19:59would like to accept a car object as
  7567. 4:20:01well as a color
  7568. 4:20:02so for the first parameter let's say a
  7569. 4:20:05car and make sure this is in
  7570. 4:20:06uppercase or capital because python
  7571. 4:20:08doesn't like that argument names should
  7572. 4:20:10be lowercase
  7573. 4:20:11so when we pass in a car we will give it
  7574. 4:20:13a nickname of car
  7575. 4:20:15and we would also like to pass in a
  7576. 4:20:17color so let's set up a parameter called
  7577. 4:20:19color
  7578. 4:20:19now when we pass in a car we will assign
  7579. 4:20:22that car's color with whatever color
  7580. 4:20:24that we receive so
  7581. 4:20:26car dot color equals color
  7582. 4:20:29and now let's create some car objects so
  7583. 4:20:31let's say car one
  7584. 4:20:33equals car car two equals car
  7585. 4:20:36and car three equals cart currently if i
  7586. 4:20:40were to print
  7587. 4:20:40their colors this is what we'll see
  7588. 4:20:43print car one
  7589. 4:20:44dot color and repeat the steps for two
  7590. 4:20:47and three
  7591. 4:20:48and we're not calling this function yet
  7592. 4:20:50so all of these colors should be set to
  7593. 4:20:52none
  7594. 4:20:53they are basically cars with no color
  7595. 4:20:56they're i don't know iron or something
  7596. 4:20:58whatever cars are made out of aluminum
  7597. 4:21:00plastic stuff like that okay so now
  7598. 4:21:02let's call this function
  7599. 4:21:03change color perhaps right here so in
  7600. 4:21:06order to call this function we need to
  7601. 4:21:08pass in
  7602. 4:21:09a car object as well as a color so let's
  7603. 4:21:11pass in
  7604. 4:21:12car one and a color perhaps red and
  7605. 4:21:15we'll repeat the steps for two and three
  7606. 4:21:17so let's say car two will be white
  7607. 4:21:21and car 3 will be blue
  7608. 4:21:24now after calling this function we have
  7609. 4:21:27a car
  7610. 4:21:28object and a color and we're assigning
  7611. 4:21:30the color of our car to whatever color
  7612. 4:21:32that we receive
  7613. 4:21:33now these cars in order car 1 2 and 3
  7614. 4:21:36are red white and blue now one thing
  7615. 4:21:39that you should know
  7616. 4:21:39is that the name of this parameter that
  7617. 4:21:41accepts our object
  7618. 4:21:42doesn't necessarily need the same name
  7619. 4:21:45as the class name
  7620. 4:21:46that created the object that we're
  7621. 4:21:47passing in we could name this something
  7622. 4:21:49else entirely perhaps
  7623. 4:21:51vehicle you'll just want to be sure that
  7624. 4:21:52this is at least descriptive
  7625. 4:21:54of the kind of object that you would
  7626. 4:21:55like to pass in and we're not limited to
  7627. 4:21:58just passing in car objects we can pass
  7628. 4:22:00in all sorts of objects
  7629. 4:22:02but we need to pass in an object as well
  7630. 4:22:04as a color
  7631. 4:22:05so this time let's create a class called
  7632. 4:22:07motorcycle
  7633. 4:22:08class motorcycle and this class will
  7634. 4:22:12also have a color set to none
  7635. 4:22:14so what we're doing is actually reusing
  7636. 4:22:16this function for all sorts of different
  7637. 4:22:18kinds of vehicles
  7638. 4:22:19now let's create a vehicle i'll just
  7639. 4:22:21name this bike one for short
  7640. 4:22:22bike one equals motorcycle now let's
  7641. 4:22:26call the
  7642. 4:22:27change color function and we can pass in
  7643. 4:22:30an object as well as color this time we
  7644. 4:22:33will pass in
  7645. 4:22:34our bike object as well as a color i
  7646. 4:22:37think
  7647. 4:22:37black counts as a color and let's print
  7648. 4:22:42bike once color
  7649. 4:22:46and this should work we have red white
  7650. 4:22:48blue and black
  7651. 4:22:50in conclusion we can pass objects as
  7652. 4:22:52arguments to a function
  7653. 4:22:54much like what we've been doing with
  7654. 4:22:55variables however the type of objects
  7655. 4:22:58that we pass in may be limited based on
  7656. 4:23:01the required attributes and methods
  7657. 4:23:03that that given class or object might
  7658. 4:23:05have and we'll get more into this on the
  7659. 4:23:08next video on
  7660. 4:23:09duct typing so if you would like a copy
  7661. 4:23:11of all this code i will post all of this
  7662. 4:23:13in the comment section down below
  7663. 4:23:15but yeah that is how to pass objects as
  7664. 4:23:17arguments
  7665. 4:23:18using python hey what's going on
  7666. 4:23:21everybody
  7667. 4:23:21it's bro hope you're doing well and in
  7668. 4:23:23this video i'm going to explain duck
  7669. 4:23:25typing in python so
  7670. 4:23:26sit back relax and enjoy the show
  7671. 4:23:31i hope you all like ducks because that's
  7672. 4:23:33the topic of this video
  7673. 4:23:34duct typing is the concept where the
  7674. 4:23:37class of an object is less important
  7675. 4:23:39than the methods and or attributes that
  7676. 4:23:41that class might have
  7677. 4:23:43the class type is not checked if the
  7678. 4:23:45minimum methods and or attributes
  7679. 4:23:47are present it's based off of a popular
  7680. 4:23:50phrase
  7681. 4:23:50if it walks like a duck and it quacks
  7682. 4:23:52like a duck then it must be a duck
  7683. 4:23:54here's an example i have two classes
  7684. 4:23:57class duck and class chicken
  7685. 4:23:59both duck and chicken have walk and talk
  7686. 4:24:01methods
  7687. 4:24:02but they print something different with
  7688. 4:24:04the duck class the walk method will
  7689. 4:24:06print
  7690. 4:24:06this duck is walking and the duck talk
  7691. 4:24:09method will
  7692. 4:24:10print this duck is quacking the chicken
  7693. 4:24:12class
  7694. 4:24:13has the same named methods walk and talk
  7695. 4:24:15but they print something slightly
  7696. 4:24:16different
  7697. 4:24:17a slightly different variation the print
  7698. 4:24:19method will print
  7699. 4:24:20this chicken is walking and the talk
  7700. 4:24:22method will print
  7701. 4:24:23this chicken is clucking now let's say
  7702. 4:24:25we have a third class a class
  7703. 4:24:28called person
  7704. 4:24:32now there is one method within our
  7705. 4:24:34person class
  7706. 4:24:35let's say we're attempting to catch a
  7707. 4:24:37duck
  7708. 4:24:39we'll pass in self and we need to pass
  7709. 4:24:41in a
  7710. 4:24:42duck object as an argument so let's type
  7711. 4:24:45duck now within this method
  7712. 4:24:48we'll have our duck use its walk method
  7713. 4:24:53as well as its talk method duck.walk and
  7714. 4:24:56duck.talk
  7715. 4:24:58and then maybe we'll print something
  7716. 4:25:02such as you caught the critter
  7717. 4:25:07now let's create an object from each of
  7718. 4:25:09these classes let's create a
  7719. 4:25:10duck object duck equals duck
  7720. 4:25:14a chicken object chicken equals chicken
  7721. 4:25:17and lastly person person equals person
  7722. 4:25:21now if we would like our person to use
  7723. 4:25:23the catch method
  7724. 4:25:24we need to pass in a duck as an argument
  7725. 4:25:27so with our person let's type person
  7726. 4:25:29dot catch and we need to send a duck
  7727. 4:25:32object because that's a required
  7728. 4:25:34parameter here
  7729. 4:25:35so we're passing in our duck object that
  7730. 4:25:37we created and as soon as we catch the
  7731. 4:25:39duck
  7732. 4:25:39it's going to use its walk and talk
  7733. 4:25:41method and then we'll print
  7734. 4:25:43you caught the critter so as you would
  7735. 4:25:45expect this will print
  7736. 4:25:46this duck is walking the stuck is
  7737. 4:25:48quacking you caught the critter
  7738. 4:25:50now with duct typing we can pass in
  7739. 4:25:53a different type of object as long as it
  7740. 4:25:56has
  7741. 4:25:56the same methods and or attributes as
  7742. 4:25:59our duck
  7743. 4:26:00we could pass in a chicken because a
  7744. 4:26:02chicken can also walk
  7745. 4:26:03and talk so this time let's pass in our
  7746. 4:26:06chicken
  7747. 4:26:06as an argument and this will still work
  7748. 4:26:09even though our parameter is set up to
  7749. 4:26:10take a duck object
  7750. 4:26:13this chicken is walking this chicken is
  7751. 4:26:15clucking you caught the critter
  7752. 4:26:17in layman's terms think of it like this
  7753. 4:26:19python is examining this chicken object
  7754. 4:26:21it's using its walk method it's using
  7755. 4:26:23its talk method which are required
  7756. 4:26:25and python is saying well that's close
  7757. 4:26:27enough remember if it walks like a duck
  7758. 4:26:29and it quacks like a duck
  7759. 4:26:30then it must be a duck so duct typing is
  7760. 4:26:33the concept where the class of an object
  7761. 4:26:35is less important than the methods and
  7762. 4:26:38or attributes that it might have
  7763. 4:26:39the class type is not checked if the
  7764. 4:26:41minimum methods and or attributes
  7765. 4:26:44are present now let's say with our
  7766. 4:26:46chicken object our chicken can no longer
  7767. 4:26:48walk
  7768. 4:26:50well we cannot pass in our chicken
  7769. 4:26:51object anymore
  7770. 4:26:53we have an attribute error chicken
  7771. 4:26:55object has no attribute walk
  7772. 4:26:57so python took a look at this chicken
  7773. 4:26:59and noticed that it does not have a walk
  7774. 4:27:01method
  7775. 4:27:01well it's not walking like a duck but
  7776. 4:27:03it's talking like a duck
  7777. 4:27:05so it doesn't count as a duck because it
  7778. 4:27:07doesn't have its walk method
  7779. 4:27:08in conclusion the class type of an
  7780. 4:27:10object is not as important as the
  7781. 4:27:12methods and or attributes that that
  7782. 4:27:14class might have when using
  7783. 4:27:16duct typing the class type will not be
  7784. 4:27:18checked if the minimum
  7785. 4:27:19methods and or attributes are present
  7786. 4:27:22since
  7787. 4:27:22chickens can both walk and talk like
  7788. 4:27:24ducks then
  7789. 4:27:25they can be a substitute for ducks when
  7790. 4:27:27we pass in our duck object to this
  7791. 4:27:29method
  7792. 4:27:30so if you would like a copy of this code
  7793. 4:27:32i will post all of this in the comment
  7794. 4:27:33section down below
  7795. 4:27:35but yeah that is the basics of duct
  7796. 4:27:37typing in python
  7797. 4:27:38hey what's going on everybody it's you
  7798. 4:27:40bro hope you're doing well
  7799. 4:27:42and in this video i'm going to discuss
  7800. 4:27:44the walrus operator in python
  7801. 4:27:46so sit back relax and enjoy the show
  7802. 4:27:51all right people i hope you like
  7803. 4:27:52walruses because that's the topic of
  7804. 4:27:54this video we'll be discussing
  7805. 4:27:56the walrus operator also known as an
  7806. 4:27:58assignment expression
  7807. 4:28:00it's a colon followed by an equal sign
  7808. 4:28:02and it kind of looks like a walrus on
  7809. 4:28:04its side
  7810. 4:28:05now this is a new feature for python 3.8
  7811. 4:28:08what it does is that it assigns values
  7812. 4:28:10to variables
  7813. 4:28:11as part of a larger expression here's an
  7814. 4:28:14example of why the walrus operator would
  7815. 4:28:16be useful
  7816. 4:28:16let's say we have a variable named happy
  7817. 4:28:19happy
  7818. 4:28:19equals true if you're not happy or
  7819. 4:28:21you're feeling so so you can set this to
  7820. 4:28:23false if you want
  7821. 4:28:24so let's print the value contained
  7822. 4:28:27within happy
  7823. 4:28:28so this will print true to the console
  7824. 4:28:30window
  7825. 4:28:31now wouldn't it be cool if we could
  7826. 4:28:33combine both of these
  7827. 4:28:35statements together so we would want to
  7828. 4:28:37say something such as
  7829. 4:28:39happy equals true and then print it to
  7830. 4:28:42the console window using one line of
  7831. 4:28:44code
  7832. 4:28:44well normally we can't do this using the
  7833. 4:28:47standard assignment operator now one
  7834. 4:28:49thing that we can do is use
  7835. 4:28:50the walrus operator this assigns a value
  7836. 4:28:53to a variable
  7837. 4:28:54as part of a larger expression so this
  7838. 4:28:56would technically
  7839. 4:28:57be allowed if this assignment operator
  7840. 4:29:00was instead
  7841. 4:29:01an assignment expression the walrus
  7842. 4:29:03operator happy
  7843. 4:29:04walrus true and when they're all within
  7844. 4:29:07a print statement
  7845. 4:29:08we can assign a value to a variable and
  7846. 4:29:11use it as a part of a larger expression
  7847. 4:29:14so as you can see
  7848. 4:29:15this new variable of happy was assigned
  7849. 4:29:17and used in one expression
  7850. 4:29:19now here's a more practical example of
  7851. 4:29:22why the walrus operator would be useful
  7852. 4:29:24let's say we have the smell program what
  7853. 4:29:26this will do is consistently ask you
  7854. 4:29:28what food you like and put it into a
  7855. 4:29:30list called foods
  7856. 4:29:32foods equals list while true food equals
  7857. 4:29:35input what food do you like if food
  7858. 4:29:38equals quit
  7859. 4:29:39break then append our list of foods with
  7860. 4:29:42this new food item that we have
  7861. 4:29:44okay let's run this then so this while
  7862. 4:29:47loop
  7863. 4:29:47will continue as long as i don't type in
  7864. 4:29:49quit and it will consistently ask me
  7865. 4:29:51what food do i like and place that food
  7866. 4:29:54item into a list
  7867. 4:29:55named foods so let's begin what food do
  7868. 4:29:58i like well i do like pizza
  7869. 4:30:00i like sushi and i like ice cream
  7870. 4:30:04and that should be enough for me i'm
  7871. 4:30:06going to type in quit to exit this
  7872. 4:30:07program
  7873. 4:30:08now let's write the same program but
  7874. 4:30:10instead use
  7875. 4:30:11a walrus operator so that we can write
  7876. 4:30:13this program using
  7877. 4:30:15less lines of code now if i were to use
  7878. 4:30:17the walrus operator this is how i would
  7879. 4:30:19write the same program
  7880. 4:30:20we would still need our list foods
  7881. 4:30:22equals
  7882. 4:30:23list now for my while loop it's going to
  7883. 4:30:26look something like this
  7884. 4:30:28while food walrus operator
  7885. 4:30:32input what
  7886. 4:30:36food do you like
  7887. 4:30:40all right this is the assignment portion
  7888. 4:30:42now we can use
  7889. 4:30:43this assignment portion within a larger
  7890. 4:30:46expression
  7891. 4:30:47so let's also check to see if our input
  7892. 4:30:50does not equal quit alright
  7893. 4:30:54so then after each iteration of this
  7894. 4:30:56while loop
  7895. 4:30:57we will take our list foods dot
  7896. 4:31:00append food and
  7897. 4:31:04that is it so we have written the same
  7898. 4:31:06program in about half the lines of code
  7899. 4:31:08that we did previously so
  7900. 4:31:10let's test it what food do you like
  7901. 4:31:12pizza
  7902. 4:31:13sushi ice cream
  7903. 4:31:17and quit well everybody that is the
  7904. 4:31:20basics of the walrus operator also known
  7905. 4:31:22as an assignment
  7906. 4:31:23expression it's a new feature for python
  7907. 4:31:253.8
  7908. 4:31:26and beyond so if this doesn't work for
  7909. 4:31:28you i would check to see what version of
  7910. 4:31:30python you're using first
  7911. 4:31:31it assigns values to variables as part
  7912. 4:31:34of a larger expression
  7913. 4:31:36if you would like a copy of this code i
  7914. 4:31:37will post all of this in the comment
  7915. 4:31:39section down below
  7916. 4:31:40but yeah that is how the walrus operator
  7917. 4:31:42works in python
  7918. 4:31:45hello what's going on everybody it's bro
  7919. 4:31:47hope you're doing well and in this video
  7920. 4:31:48i'm going to show you guys how we can
  7921. 4:31:50assign a function to a variable in
  7922. 4:31:52python
  7923. 4:31:52so sit back relax and enjoy the show
  7924. 4:31:57hey all in today's video i will show you
  7925. 4:31:59all how we can assign a function to a
  7926. 4:32:01variable
  7927. 4:32:02this video will be a prerequisite for a
  7928. 4:32:04few videos i have coming up regarding
  7929. 4:32:06functions
  7930. 4:32:06now let's say we have a function named
  7931. 4:32:08hello and all we'll do is print the word
  7932. 4:32:11hello
  7933. 4:32:11and as you know to call this function
  7934. 4:32:13you would type the name of the function
  7935. 4:32:14followed by a set of parentheses and
  7936. 4:32:16this will print the word
  7937. 4:32:17hello the set of parenthesis that comes
  7938. 4:32:19after a function's name
  7939. 4:32:21is the portion that will call the
  7940. 4:32:23function if you were to remove
  7941. 4:32:24that set of parenthesis afterwards we
  7942. 4:32:26would not in fact call that function
  7943. 4:32:29now with python python will pretty much
  7944. 4:32:31treat everything as objects
  7945. 4:32:32including functions so there's something
  7946. 4:32:34i want to show you guys
  7947. 4:32:35if i was to print the name of my
  7948. 4:32:37function hello
  7949. 4:32:39what will be displayed is the memory
  7950. 4:32:41address of this function
  7951. 4:32:43this is the memory address of where this
  7952. 4:32:45function is located within my computer's
  7953. 4:32:47memory
  7954. 4:32:48and it's in hexadecimal think of it like
  7955. 4:32:50a street address such as
  7956. 4:32:51one two three fake street this is the
  7957. 4:32:53address of where this function is within
  7958. 4:32:55my computer's memory
  7959. 4:32:56and each time that i run this program
  7960. 4:32:58this number can change as you can see
  7961. 4:33:00here
  7962. 4:33:01now one thing that we could do we could
  7963. 4:33:02assign this address to a variable
  7964. 4:33:04let's say hi equals hello and be sure
  7965. 4:33:06that you're not adding that set of
  7966. 4:33:08parentheses afterwards because
  7967. 4:33:09then you would be calling the hello
  7968. 4:33:11function and returning the result to
  7969. 4:33:12high
  7970. 4:33:13so high equals hello no parentheses
  7971. 4:33:16and if i was to print hi well
  7972. 4:33:20the address of hello and hi will be at
  7973. 4:33:22the same memory address
  7974. 4:33:23both of these numbers are the same now
  7975. 4:33:26what do you imagine would happen
  7976. 4:33:27if i was to call the high function after
  7977. 4:33:30we assigned the memory address of hello
  7978. 4:33:32to hi
  7979. 4:33:33well then what we'll end up doing is
  7980. 4:33:34calling the hello function
  7981. 4:33:36even though we're listing that we would
  7982. 4:33:38like to call the
  7983. 4:33:39high function even though it doesn't
  7984. 4:33:41exist so it's as if this hello function
  7985. 4:33:43has two names
  7986. 4:33:44you can either use hello or you can use
  7987. 4:33:46hi and let me demonstrate that
  7988. 4:33:49so you can call either hello hi or both
  7989. 4:33:52for this example
  7990. 4:33:53that's because we're assigning the
  7991. 4:33:55memory address of hello to this variable
  7992. 4:33:57of high
  7993. 4:33:58so we could treat high as a function
  7994. 4:33:59it's kind of like
  7995. 4:34:01an alias where this function has two
  7996. 4:34:03names now here's another demonstration
  7997. 4:34:05let's say that we would like to assign
  7998. 4:34:07our built-in print function to
  7999. 4:34:10a variable so let's say maybe say
  8000. 4:34:13say equals print and when i say print
  8001. 4:34:16i'm referring to the print function and
  8002. 4:34:18be sure you're not adding that set of
  8003. 4:34:19parentheses afterwards
  8004. 4:34:20now if i need to print something to the
  8005. 4:34:22console window i can either use
  8006. 4:34:24print like what we've previously been
  8007. 4:34:26doing or i could use
  8008. 4:34:28say because say has the same memory
  8009. 4:34:30address let's say
  8010. 4:34:32perhaps whoa i can't
  8011. 4:34:35believe this works
  8012. 4:34:39surprise face all right so
  8013. 4:34:42we can call the print function by saying
  8014. 4:34:44the word say
  8015. 4:34:45say all of this text and this will print
  8016. 4:34:48all of this text
  8017. 4:34:49well say all of this text to the console
  8018. 4:34:51window
  8019. 4:34:52under normal circumstances i'm not sure
  8020. 4:34:54why or when
  8021. 4:34:55you would need to assign the print
  8022. 4:34:57function to a variable but
  8023. 4:34:58you can and well that's kind of cool so
  8024. 4:35:00i thought i'd show you guys that
  8025. 4:35:02so everybody that's how to assign a
  8026. 4:35:04function to a variable
  8027. 4:35:06like i said we'll be building upon this
  8028. 4:35:07topic so you want to be sure that you
  8029. 4:35:09understand this
  8030. 4:35:10all right then well if you would like a
  8031. 4:35:12copy of this code i will post all of
  8032. 4:35:14this to the comments section down below
  8033. 4:35:15but yeah that's how to assign a function
  8034. 4:35:18to a variable
  8035. 4:35:19in python hey uh what's going on
  8036. 4:35:22everybody
  8037. 4:35:22it's bro hope you're doing well and in
  8038. 4:35:24today's video i'm going to explain
  8039. 4:35:26higher order functions in python so
  8040. 4:35:28sit back relax and enjoy the show
  8041. 4:35:33all right ladies and gentlemen let's
  8042. 4:35:34talk about higher order functions
  8043. 4:35:36these are functions that do one of two
  8044. 4:35:38things one they either accept a function
  8045. 4:35:40as an argument
  8046. 4:35:42or two they return a function as output
  8047. 4:35:44and in python this is totally allowed
  8048. 4:35:46because functions are also treated as
  8049. 4:35:48objects
  8050. 4:35:48i'll give you guys an example of both of
  8051. 4:35:50these but let's begin with number one
  8052. 4:35:52here's an example of number one let's
  8053. 4:35:54say that i have two functions
  8054. 4:35:55named loud and quiet both of these
  8055. 4:35:57functions will accept a string as an
  8056. 4:35:59argument
  8057. 4:36:00loud will return that string all
  8058. 4:36:02uppercase as if we're shouting something
  8059. 4:36:05quiet we'll return that text i'll
  8060. 4:36:06lowercase as if we're whispering
  8061. 4:36:08something
  8062. 4:36:09now i'm going to create a third function
  8063. 4:36:11called hello
  8064. 4:36:13and this will be the higher order
  8065. 4:36:14function it accepts a function as an
  8066. 4:36:17argument
  8067. 4:36:17i'm not sure if we're going to be
  8068. 4:36:19passing in loud or quiet
  8069. 4:36:21whatever it is i'm going to give it a
  8070. 4:36:22name of funk
  8071. 4:36:24short for function each of these
  8072. 4:36:26functions will return some text
  8073. 4:36:28either all uppercase or lowercase i'm
  8074. 4:36:30going to assign that to
  8075. 4:36:32text text equals the name of our
  8076. 4:36:34function we're not sure what it's going
  8077. 4:36:36to be
  8078. 4:36:37and i will pass in some text in order to
  8079. 4:36:39call one of these functions
  8080. 4:36:41i will pass in just the word hello
  8081. 4:36:42that's capitalized
  8082. 4:36:44and at the end i will print whatever
  8083. 4:36:46text that we have
  8084. 4:36:47in order to call this function of hello
  8085. 4:36:49i need to pass in a function as an
  8086. 4:36:51argument
  8087. 4:36:51do i want to pass in loud or quiet do i
  8088. 4:36:54want the
  8089. 4:36:55loud variety of hello where all of the
  8090. 4:36:57text will be uppercase
  8091. 4:36:58or the quiet variety of flow where all
  8092. 4:37:00the text will be lowercase
  8093. 4:37:02i would like to pass in loud because i
  8094. 4:37:05want to make my text all uppercase
  8095. 4:37:08so i'm typing the name of the higher
  8096. 4:37:10order function and passing in
  8097. 4:37:12a function as an argument and the result
  8098. 4:37:14is that the word hello will be printed
  8099. 4:37:16to the console window
  8100. 4:37:17all uppercase now here's a rundown of
  8101. 4:37:19what just happened
  8102. 4:37:20we're calling the hello function and
  8103. 4:37:21we're passing in loud
  8104. 4:37:23as an argument we're naming loud as
  8105. 4:37:26funk while we're within this hello
  8106. 4:37:28function
  8107. 4:37:29text equals loud and we're sending a
  8108. 4:37:33string of text
  8109. 4:37:34that says hello we're returning that
  8110. 4:37:37text all uppercase
  8111. 4:37:38assigning it to a variable called text
  8112. 4:37:41and printing that text
  8113. 4:37:42to the console window now if i would
  8114. 4:37:44like to
  8115. 4:37:45use the quiet variety of hello then i'm
  8116. 4:37:47going to pass in
  8117. 4:37:48quiet as an argument so quiet will make
  8118. 4:37:52all of this text
  8119. 4:37:53lowercase alright people so that's
  8120. 4:37:55example
  8121. 4:37:56one of a higher order function a higher
  8122. 4:37:58order function
  8123. 4:37:59is a function that accepts a function as
  8124. 4:38:02an argument
  8125. 4:38:03this hello function is an example of a
  8126. 4:38:05higher order function
  8127. 4:38:06because we're accepting either loud or
  8128. 4:38:09quiet as arguments
  8129. 4:38:10all right people let's move on to the
  8130. 4:38:12second part of this definition for
  8131. 4:38:14higher order functions
  8132. 4:38:15a higher order function is a function
  8133. 4:38:17that returns a function
  8134. 4:38:18i'm going to give a different example
  8135. 4:38:20this time let's say we have a pair of
  8136. 4:38:22nested functions the outer function will
  8137. 4:38:24be named divisor
  8138. 4:38:26and we will accept a number as an
  8139. 4:38:28argument that we will call x
  8140. 4:38:30a divisor is a number that is used to
  8141. 4:38:32divide
  8142. 4:38:33another number when using division and
  8143. 4:38:35inside this function we have an inner
  8144. 4:38:37function
  8145. 4:38:38named dividend dividend is the number
  8146. 4:38:41that's going to be divided
  8147. 4:38:42and we will call the argument that is
  8148. 4:38:44passed in y
  8149. 4:38:45all we'll do is return y divided by x
  8150. 4:38:49we're dividing the dividend by the
  8151. 4:38:51divisor y
  8152. 4:38:52divided by x now within the
  8153. 4:38:55outer function but not within the inner
  8154. 4:38:57function we're going to return
  8155. 4:39:00our dividend function so a higher order
  8156. 4:39:02function is a function that returns a
  8157. 4:39:04function
  8158. 4:39:05divisor is a higher order function
  8159. 4:39:07because we're returning
  8160. 4:39:09dividend now if i would like to access
  8161. 4:39:11this nested
  8162. 4:39:12dividend function i first need to call
  8163. 4:39:14the outside
  8164. 4:39:15divisor function and pass in a number as
  8165. 4:39:18an argument to serve as the divisor
  8166. 4:39:20the divisor function is going to return
  8167. 4:39:22my dividend function which we can then
  8168. 4:39:24assign to a variable
  8169. 4:39:26so the variable i'm going to name divide
  8170. 4:39:29divide equals and i need to call divisor
  8171. 4:39:32and set a divisor let's say that i would
  8172. 4:39:35like to divide all numbers
  8173. 4:39:37by two so i will set x to be two
  8174. 4:39:40and now for the dividend i will
  8175. 4:39:44print call the
  8176. 4:39:47divide variable and pass in a number as
  8177. 4:39:50the dividend the number that's going to
  8178. 4:39:52be divided
  8179. 4:39:52and let's say i would like to divide 10
  8180. 4:39:55by two
  8181. 4:39:56and this will work as you can see we
  8182. 4:39:58have five in the console window
  8183. 4:40:00so let me explain what just happened
  8184. 4:40:01here so our program
  8185. 4:40:03begins here divisor and we're passing in
  8186. 4:40:06two
  8187. 4:40:07x will be two and it will stay that way
  8188. 4:40:09until we finish this program or until we
  8189. 4:40:12reassign x
  8190. 4:40:13now x equals two we're skipping this
  8191. 4:40:16function because we did not call it yet
  8192. 4:40:18we're returning dividend and assigning
  8193. 4:40:21it to a variable
  8194. 4:40:22and we can call a variable if it has the
  8195. 4:40:24memory address of a function
  8196. 4:40:26which is what we're doing in this line
  8197. 4:40:28now we're calling
  8198. 4:40:29dividend and passing in 10 so y equals
  8199. 4:40:3210
  8200. 4:40:33and x still equals to we're returning 10
  8201. 4:40:36divided by 2 and printing it to the
  8202. 4:40:38console window
  8203. 4:40:40well everyone in conclusion a higher
  8204. 4:40:42order function
  8205. 4:40:43is a function that either one accepts a
  8206. 4:40:46function as an argument
  8207. 4:40:47or two they return a function the format
  8208. 4:40:51is a little bit strange and we're not
  8209. 4:40:52quite used to it yet
  8210. 4:40:53but in future videos we'll have more
  8211. 4:40:55practice with high order functions
  8212. 4:40:57if you would like a copy of all this
  8213. 4:40:58code i will post all of this to the
  8214. 4:41:00comment section down below
  8215. 4:41:02but yeah that is how higher order
  8216. 4:41:03functions work in python
  8217. 4:41:06yo what's going on everybody it's bro
  8218. 4:41:08hope you're doing well in today's video
  8219. 4:41:10i'm going to explain
  8220. 4:41:11lambda functions in python so sit back
  8221. 4:41:14relax
  8222. 4:41:15and enjoy the show all right
  8223. 4:41:19ladies and gentlemen let's talk about
  8224. 4:41:20lambda functions these are functions
  8225. 4:41:22that are written in one line and use
  8226. 4:41:24this lambda keyword
  8227. 4:41:26they accept any number of arguments but
  8228. 4:41:28they only have one expression
  8229. 4:41:30think of it as a shortcut they're useful
  8230. 4:41:32if you need a function for only one use
  8231. 4:41:34or a short period of time
  8232. 4:41:35and then you're just planning on
  8233. 4:41:36throwing away that function afterwards
  8234. 4:41:38here's the syntax for a lambda function
  8235. 4:41:40you type lambda
  8236. 4:41:42you add the parameters followed by a
  8237. 4:41:44colon and then your expression
  8238. 4:41:45let's say we have this function named
  8239. 4:41:47double double accepts one argument that
  8240. 4:41:49we will name
  8241. 4:41:50x we're returning x times two
  8242. 4:41:52effectively doubling it
  8243. 4:41:54so if i need to call this function i
  8244. 4:41:56would type double
  8245. 4:41:57and then pass in a number as an argument
  8246. 4:41:59so the result if i pass in 5 would be
  8247. 4:42:0110. now let's write the same function
  8248. 4:42:03but instead write it as a
  8249. 4:42:04lambda function so let me turn these
  8250. 4:42:07lines into comments
  8251. 4:42:09now the first step is to type lambda
  8252. 4:42:12then our parameters we only have one
  8253. 4:42:14parameter and that is x
  8254. 4:42:16colon and then our expression we would
  8255. 4:42:18like to return x
  8256. 4:42:19times two now this lambda function is
  8257. 4:42:21going to return a function object and we
  8258. 4:42:23can assign that much like what we do
  8259. 4:42:25with variables
  8260. 4:42:26let's say double equals lambda x colon
  8261. 4:42:29x times two and in order to call this
  8262. 4:42:32lambda function
  8263. 4:42:33we're going to type double then a set of
  8264. 4:42:35parentheses
  8265. 4:42:36to call this function and then pass in
  8266. 4:42:38our arguments
  8267. 4:42:39so let's say that x equals 5 and then
  8268. 4:42:42we're going to double it
  8269. 4:42:43and the result is 10. so that's the
  8270. 4:42:45syntax for a lambda function you type
  8271. 4:42:47lambda
  8272. 4:42:48your parameters colon and then your
  8273. 4:42:50expression this time let's say that we
  8274. 4:42:52have two parameters
  8275. 4:42:53we have two numbers that we want to
  8276. 4:42:55multiply by each other
  8277. 4:42:56so let's say that this function will be
  8278. 4:42:59called multiply
  8279. 4:43:00multiply equals lambda
  8280. 4:43:03x comma y because we have two parameters
  8281. 4:43:07colon followed by our expression we
  8282. 4:43:10would like to multiply
  8283. 4:43:11x times y and that is it
  8284. 4:43:14now in order to call multiply we need to
  8285. 4:43:16pass in two arguments
  8286. 4:43:18if i were to pass in just one well we'll
  8287. 4:43:20have a type error
  8288. 4:43:21so let's pass in five and maybe six and
  8289. 4:43:24the result
  8290. 4:43:24is thirty now let's try three parameters
  8291. 4:43:28let's say we would like to add three
  8292. 4:43:30numbers together
  8293. 4:43:31let's say add lambda x
  8294. 4:43:35comma y comma z so we have all of our
  8295. 4:43:38parameters
  8296. 4:43:38we have our colon here and then we need
  8297. 4:43:41our expression
  8298. 4:43:42x plus y plus z and
  8299. 4:43:46let's add five six and seven together
  8300. 4:43:49the result is 18. now let's try
  8301. 4:43:52something a little more complex let's
  8302. 4:43:54pass in some strings this time
  8303. 4:43:56i'll create a function named full name
  8304. 4:43:58full name
  8305. 4:43:59equals lambda
  8306. 4:44:02first name comma
  8307. 4:44:06last name the expression is going to be
  8308. 4:44:09first name plus perhaps a space
  8309. 4:44:12plus last name now i need to pass in a
  8310. 4:44:16first name and a last name
  8311. 4:44:19full name and you can use your own name
  8312. 4:44:22for this example if you want
  8313. 4:44:23let's say bro comma code
  8314. 4:44:29yep this will print your first name and
  8315. 4:44:31your last name to the console window
  8316. 4:44:33now let's take it a step further let's
  8317. 4:44:34say we would like to check somebody's
  8318. 4:44:36age once using a lambda function we
  8319. 4:44:38would like to check to see if they're 18
  8320. 4:44:40or older because they're signing up for
  8321. 4:44:41something perhaps let's say a credit
  8322. 4:44:43card
  8323. 4:44:43so let's create a function object age
  8324. 4:44:47check equals lambda we have one
  8325. 4:44:50parameter
  8326. 4:44:51somebody's edge colon and then our
  8327. 4:44:53expression
  8328. 4:44:55let's type true if age is greater than
  8329. 4:44:59or equal to
  8330. 4:45:0018 else
  8331. 4:45:03false to call this age track function
  8332. 4:45:07we need to pass in an age let's say the
  8333. 4:45:10user is 12
  8334. 4:45:11and they're trying to sign up for a
  8335. 4:45:13credit card while this will return false
  8336. 4:45:15let's say they're 18 now and this will
  8337. 4:45:18return
  8338. 4:45:19true so that's a lambda function it's a
  8339. 4:45:22function
  8340. 4:45:22that is written in one line using this
  8341. 4:45:24lambda keyword
  8342. 4:45:25they accept any number of arguments but
  8343. 4:45:27they only have one expression
  8344. 4:45:29think of it like a shortcut and they're
  8345. 4:45:31useful if you need to use a function
  8346. 4:45:32only once and then you're just planning
  8347. 4:45:34on throwing it away afterwards
  8348. 4:45:36so if you would like a copy of all this
  8349. 4:45:37code i will post all of this to the
  8350. 4:45:39comment section down below
  8351. 4:45:40but yeah those are lambda functions in
  8352. 4:45:43python
  8353. 4:45:44hey what's going on everybody it's bro
  8354. 4:45:46hope you're doing well
  8355. 4:45:47and in this video i'm going to show you
  8356. 4:45:49all how we can sort iterables in python
  8357. 4:45:51so sit back
  8358. 4:45:52relax and enjoy the show
  8359. 4:45:57well well well welcome back everybody in
  8360. 4:45:59this video i'm going to show you all how
  8361. 4:46:00we can sort data using python we'll
  8362. 4:46:02begin with the sort method which is used
  8363. 4:46:04with lists and the sort function which
  8364. 4:46:06is used with iterables and that would
  8365. 4:46:07include lists let's start with something
  8366. 4:46:09very basic and then we'll crank up the
  8367. 4:46:11difficulty one step at a time
  8368. 4:46:13i have a list named students and i have
  8369. 4:46:15a bunch of student names within this
  8370. 4:46:17list
  8371. 4:46:17we have squidward sandy patrick
  8372. 4:46:19spongebob mr krabs
  8373. 4:46:21let's say that all of these students are
  8374. 4:46:23taking a college course
  8375. 4:46:25perhaps on marine biology what i would
  8376. 4:46:27like to do is to sort this list in
  8377. 4:46:28alphabetical order
  8378. 4:46:29and to do that we have a method that is
  8379. 4:46:31built in with lists
  8380. 4:46:33so type the name of your list students
  8381. 4:46:35dot
  8382. 4:46:36sort and it's easy as that and to
  8383. 4:46:38display this list i think i'll use a for
  8384. 4:46:40loop
  8385. 4:46:40for i in students
  8386. 4:46:44print i
  8387. 4:46:48and there you go that's how to sort a
  8388. 4:46:50list starting with the top we have mr
  8389. 4:46:52krabs
  8390. 4:46:52then patrick sandy spongebob and
  8391. 4:46:54squidward the sort method of lists can
  8392. 4:46:57accept keyword arguments
  8393. 4:46:58there are two optional keyword arguments
  8394. 4:47:00that we can pass in we can pass
  8395. 4:47:02in key and or reverse we'll cover key a
  8396. 4:47:05little bit later
  8397. 4:47:06if we set reverse to true then our list
  8398. 4:47:09will be sorted by reverse alphabetical
  8399. 4:47:12order beginning with
  8400. 4:47:13squidward spongebob sandy patrick then
  8401. 4:47:16mr krabs
  8402. 4:47:17now the sort method does not work with
  8403. 4:47:19other iterables it's a built-in method
  8404. 4:47:21for lists
  8405. 4:47:22if our list of students was instead
  8406. 4:47:25let's say a tuple
  8407. 4:47:26well then the sort method is not going
  8408. 4:47:28to work you can see here that
  8409. 4:47:30we have an attribute error tuple object
  8410. 4:47:32has no attribute of sort
  8411. 4:47:34that's where the sort function would
  8412. 4:47:36come in because that's useful for other
  8413. 4:47:38iterables
  8414. 4:47:38you can also use this for a list 2 i
  8415. 4:47:40suppose the sort function will return a
  8416. 4:47:42sorted list so let's use the sort
  8417. 4:47:45function this time
  8418. 4:47:47and we'll assign the result to a list
  8419. 4:47:50called sorted students
  8420. 4:47:53equals sorted and then we need to pass
  8421. 4:47:57in
  8422. 4:47:57an iterable and we have the option of
  8423. 4:47:59passing in a key
  8424. 4:48:01and or reverse the iterable that we'll
  8425. 4:48:03pass in as an argument is our tuple
  8426. 4:48:06named
  8427. 4:48:06students so this line will be sorted
  8428. 4:48:09students
  8429. 4:48:10equals sorted students sorted students
  8430. 4:48:13is a list the sorted function will
  8431. 4:48:14return a list but it accepts an iterable
  8432. 4:48:17as an argument
  8433. 4:48:18and to display this we should change 4i
  8434. 4:48:20and students
  8435. 4:48:21to 4i in sorted students and now
  8436. 4:48:25this function will accept our iterable
  8437. 4:48:27our tuple
  8438. 4:48:28and create a sorted list and all of
  8439. 4:48:30these are in alphabetical order
  8440. 4:48:32and to reverse this we can pass in the
  8441. 4:48:35keyword argument of
  8442. 4:48:36reverse equals true and now our list is
  8443. 4:48:39sorted in
  8444. 4:48:40reverse alphabetical order all right
  8445. 4:48:42we're gonna take it up a level
  8446. 4:48:44welcome to level two sometimes data
  8447. 4:48:46isn't always as simple here we have a
  8448. 4:48:48list of tuples each tuple has a
  8449. 4:48:51corresponding student record
  8450. 4:48:53we have a name a letter grade for their
  8451. 4:48:55college course
  8452. 4:48:56and the student's age now how can we
  8453. 4:48:58sort this
  8454. 4:48:59list of tuples by either the student's
  8455. 4:49:02name
  8456. 4:49:03their grade or their age well that's
  8457. 4:49:05where the key
  8458. 4:49:06keyword argument's going to come in with
  8459. 4:49:08sorting if you take a look at this list
  8460. 4:49:10of tuples it somewhat resembles a
  8461. 4:49:12spreadsheet there's rows and then
  8462. 4:49:14there's columns
  8463. 4:49:15the first column corresponds to student
  8464. 4:49:17names the second column
  8465. 4:49:19are grades and the third column are all
  8466. 4:49:21the ages of the students
  8467. 4:49:23so by default sorting by the first
  8468. 4:49:25column is actually really easy that's
  8469. 4:49:27the default
  8470. 4:49:28so if we need to sort alphabetically
  8471. 4:49:30that would be the same process
  8472. 4:49:31as before we would type the name of the
  8473. 4:49:33list students
  8474. 4:49:35dot sort but if we were to print
  8475. 4:49:38this iterable for i in students
  8476. 4:49:42instead of just the individual student
  8477. 4:49:44names we're going to get
  8478. 4:49:45each tuple that we have so now all of
  8479. 4:49:48these tuples are arranged in
  8480. 4:49:50alphabetical order starting with
  8481. 4:49:52the first column that we have which are
  8482. 4:49:54all the student names
  8483. 4:49:56now how can we sort these iterables by
  8484. 4:49:58their second column
  8485. 4:49:59for this case it would be student grades
  8486. 4:50:01or even the third column which would be
  8487. 4:50:03the student ages
  8488. 4:50:04well that's where the key keyword
  8489. 4:50:07argument's going to come in
  8490. 4:50:08this is a keyword argument and we set
  8491. 4:50:10key equal to a function that's going to
  8492. 4:50:13return
  8493. 4:50:13the index of that specific column that
  8494. 4:50:16we have
  8495. 4:50:17so let's say key equals grid grade is
  8496. 4:50:20going to be a function object
  8497. 4:50:22grade equals and we can easily use a
  8498. 4:50:26lambda expression for this lambda
  8499. 4:50:29let's say grades colon
  8500. 4:50:32grades and we will set an index of one
  8501. 4:50:36now the first index is zero that's the
  8502. 4:50:39first
  8503. 4:50:39column because computers always start
  8504. 4:50:41with zero column two would have an index
  8505. 4:50:44of one
  8506. 4:50:44and then column three would have an
  8507. 4:50:47index of two
  8508. 4:50:48so grade equals lambda grids
  8509. 4:50:51colon grids index one you can also
  8510. 4:50:54rename these if you want
  8511. 4:50:55so students dot sort we're setting the
  8512. 4:50:58key
  8513. 4:50:59equal to grade and grade is a function
  8514. 4:51:01object
  8515. 4:51:02via a lambda function and now all of
  8516. 4:51:05these students
  8517. 4:51:05will be sorted by their grades starting
  8518. 4:51:08with sandy
  8519. 4:51:09then spongebob mr krabs patrick then
  8520. 4:51:11squidward
  8521. 4:51:12if this needs to be in reverse order we
  8522. 4:51:15can pass in that other keyword argument
  8523. 4:51:16of reverse
  8524. 4:51:17equals true and for practice if you
  8525. 4:51:21wanted to sort all of this data by each
  8526. 4:51:23student's age
  8527. 4:51:24then we would change grade to let's say
  8528. 4:51:26age age equals lambda
  8529. 4:51:28we'll change grades to maybe ages colon
  8530. 4:51:31ages and the index would then be two
  8531. 4:51:34because the first column here is zero
  8532. 4:51:37then one
  8533. 4:51:38then two and change key to our function
  8534. 4:51:41object
  8535. 4:51:42of edge now each student is
  8536. 4:51:45sorted numerically beginning with the
  8537. 4:51:47smallest edge
  8538. 4:51:48well the youngest person starting with
  8539. 4:51:50spongebob sandy patrick squidward then
  8540. 4:51:52mr krabs
  8541. 4:51:53and like i said before if you want your
  8542. 4:51:56data arranged in reverse order you can
  8543. 4:51:58set
  8544. 4:51:58that keyword argument of reverse to true
  8545. 4:52:01within the sort method
  8546. 4:52:02and all of our student data is sorted
  8547. 4:52:05beginning with the eldest student which
  8548. 4:52:07is mr krabs
  8549. 4:52:08then squidward patrick sandy and then
  8550. 4:52:10spongebob
  8551. 4:52:11and for the last part of this video
  8552. 4:52:13let's say that we're working with
  8553. 4:52:14some other iterable let's say we have a
  8554. 4:52:17tuple of tuples instead
  8555. 4:52:19well we can no longer use this sort
  8556. 4:52:21method because that only belongs to
  8557. 4:52:23lists
  8558. 4:52:24you can also use the sort function with
  8559. 4:52:25lists and it will generate a new sorted
  8560. 4:52:27list without changing the placement of
  8561. 4:52:29the original
  8562. 4:52:30so let's say we have a tuple of tuples
  8563. 4:52:33and this will create a
  8564. 4:52:34new sorted list let's call this list
  8565. 4:52:37sorted
  8566. 4:52:38students equals then we'll use the
  8567. 4:52:41sorted function we need to pass in an
  8568. 4:52:43iterable and we can pass in
  8569. 4:52:46both a key and or the reverse keyword
  8570. 4:52:48argument
  8571. 4:52:49so the iterable would be students
  8572. 4:52:53and let's set the key equal to edge
  8573. 4:52:56and i will not reverse it we can keep it
  8574. 4:52:58as it is but let's change
  8575. 4:53:00for i in students to sorted students
  8576. 4:53:04and that's how to use the sorted
  8577. 4:53:06function to sort
  8578. 4:53:07an iterable including a list well all
  8579. 4:53:10right everyone
  8580. 4:53:11that's how to sort iterables in python
  8581. 4:53:13if you would like a copy of all this
  8582. 4:53:15code i will post all of this to the
  8583. 4:53:16comments section down below
  8584. 4:53:18but yeah that's a basic way to sort
  8585. 4:53:20iterables
  8586. 4:53:21in python hey what's going on everybody
  8587. 4:53:24it's your bro hope you're doing well in
  8588. 4:53:26today's video i'm going to show you all
  8589. 4:53:27how the map function works in python so
  8590. 4:53:30sit back relax and enjoy the show
  8591. 4:53:34what up everybody let's discuss the map
  8592. 4:53:36function the map function will apply a
  8593. 4:53:39function to each item in an interval
  8594. 4:53:41that would include lists tuples things
  8595. 4:53:43like that
  8596. 4:53:44and the map function accepts two
  8597. 4:53:46arguments are iterable
  8598. 4:53:48as well as the function we would like to
  8599. 4:53:50apply to each item within our iterable
  8600. 4:53:52for example let's say that we have an
  8601. 4:53:54online store to represent this i have
  8602. 4:53:56a list of tuples named store each tuple
  8603. 4:53:59within this list
  8604. 4:54:01corresponds to an item for sale within
  8605. 4:54:03our store
  8606. 4:54:04there's shirts pants jackets and socks
  8607. 4:54:06and each item has
  8608. 4:54:07a price in us dollars let's say now what
  8609. 4:54:10i would like to do is to convert all of
  8610. 4:54:12the prices within my store
  8611. 4:54:14to euros and currently the conversion
  8612. 4:54:16rate as of the filming of this video is
  8613. 4:54:18one us dollar to 0.82 euros
  8614. 4:54:22so let's begin we can easily convert
  8615. 4:54:24dollars to euros with a
  8616. 4:54:25lambda function so let's say two euros
  8617. 4:54:29equals lambda the parameter will be data
  8618. 4:54:34colon and the next part's a little bit
  8619. 4:54:36tricky within a set of parenthesis to
  8620. 4:54:38represent a tuple
  8621. 4:54:39we'll take data at index 0 that
  8622. 4:54:42corresponds to the first column
  8623. 4:54:45comma data at index 1.
  8624. 4:54:49this represents the index of our prices
  8625. 4:54:52we're going to take
  8626. 4:54:52data at index 1 times 0.82
  8627. 4:54:57and that's it the first column will be
  8628. 4:54:59left untouched
  8629. 4:55:00but the data in column 1 will be
  8630. 4:55:03multiplied by 0.82
  8631. 4:55:05now our map function is going to create
  8632. 4:55:07a map object but we can easily cast that
  8633. 4:55:10to a different type of iterable
  8634. 4:55:12so let's say that store underscore
  8635. 4:55:16euros equals map
  8636. 4:55:19parenthesis and then we can pass in our
  8637. 4:55:21function as well as our iterable
  8638. 4:55:23our function is our two euros function
  8639. 4:55:27comma and our iterable is our store
  8640. 4:55:31and that is it now if you want to
  8641. 4:55:33convert this map object to an iterable
  8642. 4:55:36you can easily surround this with a cast
  8643. 4:55:38i would like to convert this map object
  8644. 4:55:40to a list
  8645. 4:55:42and we will now have a new store named
  8646. 4:55:44store
  8647. 4:55:45euros which will have all of the
  8648. 4:55:47different prices of our store in euros
  8649. 4:55:49and to display this i'll use a for loop
  8650. 4:55:52for i
  8651. 4:55:52in store euros
  8652. 4:55:56print i and
  8653. 4:56:00let's try it yep that seems about right
  8654. 4:56:03all of the prices in our store are now
  8655. 4:56:05in euros
  8656. 4:56:06let's pretend that these prices are
  8657. 4:56:08already in euros and we would like to
  8658. 4:56:10convert these
  8659. 4:56:10to american dollars so we will use the
  8660. 4:56:13same conversion rate but divide
  8661. 4:56:15by 0.82 so let's create a separate
  8662. 4:56:18lambda function
  8663. 4:56:19two dollars equals
  8664. 4:56:22i think i'll just copy this but we are
  8665. 4:56:25going to divide
  8666. 4:56:26the first index of data by 0.82
  8667. 4:56:30and let's say store dollars to represent
  8668. 4:56:34our store
  8669. 4:56:35in dollars because we're pretending now
  8670. 4:56:37that it's currently in euros
  8671. 4:56:41and we will display 4i in store dollars
  8672. 4:56:45so if all of these prices are in euros
  8673. 4:56:48we are now converting them to american
  8674. 4:56:50dollars
  8675. 4:56:51but you may want to do some rounding as
  8676. 4:56:52opposed to the nearest descent
  8677. 4:56:54well everyone that is the map function
  8678. 4:56:56it applies a function to
  8679. 4:56:58each item in an iterable those include
  8680. 4:57:00lists tuples
  8681. 4:57:01the like you pass in your iterable and
  8682. 4:57:04your function as an argument to the map
  8683. 4:57:07function so if you would like a copy of
  8684. 4:57:09all this code i will post all of this to
  8685. 4:57:11the comment section down below
  8686. 4:57:12but yeah that is how the map function
  8687. 4:57:14works in python
  8688. 4:57:17what's going on everybody it's you bro
  8689. 4:57:19hope you're doing well and in today's
  8690. 4:57:21video i'm going to show you all how the
  8691. 4:57:22filter function works in python
  8692. 4:57:24so sit back relax and enjoy the show
  8693. 4:57:30all right all right let's discuss the
  8694. 4:57:32filter function
  8695. 4:57:33the filter function creates a collection
  8696. 4:57:35of elements from an
  8697. 4:57:36iterable for which a function returns
  8698. 4:57:39true
  8699. 4:57:40now what the heck does that mean let me
  8700. 4:57:42explain let's say that we have a list of
  8701. 4:57:44tuples named
  8702. 4:57:45friends each tuple has the name of a
  8703. 4:57:48friend as well as their age
  8704. 4:57:49i just made up some ages for all of
  8705. 4:57:51these friends what i would like to do is
  8706. 4:57:53to create a
  8707. 4:57:53separate list for all the friends that
  8708. 4:57:55are 18 or older
  8709. 4:57:57so let's say that we're all going out
  8710. 4:57:59drinking and i would like to create a
  8711. 4:58:01list of my drinking buddies
  8712. 4:58:02because they're of drinking age i'm in
  8713. 4:58:04the united states and the drinking age
  8714. 4:58:06is currently 21
  8715. 4:58:07but i think worldwide is 18 so let's go
  8716. 4:58:09with 18 for this example since
  8717. 4:58:11i have a worldwide audience all right
  8718. 4:58:13now with this filter function
  8719. 4:58:15we have to pass in our iterable as well
  8720. 4:58:17as a function
  8721. 4:58:18to get anybody's edge that is over 18.
  8722. 4:58:21so let's begin by writing a
  8723. 4:58:22lambda function for this expression here
  8724. 4:58:24our filter function
  8725. 4:58:26so let's say age equals lambda
  8726. 4:58:30data will be the parameter colon data
  8727. 4:58:34at index one so the first index
  8728. 4:58:38the first column in this chart here
  8729. 4:58:40would be zero for other names the next
  8730. 4:58:42index of one would be all the ages
  8731. 4:58:45data at index one is greater than
  8732. 4:58:48or equal to eighteen and that is it
  8733. 4:58:51that is our lambda function and now
  8734. 4:58:53we're going to filter
  8735. 4:58:55our iterable by this function of age so
  8736. 4:58:57let's write
  8737. 4:58:59filter pass in our function
  8738. 4:59:02which is edge comma and our iterable
  8739. 4:59:06friends now this filter function will
  8740. 4:59:08return a filter
  8741. 4:59:10object you can easily cast this i would
  8742. 4:59:11like to cast
  8743. 4:59:13my friends list back into a list
  8744. 4:59:17and i will assign this to a new list
  8745. 4:59:19let's say
  8746. 4:59:20drinking buddies
  8747. 4:59:24equals list filter age
  8748. 4:59:27and friends and i should be able to
  8749. 4:59:30print
  8750. 4:59:30all of the friends that are 18 or older
  8751. 4:59:33for i
  8752. 4:59:34in drinking bodies
  8753. 4:59:38print i
  8754. 4:59:42and we have rachel monica chandler and
  8755. 4:59:44ross
  8756. 4:59:45well everyone that is the filter
  8757. 4:59:47function it creates a collection of
  8758. 4:59:49elements
  8759. 4:59:50from an iterable for which a function
  8760. 4:59:52returns true
  8761. 4:59:53i like to think of it as the search
  8762. 4:59:55results we're searching for any results
  8763. 4:59:57that meet this criteria where age is
  8764. 5:00:00greater than or equal to 18
  8765. 5:00:02and then you can create a separate
  8766. 5:00:03collection or other list if you want
  8767. 5:00:05from the results
  8768. 5:00:06but yeah that is how the filter function
  8769. 5:00:08works in python
  8770. 5:00:10yo what's going on everybody it's bro
  8771. 5:00:12hope you're doing well
  8772. 5:00:13and in today's video i'm going to show
  8773. 5:00:15you how the reduce function works in
  8774. 5:00:17python so
  8775. 5:00:18sit back relax and enjoy the show
  8776. 5:00:22well i suppose we're on the reduce
  8777. 5:00:24function today the reduce function
  8778. 5:00:26applies a function of our choosing
  8779. 5:00:28to an iterable and reduces that iterable
  8780. 5:00:31to a single cumulative value the way
  8781. 5:00:34this works is that the reduce function
  8782. 5:00:36performs our function
  8783. 5:00:37on the first two elements of our
  8784. 5:00:39iterable and repeats the process
  8785. 5:00:41until only one value remains i kind of
  8786. 5:00:44like to think of it like
  8787. 5:00:45we're recycling elements within an
  8788. 5:00:47iterable until a single
  8789. 5:00:49value remains our finished product so
  8790. 5:00:51how is this useful let's say that
  8791. 5:00:53we're playing a game scramble and we
  8792. 5:00:55have a bunch of different letters within
  8793. 5:00:57an iterable
  8794. 5:00:58a list is a type of iterable so i have a
  8795. 5:01:01list named letters and we have all of
  8796. 5:01:03the letters we need to spell the word
  8797. 5:01:05what i would like to do is to reduce all
  8798. 5:01:07of these individual elements into a
  8799. 5:01:09single cumulative value until only one
  8800. 5:01:12value remains so we can do that using
  8801. 5:01:14the reduce function you can also do this
  8802. 5:01:16with the for loop too there's multiple
  8803. 5:01:18ways of doing this
  8804. 5:01:19so using the reduce function we're going
  8805. 5:01:21to import
  8806. 5:01:23func tools and now let's say we have
  8807. 5:01:28our cumulative value stored within a
  8808. 5:01:30variable named word
  8809. 5:01:32word equals func tools dot reduce
  8810. 5:01:36now the reduce function has two
  8811. 5:01:38arguments the function we would like to
  8812. 5:01:40apply
  8813. 5:01:41to the first two elements and then our
  8814. 5:01:43iterable
  8815. 5:01:44the interval is easy that is just
  8816. 5:01:45letters so let's put that in
  8817. 5:01:48letters and for the function we can
  8818. 5:01:50easily write a lambda function for this
  8819. 5:01:52lambda and now this function needs two
  8820. 5:01:55parameters
  8821. 5:01:56let's say x and y now what kind of
  8822. 5:02:00function do we want to perform on the
  8823. 5:02:02first two elements of our iterable
  8824. 5:02:04let's combine them together so let's say
  8825. 5:02:07x
  8826. 5:02:07plus y and now
  8827. 5:02:10let's print our single cumulative value
  8828. 5:02:14of word so this should print
  8829. 5:02:18the word hello i'm fairly confident that
  8830. 5:02:21the word hello is worth like
  8831. 5:02:2220 points in scrabble now let me explain
  8832. 5:02:24what happened step by
  8833. 5:02:26step our reduce function applies our
  8834. 5:02:28function
  8835. 5:02:29to the first two elements within our
  8836. 5:02:31iterable
  8837. 5:02:32it performs this expression that we set
  8838. 5:02:35x plus y but really we can write
  8839. 5:02:37anything here
  8840. 5:02:38so we perform this expression on the
  8841. 5:02:41first two elements
  8842. 5:02:43then we repeat the process again using
  8843. 5:02:46the result
  8844. 5:02:46from the first time we use this
  8845. 5:02:48expression and the next element
  8846. 5:02:50so we're combining x plus y again
  8847. 5:02:54and then we're just going to repeat this
  8848. 5:02:56process until only
  8849. 5:02:58one value remains it's kind of like
  8850. 5:03:00we're recycling
  8851. 5:03:01all of these letters and creating a
  8852. 5:03:03finished product out of all of these
  8853. 5:03:05that's how i think of it at least here's
  8854. 5:03:07a different example let's say that i
  8855. 5:03:08would like to find
  8856. 5:03:10the factorial of 5 so i have the numbers
  8857. 5:03:135 through 1 within a list named
  8858. 5:03:16factorial
  8859. 5:03:17so if i was to find the factorial of 5
  8860. 5:03:20via the reduce function
  8861. 5:03:21i would probably write something like
  8862. 5:03:23this so let me change
  8863. 5:03:25word to let's say result result
  8864. 5:03:28equals functools reduce now our lambda
  8865. 5:03:31function will be
  8866. 5:03:32x times y because with factorials we
  8867. 5:03:36multiply
  8868. 5:03:37the next two numbers together then we
  8869. 5:03:40take that result
  8870. 5:03:41and then multiply the next one in this
  8871. 5:03:42list so the
  8872. 5:03:44iterable will be factorial and
  8873. 5:03:47let's print what our result is
  8874. 5:03:51so the factorial of 5 should be 120. now
  8875. 5:03:55let's walk through this we're taking
  8876. 5:03:56the first two elements of this iterable
  8877. 5:03:59five
  8878. 5:03:59and four multiplying together in this
  8879. 5:04:02case in the last example we concatenated
  8880. 5:04:05the strings together of x and y
  8881. 5:04:07in this example we're multiplying them
  8882. 5:04:08together so we take
  8883. 5:04:10the first two elements 5 times 4
  8884. 5:04:13we take the result and apply it to the
  8885. 5:04:15next iteration
  8886. 5:04:17of x times y again so 20 times 3
  8887. 5:04:20is 60. we repeat this process again
  8888. 5:04:2460 times 2 is 120 and 120 times 1
  8889. 5:04:28is 120 so that's kind of how the reduce
  8890. 5:04:31function
  8891. 5:04:32works it applies a function of our
  8892. 5:04:34choosing to the first
  8893. 5:04:36two elements of an iterable and repeats
  8894. 5:04:38that process until
  8895. 5:04:39only a single cumulative value remains
  8896. 5:04:43so that is the reduce function if you
  8897. 5:04:44would like a copy of all this code i
  8898. 5:04:46will post all of this to the comment
  8899. 5:04:48section down below
  8900. 5:04:49but yeah that is how the reduce function
  8901. 5:04:51works in python
  8902. 5:04:54hey what's going on everybody it's your
  8903. 5:04:56bro hope you're doing well and in this
  8904. 5:04:58video i'm going to show you all how list
  8905. 5:04:59comprehensions work in python so
  8906. 5:05:02sit back relax and enjoy the show
  8907. 5:05:06all right everybody so list
  8908. 5:05:08comprehensions a list comprehension is a
  8909. 5:05:10way to create a new list with
  8910. 5:05:12less syntax you can also use a list
  8911. 5:05:15comprehension to mimic
  8912. 5:05:16certain lambda functions such as in the
  8913. 5:05:18previous videos where we use
  8914. 5:05:20the map and the filter functions and not
  8915. 5:05:23only that but a list comprehension
  8916. 5:05:24is easier to read than a lambda function
  8917. 5:05:27but there is a formula that we're going
  8918. 5:05:29to follow
  8919. 5:05:29when we create our list within our list
  8920. 5:05:32we're going to write an expression
  8921. 5:05:34for item in iterable now let me give you
  8922. 5:05:37an example of where a list comprehension
  8923. 5:05:39would be useful
  8924. 5:05:40let's take this program that will create
  8925. 5:05:42a list of all the numbers
  8926. 5:05:441 through 10 squared so if i were to
  8927. 5:05:46print this we'll get the numbers 1
  8928. 5:05:48through 10
  8929. 5:05:48whatever their square is right so we
  8930. 5:05:50have 1 through 10
  8931. 5:05:52and the square of 10 is 100 so we wrote
  8932. 5:05:55this program
  8933. 5:05:56in three lines of code we're creating an
  8934. 5:05:58empty list we're creating a for loop
  8935. 5:06:00and then we're writing an expression for
  8936. 5:06:01what we want each loop iteration to do
  8937. 5:06:04now let's write the same program but
  8938. 5:06:05used a list comprehension
  8939. 5:06:07and we can write the same program with
  8940. 5:06:09less syntax
  8941. 5:06:10so i would write something like this and
  8942. 5:06:12we're going to follow this formula
  8943. 5:06:14so our list is going to be named
  8944. 5:06:17squares equals then within a set of
  8945. 5:06:21straight brackets
  8946. 5:06:22we're going to write our expression
  8947. 5:06:24which is this portion i times i
  8948. 5:06:27for item so that would be 4i
  8949. 5:06:31in our iterable that would be range
  8950. 5:06:341 through 11 because remember 11's
  8951. 5:06:38exclusive
  8952. 5:06:39and then let's print our squares print
  8953. 5:06:43squares and there you go
  8954. 5:06:47we made the exact same program using a
  8955. 5:06:49list comprehension
  8956. 5:06:50so basically speaking you just follow
  8957. 5:06:52this formula you set your list equal to
  8958. 5:06:55your expression and our expression for
  8959. 5:06:58this example is i times i
  8960. 5:07:00for item for i in
  8961. 5:07:03your interval in range one through
  8962. 5:07:05eleven
  8963. 5:07:06so this took three lines of code well if
  8964. 5:07:09you exclude us printing
  8965. 5:07:11our list and this program took one line
  8966. 5:07:14of code
  8967. 5:07:14now we can also use a list comprehension
  8968. 5:07:17to mimic certain lambda functions i
  8969. 5:07:19wrote a program
  8970. 5:07:20where we will be filtering a list of
  8971. 5:07:22student grades
  8972. 5:07:23let's pretend that all of these students
  8973. 5:07:25took some sort of exam
  8974. 5:07:26so we have one student that received a
  8975. 5:07:28100 90 80 70 60 50 40 30
  8976. 5:07:32and then one student got a zero because
  8977. 5:07:34they didn't show up so what i would like
  8978. 5:07:35to do is to filter
  8979. 5:07:36all of these student grades into a list
  8980. 5:07:39that only contains
  8981. 5:07:40students that passed and the criteria
  8982. 5:07:42for a passing grade will be
  8983. 5:07:44a 60 or above so i would like to filter
  8984. 5:07:48all passing students and if i was to
  8985. 5:07:50write this program using the filter
  8986. 5:07:52function i would probably write
  8987. 5:07:53something like this
  8988. 5:07:54so i need a function and then my list
  8989. 5:07:56that i'm working with
  8990. 5:07:57lambda x colon x is greater than or
  8991. 5:08:01equal to 60
  8992. 5:08:02will filter all of the results and then
  8993. 5:08:04convert it to a list named past students
  8994. 5:08:06so the result will look something like
  8995. 5:08:08this i have five student grades in here
  8996. 5:08:11with 100 90 80 70 and 60.
  8997. 5:08:14now let's write the same program using a
  8998. 5:08:16list comprehension
  8999. 5:08:17now there's just one portion to this
  9000. 5:08:19formula for list comprehension that
  9001. 5:08:20we're going to add
  9002. 5:08:21at the end we're going to add if
  9003. 5:08:23conditional we can check
  9004. 5:08:25some sort of condition after each
  9005. 5:08:27iteration so
  9006. 5:08:28let's write the same program using a
  9007. 5:08:30list comprehension following
  9008. 5:08:31this new updated formula that we have so
  9009. 5:08:34we need
  9010. 5:08:34our list which will be named past
  9011. 5:08:38students equals first comes our
  9012. 5:08:41expression
  9013. 5:08:41and it's just i for this example for
  9014. 5:08:44item
  9015. 5:08:45i in our iterable
  9016. 5:08:48of students and then if we can check our
  9017. 5:08:51conditional
  9018. 5:08:52if i is greater than or equal to 60
  9019. 5:08:56and this will have the same effect as
  9020. 5:08:59our previous program but instead it uses
  9021. 5:09:01a list comprehension
  9022. 5:09:03now if your program has multiple output
  9023. 5:09:06if you need an else statement within
  9024. 5:09:07your conditional
  9025. 5:09:08you're actually going to move this
  9026. 5:09:09portion of the if conditional within
  9027. 5:09:12our formula to right after the
  9028. 5:09:13expression so let's write
  9029. 5:09:15an if else statement here
  9030. 5:09:18so let's say that instead of just
  9031. 5:09:20excluding any student that didn't pass
  9032. 5:09:22we'll replace their grade with the word
  9033. 5:09:24failed and we can do that
  9034. 5:09:26using an if else statement so let's do
  9035. 5:09:28this again i'm going to copy what we
  9036. 5:09:30have here
  9037. 5:09:31paste it and we'll move our conditional
  9038. 5:09:33from the end
  9039. 5:09:35to just after the expression portion i
  9040. 5:09:39if i is greater than or equal to 60 and
  9041. 5:09:41you can see here that we need to add an
  9042. 5:09:43else statement else will return
  9043. 5:09:46the word failed
  9044. 5:09:51so we're replacing each instance of
  9045. 5:09:54a failing grade with the word failed
  9046. 5:09:56instead so if your condition
  9047. 5:09:58returns whatever i is you can just add
  9048. 5:10:00that to the end
  9049. 5:10:01if you need an if else statement you're
  9050. 5:10:02going to add it right after the
  9051. 5:10:04expression
  9052. 5:10:05so everybody in conclusion a list
  9053. 5:10:07comprehension is a way to create a new
  9054. 5:10:10list
  9055. 5:10:10with less syntax you can even mimic
  9056. 5:10:13certain lambda functions which we did
  9057. 5:10:14with the
  9058. 5:10:15filter function and it has the added
  9059. 5:10:17benefit of being easier to read than a
  9060. 5:10:19lambda function
  9061. 5:10:21so you just follow one of these formulas
  9062. 5:10:22depending on what you're trying to do
  9063. 5:10:24so you need at least list equals your
  9064. 5:10:27expression
  9065. 5:10:28for item and your interval if you have a
  9066. 5:10:30condition that you want to check
  9067. 5:10:31you just add that to the end of your
  9068. 5:10:33list comprehension if you have an if
  9069. 5:10:35else statement
  9070. 5:10:36then you'll add that to right after the
  9071. 5:10:38expression so you just follow one of
  9072. 5:10:40these formulas depending on how you want
  9073. 5:10:42to write your program
  9074. 5:10:43so that is a list comprehension if you
  9075. 5:10:45would like a copy of this code i will
  9076. 5:10:47post all of this to the comments section
  9077. 5:10:48down below and well
  9078. 5:10:50yeah that's how list comprehensions work
  9079. 5:10:52in python
  9080. 5:10:54yo what's going on everybody it's bro
  9081. 5:10:56hope you're doing well
  9082. 5:10:57and in this video i'm going to explain
  9083. 5:10:59dictionary comprehensions in python so
  9084. 5:11:01sit back relax and enjoy the show
  9085. 5:11:06okay people dictionary comprehensions
  9086. 5:11:09they're very similar to list
  9087. 5:11:10comprehensions except
  9088. 5:11:11they're with dictionaries that's it
  9089. 5:11:13video over okay but seriously a
  9090. 5:11:15dictionary comprehension is a way to
  9091. 5:11:17create dictionaries using an expression
  9092. 5:11:19and they can replace for loops and
  9093. 5:11:21certain lambda functions
  9094. 5:11:22and all we have to do is follow this
  9095. 5:11:24formula dictionary equals
  9096. 5:11:26key colon our expression for key value
  9097. 5:11:29in iterable so let's go over a few
  9098. 5:11:31examples our first example we're going
  9099. 5:11:33to take
  9100. 5:11:34this dictionary named cities in f f is
  9101. 5:11:37short for fahrenheit
  9102. 5:11:38i have different city names as keys and
  9103. 5:11:41relative temperatures as
  9104. 5:11:42values in fahrenheit so new york is 32
  9105. 5:11:46degrees fahrenheit boston is 75.
  9106. 5:11:48los angeles is 100 and chicago is 50.
  9107. 5:11:51and what we'll be doing is creating a
  9108. 5:11:53separate dictionary where all of these
  9109. 5:11:55temperatures will be in celsius
  9110. 5:11:57using a dictionary comprehension so
  9111. 5:12:00let's follow this formula let's create a
  9112. 5:12:02new name for this dictionary
  9113. 5:12:03let's say cities in c short for celsius
  9114. 5:12:07equals and we will follow this pattern
  9115. 5:12:10first our key
  9116. 5:12:11golden then our expression we'll go back
  9117. 5:12:13to this in just a moment
  9118. 5:12:15for key comma
  9119. 5:12:18value in our iterable
  9120. 5:12:21of cities in f and since we're working
  9121. 5:12:25with a dictionary we're going to use
  9122. 5:12:27the items method now we just need to
  9123. 5:12:29fill in this expression
  9124. 5:12:31there is a formula to convert fahrenheit
  9125. 5:12:33to celsius and it should be on the
  9126. 5:12:34screen right about now
  9127. 5:12:36so let's follow this formula so we'll
  9128. 5:12:38take our value
  9129. 5:12:40minus 32 and just for good measure i'm
  9130. 5:12:43going to put these within parentheses
  9131. 5:12:45and we will multiply all of this by
  9132. 5:12:495 divided by 9 and that is it so we have
  9133. 5:12:52our key
  9134. 5:12:54our expression to convert fahrenheit to
  9135. 5:12:56celsius
  9136. 5:12:58for key value in our iterable of
  9137. 5:13:01cities in f our previous dictionary and
  9138. 5:13:04let's test this by printing our new
  9139. 5:13:07dictionary
  9140. 5:13:08cities in c
  9141. 5:13:12actually i think i'm going to round
  9142. 5:13:14these numbers just so it's more readable
  9143. 5:13:16so let's round all this there we go so
  9144. 5:13:19new york and celsius is zero
  9145. 5:13:21boston is 24. los angeles is 38 and
  9146. 5:13:24chicago is 10.
  9147. 5:13:26so we created a new dictionary using a
  9148. 5:13:29dictionary comprehension
  9149. 5:13:30now with these dictionary comprehensions
  9150. 5:13:32you can add an if conditional to the end
  9151. 5:13:34of this
  9152. 5:13:35so let's say that we have a separate
  9153. 5:13:37dictionary of weather
  9154. 5:13:38like a description of the weather in
  9155. 5:13:40each city new york is snowing
  9156. 5:13:42boston is sunny los angeles is sunny and
  9157. 5:13:44chicago is cloudy
  9158. 5:13:46let's say that we would like to create a
  9159. 5:13:47separate dictionary with a dictionary
  9160. 5:13:50comprehension
  9161. 5:13:51that only has cities where the weather
  9162. 5:13:53is sunny
  9163. 5:13:54so i would write something like this
  9164. 5:13:57let's say sunny
  9165. 5:13:58weather will be the name of our
  9166. 5:13:59dictionary equals and we'll follow
  9167. 5:14:02this formula and then we just tack on
  9168. 5:14:04our conditional to the end so it's kind
  9169. 5:14:06of the same as before really
  9170. 5:14:08so key colon and we don't really have an
  9171. 5:14:11expression here so let's just say
  9172. 5:14:14value then for
  9173. 5:14:19key comma value
  9174. 5:14:22in our iterable of
  9175. 5:14:25weather dot items
  9176. 5:14:28then our conditional if value
  9177. 5:14:33sunny because we are only looking for
  9178. 5:14:35sunny weather
  9179. 5:14:37and let's print this print
  9180. 5:14:40our dictionary of sunny weather
  9181. 5:14:44and let me just make this more readable
  9182. 5:14:45for you guys and our new dictionary
  9183. 5:14:47comprehension
  9184. 5:14:48will create a dictionary of key values
  9185. 5:14:51where the value
  9186. 5:14:52is sunny using an if conditional at the
  9187. 5:14:55end of our dictionary comprehension
  9188. 5:14:57here's a third example for you well if
  9189. 5:14:59you have an if else condition you can
  9190. 5:15:00add that to where the expression is
  9191. 5:15:02within your dictionary comprehension
  9192. 5:15:04so i'm going to reuse the previous
  9193. 5:15:06dictionary for the first example where
  9194. 5:15:08we have
  9195. 5:15:09cities and their temperatures as values
  9196. 5:15:11so we're going to replace each
  9197. 5:15:13temperature with a description of the
  9198. 5:15:15weather
  9199. 5:15:16is it warm or is it cold so let's do
  9200. 5:15:18that using
  9201. 5:15:19a dictionary comprehension that contains
  9202. 5:15:21an if else statement
  9203. 5:15:22so let's say we have a new dictionary
  9204. 5:15:24named desk short for description
  9205. 5:15:27cities it's a brief description of each
  9206. 5:15:29city's temperature
  9207. 5:15:31so we follow this formula key
  9208. 5:15:34then our if else conditional for key
  9209. 5:15:37value
  9210. 5:15:38and iterable actually i probably can
  9211. 5:15:40copy all this
  9212. 5:15:41just to save some time okay so our
  9213. 5:15:44iterable
  9214. 5:15:45will be cities dot items
  9215. 5:15:49and our conditional is going to be
  9216. 5:15:53we will return warm if
  9217. 5:15:56our value is greater than or equal to
  9218. 5:16:00let's say 40. else
  9219. 5:16:04cold and we will print
  9220. 5:16:07our new dictionary of desk cities
  9221. 5:16:11description of cities so new york is
  9222. 5:16:14cold because it's 32 that's below 40.
  9223. 5:16:17boston is warm los angeles is warm and
  9224. 5:16:19chicago is warm
  9225. 5:16:21now if your condition gets somewhat
  9226. 5:16:22complex you can even call a separate
  9227. 5:16:25function to keep your code more
  9228. 5:16:26organized
  9229. 5:16:27so key then we can call a function and
  9230. 5:16:29pass in a value
  9231. 5:16:30for key value in iterable so i'm going
  9232. 5:16:32to again
  9233. 5:16:33reuse our city names and our
  9234. 5:16:35temperatures
  9235. 5:16:36and this time we are going to call a
  9236. 5:16:39function instead
  9237. 5:16:40i think i'll reuse this code from the
  9238. 5:16:41previous example where we have a
  9239. 5:16:43separate dictionary named
  9240. 5:16:44desk cities and i'm going to replace
  9241. 5:16:47this if else statement
  9242. 5:16:48with a function so let's say we have a
  9243. 5:16:51function that is named
  9244. 5:16:52check temp and we will pass in our value
  9245. 5:16:56and we just need to define this function
  9246. 5:16:58so let's define that here
  9247. 5:17:00so let's define check
  9248. 5:17:03temp and our parameter is our value that
  9249. 5:17:06we're currently working on within our
  9250. 5:17:08dictionary
  9251. 5:17:09value then we'll write if
  9252. 5:17:12value is greater than or equal to
  9253. 5:17:16let's say anything above how about 70
  9254. 5:17:19will return the word hot
  9255. 5:17:24then else if
  9256. 5:17:27let's say 69 greater than or equal to
  9257. 5:17:32value and then
  9258. 5:17:35value is greater than or equal to
  9259. 5:17:39how about 40 then we will
  9260. 5:17:43return the word warm
  9261. 5:17:46and lastly else return
  9262. 5:17:51cold and let me just fix some of the
  9263. 5:17:53spacing
  9264. 5:17:56now depending on the temperature that is
  9265. 5:17:58stored within each value of our
  9266. 5:18:00dictionary
  9267. 5:18:01we will call a function that will return
  9268. 5:18:04one of a few different outputs
  9269. 5:18:06so it's kind of like we're using the map
  9270. 5:18:08function the map lambda function
  9271. 5:18:11so let's print our description
  9272. 5:18:14of cities and we should get a short
  9273. 5:18:16description of the temperature in each
  9274. 5:18:18city
  9275. 5:18:19after we call the check temperature
  9276. 5:18:21function and then pass in each value
  9277. 5:18:23from our dictionary
  9278. 5:18:24so new york is cold because it's 32
  9279. 5:18:27degrees fahrenheit
  9280. 5:18:28boston is hot because it's above 70. los
  9281. 5:18:31angeles is hot
  9282. 5:18:32because it's 100 and then chicago is
  9283. 5:18:34warm because that is 50 which is between
  9284. 5:18:3669 and 40 within our function
  9285. 5:18:40so that is a dictionary comprehension
  9286. 5:18:42it's a shortcut where you can create
  9287. 5:18:44dictionaries using an expression
  9288. 5:18:46and they can replace for loops and
  9289. 5:18:48certain lambda functions
  9290. 5:18:49so if you would like a copy of all this
  9291. 5:18:51code i will post all of this to the
  9292. 5:18:53comment section down below
  9293. 5:18:54and well yeah that's how dictionary
  9294. 5:18:56comprehensions work
  9295. 5:18:57in python hey what's going on everybody
  9296. 5:19:00it's bro hope you're doing well and in
  9297. 5:19:02today's video i'm going to show you how
  9298. 5:19:04the zip function works in python so
  9299. 5:19:06sit back relax and enjoy the show
  9300. 5:19:11all right ladies and gentlemen welcome
  9301. 5:19:13back we're talking about the zip
  9302. 5:19:14function today
  9303. 5:19:16the zip function will aggregate elements
  9304. 5:19:18from two or more iterables
  9305. 5:19:20iterables are those things like lists
  9306. 5:19:23tuples
  9307. 5:19:23sets etc and the zip function will
  9308. 5:19:26create a
  9309. 5:19:26zip object with paired elements from
  9310. 5:19:29each iterable
  9311. 5:19:30stored in a tuple for each element
  9312. 5:19:33within our zip object
  9313. 5:19:34here's an example let's say that we have
  9314. 5:19:37two different types of iterables
  9315. 5:19:38i have a list of usernames and a few
  9316. 5:19:41usernames within here are
  9317. 5:19:43dude bro and mr and i have a
  9318. 5:19:46tuple of passwords and i have some very
  9319. 5:19:48secure passwords
  9320. 5:19:49such as password abc123
  9321. 5:19:53and guest what i would like to do is to
  9322. 5:19:55zip elements from each iterable together
  9323. 5:19:58so that they're in pairs and each pair
  9324. 5:20:00is going to be stored as a tuple within
  9325. 5:20:03a zip object and here's how we can do
  9326. 5:20:05that let's say that we will create a zip
  9327. 5:20:07object called users
  9328. 5:20:09and we will use the zip function the zip
  9329. 5:20:11function will take a
  9330. 5:20:13varying amount of iterables we're going
  9331. 5:20:14to pass in our usernames
  9332. 5:20:16and passwords and zip them together so
  9333. 5:20:18let's pass in usernames
  9334. 5:20:20as well as passwords and now our zip
  9335. 5:20:23object of users
  9336. 5:20:24is actually iterable zip objects are
  9337. 5:20:26iterable so we can use them within a for
  9338. 5:20:28loop
  9339. 5:20:28so let's type for i in users
  9340. 5:20:32and print i
  9341. 5:20:36and what we get is that we have a zip
  9342. 5:20:39object
  9343. 5:20:39of tuples and each tuple is storing each
  9344. 5:20:42pair of elements from our
  9345. 5:20:44two iterables now users is a
  9346. 5:20:47zip object and if you don't believe me
  9347. 5:20:49let me prove it i'm going to print
  9348. 5:20:51the type of users and this will print
  9349. 5:20:54that users is indeed a zip object
  9350. 5:20:58but you can easily convert this to a
  9351. 5:21:00different type of iterable by using a
  9352. 5:21:02cast
  9353. 5:21:03let's say that we would like to convert
  9354. 5:21:04our zip object into a list
  9355. 5:21:07so we'll surround the zip function with
  9356. 5:21:09a cast to a list
  9357. 5:21:10and now the data type of users is now a
  9358. 5:21:13list
  9359. 5:21:14what we have is a list of tuples and
  9360. 5:21:16each tuple is storing a
  9361. 5:21:18pair of elements from our two iterables
  9362. 5:21:21of usernames and passwords
  9363. 5:21:22now currently since we're passing in
  9364. 5:21:24only two different iterables we can
  9365. 5:21:26easily make this
  9366. 5:21:27a dictionary so that these are key value
  9367. 5:21:30pairs
  9368. 5:21:31so let's cast our zip object as a
  9369. 5:21:33dictionary
  9370. 5:21:34and to display all of the elements
  9371. 5:21:36within our dictionary all the key value
  9372. 5:21:38pairs
  9373. 5:21:38we're going to change our for loop to
  9374. 5:21:40this for key value
  9375. 5:21:43in users dot and we will use the
  9376. 5:21:46items method print key
  9377. 5:21:50comma value actually i think i'm going
  9378. 5:21:53to separate each of these with
  9379. 5:21:54a colon just to make it more readable
  9380. 5:21:58and now when we zip these two intervals
  9381. 5:22:00together
  9382. 5:22:01we end up with a dictionary of usernames
  9383. 5:22:04and passwords and the name of this
  9384. 5:22:06dictionary is users
  9385. 5:22:08now you're not limited to just two
  9386. 5:22:11iterables you can add
  9387. 5:22:12a third iterable or more so this time
  9388. 5:22:14let's create a
  9389. 5:22:16maybe a list a list of last login dates
  9390. 5:22:19and i'll just call this
  9391. 5:22:20login date equals and why not make a
  9392. 5:22:23list
  9393. 5:22:24and let's make up some dates let's say 1
  9394. 5:22:271
  9395. 5:22:27dash 20 21
  9396. 5:22:3212-20-21
  9397. 5:22:34and 1-3-20
  9398. 5:22:3821 okay so let's create
  9399. 5:22:42a zip object of users and we're going to
  9400. 5:22:46zip
  9401. 5:22:48user names passwords
  9402. 5:22:52and login date
  9403. 5:22:56and let's iterate over this for i
  9404. 5:23:00in users print
  9405. 5:23:03i now we have a tuple for each element
  9406. 5:23:08and instead of a pair we now
  9407. 5:23:10have a trio i guess of all of the
  9408. 5:23:13different elements
  9409. 5:23:14from each iterable so in conclusion
  9410. 5:23:17the zip function will aggregate elements
  9411. 5:23:19from two or more
  9412. 5:23:20iterables and create a zip object with
  9413. 5:23:23paired or grouped together elements
  9414. 5:23:25stored in a
  9415. 5:23:26tuple for each element within our zip
  9416. 5:23:29object
  9417. 5:23:30so that is the zip function if you would
  9418. 5:23:32like a copy of this code i will post
  9419. 5:23:34this to the comment section down below
  9420. 5:23:36and well yeah that's how the zip
  9421. 5:23:38function works in python
  9422. 5:23:41hey what's going on everybody it's you
  9423. 5:23:43bro hope you're doing well
  9424. 5:23:44and i'm going to explain the purpose of
  9425. 5:23:46if name equals main
  9426. 5:23:48in python so sit back relax
  9427. 5:23:51and enjoy the show
  9428. 5:23:54during your programming journey you may
  9429. 5:23:56have encountered this strange statement
  9430. 5:23:58of
  9431. 5:23:58if name equals main now what the heck
  9432. 5:24:01does that mean
  9433. 5:24:02so with python files also referred to as
  9434. 5:24:05modules by including this statement it
  9435. 5:24:08gives our modules some flexibility
  9436. 5:24:10one a module that has the statement can
  9437. 5:24:12be run as a standalone program
  9438. 5:24:15or two this module can be imported
  9439. 5:24:18and used by other modules if there's
  9440. 5:24:20some sort of useful functions
  9441. 5:24:22or other resources within this module to
  9442. 5:24:24be imported
  9443. 5:24:25by including the statement of if name
  9444. 5:24:27equals main we're checking to see if a
  9445. 5:24:30user
  9446. 5:24:30is running this module as either a
  9447. 5:24:32standalone program
  9448. 5:24:34or they're importing it from another
  9449. 5:24:36module behind the scenes
  9450. 5:24:38the python interpreter will set special
  9451. 5:24:40variables
  9452. 5:24:41one of which is double underscore name
  9453. 5:24:44it's a variable
  9454. 5:24:45and python will assign the name variable
  9455. 5:24:48a value
  9456. 5:24:49of main if it's the initial module being
  9457. 5:24:52run
  9458. 5:24:52and we can actually test that by
  9459. 5:24:54printing double
  9460. 5:24:56underscore name so since this is the
  9461. 5:24:59initial module being run
  9462. 5:25:01module one for my example name is going
  9463. 5:25:04to be assigned a value
  9464. 5:25:05a string of main now check this out i
  9465. 5:25:09have two modules what if i were to
  9466. 5:25:12import module 2 and check the name
  9467. 5:25:14variable of module 2.
  9468. 5:25:16let's try it import module
  9469. 5:25:20two
  9470. 5:25:23in order to access a variable from
  9471. 5:25:26another module i need to type the name
  9472. 5:25:28of the module
  9473. 5:25:29module two dot and i would like to check
  9474. 5:25:32the special name variable of module 2
  9475. 5:25:35and print it
  9476. 5:25:37so when you import a module this name
  9477. 5:25:40variable is going to be assigned
  9478. 5:25:41the name of the module in this case
  9479. 5:25:43module 2.
  9480. 5:25:44this time what if we change the roles
  9481. 5:25:46around i'm going to copy all of this
  9482. 5:25:48text
  9483. 5:25:49and paste it within module two we're
  9484. 5:25:52going to
  9485. 5:25:52import from module one we will
  9486. 5:25:56print that special name variable of
  9487. 5:25:59this module module 2 and then print the
  9488. 5:26:02name variable
  9489. 5:26:03of module 1 which is being imported now
  9490. 5:26:06when i run this program i'm going to
  9491. 5:26:08instead
  9492. 5:26:09run from module 2.
  9493. 5:26:12now this time that special name variable
  9494. 5:26:15of module 2 is main and the special name
  9495. 5:26:19variable of module 1
  9496. 5:26:21is module 1 the name of the module
  9497. 5:26:24so by including a statement such as
  9498. 5:26:27if name equals main we're checking to
  9499. 5:26:30see if this module is being run
  9500. 5:26:32directly or indirectly so let's test it
  9501. 5:26:36within this statement of if name equals
  9502. 5:26:39main
  9503. 5:26:39let's print running this
  9504. 5:26:43module directly
  9505. 5:26:47else that must mean that we're running
  9506. 5:26:49this module
  9507. 5:26:50indirectly so let's print
  9508. 5:26:54running other
  9509. 5:26:57module indirectly
  9510. 5:27:00okay so i'm going to go to module 1
  9511. 5:27:03and run from here running this module
  9512. 5:27:07directly
  9513. 5:27:08now let's do the same thing with module
  9514. 5:27:10two but we need to
  9515. 5:27:12import module
  9516. 5:27:16one and let's run for module two
  9517. 5:27:24running other module indirectly for the
  9518. 5:27:27time being i'm just going to write pass
  9519. 5:27:28within
  9520. 5:27:29our if name equals main as a placeholder
  9521. 5:27:32and get rid of our if statement
  9522. 5:27:33so let's say within module 1 there's a
  9523. 5:27:36useful function or resource we would
  9524. 5:27:38like to access
  9525. 5:27:39from module 2. so let's say we have a
  9526. 5:27:41function named hello
  9527. 5:27:43and all this will do is print the word
  9528. 5:27:46hello
  9529. 5:27:47let's just pretend that it's a useful
  9530. 5:27:49function not really but
  9531. 5:27:50let's pretend so in order to use this
  9532. 5:27:54function from module two i need to
  9533. 5:27:56import module one
  9534. 5:27:58then type module one dot and
  9535. 5:28:02then the name of the function so i'm
  9536. 5:28:04going to run
  9537. 5:28:05from module 2 and this will print
  9538. 5:28:08hello but what if i run this program
  9539. 5:28:11from module 1.
  9540. 5:28:13so right now module 1 cannot be run
  9541. 5:28:16directly as a standalone program
  9542. 5:28:18so i cannot print the word hello so what
  9543. 5:28:21i could do
  9544. 5:28:22is within if name equals main i can
  9545. 5:28:25call that hello function directly
  9546. 5:28:28so let's run from module one
  9547. 5:28:31hello one other thing that you might see
  9548. 5:28:34too within
  9549. 5:28:35the if statement if name equals main
  9550. 5:28:37there might be a call for
  9551. 5:28:39a main function and you can write the
  9552. 5:28:41main body of your program within
  9553. 5:28:43a main function so everybody in
  9554. 5:28:46conclusion
  9555. 5:28:47the reason that people may include this
  9556. 5:28:49statement if name
  9557. 5:28:50equals main is that it allows our
  9558. 5:28:52modules to have some flexibility
  9559. 5:28:54they can be run as a standalone program
  9560. 5:28:57or they can be imported and used by
  9561. 5:28:59other modules
  9562. 5:29:00and this is because the python
  9563. 5:29:02interpreter sets
  9564. 5:29:03that special variable of name with a
  9565. 5:29:06value
  9566. 5:29:07of main if it's the initial module being
  9567. 5:29:10run
  9568. 5:29:10if you would like a copy of my code and
  9569. 5:29:12my notes i will post all of this to the
  9570. 5:29:14comment section down below
  9571. 5:29:16but yeah that's the purpose of if name
  9572. 5:29:19equals main
  9573. 5:29:19in python hey what's going on everybody
  9574. 5:29:23it's bro hope you're doing well and in
  9575. 5:29:25this video i'm going to show you all a
  9576. 5:29:26few useful functions related to
  9577. 5:29:28times and dates using the time module in
  9578. 5:29:31python so
  9579. 5:29:32sit back relax and enjoy the show
  9580. 5:29:37alright everybody so the time module
  9581. 5:29:39let's begin by importing time
  9582. 5:29:41and the first thing that i'm going to
  9583. 5:29:42explain to you while is how we can find
  9584. 5:29:44our computer's epic
  9585. 5:29:46also pronounced as epoch so this is a
  9586. 5:29:49date and time
  9587. 5:29:50in which your computer thinks time began
  9588. 5:29:52think of it that way at least
  9589. 5:29:54so we use our epic as a reference point
  9590. 5:29:58so to find your computer's epic it's
  9591. 5:29:59going to vary based on your computer and
  9592. 5:30:01your operating system
  9593. 5:30:02so to find your computer's epic type
  9594. 5:30:04time dot
  9595. 5:30:06c time and as an argument we will pass
  9596. 5:30:08in zero
  9597. 5:30:10now what this method will do is that
  9598. 5:30:12this method will convert a time
  9599. 5:30:14expressed in seconds and convert it to a
  9600. 5:30:16readable string
  9601. 5:30:18so if i was to pass in 0 and print it
  9602. 5:30:20well then
  9603. 5:30:21this will display the date and time
  9604. 5:30:23which is my epic
  9605. 5:30:25our reference point so for me my epic is
  9606. 5:30:28wednesday december 31st 6 p.m
  9607. 5:30:311969 so i've just added a note
  9608. 5:30:34that the c time method will convert a
  9609. 5:30:37time expressed in seconds
  9610. 5:30:39since epic and convert it to a readable
  9611. 5:30:42string a readable
  9612. 5:30:44date and time so for practice let's pass
  9613. 5:30:47in perhaps
  9614. 5:30:471 million seconds and see what date and
  9615. 5:30:50time that we receive
  9616. 5:30:51so our c time method will return a date
  9617. 5:30:54and time
  9618. 5:30:55one million seconds past this epic here
  9619. 5:30:59so that would be for me monday january
  9620. 5:31:0212th
  9621. 5:31:02about 7 a.m 1970. our next method is
  9622. 5:31:06the time method of the time module let's
  9623. 5:31:09print
  9624. 5:31:10time dot time method and what this
  9625. 5:31:12method will do
  9626. 5:31:13is return the current seconds that have
  9627. 5:31:16passed
  9628. 5:31:16since our epic using our computer's
  9629. 5:31:19clock
  9630. 5:31:20so the number that i end up with is just
  9631. 5:31:23over
  9632. 5:31:231.6 billion and this is in seconds
  9633. 5:31:27so for me 1.6 billion seconds
  9634. 5:31:30has passed since that date which is my
  9635. 5:31:33epic
  9636. 5:31:33i believe it was december 31st 1969
  9637. 5:31:37and every time that i rerun this program
  9638. 5:31:39you can see that the amount of seconds
  9639. 5:31:41that has passed is actually increasing
  9640. 5:31:44so
  9641. 5:31:45that is the time method it will return
  9642. 5:31:47the current seconds
  9643. 5:31:49since you're epic using your computer's
  9644. 5:31:51clock and you know what
  9645. 5:31:52just for fun let's change the date and
  9646. 5:31:55time under clock and see what happens
  9647. 5:31:57so let's change the year to
  9648. 5:32:00how about the year 2000 change
  9649. 5:32:04and close so let's see what number we
  9650. 5:32:07get with the time method now
  9651. 5:32:09so we get just under 1 billion 948
  9652. 5:32:14million seconds have passed
  9653. 5:32:16and now if you need to retrieve the
  9654. 5:32:18current date and time
  9655. 5:32:19well there's one of a few ways in which
  9656. 5:32:21you could retrieve that but one way is
  9657. 5:32:23that we can combine
  9658. 5:32:24both of these methods of the see time
  9659. 5:32:27method and the time method of the time
  9660. 5:32:29module
  9661. 5:32:30so let's print time dot c
  9662. 5:32:33time and we're going to pass in an
  9663. 5:32:35amount of seconds
  9664. 5:32:36into the c time method as an argument so
  9665. 5:32:39within
  9666. 5:32:40the c time method we're going to call
  9667. 5:32:42the time method
  9668. 5:32:44so the time method will return an amount
  9669. 5:32:46of seconds
  9670. 5:32:47since our epic and the c time method
  9671. 5:32:49will convert
  9672. 5:32:50that amount of seconds to a readable
  9673. 5:32:52date and time
  9674. 5:32:53so the current date and time is saturday
  9675. 5:32:57january 23rd about 3 p.m 20 21
  9676. 5:33:00now there is more than one way to get
  9677. 5:33:02the current date and time
  9678. 5:33:03another way is to use the local time
  9679. 5:33:06method and the local time method will
  9680. 5:33:08create a
  9681. 5:33:09time object based on the current time so
  9682. 5:33:12what i'm going to do is
  9683. 5:33:14create a variable called time object
  9684. 5:33:17and i just need to explain a few things
  9685. 5:33:19about time objects
  9686. 5:33:20so to best explain this i'm going to
  9687. 5:33:22print our time
  9688. 5:33:23object now a time object is also
  9689. 5:33:26referred to as a
  9690. 5:33:27struct time object it is made up of
  9691. 5:33:30different keyword arguments
  9692. 5:33:32there's a year a month day
  9693. 5:33:35hour minutes seconds day of the week
  9694. 5:33:39day of the year and this keyword
  9695. 5:33:41argument here has something to do with
  9696. 5:33:42daylight savings time
  9697. 5:33:44so there's quite a few uses with time
  9698. 5:33:46objects
  9699. 5:33:47and one way is that we can format them
  9700. 5:33:50however we want because right now
  9701. 5:33:52this time object is not in a readable
  9702. 5:33:54format
  9703. 5:33:55so to convert this time object into a
  9704. 5:33:58readable string we'll need the help of a
  9705. 5:34:00separate function
  9706. 5:34:01and that is the strf time function
  9707. 5:34:05str is short for string f4 format and
  9708. 5:34:08time
  9709. 5:34:09well for time i guess so this function
  9710. 5:34:12needs two arguments a format and a time
  9711. 5:34:15object so our strf time function will
  9712. 5:34:18accept
  9713. 5:34:19a format and a time object as an
  9714. 5:34:22argument
  9715. 5:34:23so our format is really just a string
  9716. 5:34:26of different directives and to best
  9717. 5:34:28explain these i'm going to head to
  9718. 5:34:30python's official documentation on the
  9719. 5:34:32subject
  9720. 5:34:33so here i am on python's website
  9721. 5:34:35regarding the time module and underneath
  9722. 5:34:38this section on the strf time function
  9723. 5:34:41there are different directives that we
  9724. 5:34:43can embed within our format string that
  9725. 5:34:45we pass in as an argument
  9726. 5:34:47depending on the directive that we add
  9727. 5:34:49we can display
  9728. 5:34:50a certain format of our date and time
  9729. 5:34:54so for example if i was to pass in let's
  9730. 5:34:57say
  9731. 5:34:57percent lowercase a then we will display
  9732. 5:35:01the time object's weekday name and
  9733. 5:35:04you're not limited to just one directive
  9734. 5:35:05you can add any combination of
  9735. 5:35:07directives
  9736. 5:35:08so if i was to add percent m well we
  9737. 5:35:11would display
  9738. 5:35:12the month of our date time object as a
  9739. 5:35:14number
  9740. 5:35:151 through 12. so there's a bunch of
  9741. 5:35:17directives here and i'm going to be
  9742. 5:35:18using some of these
  9743. 5:35:20so within a string for the format
  9744. 5:35:22argument i'm going to pass in
  9745. 5:35:24percent capital b for the name of the
  9746. 5:35:27month
  9747. 5:35:28percent d for the day
  9748. 5:35:31percent y for the year
  9749. 5:35:35percent h for the hour and to format
  9750. 5:35:38this i'm going to add a colon
  9751. 5:35:40to separate hours and minutes percent
  9752. 5:35:43m for minutes colon percent s
  9753. 5:35:48for seconds and then we are going to
  9754. 5:35:51assign
  9755. 5:35:51all of this to a variable let's say
  9756. 5:35:54local
  9757. 5:35:55time and local time will be a string
  9758. 5:35:58so let's print our local time
  9759. 5:36:03and the current time is january
  9760. 5:36:0623rd 2021 about 3 p.m
  9761. 5:36:10oh and i almost forgot you can also get
  9762. 5:36:12the utc
  9763. 5:36:13time that is the coordinated universal
  9764. 5:36:16time if you know how that works
  9765. 5:36:18so if you need that you would just use
  9766. 5:36:20the gm
  9767. 5:36:21time method for the utc time coordinated
  9768. 5:36:25universal time
  9769. 5:36:26okay next up we have the strp
  9770. 5:36:29time function and this function will
  9771. 5:36:32parse
  9772. 5:36:32a string representation of a time and or
  9773. 5:36:36date
  9774. 5:36:36and return a time object so we need to
  9775. 5:36:39pass in
  9776. 5:36:39a string representing the date and or
  9777. 5:36:42time as well as a format
  9778. 5:36:44string so let's create a time string and
  9779. 5:36:47this variable is going to be a string
  9780. 5:36:49representation of
  9781. 5:36:50a date let's say 20th of april 2020
  9782. 5:36:54and what we can do is take this string
  9783. 5:36:56representation of a time and or date
  9784. 5:36:58and parse it to a time object so we're
  9785. 5:37:01going to pass in our time
  9786. 5:37:02string variable as well as a format
  9787. 5:37:05string
  9788. 5:37:06so let's say i would like to parse
  9789. 5:37:10the day so that would be percent d for
  9790. 5:37:12day
  9791. 5:37:13then percent b for name of the month
  9792. 5:37:17and then comma percent y for year
  9793. 5:37:21this function will create a time object
  9794. 5:37:23so let's assign that to a variable
  9795. 5:37:26time object equals time dot strp time
  9796. 5:37:29we're passing in our string
  9797. 5:37:31representation of a time
  9798. 5:37:32or date as well as a format string
  9799. 5:37:35and we can print our time object using a
  9800. 5:37:38print statement
  9801. 5:37:40however this is going to be in a form
  9802. 5:37:41that is somewhat difficult to read but
  9803. 5:37:43you can see at least we have a time
  9804. 5:37:45object
  9805. 5:37:46with all of these keywords filled in
  9806. 5:37:48with anything that we passed in
  9807. 5:37:49via these format directives that we have
  9808. 5:37:52now the next function
  9809. 5:37:53is the asc time function
  9810. 5:37:56and this function accepts a time object
  9811. 5:37:59or a tuple representation
  9812. 5:38:01of a relative time so this time let's
  9813. 5:38:04create a
  9814. 5:38:04time tuple and we're going to follow
  9815. 5:38:08this order we can pass in
  9816. 5:38:09up to nine values the first value is a
  9817. 5:38:12year so let's pass in perhaps
  9818. 5:38:142020 a month let's say four
  9819. 5:38:17a day how about 20 four hours
  9820. 5:38:20let's say four minutes 20 and seconds
  9821. 5:38:24maybe zero let me just fix some of the
  9822. 5:38:26spacing here
  9823. 5:38:27okay you can also pass in a numbered day
  9824. 5:38:30of the week
  9825. 5:38:31um i'm just going to say zero i don't
  9826. 5:38:33think it's really that important
  9827. 5:38:35a day of the year zero and
  9828. 5:38:38negative one or zero for daylight
  9829. 5:38:39savings time so
  9830. 5:38:41we created a time tuple and we can pass
  9831. 5:38:44in a time
  9832. 5:38:44object or a tuple representation of a
  9833. 5:38:47time
  9834. 5:38:48following this formula so let's pass in
  9835. 5:38:51our time tuple
  9836. 5:38:52and this will create a time string a
  9837. 5:38:55string representation of the time that
  9838. 5:38:57we create
  9839. 5:38:58and let's print our time string
  9840. 5:39:02and we should have april 20th
  9841. 5:39:05about four in the morning the year 2020.
  9842. 5:39:08so that is asc time it will convert a
  9843. 5:39:11tuple representation of a time and date
  9844. 5:39:15or a time object and convert it to a
  9845. 5:39:17readable
  9846. 5:39:18string now another option is to use m
  9847. 5:39:21k time and mk time will take a
  9848. 5:39:24tuple representation of a time or a time
  9849. 5:39:28object
  9850. 5:39:28and convert it to seconds since epic
  9851. 5:39:32so april 20th of the year 2020
  9852. 5:39:35is about 1.5 billion seconds
  9853. 5:39:38since our epic date and for me that was
  9854. 5:39:40december 31st the year 1969
  9855. 5:39:44so that is a few useful functions of the
  9856. 5:39:47time module
  9857. 5:39:49if you would like a copy of this code i
  9858. 5:39:50will post all this to the comment
  9859. 5:39:52section down below
  9860. 5:39:53and well yeah that's the time module in
  9861. 5:39:56python
  9862. 5:39:58yo what's going on everybody it's bro
  9863. 5:40:00hope you're doing well
  9864. 5:40:01and in this video i'm going to explain
  9865. 5:40:03multi-threading in python so
  9866. 5:40:04sit back relax and enjoy the show
  9867. 5:40:09think of a thread as a flow of execution
  9868. 5:40:12like a river
  9869. 5:40:13and each thread can carry out its own
  9870. 5:40:15separate order of instructions
  9871. 5:40:17if we use this process of
  9872. 5:40:18multi-threading we can have our program
  9873. 5:40:21run
  9874. 5:40:21different parts of its program at
  9875. 5:40:23different times
  9876. 5:40:24they all run concurrently but not truly
  9877. 5:40:27in parallel that is a concept for later
  9878. 5:40:29referred to as multi-processing
  9879. 5:40:31so with reds they each take a turn
  9880. 5:40:34running to achieve concurrency
  9881. 5:40:36this is due to a notorious feature known
  9882. 5:40:38as the gil
  9883. 5:40:40the global interpreter lock only one
  9884. 5:40:43thread can be running at one time
  9885. 5:40:45but they can all take turns when one
  9886. 5:40:47thread is idle
  9887. 5:40:48so this allows one thread to hold
  9888. 5:40:50control of the python interpreter
  9889. 5:40:53at any one time so they run concurrently
  9890. 5:40:55but not truly in parallel
  9891. 5:40:57which is what we do with
  9892. 5:40:59multi-processing now programs and tasks
  9893. 5:41:01can be divided into
  9894. 5:41:02two different categories they can be cpu
  9895. 5:41:05bound that is a program or a task
  9896. 5:41:08that spends most of its time waiting for
  9897. 5:41:10internal events
  9898. 5:41:11such as a task that is cpu intensive
  9899. 5:41:14it is better to use multi-processing for
  9900. 5:41:17tasks that are cpu bound
  9901. 5:41:19now tasks that are io bound means that
  9902. 5:41:22the program will spend most of its time
  9903. 5:41:24waiting for external events
  9904. 5:41:26such as waiting for user input or if
  9905. 5:41:29you're doing activities like web
  9906. 5:41:30scraping
  9907. 5:41:31you do a lot of sitting around so with i
  9908. 5:41:33o bound tasks it's better to use
  9909. 5:41:35multi-threading
  9910. 5:41:36because we can have multiple threads
  9911. 5:41:38running concurrently
  9912. 5:41:40but not truly in parallel like what we
  9913. 5:41:42do with multi-processing
  9914. 5:41:43after importing the threading module we
  9915. 5:41:46can count the number of threads that are
  9916. 5:41:48currently running
  9917. 5:41:48in the background whenever we run a
  9918. 5:41:50program we have one thread that is
  9919. 5:41:52running that is in charge of
  9920. 5:41:54executing our program and we can print
  9921. 5:41:56the active count of threads running in
  9922. 5:41:58our program
  9923. 5:41:59using the active count function of the
  9924. 5:42:01threading module
  9925. 5:42:03so this will print one we have one
  9926. 5:42:05thread that is running
  9927. 5:42:06and we can print a list of all the
  9928. 5:42:09threads that are running
  9929. 5:42:11by using the enumerate function so
  9930. 5:42:14the one thread that is in charge of
  9931. 5:42:16running our program
  9932. 5:42:17is referred to as the main thread by
  9933. 5:42:20using this concept of multi-threading we
  9934. 5:42:22can have
  9935. 5:42:22more than one thread running
  9936. 5:42:24concurrently not truly in parallel
  9937. 5:42:27all the threads will take turns while
  9938. 5:42:28one of them is idle
  9939. 5:42:30so we can have more than one thread
  9940. 5:42:32running more than just the main
  9941. 5:42:34thread which is in charge of running the
  9942. 5:42:35main body of our program
  9943. 5:42:37so while our main thread is in charge of
  9944. 5:42:39running the main body of our program
  9945. 5:42:41we can have another thread that's in
  9946. 5:42:43charge of a separate part of it
  9947. 5:42:45maybe like a countdown timer or
  9948. 5:42:47something so
  9949. 5:42:48one good example take that quiz game
  9950. 5:42:50that we made some number of videos ago
  9951. 5:42:52while we were waiting for user input
  9952. 5:42:55which is a
  9953. 5:42:56i o bound task we could have had a
  9954. 5:42:58countdown timer going
  9955. 5:42:59like you only have so many seconds to
  9956. 5:43:01answer this question we could have had
  9957. 5:43:03one thread in charge of waiting for user
  9958. 5:43:05input and another thread
  9959. 5:43:06in charge of the countdown timer so
  9960. 5:43:09that's an example of multi-threading
  9961. 5:43:11we had two threads running concurrently
  9962. 5:43:14and what we'll be doing in this video is
  9963. 5:43:15creating a program that involves
  9964. 5:43:17multi-threading we can have
  9965. 5:43:19different threads in charge of different
  9966. 5:43:21parts of our program and they can all
  9967. 5:43:22run
  9968. 5:43:23concurrently they'll all take turns
  9969. 5:43:25while one of them is idle
  9970. 5:43:26so let's say that we're running late for
  9971. 5:43:29school
  9972. 5:43:30or work in the morning and we have three
  9973. 5:43:32different tasks that we need to complete
  9974. 5:43:34before we can leave
  9975. 5:43:35for school or work so think of three
  9976. 5:43:38different
  9977. 5:43:38things you do in the morning such as
  9978. 5:43:40maybe eat
  9979. 5:43:41breakfast some people drink coffee or
  9980. 5:43:44maybe a beverage of your choice so drink
  9981. 5:43:48coffee and some people like me they like
  9982. 5:43:51to
  9983. 5:43:51do their homework last minute so i'll
  9984. 5:43:53say study
  9985. 5:43:54before i leave for work or school in the
  9986. 5:43:57morning
  9987. 5:43:58so what we'll do in each of these
  9988. 5:44:00functions each of these functions
  9989. 5:44:01should take some amount of time to
  9990. 5:44:03complete so we can have our main thread
  9991. 5:44:06sleep for a given number of seconds
  9992. 5:44:07using the sleep function
  9993. 5:44:10time dot sleep and let's say that in
  9994. 5:44:12order for me to eat breakfast
  9995. 5:44:14this task will take me three seconds
  9996. 5:44:16let's just pretend that instead of
  9997. 5:44:18minutes this will be
  9998. 5:44:19in seconds so drink coffee will take me
  9999. 5:44:23four seconds and study will take me
  10000. 5:44:26five seconds then when we finish
  10001. 5:44:30sleeping
  10002. 5:44:31let's print a confirmation message let's
  10003. 5:44:34say
  10004. 5:44:35you eat breakfast
  10005. 5:44:38as in you finish eating breakfast with
  10006. 5:44:41drink coffee
  10007. 5:44:43you drink coffee
  10008. 5:44:48and with study
  10009. 5:44:51you finish studying
  10010. 5:44:56now each of these tasks are i o bound
  10011. 5:44:58they're going to be spending a lot of
  10012. 5:45:00time just waiting around
  10013. 5:45:01for external events they're waiting for
  10014. 5:45:03the sleep function to expire before they
  10015. 5:45:06can finish their task
  10016. 5:45:07so we're going to have all of these
  10017. 5:45:09three functions run on our main thread
  10018. 5:45:12and we'll see how long it takes for us
  10019. 5:45:13to complete our morning ritual
  10020. 5:45:15these three tasks so let's call these
  10021. 5:45:18three functions within
  10022. 5:45:19our main thread so let's call the
  10023. 5:45:22eat breakfast function first followed by
  10024. 5:45:26drink coffee and then study
  10025. 5:45:30in that order so this program is going
  10026. 5:45:33to take approximately
  10027. 5:45:35let's see 12 seconds to complete so
  10028. 5:45:38there's going to be a pause for a second
  10029. 5:45:40you eat breakfast then followed by
  10030. 5:45:44you drank coffee and then study
  10031. 5:45:50you finished studying so this program
  10032. 5:45:52took about
  10033. 5:45:5312 seconds overall so if this were
  10034. 5:45:56realistic
  10035. 5:45:57what we would have done is we would sit
  10036. 5:45:59down and eat breakfast for
  10037. 5:46:01three minutes well three seconds in this
  10038. 5:46:03case and then
  10039. 5:46:04once we finish eating breakfast only
  10040. 5:46:06then are we allowed to drink our coffee
  10041. 5:46:09and once we finish our coffee only then
  10042. 5:46:12can we study
  10043. 5:46:13so we completed these tasks sequentially
  10044. 5:46:16and not concurrently for us to move down
  10045. 5:46:19to the next
  10046. 5:46:19function we need to complete the
  10047. 5:46:21previous functions because we're doing
  10048. 5:46:23this in order
  10049. 5:46:24but realistically us human beings we
  10050. 5:46:27would probably
  10051. 5:46:28eat breakfast drink coffee and study
  10052. 5:46:30altogether because
  10053. 5:46:32we can multitask and we can complete
  10054. 5:46:34these three functions in less time
  10055. 5:46:36and that's kind of the same process as
  10056. 5:46:38multi-threading we can have
  10057. 5:46:40these three separate functions running
  10058. 5:46:42concurrently
  10059. 5:46:43as if we're multitasking we're eating
  10060. 5:46:45breakfast drinking coffee
  10061. 5:46:46and studying all at once now currently
  10062. 5:46:49we have
  10063. 5:46:50one thread that is in charge of these
  10064. 5:46:52three separate functions
  10065. 5:46:54what we could do is that we can create
  10066. 5:46:56three additional threads
  10067. 5:46:57each thread will be in charge of each
  10068. 5:47:00task and then we'll have our main thread
  10069. 5:47:02running in the background that will
  10070. 5:47:03complete the rest of the program
  10071. 5:47:05so this is how to create an additional
  10072. 5:47:06thread let's say that
  10073. 5:47:08x equals threading
  10074. 5:47:12dot thread we need to pass in a target
  10075. 5:47:15target equals and then the name of the
  10076. 5:47:20function so let's say
  10077. 5:47:21thread x will be in charge of eating
  10078. 5:47:23breakfast
  10079. 5:47:24and then you can pass in arguments too
  10080. 5:47:26if your function has parameters by
  10081. 5:47:28typing args
  10082. 5:47:29and then you pass in a tuple so let's
  10083. 5:47:31say you have one argument to pass in
  10084. 5:47:33you'll type your argument followed by
  10085. 5:47:35a comma but we don't have any arguments
  10086. 5:47:37in this example but
  10087. 5:47:38you'll need to be sure to enter them in
  10088. 5:47:40if you do have any
  10089. 5:47:42okay then to begin this thread you type
  10090. 5:47:44the name of the thread
  10091. 5:47:45in this case x dot start so
  10092. 5:47:48we now have an additional thread and
  10093. 5:47:50this thread is in charge of eating
  10094. 5:47:52breakfast
  10095. 5:47:53now let's create another thread to drink
  10096. 5:47:55coffee
  10097. 5:47:57and we'll call this y
  10098. 5:48:01and the target will be drink coffee
  10099. 5:48:05and lastly we have a thread in charge of
  10100. 5:48:08studying
  10101. 5:48:10and this will be z
  10102. 5:48:14and the target is our study function
  10103. 5:48:17okay now let's see how long it takes for
  10104. 5:48:19us to complete our program
  10105. 5:48:21oh and be sure to comment out these
  10106. 5:48:23function calls within the main thread
  10107. 5:48:25because we don't want the main thread in
  10108. 5:48:26charge of those anymore
  10109. 5:48:27okay now we can run the program so we
  10110. 5:48:30have four threads running this time
  10111. 5:48:32you eat breakfast you drink coffee you
  10112. 5:48:34finish studying
  10113. 5:48:35so this program took approximately five
  10114. 5:48:38seconds to finish
  10115. 5:48:39and the reason that this program took
  10116. 5:48:41five seconds instead of 12
  10117. 5:48:43is because before our main thread was in
  10118. 5:48:45charge of running
  10119. 5:48:46these tasks sequentially in order but
  10120. 5:48:49now since we have a thread
  10121. 5:48:50dedicated to each task we can run them
  10122. 5:48:53all
  10123. 5:48:54concurrently instead of sequentially so
  10124. 5:48:56that this program
  10125. 5:48:57now took about five seconds to complete
  10126. 5:48:59and you may have noticed too
  10127. 5:49:01that the activecount function as well as
  10128. 5:49:03the enumerate function
  10129. 5:49:04were called before threads 1 2 and 3
  10130. 5:49:07finished their respective tasks
  10131. 5:49:09that's because the main thread is not
  10132. 5:49:11going to wait around for
  10133. 5:49:12these three threads to complete it has
  10134. 5:49:14its own set of instructions to do
  10135. 5:49:16so it is no longer in charge of these
  10136. 5:49:19three functions
  10137. 5:49:21the program is going to handle those
  10138. 5:49:23three functions to our three threads
  10139. 5:49:25and our main thread is going to continue
  10140. 5:49:27its own set of instructions
  10141. 5:49:28its job is to print the active count as
  10142. 5:49:31well as
  10143. 5:49:32call the enumerate function which it did
  10144. 5:49:35and it finished its tasks
  10145. 5:49:36before threads one two and three now one
  10146. 5:49:39trick that you can do too
  10147. 5:49:40is that you can use the time modules
  10148. 5:49:43performance
  10149. 5:49:43counter function and this function will
  10150. 5:49:46return
  10151. 5:49:47how long it takes our calling thread as
  10152. 5:49:49in our main thread
  10153. 5:49:50to finish its set of instructions so our
  10154. 5:49:53main thread is not in charge of
  10155. 5:49:55executing
  10156. 5:49:56these three functions our main thread is
  10157. 5:49:59in charge of creating
  10158. 5:50:00three additional threads and then
  10159. 5:50:02calling the activecount function
  10160. 5:50:04and the enumerate function as well as
  10161. 5:50:06the performance counter function
  10162. 5:50:09so our main thread will take
  10163. 5:50:11approximately
  10164. 5:50:130.075 seconds to complete
  10165. 5:50:15and our three threads are still running
  10166. 5:50:17in the background our main threads job
  10167. 5:50:19its order of instructions is to create
  10168. 5:50:22three additional threads and then
  10169. 5:50:24immediately
  10170. 5:50:25print whatever is returned via the
  10171. 5:50:28activecount function
  10172. 5:50:29enumerate and then your performance
  10173. 5:50:31counter so
  10174. 5:50:33our main thread says that it finished
  10175. 5:50:35its tasks in about
  10176. 5:50:36.07 seconds but threads one
  10177. 5:50:40through three were still trying to catch
  10178. 5:50:42up that's why the main thread
  10179. 5:50:44finished before are three additional
  10180. 5:50:45threads and once
  10181. 5:50:47all active threads have completed their
  10182. 5:50:49tasks
  10183. 5:50:50then your program will finish and exit
  10184. 5:50:52there's also this concept called
  10185. 5:50:54thread synchronization we can have a
  10186. 5:50:56calling thread in this case our main
  10187. 5:50:58thread
  10188. 5:50:59wait around for another thread to finish
  10189. 5:51:01before it can move on
  10190. 5:51:02with its own instruction set so let's
  10191. 5:51:05say we would like
  10192. 5:51:06our main thread to wait around for
  10193. 5:51:08thread 1 also known as
  10194. 5:51:10x so we're going to use the join
  10195. 5:51:13function
  10196. 5:51:14of thread x and now our main thread
  10197. 5:51:17has to wait around for thread x also
  10198. 5:51:19known as
  10199. 5:51:20thread 1 to finish before it can move on
  10200. 5:51:22with its instruction set
  10201. 5:51:24so let's do the same with y and
  10202. 5:51:27z so now our main thread before it can
  10203. 5:51:30move on with the rest of the program has
  10204. 5:51:32to wait for all of these threads
  10205. 5:51:33to synchronize and join and then and
  10206. 5:51:36only then
  10207. 5:51:37can it move on with the rest of its own
  10208. 5:51:39instruction set
  10209. 5:51:40so this time our program is going to
  10210. 5:51:42look a little something like this
  10211. 5:51:44you eat breakfast you drink coffee you
  10212. 5:51:46finish studying
  10213. 5:51:47so by the time we reach our active count
  10214. 5:51:50these threads
  10215. 5:51:50are already joined and synchronized
  10216. 5:51:52they're no longer active
  10217. 5:51:54when we enumerate over our active
  10218. 5:51:56threads we only have
  10219. 5:51:57our main thread and our main thread
  10220. 5:51:59completed in
  10221. 5:52:00about 5.1 seconds this time because it
  10222. 5:52:03did a lot of waiting around
  10223. 5:52:04it was waiting for threads x y and z to
  10224. 5:52:08join and finish in conclusion a thread
  10225. 5:52:10is
  10226. 5:52:11a flow of execution like a separate
  10227. 5:52:14order of instructions that a program can
  10228. 5:52:16follow
  10229. 5:52:16and when we run a program we always have
  10230. 5:52:19at least one
  10231. 5:52:20thread running initially and that is
  10232. 5:52:22referred to as the main thread
  10233. 5:52:24however if you have multiple threads
  10234. 5:52:26using the concept of multi-threading
  10235. 5:52:28we can have multiple threads running
  10236. 5:52:30concurrently but not truly in parallel
  10237. 5:52:33which is what we'll cover
  10238. 5:52:34during the video on multi-processing
  10239. 5:52:36this is due
  10240. 5:52:37to a feature known as the gil
  10241. 5:52:40the global interpreter lock which allows
  10242. 5:52:43only
  10243. 5:52:43one thread to hold control of the python
  10244. 5:52:46interpreter
  10245. 5:52:47at any one time and tasks can be either
  10246. 5:52:50cpu bound or io bound a cpu bound task
  10247. 5:52:53is a program or task
  10248. 5:52:55that spends most of its time waiting for
  10249. 5:52:58internal events
  10250. 5:52:59such as a task that is cpu intensive
  10251. 5:53:02and o bound tasks spend most of their
  10252. 5:53:05time waiting for
  10253. 5:53:06external events such as waiting around
  10254. 5:53:09for user input
  10255. 5:53:10or web scraping with i o bound tasks
  10256. 5:53:13it's better to use multi-threading
  10257. 5:53:15with cpu bound tasks it's better to use
  10258. 5:53:18multi-processing
  10259. 5:53:19well everybody that is a quick overview
  10260. 5:53:21of multi-threading i will post all of
  10261. 5:53:23this code to the comment section down
  10262. 5:53:25below
  10263. 5:53:26and well yeah that's how to achieve
  10264. 5:53:28multi-threading
  10265. 5:53:29using python hey what's going on
  10266. 5:53:32everybody
  10267. 5:53:33it's you bro hope you're doing well and
  10268. 5:53:35in this video i'm going to explain demon
  10269. 5:53:36threads in python so
  10270. 5:53:38sit back relax and enjoy the show
  10271. 5:53:43okay so demon threads also pronounced as
  10272. 5:53:46daemon threads a demon thread is a
  10273. 5:53:49thread that runs in the background and
  10274. 5:53:51they are normally not important for your
  10275. 5:53:52program in order to run
  10276. 5:53:54your program will not wait for demon
  10277. 5:53:56threads to complete before
  10278. 5:53:57exiting whereas non-demon threads they
  10279. 5:54:00cannot normally be killed
  10280. 5:54:02they will stay alive until their task is
  10281. 5:54:04complete so
  10282. 5:54:05a few common uses of demon threads would
  10283. 5:54:08be
  10284. 5:54:08background tasks garbage collection
  10285. 5:54:11waiting for input
  10286. 5:54:12or other long-running processes so
  10287. 5:54:15here's an example of why demon threads
  10288. 5:54:17would be useful
  10289. 5:54:18let's say that we'll have two threads
  10290. 5:54:20our main thread will be in charge of
  10291. 5:54:22waiting around for some user input we'll
  10292. 5:54:24ask
  10293. 5:54:24do you wish to exit and in the
  10294. 5:54:26background we'll have a timer going that
  10295. 5:54:28will display
  10296. 5:54:29how long somebody is logged in so let's
  10297. 5:54:32create
  10298. 5:54:32a function named timer
  10299. 5:54:36and what we'll do is just display how
  10300. 5:54:38long somebody's logged in
  10301. 5:54:40so let's say we have a count variable
  10302. 5:54:43while true we'll use
  10303. 5:54:46the sleep function of the time module to
  10304. 5:54:49sleep for approximately one second
  10305. 5:54:51then we'll increment our count variable
  10306. 5:54:53by one and we'll print a message
  10307. 5:54:56so let's say logged in
  10308. 5:54:59for
  10309. 5:55:03count
  10310. 5:55:06seconds and let's create a thread that
  10311. 5:55:09will be in charge of this timer and run
  10312. 5:55:11it in the background so let's say
  10313. 5:55:13x equals threading
  10314. 5:55:16dot thread the target will be our
  10315. 5:55:19function
  10316. 5:55:19of timer and we need to start this
  10317. 5:55:22thread
  10318. 5:55:23x dot start so our thread
  10319. 5:55:26x will be in charge of our timer and run
  10320. 5:55:29it concurrently
  10321. 5:55:30while we're waiting for some user input
  10322. 5:55:32our main thread will be in charge of
  10323. 5:55:34waiting for this user input do you wish
  10324. 5:55:36to exit now here's the issue
  10325. 5:55:38of us using a non-demon thread for a
  10326. 5:55:40background task such as a countdown
  10327. 5:55:42timer
  10328. 5:55:43so our main thread is in charge of
  10329. 5:55:45waiting around for some user input
  10330. 5:55:46and thread x our additional thread is in
  10331. 5:55:49charge of
  10332. 5:55:50running this countdown timer so if i
  10333. 5:55:52wish to exit this program i'm just going
  10334. 5:55:54to type
  10335. 5:55:55okay and hit enter well my background
  10336. 5:55:57timer is still going but my main thread
  10337. 5:56:00is complete so i would like to exit this
  10338. 5:56:02program but
  10339. 5:56:03my program will not exit as long as
  10340. 5:56:06there are non-demon threads that are
  10341. 5:56:08still alive
  10342. 5:56:09we cannot normally close this program
  10343. 5:56:11unless we do like a brute force
  10344. 5:56:13you know close and exit of the program
  10345. 5:56:15so we can turn
  10346. 5:56:17this thread of x into a demon thread
  10347. 5:56:20so that when we wish to exit the program
  10348. 5:56:22when there are no more
  10349. 5:56:24non-demon threads alive and to change a
  10350. 5:56:26thread to a demon thread it's actually
  10351. 5:56:28fairly easy when you create your thread
  10352. 5:56:30you can set
  10353. 5:56:31this flag of demon equal to true and
  10354. 5:56:33that's it
  10355. 5:56:34so when all non-demon threads have
  10356. 5:56:37finished their tasks
  10357. 5:56:38your program will complete and exit but
  10358. 5:56:41if there are demon tasks running in the
  10359. 5:56:43background
  10360. 5:56:43they will be killed automatically so
  10361. 5:56:45let's try this program again
  10362. 5:56:47so do you wish to exit my program is
  10363. 5:56:49going to continue running
  10364. 5:56:50my demon thread is in charge of the
  10365. 5:56:52timer and it's going to continue running
  10366. 5:56:53until
  10367. 5:56:54all non-daemon threads have completed
  10368. 5:56:57their tasks
  10369. 5:56:58so once i type ok my program will
  10370. 5:57:02finish running and my countdown timer
  10371. 5:57:04will stop because
  10372. 5:57:05all demons are killed when your program
  10373. 5:57:07is finished running there's also two
  10374. 5:57:09additional methods that you might be
  10375. 5:57:11interested in
  10376. 5:57:11you can use the set daemon method
  10377. 5:57:14of a thread and you can set it to false
  10378. 5:57:16or true if you would like to change
  10379. 5:57:18a thread to a non-demon or a demon
  10380. 5:57:21however if your thread is currently
  10381. 5:57:23running well you cannot actually change
  10382. 5:57:25it from
  10383. 5:57:26a non-demon thread to a demon thread
  10384. 5:57:28while it's currently running
  10385. 5:57:30so you would have to do that before you
  10386. 5:57:31actually use the start function
  10387. 5:57:34and you can also check to see if a
  10388. 5:57:36thread is a demon or not
  10389. 5:57:37by using the is demon method this will
  10390. 5:57:40return
  10391. 5:57:41true or false so everybody in conclusion
  10392. 5:57:44a demon thread is a thread that runs in
  10393. 5:57:46the background
  10394. 5:57:47and they're not important for your
  10395. 5:57:49program in order to continue running
  10396. 5:57:51your program will not wait for demon
  10397. 5:57:53threads to complete before exiting the
  10398. 5:57:55program
  10399. 5:57:56whereas non-demon threads cannot
  10400. 5:57:58normally be killed
  10401. 5:57:59they will stay alive until their task is
  10402. 5:58:01complete and demon threads are commonly
  10403. 5:58:04used for
  10404. 5:58:04background tasks garbage collection
  10405. 5:58:07waiting around for input
  10406. 5:58:08or other long running processes so if
  10407. 5:58:11you would like a copy of this code i
  10408. 5:58:13will post all of this to the comment
  10409. 5:58:14section down below
  10410. 5:58:15but yeah those are demon threads in
  10411. 5:58:18python
  10412. 5:58:19yo what's going on everybody it's bro
  10413. 5:58:22hope you're doing well
  10414. 5:58:23and in this video i'm going to explain
  10415. 5:58:24multi-processing in python so
  10416. 5:58:26sit back relax and enjoy the show
  10417. 5:58:31what's up let's talk about
  10418. 5:58:33multi-processing multi-processing
  10419. 5:58:35is the act of running tasks in parallel
  10420. 5:58:38on a different cpu cores it's different
  10421. 5:58:41from multi-threading
  10422. 5:58:42although it sounds similar because with
  10423. 5:58:44multi-threading
  10424. 5:58:45we're limited to running one thread at a
  10425. 5:58:48time
  10426. 5:58:48because of the gil that lock which is
  10427. 5:58:51used for threading we can run threads
  10428. 5:58:53concurrently
  10429. 5:58:54but not in parallel however with
  10430. 5:58:57multi-processing
  10431. 5:58:58we can create processes and we can run
  10432. 5:59:01each process
  10433. 5:59:02in parallel on a different cpu course so
  10434. 5:59:05with multi-processing
  10435. 5:59:06it's better for tasks that are cpu bound
  10436. 5:59:09where you need heavy cpu usage whereas
  10437. 5:59:13multi-threading that is better for i o
  10438. 5:59:15bound tasks where you do a lot of
  10439. 5:59:17waiting around
  10440. 5:59:18so before we begin i recommend these two
  10441. 5:59:20imports
  10442. 5:59:21multi-processing and time let's begin
  10443. 5:59:24quick note if you're running the windows
  10444. 5:59:26operating system you'll probably need to
  10445. 5:59:28add
  10446. 5:59:28this line if name is equal to
  10447. 5:59:31main so when we run a program we have a
  10448. 5:59:34main process that is running and if we
  10449. 5:59:37create a child process
  10450. 5:59:38from that process it's going to copy the
  10451. 5:59:41module that we're currently working with
  10452. 5:59:43and that child process will create its
  10453. 5:59:46own children processes and it's going to
  10454. 5:59:48be a problem
  10455. 5:59:49so we're going to add this line if name
  10456. 5:59:51is equal to main
  10457. 5:59:52so when we create a child process it
  10458. 5:59:54will copy our module but it's not going
  10459. 5:59:56to execute it
  10460. 5:59:58so let's create a main function and a
  10461. 6:00:00majority of our code is going to be
  10462. 6:00:02within our main function
  10463. 6:00:04if you're running a different operating
  10464. 6:00:05system you probably don't have to do
  10465. 6:00:07this but if you're running windows
  10466. 6:00:08you probably will now with
  10467. 6:00:10multi-processing multi-processing is
  10468. 6:00:12better for tasks or functions
  10469. 6:00:14that are cpu bound where they require
  10470. 6:00:17heavy cpu usage
  10471. 6:00:19let's say that we have a function named
  10472. 6:00:21counter
  10473. 6:00:22and we'll pass in a number to count up
  10474. 6:00:24to but the number we're going to pass in
  10475. 6:00:26is a ridiculously large number like a
  10476. 6:00:28billion so let's create a function that
  10477. 6:00:30will count
  10478. 6:00:31from zero so count equals zero and
  10479. 6:00:35while count is less than
  10480. 6:00:38our number that we pass in we will
  10481. 6:00:40increment our counter
  10482. 6:00:42by one so that is the function that
  10483. 6:00:45we'll call with
  10484. 6:00:46our processes that we create now within
  10485. 6:00:48our main function
  10486. 6:00:49we'll create a process and to do that
  10487. 6:00:52you'll need the multi-processing module
  10488. 6:00:54so import process and cpu account and
  10489. 6:00:57we'll save this for a little bit later
  10490. 6:00:59to create a process let's say we have
  10491. 6:01:01process a
  10492. 6:01:02a equals process this step is very
  10493. 6:01:06similar to creating a thread
  10494. 6:01:08we have a target our target will be our
  10495. 6:01:10function of counter
  10496. 6:01:13and if we have arguments we will pass
  10497. 6:01:16those in
  10498. 6:01:16so remember with our arguments we have
  10499. 6:01:18to pass in a tuple
  10500. 6:01:20since we only have one argument to pass
  10501. 6:01:23in
  10502. 6:01:23to differentiate this from an expression
  10503. 6:01:25we have to add a comma at the end
  10504. 6:01:27so our number let's say is 1 billion
  10505. 6:01:31so that's a million 10 million 100
  10506. 6:01:35million
  10507. 6:01:351 billion let me just verify that okay
  10508. 6:01:38so that's 1 billion
  10509. 6:01:39we're going to count from zero to a
  10510. 6:01:42billion
  10511. 6:01:42and in order to start this process a
  10512. 6:01:46my process use the start function
  10513. 6:01:50and then i will use some process
  10514. 6:01:52synchronization by using the
  10515. 6:01:54join function my main process is going
  10516. 6:01:57to wait around
  10517. 6:01:58for my child process of a to finish
  10518. 6:02:01before continuing
  10519. 6:02:03and let's print our performance counter
  10520. 6:02:06so we'll print finished in
  10521. 6:02:12let's say time dot performance counter
  10522. 6:02:19seconds okay so let's see how long it'll
  10523. 6:02:23take to count from zero to a billion
  10524. 6:02:25using
  10525. 6:02:26one process i fast forwarded this video
  10526. 6:02:28but it took my program
  10527. 6:02:3056.7 seconds to count from zero to a
  10528. 6:02:33billion
  10529. 6:02:34but we can speed this up by using
  10530. 6:02:35multi-processing let's create a second
  10531. 6:02:38process named b
  10532. 6:02:40and i'm going to divide the amount of
  10533. 6:02:42work in half so
  10534. 6:02:44each process will count to 500 million
  10535. 6:02:47half of a billion so let's change a to b
  10536. 6:02:52and i'm gonna group these start
  10537. 6:02:53functions together
  10538. 6:02:57just so it's easier to read and then b
  10539. 6:02:59dot join just to synchronize everything
  10540. 6:03:02okay and then let's change 1 billion to
  10541. 6:03:06500 million for each it's the same
  10542. 6:03:09amount of work but divided among workers
  10543. 6:03:12so that is 500 million let's see how
  10544. 6:03:14well it runs this time
  10545. 6:03:16so it took my program about 40.3 seconds
  10546. 6:03:19to finish
  10547. 6:03:20counting from zero to a billion but i
  10548. 6:03:22divided that task among
  10549. 6:03:24different processes each of my two
  10550. 6:03:26processes counted from
  10551. 6:03:27zero to 500 million this time so this
  10552. 6:03:30time let's create
  10553. 6:03:31four processes so we have a
  10554. 6:03:35b c and d
  10555. 6:03:39and we'll need to start them
  10556. 6:03:44and i'm going to join them now this
  10557. 6:03:48might not actually speed it up for you
  10558. 6:03:50and i'll explain why later
  10559. 6:03:52it depends on the amount of cpu cores
  10560. 6:03:54that you have
  10561. 6:03:55okay so oh let's change this to so 250
  10562. 6:03:59million for each
  10563. 6:04:02okay see you in a couple seconds
  10564. 6:04:06okay welcome back so on my computer when
  10565. 6:04:08i ran
  10566. 6:04:09four processes and each was counting up
  10567. 6:04:11to 250 million
  10568. 6:04:14i could finish my program in 27.3
  10569. 6:04:17seconds now for me if i created
  10570. 6:04:20more processes than this it would
  10571. 6:04:22probably take longer and here's why
  10572. 6:04:24so you can print the cpu count of your
  10573. 6:04:27computer
  10574. 6:04:27using this function of cpu count so if i
  10575. 6:04:31were to
  10576. 6:04:31print whatever this function returns i
  10577. 6:04:34can get the count
  10578. 6:04:35of the number of additional processes
  10579. 6:04:37that i can run
  10580. 6:04:38for the time being i'm just going to
  10581. 6:04:40comment out this start function because
  10582. 6:04:42i just want to print whatever this value
  10583. 6:04:44is so my cpu count on my computer
  10584. 6:04:47is four so i can run four additional
  10585. 6:04:51processes
  10586. 6:04:52but let's attempt to run eight processes
  10587. 6:04:55so i'm going to copy all these and let's
  10588. 6:04:57say that we have
  10589. 6:04:59e f g
  10590. 6:05:02and h and we will start all of those two
  10591. 6:05:06so give me a second just to start them
  10592. 6:05:10so a b c d e
  10593. 6:05:14f g
  10594. 6:05:18h and then i'm going to join all of them
  10595. 6:05:23e f g
  10596. 6:05:28and h okay so
  10597. 6:05:31let's run this now oh and change the
  10598. 6:05:34arguments to i keep forgetting to do
  10599. 6:05:35that
  10600. 6:05:36so let's say 125 for each
  10601. 6:05:40so 125 million times eight is
  10602. 6:05:43one billion okay see you in a bit
  10603. 6:05:47all right welcome back this time when i
  10604. 6:05:49had eight processes working it actually
  10605. 6:05:51took me
  10606. 6:05:52longer than when i had four processes
  10607. 6:05:54working when i had four processes
  10608. 6:05:56working it took me about 27 seconds
  10609. 6:05:58this time it took 30 seconds when i had
  10610. 6:06:01eight processes working
  10611. 6:06:02that's because i had more processes than
  10612. 6:06:05my cpu count
  10613. 6:06:06and that's partially because whenever
  10614. 6:06:08you create a process
  10615. 6:06:09there's significant overhead with
  10616. 6:06:11beginning and
  10617. 6:06:12destroying a process and if i can only
  10618. 6:06:15run four
  10619. 6:06:16processes on my computer at one time
  10620. 6:06:18well then i'm creating additional
  10621. 6:06:20processes
  10622. 6:06:21to no extra benefit it's actually
  10623. 6:06:23hindering the performance of the
  10624. 6:06:24computer
  10625. 6:06:25because i'm creating all of these
  10626. 6:06:27additional processes when it really
  10627. 6:06:28doesn't help me
  10628. 6:06:30in conclusion multi-processing is the
  10629. 6:06:32concept
  10630. 6:06:33of running tasks in parallel on
  10631. 6:06:35different cpu cores
  10632. 6:06:37it's similar but different from
  10633. 6:06:38multi-threading because with
  10634. 6:06:40multi-threading
  10635. 6:06:41we can run tasks concurrently but
  10636. 6:06:43they're all taking turns because of the
  10637. 6:06:45gil
  10638. 6:06:46with multi-processing we can run all of
  10639. 6:06:49these different tasks
  10640. 6:06:50together in parallel multi-processing is
  10641. 6:06:53better for
  10642. 6:06:53cpu bound tasks where a task has
  10643. 6:06:56heavy cpu usage and multi-threading
  10644. 6:07:00is better for i o bound tasks tasks that
  10645. 6:07:02involve a lot of waiting around
  10646. 6:07:04so that's multi-processing if you would
  10647. 6:07:06like a copy of all this code i will post
  10648. 6:07:08all of this to the comment section down
  10649. 6:07:10below
  10650. 6:07:10but yeah that is how multi-processing
  10651. 6:07:12works in python
  10652. 6:07:15hey what's going on everybody two bro
  10653. 6:07:17here hope you're doing well and in this
  10654. 6:07:18video we're going to be creating our
  10655. 6:07:19very first
  10656. 6:07:20graphical user interface in python so
  10657. 6:07:22sit back relax and enjoy the show
  10658. 6:07:27welcome one and all today we'll be
  10659. 6:07:28creating our very first
  10660. 6:07:30graphical user interface in python also
  10661. 6:07:32known as a
  10662. 6:07:33gui for short we're going to be using
  10663. 6:07:35the tk inter gui which is a module that
  10664. 6:07:38is included with python
  10665. 6:07:40our first step in order to use tk enter
  10666. 6:07:42is to import this module so that we can
  10667. 6:07:44begin using it
  10668. 6:07:45our first line of code will be from tk
  10669. 6:07:48enter
  10670. 6:07:49import asterisk this will import
  10671. 6:07:52everything related to the tk inter
  10672. 6:07:54module
  10673. 6:07:55and with this import we can now use all
  10674. 6:07:57of the gui features that this module has
  10675. 6:07:59to offer
  10676. 6:08:00now there is one important distinction
  10677. 6:08:02that we need to make we need to discuss
  10678. 6:08:04the differences between
  10679. 6:08:05windows and widgets here's a few quick
  10680. 6:08:09definitions that i definitely did not
  10681. 6:08:11copy from the internet
  10682. 6:08:12widgets are gui elements such as buttons
  10683. 6:08:15text boxes labels images you name it
  10684. 6:08:18and windows they serve as a container
  10685. 6:08:21to hold or contain these widgets for
  10686. 6:08:24this topic we'll be creating and
  10687. 6:08:26customizing our own window
  10688. 6:08:28and in future topics we'll be exploring
  10689. 6:08:30different widgets that are available to
  10690. 6:08:32us
  10691. 6:08:33so let's begin by creating a simple
  10692. 6:08:35window and we should give our window a
  10693. 6:08:37unique name
  10694. 6:08:38such as window that's pretty creative
  10695. 6:08:40and in order to instantiate this window
  10696. 6:08:42we're going to follow
  10697. 6:08:43this window name with equals tk make
  10698. 6:08:46sure you get the capitalization right
  10699. 6:08:48this is a
  10700. 6:08:49uppercase t and a set of parentheses to
  10701. 6:08:51serve as the constructor
  10702. 6:08:53what this will do and i'm just going to
  10703. 6:08:54add a comment is instantiate
  10704. 6:08:57an instance of a window for us
  10705. 6:09:02and when we compile and run this nothing
  10706. 6:09:04appears to happen
  10707. 6:09:05that's because we only instantiated our
  10708. 6:09:07window and we're not actually displaying
  10709. 6:09:09it
  10710. 6:09:10in order to display our window we're
  10711. 6:09:11going to follow this with
  10712. 6:09:13the name of the window dot main loop
  10713. 6:09:16and this will display our window and
  10714. 6:09:19this is our first
  10715. 6:09:20graphical user interface and we'll be
  10716. 6:09:22adding widgets
  10717. 6:09:23to this window so i'm going to add a
  10718. 6:09:25comment that
  10719. 6:09:27this will place window on
  10720. 6:09:30computer screen and it will also listen
  10721. 6:09:34for events but we'll get to that in
  10722. 6:09:36future videos
  10723. 6:09:37now what we'll be doing is customizing
  10724. 6:09:39the appearance of this window
  10725. 6:09:41let's begin by changing the size and we
  10726. 6:09:43can do that with the geometry function
  10727. 6:09:46so we're going to first begin by typing
  10728. 6:09:48the name of our window which is window
  10729. 6:09:51follow this by using the geometry
  10730. 6:09:53function and we pass in a width and a
  10731. 6:09:55height
  10732. 6:09:56but make sure this is within quotes and
  10733. 6:09:58set this to whatever width and height
  10734. 6:09:59that you want
  10735. 6:10:00so if i want 420 by 420 that would be
  10736. 6:10:03420
  10737. 6:10:04x 420 and the size of our window
  10738. 6:10:07is now the size that we set with the
  10739. 6:10:09geometry function
  10740. 6:10:11now take a look at the top left of this
  10741. 6:10:13window we have
  10742. 6:10:14this feather icon as well as a title of
  10743. 6:10:17tk
  10744. 6:10:18that's kind of lame so let's change that
  10745. 6:10:21let's first
  10746. 6:10:21change the window title and there is a
  10747. 6:10:24function to do that
  10748. 6:10:25so we type in the name of our window
  10749. 6:10:28window
  10750. 6:10:29dot title function and we can pass in a
  10751. 6:10:33new title that we want to set
  10752. 6:10:35and i will set the title to be maybe bro
  10753. 6:10:38code first gui
  10754. 6:10:42program that'll work and we now changed
  10755. 6:10:46the title of this window and it says
  10756. 6:10:48bro code first gui program
  10757. 6:10:51the next thing that we can do although
  10758. 6:10:53not necessary
  10759. 6:10:54is that we can change the icon of our
  10760. 6:10:57window bar at the top
  10761. 6:10:58i would like to replace this feather
  10762. 6:11:00icon with an image of my choosing
  10763. 6:11:03here's my desktop don't mind all of the
  10764. 6:11:05clutter and i would like to use this
  10765. 6:11:07image of the logo for my channel and set
  10766. 6:11:09this as the icon
  10767. 6:11:10for my window so what i'm going to do is
  10768. 6:11:13copy
  10769. 6:11:15and then go to my project folder and
  10770. 6:11:17then paste it
  10771. 6:11:19click ok and i now have this image
  10772. 6:11:22within my project folder
  10773. 6:11:24but it's in a format that we cannot use
  10774. 6:11:27currently we need to convert this to
  10775. 6:11:28what's called a photo image
  10776. 6:11:30that is a format that tk enter can use
  10777. 6:11:34so we'll need to convert our image to a
  10778. 6:11:36photo image
  10779. 6:11:37so do that anywhere within the window
  10780. 6:11:39between window equals tk and window.main
  10781. 6:11:42loop
  10782. 6:11:42right here is good i suppose we're going
  10783. 6:11:44to create a photo image
  10784. 6:11:46from this image and this is a png file
  10785. 6:11:48but yours might be something different
  10786. 6:11:50so let's give our photo image a unique
  10787. 6:11:53name like maybe icon
  10788. 6:11:54and in order to create a photo image
  10789. 6:11:56we'll follow this with equals
  10790. 6:11:58photo image and within the constructor
  10791. 6:12:01of our photo image
  10792. 6:12:02there is an option for file we can
  10793. 6:12:05either list
  10794. 6:12:05the file name or the file path if it's
  10795. 6:12:08somewhere else on your computer
  10796. 6:12:09since this image is within my same
  10797. 6:12:12project folder i only have to list the
  10798. 6:12:14file name
  10799. 6:12:15and this is called logo.png
  10800. 6:12:18and this is probably going to be named
  10801. 6:12:19something else for you though
  10802. 6:12:21so we now have this photo image called
  10803. 6:12:24icon
  10804. 6:12:24that we can use and i need to set the
  10805. 6:12:27icon
  10806. 6:12:28of my window to this icon and luckily
  10807. 6:12:31there is a function to take care of that
  10808. 6:12:33for us
  10809. 6:12:33type in the name of the window which is
  10810. 6:12:35window
  10811. 6:12:36dot and we're going to use the icon
  10812. 6:12:39photo
  10813. 6:12:40function there are two arguments the
  10814. 6:12:43first we're going to set to true
  10815. 6:12:46and the second is the photo image that
  10816. 6:12:48we want to use
  10817. 6:12:49and this photo image is called icon so
  10818. 6:12:51we're going to pass
  10819. 6:12:53this photo image as an argument to this
  10820. 6:12:55function
  10821. 6:12:56of icon photo and now when we compile
  10822. 6:12:58and run this
  10823. 6:12:59the image that we wanted to use is now
  10824. 6:13:02set for the top left icon of this window
  10825. 6:13:05bar
  10826. 6:13:05however it loses a lot of the quality
  10827. 6:13:07since the dimensions are so small
  10828. 6:13:09but you can still tell that it's my logo
  10829. 6:13:11though so that's how to replace
  10830. 6:13:13the icon of your window bar in the top
  10831. 6:13:15left corner now let's change the
  10832. 6:13:17background color
  10833. 6:13:18of this window and one way in which we
  10834. 6:13:20can do that is to use the config
  10835. 6:13:22function you can use the config function
  10836. 6:13:24anytime that you want to make any
  10837. 6:13:26changes to this window
  10838. 6:13:27so let's change the background color
  10839. 6:13:29here and there is an option called
  10840. 6:13:31background and we can set this equal to
  10841. 6:13:34either
  10842. 6:13:34a color name or a hexadecimal value
  10843. 6:13:37let's begin with just some color names
  10844. 6:13:39let's say that i want the background
  10845. 6:13:41color to be black
  10846. 6:13:42i'll just type in the word black here
  10847. 6:13:44and this will change the background
  10848. 6:13:46color of my window to black
  10849. 6:13:48alternatively you can set a hex value as
  10850. 6:13:51the color
  10851. 6:13:52so if you were to look up or google hex
  10852. 6:13:55color picker
  10853. 6:13:56you can select whatever color you want
  10854. 6:13:58let's say that i want
  10855. 6:13:59a sky blue color like this well this is
  10856. 6:14:03the hex value i'm going to copy this
  10857. 6:14:05value
  10858. 6:14:07and then paste it within the option for
  10859. 6:14:09my background
  10860. 6:14:10make sure you have this hashtag as well
  10861. 6:14:12because that represents that this is a
  10862. 6:14:14hex value so now the background color is
  10863. 6:14:17that sky blue color that i picked
  10864. 6:14:20well everybody that's the basics of
  10865. 6:14:21creating a window in python and in
  10866. 6:14:24future videos we're going to be
  10867. 6:14:25exploring
  10868. 6:14:26different widgets that we can add to
  10869. 6:14:28this window that have some functionality
  10870. 6:14:30so if you would like a copy of all this
  10871. 6:14:32code i'll post all of this in the
  10872. 6:14:33comments down below
  10873. 6:14:34but yeah that's how to create your very
  10874. 6:14:36first gui in python
  10875. 6:14:38hey what's going on everybody it's your
  10876. 6:14:40bro here hope you're doing well and in
  10877. 6:14:42this video i'm going to teach you guys
  10878. 6:14:43how we can create
  10879. 6:14:44labels in python so sit back relax and
  10880. 6:14:47enjoy the show
  10881. 6:14:49what is a label well i'm glad you asked
  10882. 6:14:52that question
  10883. 6:14:52it's an area widget that holds text and
  10884. 6:14:55or
  10885. 6:14:56an image within a window so to begin
  10886. 6:14:58we'll need to create a window to act as
  10887. 6:15:00a container
  10888. 6:15:02to hold this label we can create a
  10889. 6:15:04window with
  10890. 6:15:05window equals tk followed by
  10891. 6:15:07window.mainloop
  10892. 6:15:08and this will give us a window in which
  10893. 6:15:10we can add a label to
  10894. 6:15:12now let's give our label a unique name
  10895. 6:15:15such as label
  10896. 6:15:16and in order to instantiate a label
  10897. 6:15:18we'll follow this label name
  10898. 6:15:20with equals label with a capital l and a
  10899. 6:15:23set of parentheses
  10900. 6:15:24the parentheses are acting as the
  10901. 6:15:26constructor for this widget
  10902. 6:15:28our label and we can pass in arguments
  10903. 6:15:30into
  10904. 6:15:31these parentheses the first argument
  10905. 6:15:33that we're going to pass
  10906. 6:15:34in is a master the container for this
  10907. 6:15:37label
  10908. 6:15:38we're going to pass in the name of our
  10909. 6:15:40window because our window is acting as
  10910. 6:15:42the container
  10911. 6:15:43with widgets in python we can
  10912. 6:15:45de-eliminate the arguments that we're
  10913. 6:15:47passing in
  10914. 6:15:48with a comma and there are options
  10915. 6:15:50available to us
  10916. 6:15:52options are keyword arguments that we
  10917. 6:15:54can pass in to the constructor for this
  10918. 6:15:56widget
  10919. 6:15:57one option available to us is text and
  10920. 6:16:00we can set the text of this label equal
  10921. 6:16:02to
  10922. 6:16:02a string that we set so let's say that
  10923. 6:16:05we want this label to say
  10924. 6:16:07hello world well we're going to pass in
  10925. 6:16:09this option
  10926. 6:16:10text and it's a keyword argument
  10927. 6:16:14so if we were to do this we're going to
  10928. 6:16:16set the text
  10929. 6:16:17of the label but this label currently
  10930. 6:16:20doesn't appear within our window
  10931. 6:16:21what gives bro i thought you knew how to
  10932. 6:16:23code hold up wait a minute there's one
  10933. 6:16:25more thing that we need to do
  10934. 6:16:27so we're going to actually add this
  10935. 6:16:29label
  10936. 6:16:30to the window now we set the master
  10937. 6:16:33of the label to be our window but now we
  10938. 6:16:35actually have to add it as step two
  10939. 6:16:38one way which we can do that is to use a
  10940. 6:16:40pack
  10941. 6:16:41function of our label so follow label
  10942. 6:16:44equals label and pass in all of your
  10943. 6:16:46arguments
  10944. 6:16:46and then follow this with the name of
  10945. 6:16:48the label dot pack
  10946. 6:16:51so with the pack option by default this
  10947. 6:16:53will place our widget in the top
  10948. 6:16:55center of our window another way in
  10949. 6:16:58which we can add a label
  10950. 6:17:00to a container or window is to use the
  10951. 6:17:02place function
  10952. 6:17:03and we can set some coordinates of where
  10953. 6:17:05we want this label or other widget to
  10954. 6:17:08appear
  10955. 6:17:08within the window if we want this to
  10956. 6:17:10appear in the top left corner
  10957. 6:17:12we can set x to equal zero and y t
  10958. 6:17:15equals zero and these are options
  10959. 6:17:17available to us too that we can pass
  10960. 6:17:18in so this place function will place
  10961. 6:17:22our label at some position or
  10962. 6:17:25some set of coordinates within the
  10963. 6:17:27window and with
  10964. 6:17:28x equals 0 and y equals 0 that will
  10965. 6:17:31place our widget in the top left corner
  10966. 6:17:33of the window
  10967. 6:17:34and if i were to change these to let's
  10968. 6:17:36say x equals 100
  10969. 6:17:38and y equals 100 well this is going to
  10970. 6:17:40be placed
  10971. 6:17:42down by 100 pixels and to the right by
  10972. 6:17:45100 pixels
  10973. 6:17:46so you can use pack or place there's a
  10974. 6:17:48few others
  10975. 6:17:49like grid but they're a little more
  10976. 6:17:51complex so we'll get to that in a future
  10977. 6:17:53video
  10978. 6:17:53for the time being i'm going to use the
  10979. 6:17:55pack function of the label
  10980. 6:17:57and turn this place function into a
  10981. 6:17:59comment
  10982. 6:18:01dot pack all right now
  10983. 6:18:04how can we customize this label we can
  10984. 6:18:07pass in different options
  10985. 6:18:08when we instantiate this label let's
  10986. 6:18:10change the font and that's another
  10987. 6:18:12option
  10988. 6:18:13we can change the font with font equals
  10989. 6:18:16and then we can pass in a font let's
  10990. 6:18:18begin with a
  10991. 6:18:19font family i will say maybe ariel
  10992. 6:18:22a size perhaps 40 and then a style if
  10993. 6:18:26you so choose
  10994. 6:18:28like bold there's also italic and
  10995. 6:18:30underline
  10996. 6:18:31uh but this will work just fine then so
  10997. 6:18:33this will change the font
  10998. 6:18:35of the text of our label and it says
  10999. 6:18:38hello world still
  11000. 6:18:39now let's change the foreground color
  11001. 6:18:43so that is another option the foreground
  11002. 6:18:46is the font color
  11003. 6:18:47and the option is fg for foreground it's
  11004. 6:18:50short for foreground
  11005. 6:18:51foreground equals we can state a color
  11006. 6:18:54name
  11007. 6:18:55such as green and our text
  11008. 6:18:58should now be green you can select
  11009. 6:19:00something more specific by
  11010. 6:19:02passing in a hex value so a custom color
  11011. 6:19:05that i like
  11012. 6:19:06is hashtag zero zero ff00
  11013. 6:19:10this is bright green you can always look
  11014. 6:19:12up what different hex values are
  11015. 6:19:14and we can also change the background
  11016. 6:19:16color too
  11017. 6:19:17that is bg short for background color
  11018. 6:19:20and we can pass in a color name
  11019. 6:19:22or a hex value i'm just going to set
  11020. 6:19:24this to black just to demonstrate that
  11021. 6:19:26we can pass in both
  11022. 6:19:27either hex values or color names and
  11023. 6:19:30what we have
  11024. 6:19:31is green text on a black background
  11025. 6:19:34and this is the limits of our label
  11026. 6:19:37within the window
  11027. 6:19:38because when we expand this window the
  11028. 6:19:41label is still going to stay the same
  11029. 6:19:42size
  11030. 6:19:43if we were to change the text within the
  11031. 6:19:46label
  11032. 6:19:46our label is only going to take up the
  11033. 6:19:49room that it needs
  11034. 6:19:50and you can see that our label is now
  11035. 6:19:52smaller now we have all of these
  11036. 6:19:54arguments within our constructor and
  11037. 6:19:55it's getting a little bit difficult to
  11038. 6:19:57actually read
  11039. 6:19:58so i'm going to separate each of these
  11040. 6:20:00arguments line by line just so that this
  11041. 6:20:02is more organized
  11042. 6:20:03but it'll work just the same as it did
  11043. 6:20:05before now how do we add a
  11044. 6:20:07border around our label one we can set
  11045. 6:20:10the border style
  11046. 6:20:11with the relief option and one border
  11047. 6:20:14that i like
  11048. 6:20:14is raised but right now the border width
  11049. 6:20:18is one so it's a little difficult for us
  11050. 6:20:21to actually see the border
  11051. 6:20:22let's increase the border width with the
  11052. 6:20:24option bd
  11053. 6:20:26and i'll set this equal to maybe 10.
  11054. 6:20:29so this border is now more visible
  11055. 6:20:32another relief
  11056. 6:20:33which is the border style is sunken and
  11057. 6:20:36this will give our label a
  11058. 6:20:38sunken appearance but i think i'll
  11059. 6:20:40change this back to raised
  11060. 6:20:42we can also add some padding around the
  11061. 6:20:45text
  11062. 6:20:45between the text and the border we can
  11063. 6:20:47pad x and pad y
  11064. 6:20:49pad x will add some padding some space
  11065. 6:20:52between
  11066. 6:20:53the x-axis of our text between the
  11067. 6:20:55border so let's set this to maybe 20
  11068. 6:20:57and we now have 20 pixels worth of space
  11069. 6:21:00between
  11070. 6:21:01the text and the border let's also pad y
  11071. 6:21:06this will add some space above and below
  11072. 6:21:10our text between the text and the border
  11073. 6:21:13now how do we add an image to a label we
  11074. 6:21:16first need to create what's called
  11075. 6:21:18a photo image and we will create this
  11076. 6:21:21within
  11077. 6:21:21the lines of window equals tk and
  11078. 6:21:24window.main loop
  11079. 6:21:25and let's give this photo image a unique
  11080. 6:21:27name like
  11081. 6:21:28photo and in order to create a photo
  11082. 6:21:31image we're going to follow this with
  11083. 6:21:33photo equals photo image
  11084. 6:21:36and within the constructor of our photo
  11085. 6:21:38image there's an option called
  11086. 6:21:40file and we can list a file path or a
  11087. 6:21:43file name
  11088. 6:21:44right now on my desktop there is a image
  11089. 6:21:46that i want to use and i need to convert
  11090. 6:21:48this to a photo image
  11091. 6:21:50here's my desktop and this is an image
  11092. 6:21:52that i want to use to add to my label
  11093. 6:21:54i need to get the file path and i can
  11094. 6:21:56get that by going to properties
  11095. 6:21:58then underneath location i can copy this
  11096. 6:22:01location address
  11097. 6:22:03and paste it within the quotes for file
  11098. 6:22:07and then i need to add the image name
  11099. 6:22:09and this is person.png
  11100. 6:22:12and with these backslashes these need to
  11101. 6:22:14be double backslashes because
  11102. 6:22:16that is the escape character for a
  11103. 6:22:18backslash
  11104. 6:22:20but what would be a better option is to
  11105. 6:22:23add
  11106. 6:22:23this image to my project folder that
  11107. 6:22:25contains the python file that i'm
  11108. 6:22:27working with
  11109. 6:22:28so in place of listing the entire file
  11110. 6:22:31path
  11111. 6:22:31i'm going to copy the image that i want
  11112. 6:22:33to use go to my project folder
  11113. 6:22:36and then paste it and i no longer need
  11114. 6:22:40this entire file path i can just list
  11115. 6:22:42the image name that i have
  11116. 6:22:44and now what we need to do is add this
  11117. 6:22:46photo to the label and there's an option
  11118. 6:22:48to do so
  11119. 6:22:49that is the image option and we'll place
  11120. 6:22:52this within the constructor for the
  11121. 6:22:53label
  11122. 6:22:54we're going to set image equal to the
  11123. 6:22:56photo image that we have
  11124. 6:22:58and our photo image is called photo this
  11125. 6:23:00will add our photo image
  11126. 6:23:02to our label or other widget depending
  11127. 6:23:04on what we're working with
  11128. 6:23:06so we have our image but it replaced all
  11129. 6:23:09of the text
  11130. 6:23:10that we had for this label how do we add
  11131. 6:23:12both
  11132. 6:23:13text and an image to a window well we
  11133. 6:23:16can follow this
  11134. 6:23:16with the compound option we can set a
  11135. 6:23:19direction of where
  11136. 6:23:21we want this image to be placed relative
  11137. 6:23:23to the text that we have
  11138. 6:23:25so i'm going to set compound equal to
  11139. 6:23:28bottom
  11140. 6:23:28so that the image appears on the bottom
  11141. 6:23:31or underneath
  11142. 6:23:31our text so we can both have text and an
  11143. 6:23:35image
  11144. 6:23:35or if i want this to be in a different
  11145. 6:23:37location i could set this to maybe top
  11146. 6:23:39left or right if i were to set this to
  11147. 6:23:41top we have
  11148. 6:23:42our image on top of our text and one
  11149. 6:23:46thing to know about labels and many
  11150. 6:23:48other widgets in python is that
  11151. 6:23:50the size of the widget will actually
  11152. 6:23:52increase to accommodate
  11153. 6:23:54the size of all the components that's
  11154. 6:23:56contained within if i were to give the
  11155. 6:23:58text a longer string of characters such
  11156. 6:24:01as
  11157. 6:24:02bro do you even code then
  11158. 6:24:05the width of our label is actually going
  11159. 6:24:07to expand
  11160. 6:24:08to accommodate the size of the longer
  11161. 6:24:11string of text that we have
  11162. 6:24:13so that's the basics of labels everyone
  11163. 6:24:15if you would like a copy of all this
  11164. 6:24:17code i will post all of this in the
  11165. 6:24:18comments down below
  11166. 6:24:20but yeah that's the basics of labels in
  11167. 6:24:22python
  11168. 6:24:24hey what's going on everybody it's your
  11169. 6:24:26bro here hope you're doing well
  11170. 6:24:27and in this video i'm going to teach you
  11171. 6:24:28guys how we can create buttons in python
  11172. 6:24:31so sit back relax and enjoy the show
  11173. 6:24:35buttons you click them and then they do
  11174. 6:24:38stuff that's pretty much it
  11175. 6:24:39so this is how to create a button in
  11176. 6:24:41python we'll need a window
  11177. 6:24:43window equals tk followed by window.main
  11178. 6:24:46loop
  11179. 6:24:46let's call our button just button and in
  11180. 6:24:49order to create this we'll follow this
  11181. 6:24:51with
  11182. 6:24:51equals button and within the constructor
  11183. 6:24:55for this button we're going to list
  11184. 6:24:56what we want to add this button to what
  11185. 6:24:59is the master
  11186. 6:25:00the master is the window and in order to
  11187. 6:25:03display this button
  11188. 6:25:04we're going to follow this with button
  11189. 6:25:07dot
  11190. 6:25:07pack and we should have a small
  11191. 6:25:11rectangular button within our window
  11192. 6:25:13but it doesn't display any text nor does
  11193. 6:25:16it do anything
  11194. 6:25:17so let's add some text and there is an
  11195. 6:25:19option to do that
  11196. 6:25:20within the constructor we're going to
  11197. 6:25:22list the option for text
  11198. 6:25:23and set this to whatever text you want
  11199. 6:25:25to display let's
  11200. 6:25:26say click me and then when we run
  11201. 6:25:30this our button now says the text that
  11202. 6:25:32we set
  11203. 6:25:33and it says click me however it
  11204. 6:25:35currently doesn't do anything though
  11205. 6:25:37we need to set a command that is another
  11206. 6:25:39option
  11207. 6:25:40command equals and we list a function
  11208. 6:25:43name this is what's known as a callback
  11209. 6:25:46so let's create a function
  11210. 6:25:47called click and then outside of the
  11211. 6:25:50window let's define what our click
  11212. 6:25:51function does def click
  11213. 6:25:55and let's print a message you
  11214. 6:25:58clicked the button
  11215. 6:26:02now with the command we're going to list
  11216. 6:26:05the
  11217. 6:26:05function name without the parentheses so
  11218. 6:26:08make sure you're writing it as
  11219. 6:26:10this and not that this is what's known
  11220. 6:26:12as a callback
  11221. 6:26:14so when we click on this button it's
  11222. 6:26:16going to perform our callback
  11223. 6:26:19and perform whatever is within our
  11224. 6:26:21function
  11225. 6:26:22click just like that let's customize the
  11226. 6:26:25appearance of this button i'm going to
  11227. 6:26:27change the font
  11228. 6:26:28that is another option font equals
  11229. 6:26:31and let's pick a professional font such
  11230. 6:26:34as comic
  11231. 6:26:35sans it's very professional and i'll set
  11232. 6:26:38a size of 30.
  11233. 6:26:41and now you can actually read what's on
  11234. 6:26:43this button it's large enough
  11235. 6:26:46let's set the foreground color that is
  11236. 6:26:48the font color fg
  11237. 6:26:49you can place a color name or a hex
  11238. 6:26:52value here
  11239. 6:26:53i'll pick green that is 0 0 ff00
  11240. 6:26:57and our text should now be green which
  11241. 6:27:00it is
  11242. 6:27:01let's also change the background color
  11243. 6:27:04that is bg
  11244. 6:27:05i will pick the color name of black and
  11245. 6:27:08now we have
  11246. 6:27:09green text on a black background notice
  11247. 6:27:12that when i click on this button
  11248. 6:27:14or hold my mouse button down the color
  11249. 6:27:16scheme has changed that's because
  11250. 6:27:18there is a different active foreground
  11251. 6:27:21and active background
  11252. 6:27:22and we can change those too so that this
  11253. 6:27:24won't flash every time you click on the
  11254. 6:27:26button
  11255. 6:27:27so let's change the active foreground
  11256. 6:27:29and active background
  11257. 6:27:30those are other options active
  11258. 6:27:33foreground
  11259. 6:27:34and i'll set this to the same color as
  11260. 6:27:36my foreground
  11261. 6:27:38which is green so the text color is
  11262. 6:27:41going to stay the same
  11263. 6:27:42and i'll also change the active
  11264. 6:27:44background
  11265. 6:27:47and i will also set this to black to
  11266. 6:27:49match my
  11267. 6:27:50background and then this should no
  11268. 6:27:53longer flash
  11269. 6:27:54when we click on our button
  11270. 6:27:57if you need to disable somebody from
  11271. 6:27:59clicking on this button
  11272. 6:28:00there is an option to do so and that
  11273. 6:28:02option is the state
  11274. 6:28:04option and normally this is active but
  11275. 6:28:07you can set this to
  11276. 6:28:08disabled and we can no longer click on
  11277. 6:28:11this button
  11278. 6:28:12and you can see that the color scheme
  11279. 6:28:14has changed too it's all grayed out and
  11280. 6:28:16whatnot
  11281. 6:28:17so i'm going to replace this back with
  11282. 6:28:19active because we still have a few more
  11283. 6:28:21things to do
  11284. 6:28:22now how do we add an image to a button
  11285. 6:28:24there is an
  11286. 6:28:25image option and we set this equal to a
  11287. 6:28:28photo image
  11288. 6:28:28but first we need to create a photo
  11289. 6:28:30image i have this photo of
  11290. 6:28:32a like button a thumbs up that i want to
  11291. 6:28:34use i need to create a photo image from
  11292. 6:28:37this image file
  11293. 6:28:38so within the window let's call this
  11294. 6:28:40photo image just
  11295. 6:28:42photo photo equals
  11296. 6:28:46photo image then i'm going to list the
  11297. 6:28:49file path or the file name
  11298. 6:28:51since this image is within my same
  11299. 6:28:53project folder
  11300. 6:28:54i only have to list the file name and
  11301. 6:28:57this is called
  11302. 6:28:58like.png but if you use your own image
  11303. 6:29:00it's probably going to be named
  11304. 6:29:01something else
  11305. 6:29:02then i need to set the image equal to my
  11306. 6:29:05photo image which i named
  11307. 6:29:07photo but the photo replaces the text on
  11308. 6:29:11this button but it still works the same
  11309. 6:29:14so we would like to display some text as
  11310. 6:29:16well as our image
  11311. 6:29:17we need to use the compound option
  11312. 6:29:21and we list a direction we can list
  11313. 6:29:24top bottom left right we want this image
  11314. 6:29:27to be
  11315. 6:29:28on the perhaps bottom so let's list the
  11316. 6:29:31bottom
  11317. 6:29:32and now when we run this we have our
  11318. 6:29:34text as well as our image
  11319. 6:29:36and if we changed compound to a
  11320. 6:29:37different direction like
  11321. 6:29:39top then the image is going to be
  11322. 6:29:41displayed
  11323. 6:29:42on the top relative to the text
  11324. 6:29:45what if we wanted to count the amount of
  11325. 6:29:47times that we clicked this button
  11326. 6:29:49we could create a variable called count
  11327. 6:29:52and we'll set this equal to
  11328. 6:29:53zero but in order for us to access this
  11329. 6:29:56count variable
  11330. 6:29:58within the function of click we're going
  11331. 6:30:01to list
  11332. 6:30:02count as global so that this is a global
  11333. 6:30:06variable
  11334. 6:30:06every time we click this button let's
  11335. 6:30:09increment our variable count
  11336. 6:30:10by one by saying count plus equals one
  11337. 6:30:14and then we will print whatever the
  11338. 6:30:16value of count is
  11339. 6:30:18so within a print statement we'll just
  11340. 6:30:20say count
  11341. 6:30:21now when we click on this button we have
  11342. 6:30:23access to our count
  11343. 6:30:24variable that's outside of this function
  11344. 6:30:27and every time we click on this button
  11345. 6:30:28it's going to increase our count
  11346. 6:30:31variable by
  11347. 6:30:32one well everybody that's the basics of
  11348. 6:30:34creating
  11349. 6:30:35buttons in python if you want a copy of
  11350. 6:30:37all this code i'll post all of this in
  11351. 6:30:39the comments down below
  11352. 6:30:40but yeah that's the basics of buttons in
  11353. 6:30:43python
  11354. 6:30:44hey what's going on everybody it's your
  11355. 6:30:46bro here hope you're doing well and in
  11356. 6:30:48this video i'm going to teach you guys
  11357. 6:30:49how we can create a simple entry box in
  11358. 6:30:51python
  11359. 6:30:52so sit back relax and enjoy the show
  11360. 6:30:56if you find this video helpful please
  11361. 6:30:58remember to like
  11362. 6:30:59comment and subscribe your support will
  11363. 6:31:02help keep this channel running
  11364. 6:31:04alright my fellow bros in this video
  11365. 6:31:06we're going to be discussing the entry
  11366. 6:31:07widget
  11367. 6:31:08this is a text box that accepts a single
  11368. 6:31:10line of user input
  11369. 6:31:12as always we'll need a window window
  11370. 6:31:14equals tk
  11371. 6:31:15followed by window.main loop and let's
  11372. 6:31:17give our entry box a unique name let's
  11373. 6:31:19call
  11374. 6:31:20this entry for short and then to create
  11375. 6:31:22this entry widget we're going to follow
  11376. 6:31:23this with equals
  11377. 6:31:25entry and within the constructor we have
  11378. 6:31:28to state what we're adding this to
  11379. 6:31:29we're going to add this to our window
  11380. 6:31:32and we should finish this by packing it
  11381. 6:31:34or placing it
  11382. 6:31:36entry dot pack and we should have an
  11383. 6:31:39entry box within
  11384. 6:31:40our window however the text is somewhat
  11385. 6:31:43small so let's change that
  11386. 6:31:45we can set this as an option within the
  11387. 6:31:47constructor of our entry box
  11388. 6:31:50so let's set the font font equals and
  11389. 6:31:53pick whatever font you want
  11390. 6:31:54i'm going to pick maybe ariel and then a
  11391. 6:31:57size
  11392. 6:31:58it'd be 50. and this should be a little
  11393. 6:32:00bit larger
  11394. 6:32:02and we can type in some
  11395. 6:32:06text like this cool right
  11396. 6:32:10now we should add a submit button
  11397. 6:32:13because right now this currently doesn't
  11398. 6:32:14do anything if we type some text in
  11399. 6:32:16so let's create a button i'm going to
  11400. 6:32:18create this button maybe after
  11401. 6:32:20the entry box and let's say that this is
  11402. 6:32:23a submit button so let's call this
  11403. 6:32:25submit button equals button
  11404. 6:32:28what are we adding this to writing the
  11405. 6:32:30square window
  11406. 6:32:32and we'll set some text text equals
  11407. 6:32:35submit
  11408. 6:32:36we'll also need a command for this
  11409. 6:32:37button
  11410. 6:32:39command equals and we'll create a
  11411. 6:32:42function called submit
  11412. 6:32:43and we want to pack the submit button to
  11413. 6:32:46add it to the window
  11414. 6:32:47submit button.pack
  11415. 6:32:50all right now let's put our entry box on
  11416. 6:32:53the left hand side and our submit button
  11417. 6:32:55on the right hand side
  11418. 6:32:56when you pack these widgets you can set
  11419. 6:32:58these on a certain side
  11420. 6:33:00side equals left and our submit button
  11421. 6:33:04we want this on the right not necessary
  11422. 6:33:06but i think it looks better
  11423. 6:33:08and we also need to define our submit
  11424. 6:33:10function outside of the window
  11425. 6:33:12let's define what this does def submit
  11426. 6:33:16what's this going to do we need to get
  11427. 6:33:19the current value that's within our
  11428. 6:33:21entry box
  11429. 6:33:22one way in which we can do that is use
  11430. 6:33:24the entry boxes get
  11431. 6:33:26function this will return a string and
  11432. 6:33:29we can do something with that string
  11433. 6:33:30such as store this within a variable so
  11434. 6:33:33let's create a variable called maybe
  11435. 6:33:35username
  11436. 6:33:35let's pretend we're having a user submit
  11437. 6:33:38their username to login to something i
  11438. 6:33:40suppose
  11439. 6:33:41so we'll get the current value of the
  11440. 6:33:43entry box
  11441. 6:33:44store that within a variable and we'll
  11442. 6:33:46call this username
  11443. 6:33:47and then we will maybe print this for
  11444. 6:33:49fun print
  11445. 6:33:52hello plus user
  11446. 6:33:55name and let's try this
  11447. 6:33:58so here's our entry box here and our
  11448. 6:34:00submit button i'm just going to make up
  11449. 6:34:02something
  11450. 6:34:03click submit and it displays our message
  11451. 6:34:06hello ergo gerger gerger
  11452. 6:34:09i didn't say that this video was going
  11453. 6:34:10to be professional let's add a few other
  11454. 6:34:12buttons
  11455. 6:34:13let's say we want a delete button as
  11456. 6:34:15well as a backspace button
  11457. 6:34:18so let's copy what we have for our
  11458. 6:34:20submit button and create a delete button
  11459. 6:34:22and then a backspace button the delete
  11460. 6:34:24button is a little bit easier to code
  11461. 6:34:26so let's rename our submit button as
  11462. 6:34:28delete button
  11463. 6:34:31make sure to change the name to when you
  11464. 6:34:33pack this and the text
  11465. 6:34:34we will say delete and we'll create a
  11466. 6:34:38function called
  11467. 6:34:38delete and let's define what delete does
  11468. 6:34:42so after our submit function let's
  11469. 6:34:44define
  11470. 6:34:46our delete function and we need to
  11471. 6:34:50delete all of the text within our entry
  11472. 6:34:52box
  11473. 6:34:53one way in which we can do that is to
  11474. 6:34:55type in the name of our entry box
  11475. 6:34:57dot delete function
  11476. 6:35:00and this takes two positional arguments
  11477. 6:35:03which portion of our entry box do we
  11478. 6:35:06want to delete
  11479. 6:35:07so we want the very first character that
  11480. 6:35:09is index
  11481. 6:35:100 followed by the last character and we
  11482. 6:35:13can say
  11483. 6:35:13end so this will delete all of the
  11484. 6:35:16characters
  11485. 6:35:16within our entry box and let's try it so
  11486. 6:35:19i'm just going to type in my name
  11487. 6:35:21bro i'm going to delete this and all of
  11488. 6:35:24the text is now gone
  11489. 6:35:27just like that let's create a backspace
  11490. 6:35:29button we'll copy what we have for our
  11491. 6:35:31delete button and change
  11492. 6:35:32the word delete to backspace
  11493. 6:35:37make sure you change that when you pack
  11494. 6:35:38this as well and for the text we'll set
  11495. 6:35:40this to
  11496. 6:35:41backspace and we'll create a function
  11497. 6:35:44called backspace
  11498. 6:35:46and let's define what backspace does so
  11499. 6:35:49after
  11500. 6:35:49our delete function let's define back
  11501. 6:35:52space and this is going to do something
  11502. 6:35:55very similar to our delete function
  11503. 6:35:58except we're going to change the first
  11504. 6:36:02positional argument
  11505. 6:36:03we need to place the second to last
  11506. 6:36:06character
  11507. 6:36:06within this positional argument one way
  11508. 6:36:09in which we can do that
  11509. 6:36:10is to first get the entire length of all
  11510. 6:36:13of the characters within our entry box
  11511. 6:36:16so let's begin by typing entry dot
  11512. 6:36:19get and we need to determine the length
  11513. 6:36:21of this
  11514. 6:36:22and we can surround this by placing this
  11515. 6:36:25within the length function so this will
  11516. 6:36:28return
  11517. 6:36:29the current length of all of the
  11518. 6:36:31characters within our entry box
  11519. 6:36:33and then we're going to add -1 so that
  11520. 6:36:36this
  11521. 6:36:36first positional argument is the second
  11522. 6:36:38to last character
  11523. 6:36:40and the second positional argument is
  11524. 6:36:42the end the last character
  11525. 6:36:44and then when we run this let's type in
  11526. 6:36:47our name i'm just going to type in draw
  11527. 6:36:49code this time i'm going to click
  11528. 6:36:50backspace and then it's only going to
  11529. 6:36:52delete the last
  11530. 6:36:53character and then if you were to click
  11531. 6:36:55delete it deletes everything
  11532. 6:36:58broy and then if you click submit it's
  11533. 6:37:01going to submit your name
  11534. 6:37:03all right let's customize the appearance
  11535. 6:37:05of this entry box
  11536. 6:37:06so within the constructor for this entry
  11537. 6:37:09box
  11538. 6:37:10let's change maybe the color maybe the
  11539. 6:37:13background color and foreground color
  11540. 6:37:14let's begin with the foreground color
  11541. 6:37:16you can pass in a color name or a hex
  11542. 6:37:18value
  11543. 6:37:18i want this to be green so the hex value
  11544. 6:37:21for that is
  11545. 6:37:21zero zero f zero ff00 and the foreground
  11546. 6:37:25color
  11547. 6:37:25of the text when we type it in is now
  11548. 6:37:28green
  11549. 6:37:29and we can also change the background
  11550. 6:37:31color that is bg
  11551. 6:37:33we could pass in a color name or a hex
  11552. 6:37:35value i'm just going to say black to
  11553. 6:37:37keep it simple
  11554. 6:37:38and we now have a black background and
  11555. 6:37:40the text color is now
  11556. 6:37:42green here's a few useful features that
  11557. 6:37:45you might be interested in
  11558. 6:37:47let's insert some default text for our
  11559. 6:37:49entry box
  11560. 6:37:50so we can use the insert function entry
  11561. 6:37:53dot insert
  11562. 6:37:54we place a positional argument as well
  11563. 6:37:56as some text
  11564. 6:37:57so for the positional argument let's say
  11565. 6:37:59zero so that's the very beginning
  11566. 6:38:01index zero and let's set the text to
  11567. 6:38:04maybe
  11568. 6:38:05spongebob and then the default text
  11569. 6:38:08within our entry box is now
  11570. 6:38:10spongebob another option available to us
  11571. 6:38:12is that we can set the state
  11572. 6:38:15of this entry box we can set this to
  11573. 6:38:17active or disabled
  11574. 6:38:19let's say that after somebody submits
  11575. 6:38:20our name we want to disable
  11576. 6:38:22this entry box so we can either pass in
  11577. 6:38:25this option
  11578. 6:38:27within our constructor or we can use the
  11579. 6:38:30config function to make any changes
  11580. 6:38:32to the state of our entry box so let's
  11581. 6:38:35say after we submit our username we want
  11582. 6:38:37to disable this entry box
  11583. 6:38:39so let's type in the name of our entry
  11584. 6:38:41box dot config
  11585. 6:38:44and for the option for state let's set
  11586. 6:38:48this to
  11587. 6:38:49disabled and then once we type in a name
  11588. 6:38:54and then click submit our entry box is
  11589. 6:38:57now disabled
  11590. 6:38:58there's also an option called show where
  11591. 6:39:02you can show a certain character in
  11592. 6:39:04place of the normal text on the screen
  11593. 6:39:06so this would be useful for a password
  11594. 6:39:09let's say that we're typing in
  11595. 6:39:10a password but we don't want to display
  11596. 6:39:13the password on the screen
  11597. 6:39:14we can use the show option to show a
  11598. 6:39:17certain character in place
  11599. 6:39:19of our text so you might see bullets or
  11600. 6:39:22an asterisk
  11601. 6:39:23so let's pretend that we're now typing
  11602. 6:39:24in a password so we're going to set
  11603. 6:39:27the option for show and place a
  11604. 6:39:30character that we want to replace each
  11605. 6:39:32character with
  11606. 6:39:33let's say we want to only display
  11607. 6:39:35asterisks when we type in
  11608. 6:39:37some characters and i'm going to type in
  11609. 6:39:39the word password
  11610. 6:39:41but it's only displaying asterisks now
  11611. 6:39:43but if i were to click submit
  11612. 6:39:45it displays our text and i would say
  11613. 6:39:48plain text instead of our hidden text
  11614. 6:39:50because we're only showing
  11615. 6:39:52whatever character that we set so that's
  11616. 6:39:54an option available to you as well
  11617. 6:39:55well everybody that's how to create an
  11618. 6:39:57entry box in python you can submit
  11619. 6:39:59information
  11620. 6:40:00such as a username for example or even a
  11621. 6:40:03password if you set the
  11622. 6:40:04show option available to you if you want
  11623. 6:40:07a copy of all this code i'll post all of
  11624. 6:40:09this in the comments down below
  11625. 6:40:10but yeah that's some of the uses of the
  11626. 6:40:12entry widget in python
  11627. 6:40:14hey how's it going everybody two bro
  11628. 6:40:16here hope you're doing well and in this
  11629. 6:40:18video i'm going to teach you guys how we
  11630. 6:40:19can create and customize our own
  11631. 6:40:21check buttons in python so sit back
  11632. 6:40:23relax and enjoy the show
  11633. 6:40:27all right my fellow bros in this video
  11634. 6:40:30we're going to be creating
  11635. 6:40:31and customizing our own check buttons in
  11636. 6:40:33python i sometimes refer to these as
  11637. 6:40:35check boxes
  11638. 6:40:36so in case i do that's what i'm
  11639. 6:40:38referring to as always we'll need a
  11640. 6:40:40window
  11641. 6:40:41window equals tk followed by window.main
  11642. 6:40:44loop
  11643. 6:40:44and let's call this button just check
  11644. 6:40:47button and in order to create this check
  11645. 6:40:50button
  11646. 6:40:51we type in the name of the check button
  11647. 6:40:53equals
  11648. 6:40:54check button and within the constructor
  11649. 6:40:58what are we adding this check button to
  11650. 6:41:00we're adding this to our window
  11651. 6:41:02and also within the constructor we can
  11652. 6:41:04set the different options
  11653. 6:41:05for how we want to customize this check
  11654. 6:41:07button let's begin with the text
  11655. 6:41:10text equals i agree
  11656. 6:41:13to something and last but not least we
  11657. 6:41:17need to add
  11658. 6:41:18this check button to the window one way
  11659. 6:41:20in which we can do that is
  11660. 6:41:22to use the pack function of this widget
  11661. 6:41:24so follow this with
  11662. 6:41:26check button dot pack when we run this
  11663. 6:41:28we have our check button within our
  11664. 6:41:30window
  11665. 6:41:31and we can toggle this on or off right
  11666. 6:41:34now i'm agreeing to something
  11667. 6:41:35i'm not sure what i'm agreeing to but
  11668. 6:41:37hopefully it's not anything bad
  11669. 6:41:38now currently this doesn't do anything
  11670. 6:41:40so we'll need to associate a variable
  11671. 6:41:42with this check button
  11672. 6:41:43so we'll add that as an option variable
  11673. 6:41:46equals and let's create a variable
  11674. 6:41:49called x
  11675. 6:41:50and we'll need to define this within the
  11676. 6:41:52window so
  11677. 6:41:53after we create our instance of our
  11678. 6:41:55window let's say
  11679. 6:41:57x equals now with check buttons
  11680. 6:42:00they store a 1 or a 0 by default within
  11681. 6:42:03our variable
  11682. 6:42:04our variable that we call x so with
  11683. 6:42:08x we're going to set this equal to a int
  11684. 6:42:12var if this were to return a string we
  11685. 6:42:15would say
  11686. 6:42:16string bar but normally by default this
  11687. 6:42:19returns a one
  11688. 6:42:20or a zero and we can actually change
  11689. 6:42:23that using the on value
  11690. 6:42:25and off value options so the on value
  11691. 6:42:28is what's going to be stored within our
  11692. 6:42:31variable
  11693. 6:42:32if it's toggled on so by default this is
  11694. 6:42:35one
  11695. 6:42:36and the off value is the opposite this
  11696. 6:42:38is what is stored within
  11697. 6:42:40our variable if this is toggled off
  11698. 6:42:43alright now let's have some fun with
  11699. 6:42:44this so let's associate a command with
  11700. 6:42:47this check button
  11701. 6:42:49command equals and let's create a
  11702. 6:42:51function called
  11703. 6:42:52maybe display and then let's define this
  11704. 6:42:55before the window def display and we're
  11705. 6:43:00going to check
  11706. 6:43:00the value of x to see if it's one
  11707. 6:43:04or zero so within our display function
  11708. 6:43:07we're going to check to see
  11709. 6:43:08if x and to get
  11710. 6:43:11the value of x we're going to use the
  11711. 6:43:14get function
  11712. 6:43:15so if x dot get is equal to one
  11713. 6:43:19that means that somebody clicked on the
  11714. 6:43:20check box or toggled it on
  11715. 6:43:23so if they check the check box
  11716. 6:43:26or check the check button then they
  11717. 6:43:29agree
  11718. 6:43:30so print you agree
  11719. 6:43:34if it's zero and we can do this with an
  11720. 6:43:36else statement
  11721. 6:43:37else print
  11722. 6:43:41you don't agree frowny face
  11723. 6:43:45and let's test this theory so we have
  11724. 6:43:48our
  11725. 6:43:49check button when we click on this it's
  11726. 6:43:51actually going to call
  11727. 6:43:52our display function i agree to
  11728. 6:43:54something and it says within our console
  11729. 6:43:56window
  11730. 6:43:57you agree and if i were to toggle this
  11731. 6:43:59off it says you don't agree
  11732. 6:44:02let's change the font of our check
  11733. 6:44:03button so we'll add that as an option
  11734. 6:44:05font equals and we can pass in a font
  11735. 6:44:08that we want
  11736. 6:44:09i'll say maybe ariel and then a size
  11737. 6:44:12of 20 and now this should be readable
  11738. 6:44:16i agree to something let's also color
  11739. 6:44:19this
  11740. 6:44:20because well i like coloring let's begin
  11741. 6:44:22with the foreground color
  11742. 6:44:24so that is fg for short fg equals
  11743. 6:44:27and then we can pass in a color name or
  11744. 6:44:30a hex value
  11745. 6:44:31i like hex values so i'm going to stick
  11746. 6:44:33with those i want a green
  11747. 6:44:35foreground color so that is zero zero f
  11748. 6:44:39zero zero but feel free to adjust these
  11749. 6:44:42values however you want
  11750. 6:44:44so our text should now be green that is
  11751. 6:44:47a
  11752. 6:44:47very bright green and it hurts my eyes
  11753. 6:44:49so i'm also going to change the
  11754. 6:44:51background
  11755. 6:44:51color which is bg and i'm going to set
  11756. 6:44:54this to
  11757. 6:44:54black so we'll have green text on a
  11758. 6:44:57black background
  11759. 6:44:59okay now when you click on this it
  11760. 6:45:01flashes that's kind of annoying
  11761. 6:45:03that's the active foreground and active
  11762. 6:45:06background
  11763. 6:45:07if we click on this or hold this down
  11764. 6:45:09the color scheme switches to
  11765. 6:45:11our active foreground slash background
  11766. 6:45:13and we can change those too
  11767. 6:45:15so i'm going to set those options active
  11768. 6:45:19foreground i'm going to set this the
  11769. 6:45:21same as my foreground color
  11770. 6:45:25and the active background will be the
  11771. 6:45:27same as
  11772. 6:45:28our normal background so that's active
  11773. 6:45:31background equals
  11774. 6:45:35black and this should no longer flash
  11775. 6:45:38when we click on it
  11776. 6:45:41not too bad not too bad let's add some
  11777. 6:45:43padding
  11778. 6:45:45pad x i'll set this to 25
  11779. 6:45:49and pad y i'll set this to
  11780. 6:45:5210. we have a little more padding around
  11781. 6:45:57our check button one feature that's
  11782. 6:45:59available to us
  11783. 6:46:00is that we can add an image next to our
  11784. 6:46:03check button
  11785. 6:46:04i downloaded an image of the python logo
  11786. 6:46:07that i want to add
  11787. 6:46:08so we'll use the image option to add a
  11788. 6:46:12photo image
  11789. 6:46:13to this check button but first we need
  11790. 6:46:15to create a
  11791. 6:46:16photo image from our image file so
  11792. 6:46:19within the window
  11793. 6:46:21let's call this python photo
  11794. 6:46:24but you'll probably use something
  11795. 6:46:25different for your project
  11796. 6:46:27python photo equals photo
  11797. 6:46:31image and within the constructor for our
  11798. 6:46:33photo image
  11799. 6:46:36we can set the file path or the file
  11800. 6:46:38name
  11801. 6:46:39since this image is within my project
  11802. 6:46:41folder i only have to list the file name
  11803. 6:46:44and this photo of mine is python.png
  11804. 6:46:49and now we set the image equal to
  11805. 6:46:52our photo image that we have image
  11806. 6:46:55equals
  11807. 6:46:56python photo but when we run this
  11808. 6:46:59it's going to overlap the text we still
  11809. 6:47:02have our image though
  11810. 6:47:04and this still works as intended so in
  11811. 6:47:06order to display
  11812. 6:47:08the image as well as the text we're
  11813. 6:47:11going to need to use the compound
  11814. 6:47:13option and let's add that at the end
  11815. 6:47:15compound
  11816. 6:47:17is where we're adding this photo
  11817. 6:47:19relative to the text
  11818. 6:47:21i want this photo on the left hand side
  11819. 6:47:23of the text
  11820. 6:47:24so i'm going to set this to left and now
  11821. 6:47:27when we run this
  11822. 6:47:28surprise surprise our image is on the
  11823. 6:47:30left hand side
  11824. 6:47:31one thing that you should know is that
  11825. 6:47:33with the on value and the off value data
  11826. 6:47:35type if you were to change this
  11827. 6:47:37to a non-integer value let's say we're
  11828. 6:47:40going to replace the on value with a
  11829. 6:47:42boolean value of true and the off value
  11830. 6:47:45is false with this variable make sure to
  11831. 6:47:49change the data type as well to reflect
  11832. 6:47:52what you're storing within
  11833. 6:47:53it so with our int var variable we want
  11834. 6:47:56to change this to a
  11835. 6:47:57boolean var value so replace int
  11836. 6:48:01with boolean and this will work much the
  11837. 6:48:04same
  11838. 6:48:04now with this display function we'll
  11839. 6:48:06need to account for that
  11840. 6:48:08so within the if statement here we'll
  11841. 6:48:10say if x
  11842. 6:48:11dot get we don't need to say is equal to
  11843. 6:48:14one
  11844. 6:48:15this will return true or false and this
  11845. 6:48:18will work
  11846. 6:48:19just the same as it did before and if
  11847. 6:48:21you replace this with a string
  11848. 6:48:23let's say the on value is
  11849. 6:48:26yes all caps and the off value is
  11850. 6:48:30no this is now a string bar
  11851. 6:48:34and we'll change our display function to
  11852. 6:48:36say if x
  11853. 6:48:38dot get is equal to yes
  11854. 6:48:43and this should work you agree you don't
  11855. 6:48:46agree you agree
  11856. 6:48:47you don't agree and i'll change that
  11857. 6:48:50back to one so
  11858. 6:48:51one and zero is the default for this but
  11859. 6:48:53you can edit these based on what you
  11860. 6:48:55need
  11861. 6:48:56all right so that's the basics of check
  11862. 6:48:58buttons in python if you want a copy of
  11863. 6:49:00all this code
  11864. 6:49:01i'll post all of this in the comments
  11865. 6:49:03down below but yeah that's the basics of
  11866. 6:49:05check buttons in python hey what's going
  11867. 6:49:08on everybody it's your bro here hope
  11868. 6:49:10you're doing well and in this video i'm
  11869. 6:49:11going to be teaching you guys how we can
  11870. 6:49:12create radio buttons in python so sit
  11871. 6:49:15back
  11872. 6:49:15relax and enjoy the show
  11873. 6:49:19okay everybody let's create some radio
  11874. 6:49:21buttons now these are similar to check
  11875. 6:49:23boxes but you can only select one from
  11876. 6:49:25any single grouping
  11877. 6:49:27so let's pretend that i'm buying you
  11878. 6:49:29lunch and you can only select
  11879. 6:49:30one item from a menu you can select
  11880. 6:49:33either a
  11881. 6:49:33slice of pizza a hamburger or a hot dog
  11882. 6:49:37so let's begin by creating a list i will
  11883. 6:49:39call this list
  11884. 6:49:40food food equals
  11885. 6:49:44pizza hamburger
  11886. 6:49:48hot dog and let's create a window
  11887. 6:49:52window equals tk followed by
  11888. 6:49:55window dot main loop
  11889. 6:49:59so to create a radio button we'll need a
  11890. 6:50:01name so i'll call this radio button
  11891. 6:50:04radio button equals radio button
  11892. 6:50:08and we're going to be adding this to our
  11893. 6:50:10window and we're going to put this
  11894. 6:50:12within
  11895. 6:50:12a for loop so we're going to iterate
  11896. 6:50:15through all of the items within our list
  11897. 6:50:17so let's create a for loop so we'll say
  11898. 6:50:20for
  11899. 6:50:22index in range
  11900. 6:50:26length of food
  11901. 6:50:30so this is going to iterate once through
  11902. 6:50:32all of the elements within our list
  11903. 6:50:35so when we run this it's going to create
  11904. 6:50:38three
  11905. 6:50:38radio buttons for us because we're
  11906. 6:50:40instantiating
  11907. 6:50:42one radio button for each item in the
  11908. 6:50:44list
  11909. 6:50:45now what i'm going to do is set the text
  11910. 6:50:48equal to
  11911. 6:50:51our list of food at a certain index
  11912. 6:50:55so when we first run this for loop it's
  11913. 6:50:57going to be zero for the index
  11914. 6:51:00then one and then two and then so on and
  11915. 6:51:03so forth for each item within our list
  11916. 6:51:06and then we just need to pack this so
  11917. 6:51:09radio
  11918. 6:51:10button dot pack
  11919. 6:51:14and then let's take a look at this
  11920. 6:51:17alright so here are all of the radio
  11921. 6:51:19buttons that we have
  11922. 6:51:20currently they're all selected though so
  11923. 6:51:22we'll need to link these within a single
  11924. 6:51:25grouping
  11925. 6:51:25the next thing we'll add is a variable
  11926. 6:51:28and we'll set this equal to
  11927. 6:51:29x and we'll need to declare this after
  11928. 6:51:31we create an instance of our window
  11929. 6:51:33so x equals int var
  11930. 6:51:37so this will hold an integer object
  11931. 6:51:40so these are all within the same
  11932. 6:51:42grouping
  11933. 6:51:43however if we try to run this again and
  11934. 6:51:45select one of these options
  11935. 6:51:47they all become selected so we're going
  11936. 6:51:50to need to give
  11937. 6:51:51each of these individual radio buttons
  11938. 6:51:53their own
  11939. 6:51:54value because right now they're all
  11940. 6:51:56sharing the same value
  11941. 6:51:57so we can add value equals
  11942. 6:52:01whatever the index is currently so the
  11943. 6:52:04first
  11944. 6:52:04item within our list of food is going to
  11945. 6:52:07have an
  11946. 6:52:07index value of zero then one
  11947. 6:52:11then two so let's try that again and you
  11948. 6:52:14can see by default it's
  11949. 6:52:15zero so pizza is already selected then
  11950. 6:52:18we can select either hamburger or hot
  11951. 6:52:20dog
  11952. 6:52:20but we can only select one so that's
  11953. 6:52:23kind of what we want then
  11954. 6:52:25so do you guys usually know how i call
  11955. 6:52:27the config function after each change
  11956. 6:52:29that i make for an object
  11957. 6:52:30well that's because i like to add
  11958. 6:52:32comments just for teaching purposes
  11959. 6:52:34for what each change does however it
  11960. 6:52:37might be easier for you instead of
  11961. 6:52:38calling the config function a bunch of
  11962. 6:52:40times
  11963. 6:52:41just to list all of the changes within
  11964. 6:52:43the parentheses when you create an
  11965. 6:52:45instance of the
  11966. 6:52:46object that you're working with however
  11967. 6:52:48just for teaching purposes i'm going to
  11968. 6:52:50put this all within
  11969. 6:52:52separate lines that allows me to add
  11970. 6:52:54some comments
  11971. 6:52:55so i think i'm going to start doing that
  11972. 6:52:56from now on uh so what this does
  11973. 6:52:59is that this adds text to
  11974. 6:53:02radio buttons and this
  11975. 6:53:06groups radio buttons together
  11976. 6:53:10if they share the same variable
  11977. 6:53:16so if you wanted a completely different
  11978. 6:53:18grouping of radio buttons you can give
  11979. 6:53:20them a different variable
  11980. 6:53:21like y and what this line does
  11981. 6:53:25is that this assigns
  11982. 6:53:28each radio button a
  11983. 6:53:32different value
  11984. 6:53:37all right so let's make some cosmetic
  11985. 6:53:39changes to our radio buttons
  11986. 6:53:42so right now they are centered let's
  11987. 6:53:45anchor these to the west
  11988. 6:53:47so we'll place this within the pack
  11989. 6:53:49function so
  11990. 6:53:50anchor equals w alternatively you can
  11991. 6:53:54just say capital w
  11992. 6:53:56without the quotes this would also work
  11993. 6:53:58too
  11994. 6:54:00so now they're all lined up let's add
  11995. 6:54:01some padding
  11996. 6:54:03so i'm going to add pad x
  11997. 6:54:06and i'll set this to 25
  11998. 6:54:10and this adds
  11999. 6:54:13padding on x
  12000. 6:54:17axis
  12001. 6:54:20let's also change the font
  12002. 6:54:23so we'll save font
  12003. 6:54:27equals maybe i'll pick impact for a font
  12004. 6:54:31style
  12005. 6:54:32and set the font size to 50
  12006. 6:54:36and now we can actually read these
  12007. 6:54:38because the font is large enough
  12008. 6:54:40now let's add some images so let's
  12009. 6:54:43create some photo images
  12010. 6:54:44you'll want to be sure to create these
  12011. 6:54:47after you create your instance of your
  12012. 6:54:49window
  12013. 6:54:50so let's create a pizza image first
  12014. 6:54:53pizza image
  12015. 6:54:54equals photo image
  12016. 6:54:58file equals the file path or the file
  12017. 6:55:01name
  12018. 6:55:01and my file is pizza.png and it kind of
  12019. 6:55:04looks like this
  12020. 6:55:05it's just the pizza emoji so
  12021. 6:55:10pizza.png
  12022. 6:55:12and then i'll do the same thing for
  12023. 6:55:14hamburger and hotdog
  12024. 6:55:16so this will be hamburger image
  12025. 6:55:20file equals hamburger.png
  12026. 6:55:25and then hotdog image
  12027. 6:55:35and this is what the other emojis look
  12028. 6:55:37like
  12029. 6:55:40and then let's create a list of the
  12030. 6:55:42photo images that we have
  12031. 6:55:44i'll call this food images
  12032. 6:55:47equals than the names of these images
  12033. 6:55:52pizza image hamburger image
  12034. 6:55:56and hot dog image
  12035. 6:56:01so then we can set the image for each
  12036. 6:56:03radio button
  12037. 6:56:05image equals
  12038. 6:56:08the name of the list food images
  12039. 6:56:11followed by
  12040. 6:56:12the index that we're currently on within
  12041. 6:56:15our for loop
  12042. 6:56:16so this adds image
  12043. 6:56:20to radio button and
  12044. 6:56:23let's test this cool here's our images
  12045. 6:56:27now if you want some images as well as
  12046. 6:56:30text you'll have to use compound
  12047. 6:56:32so let's add that next so i'm just going
  12048. 6:56:36to add a comma
  12049. 6:56:37followed by compound
  12050. 6:56:43equals let's say left so this will add
  12051. 6:56:46the image
  12052. 6:56:47to the left of the text
  12053. 6:56:52adds image and text i'll just say left
  12054. 6:56:56side
  12055. 6:56:59so this will display both an image and
  12056. 6:57:02text
  12057. 6:57:04so with these radio buttons you can
  12058. 6:57:06eliminate these
  12059. 6:57:07circle indicators and there's actually a
  12060. 6:57:10way to do that
  12061. 6:57:11so what we'll add if you want to get rid
  12062. 6:57:13of those
  12063. 6:57:14is indicator
  12064. 6:57:18on equals zero
  12065. 6:57:21so this will i should say eliminate
  12066. 6:57:27circle indicators
  12067. 6:57:32so it's going to change these to these
  12068. 6:57:34push buttons
  12069. 6:57:35however they are going to be of uneven
  12070. 6:57:38size you can set the width
  12071. 6:57:40if you want to so we'll do that
  12072. 6:57:43so we'll say width equals
  12073. 6:57:47what about 75 i'm not sure if that's a
  12074. 6:57:50good size or not
  12075. 6:57:52okay that's way too small let's try 375
  12076. 6:57:59so yeah you can have some push buttons
  12077. 6:58:00if you want to if you don't like the
  12078. 6:58:02circle indicators
  12079. 6:58:04so this sets width
  12080. 6:58:07of radio buttons
  12081. 6:58:11now let's actually set a function to
  12082. 6:58:14uh be called when we click one of these
  12083. 6:58:16buttons
  12084. 6:58:18so let's call this function maybe order
  12085. 6:58:20like we're ordering one of these items
  12086. 6:58:24so def we'll call this order like we're
  12087. 6:58:27ordering something
  12088. 6:58:29and what we'll do is just a few if
  12089. 6:58:31statements because i'm feeling lazy
  12090. 6:58:33so we'll just say if x that's
  12091. 6:58:37the value that's going to be stored
  12092. 6:58:40because each of these radio buttons is
  12093. 6:58:42grouped together by the variable x
  12094. 6:58:45if x dot get
  12095. 6:58:48is equal to zero that's our first index
  12096. 6:58:52for pizza what we'll display is
  12097. 6:58:57print you
  12098. 6:59:01oops you ordered
  12099. 6:59:04pizza then we'll just add
  12100. 6:59:08else if x dot get
  12101. 6:59:12is equal to one we will print
  12102. 6:59:17you ordered a hamburger
  12103. 6:59:25and then else if two
  12104. 6:59:30you ordered a hot dog
  12105. 6:59:37else
  12106. 6:59:40print huh
  12107. 6:59:44so there's probably a more efficient way
  12108. 6:59:46to write this however
  12109. 6:59:48i was just feeling lazy and this is
  12110. 6:59:49probably the best way to keep this
  12111. 6:59:51simple and easy to understand
  12112. 6:59:53so let's try this now oh however we need
  12113. 6:59:55to set the command
  12114. 6:59:57for our radio buttons so
  12115. 7:00:00command equals then the
  12116. 7:00:04function name which is order and make
  12117. 7:00:07sure you do not add the parentheses
  12118. 7:00:10so don't do this do that
  12119. 7:00:13and this will set
  12120. 7:00:16command of
  12121. 7:00:19radio button to function
  12122. 7:00:25all right let's test this so if we click
  12123. 7:00:28on pizza it says you ordered pizza
  12124. 7:00:30you ordered a hamburger and you ordered
  12125. 7:00:33a hot dog
  12126. 7:00:35okay everybody so that's the basics of
  12127. 7:00:37radio buttons if you would like a copy
  12128. 7:00:39of all this code i'll post all of this
  12129. 7:00:40in the comments down below
  12130. 7:00:42but yeah that's the basics of radio
  12131. 7:00:44buttons in python
  12132. 7:00:46hey what's going on everybody it's you
  12133. 7:00:48bro here hope you're doing well and in
  12134. 7:00:50this video i'm going to teach you guys
  12135. 7:00:51how we can create
  12136. 7:00:52a sliding scale in python so sit back
  12137. 7:00:55relax and enjoy the show
  12138. 7:00:59let's create a scale so we'll need a
  12139. 7:01:00window window equals tk
  12140. 7:01:03followed by window.mainloop we'll create
  12141. 7:01:06a scale
  12142. 7:01:07called scale scale equals scale
  12143. 7:01:10we need to add the scale to our window
  12144. 7:01:12so we'll put that here
  12145. 7:01:14we can list a from position and a 2
  12146. 7:01:17position
  12147. 7:01:18both of these values will be our range
  12148. 7:01:20of values that we can use
  12149. 7:01:22for our scale so from
  12150. 7:01:25sure to add an underscore after from it
  12151. 7:01:28won't work without this underscore
  12152. 7:01:30from equals let's say 0 and 2 equals 100
  12153. 7:01:34so we're going to create a scale for
  12154. 7:01:36temperature
  12155. 7:01:37and this will be in celsius so 0 will be
  12156. 7:01:41freezing and 100 will be the boiling
  12157. 7:01:43point uh you know for water
  12158. 7:01:45so then to display the scale we'll need
  12159. 7:01:48to add
  12160. 7:01:48scale.pack and let's run this
  12161. 7:01:52so the front position is zero so that's
  12162. 7:01:54at the top here and then if you go down
  12163. 7:01:56it increases so you can actually flip
  12164. 7:01:58this so we'll save from
  12165. 7:02:01102 will be zero
  12166. 7:02:04so now you can scroll up and the number
  12167. 7:02:06will increase
  12168. 7:02:08let's also get the current value so
  12169. 7:02:12we'll need a button
  12170. 7:02:14so button equals button
  12171. 7:02:18we'll add this to the window we'll add
  12172. 7:02:20some text
  12173. 7:02:22text equals submit and then a command
  12174. 7:02:26command equals submit but we'll have to
  12175. 7:02:29make this function
  12176. 7:02:31so let's do that outside the window def
  12177. 7:02:34submit and we will simply print the
  12178. 7:02:38current temperature
  12179. 7:02:39so let's display message the temperature
  12180. 7:02:43is plus
  12181. 7:02:47scale dot get
  12182. 7:02:51plus degrees
  12183. 7:02:55celsius we can just say degree c
  12184. 7:02:59so let's try this uh but we also have to
  12185. 7:03:01pack the button so button.pack
  12186. 7:03:07so if we were to try and submit this
  12187. 7:03:10right now scale.get is going to return
  12188. 7:03:12an integer
  12189. 7:03:13so we need to convert this to a string
  12190. 7:03:16so
  12191. 7:03:16str scale.get
  12192. 7:03:20and you have to do that with python if
  12193. 7:03:22you're displaying a
  12194. 7:03:23integer value along with another string
  12195. 7:03:26uh you need to convert it to a string
  12196. 7:03:29so now this should work so it is 82
  12197. 7:03:33degrees celsius
  12198. 7:03:35so let's do some cosmetic changes for
  12199. 7:03:37our temperature gauge
  12200. 7:03:39because right now it's kind of ugly
  12201. 7:03:43so the first thing we'll change is
  12202. 7:03:46let's say the length so length equals
  12203. 7:03:49600 so that should be fairly large
  12204. 7:03:56you can also set the orientation so that
  12205. 7:03:59is
  12206. 7:03:59orient spelt like that
  12207. 7:04:04so this can either be vertical which it
  12208. 7:04:06currently is
  12209. 7:04:09or horizontal
  12210. 7:04:15but i'll keep this as vertical
  12211. 7:04:19so i'll let a comment that this is
  12212. 7:04:22orientation
  12213. 7:04:23of scale let's also change the
  12214. 7:04:27font so font equals
  12215. 7:04:30pick whatever font you want
  12216. 7:04:34and a size so maybe 20.
  12217. 7:04:41now you can add some tick intervals
  12218. 7:04:46maybe i'll set this to 10. so these will
  12219. 7:04:50be
  12220. 7:04:50numeric indicators on the scale
  12221. 7:05:00and show value this hides the current
  12222. 7:05:04value
  12223. 7:05:04so if you set this to zero this will
  12224. 7:05:06hide
  12225. 7:05:07current value
  12226. 7:05:12so it no longer displays what the
  12227. 7:05:14current value is next to the slider
  12228. 7:05:17to actually retrieve it you'd have to
  12229. 7:05:19get the value
  12230. 7:05:20so right now it's 40 but it helps that
  12231. 7:05:22you have the tick intervals on the
  12232. 7:05:24left-hand side
  12233. 7:05:26i should probably add that this adds
  12234. 7:05:31uh numeric
  12235. 7:05:34indicators for
  12236. 7:05:38value good enough let's continue on
  12237. 7:05:41now let's use the set function we can
  12238. 7:05:44set the current value of the knob
  12239. 7:05:46on our scale and by default it is zero
  12240. 7:05:49so we can use the scale
  12241. 7:05:50dot set function and we can place a
  12242. 7:05:53number in here
  12243. 7:05:54a value so let's say we want this to be
  12244. 7:05:57100
  12245. 7:05:58so this is now at the 100 position
  12246. 7:06:00instead of zero
  12247. 7:06:01if we wanted this in the middle we could
  12248. 7:06:03set this to 50
  12249. 7:06:05and now it's in the middle however if
  12250. 7:06:07you change the
  12251. 7:06:09range of your skill of from minus 2
  12252. 7:06:12and set this to 50 now 50 is all the way
  12253. 7:06:15down here so it no longer appears in the
  12254. 7:06:17middle
  12255. 7:06:18so this isn't really necessary but if
  12256. 7:06:20you want this to be a little more
  12257. 7:06:21sophisticated
  12258. 7:06:22there's actually a formula that you
  12259. 7:06:23could use we can use scale
  12260. 7:06:26straight braces and then place the word
  12261. 7:06:28from
  12262. 7:06:29within here this will give us our max
  12263. 7:06:31basically
  12264. 7:06:32uh so this is from without an underscore
  12265. 7:06:35so this not this this
  12266. 7:06:40scale from minus scale
  12267. 7:06:43two and then we're going to divide this
  12268. 7:06:47by two
  12269. 7:06:48and then i'm just going to put this
  12270. 7:06:50within a extra set of parentheses
  12271. 7:06:53plus scale two just in case
  12272. 7:06:57our two value is greater than zero
  12273. 7:07:01we just want to account for that and
  12274. 7:07:03then i'm just going to
  12275. 7:07:05put these within another set of
  12276. 7:07:06parentheses so
  12277. 7:07:08this should always appear in the middle
  12278. 7:07:10then even if we were to change this
  12279. 7:07:14range
  12280. 7:07:16yeah it seems like it works uh like i
  12281. 7:07:18said it's not really necessary but
  12282. 7:07:20if you want this to be a little more
  12283. 7:07:21sophisticated that would work
  12284. 7:07:23now let's make some cosmetic changes to
  12285. 7:07:25this scale and make it look pretty
  12286. 7:07:28so first let's change the trough color
  12287. 7:07:30this is this gray portion right here
  12288. 7:07:35so let's set the trough color
  12289. 7:07:39equals whatever color you want you could
  12290. 7:07:42say like blue
  12291. 7:07:43or you can place a hex value here and i
  12292. 7:07:45actually picked one out already
  12293. 7:07:47so i'm going to use 6 9 e
  12294. 7:07:50a f f and that is this icy blue color
  12295. 7:07:54kind of like this is sliding on ice
  12296. 7:07:58and i'm also going to change the font
  12297. 7:08:00color
  12298. 7:08:03and i'm going to pick this fiery red
  12299. 7:08:06color
  12300. 7:08:07to symbolize heat
  12301. 7:08:12and then you can change the background
  12302. 7:08:14color too
  12303. 7:08:16so that is bg and i'll just set this to
  12304. 7:08:20like black
  12305. 7:08:24it's looking kind of cool now right and
  12306. 7:08:27for no good reason i'm going to add some
  12307. 7:08:29pictures
  12308. 7:08:31so feel free to pause the video if you'd
  12309. 7:08:32like to download some images i'm going
  12310. 7:08:34to use an image of a flame
  12311. 7:08:36for the hot side of our scale and a
  12312. 7:08:39snowflake
  12313. 7:08:40for the cold side so let's begin by
  12314. 7:08:43adding the hot image uh to the scale
  12315. 7:08:46before we actually
  12316. 7:08:48add the scale to the window so we'll do
  12317. 7:08:50this after we instantiate the window
  12318. 7:08:52because that's the only place that we
  12319. 7:08:53can do that
  12320. 7:08:55so we're going to create a image i'll
  12321. 7:08:56just call this hot image
  12322. 7:08:58for the fire icon so hot image equals
  12323. 7:09:01photo image file
  12324. 7:09:04equals this is hot.png so
  12325. 7:09:08hot.png we're going to add the image to
  12326. 7:09:12a label
  12327. 7:09:13i'll call this hot label hot label
  12328. 7:09:17label image equals
  12329. 7:09:20hot image and then we need to pack this
  12330. 7:09:24so hot label dot pack
  12331. 7:09:29and this should appear at the top let's
  12332. 7:09:31test it
  12333. 7:09:32yep there it is let's do the same thing
  12334. 7:09:35for the cold side
  12335. 7:09:37so i'll add this here before the button
  12336. 7:09:41and i'm going to replace hot with cold
  12337. 7:09:44same thing with the file name
  12338. 7:09:45cold.png
  12339. 7:09:49this will be cold label cold
  12340. 7:09:52image and cold label dot pack
  12341. 7:09:57and that should be it let's take a look
  12342. 7:09:59not too shabby it's looking pretty sweet
  12343. 7:10:02it has our submit button at the bottom
  12344. 7:10:03too uh so
  12345. 7:10:05right now the temperature is 100 degrees
  12346. 7:10:07celsius
  12347. 7:10:09the temperature is 0 degrees celsius the
  12348. 7:10:11temperature is 60 degrees celsius
  12349. 7:10:14well that's how to create a scale in
  12350. 7:10:16python if you would like a copy of all
  12351. 7:10:17this code i'll post all of this in the
  12352. 7:10:19comments down below
  12353. 7:10:20but yeah that's how to create a scale in
  12354. 7:10:22python
  12355. 7:10:24hey what's going on everybody it's your
  12356. 7:10:26bro here hope you're doing well and in
  12357. 7:10:27this video i'm going to teach you guys
  12358. 7:10:29how we can create a list box in python
  12359. 7:10:31so
  12360. 7:10:32sit back relax and enjoy the show
  12361. 7:10:36all right you wonderful people let's
  12362. 7:10:38create a list box a list box
  12363. 7:10:40is a listing of selectable text items
  12364. 7:10:43within its own container
  12365. 7:10:44so as always we'll need a window window
  12366. 7:10:47equals tk
  12367. 7:10:48followed by window dot main loop let's
  12368. 7:10:51create a list box
  12369. 7:10:52simply called list box big surprise
  12370. 7:10:55there right
  12371. 7:10:55so list box equals list box what are we
  12372. 7:10:59adding this list box to
  12373. 7:11:00we're going to place that here within
  12374. 7:11:01the parentheses of the constructor
  12375. 7:11:03we're adding this to our window and then
  12376. 7:11:05we're going to follow this with
  12377. 7:11:07listbox.pack you can also do
  12378. 7:11:09listbox.place as well and place some
  12379. 7:11:11coordinates but
  12380. 7:11:12we've always done pack let's just stick
  12381. 7:11:14with that for now all right so this is
  12382. 7:11:16what our list box looks like
  12383. 7:11:18it's a small rectangular box and we're
  12384. 7:11:20going to add some text items that are
  12385. 7:11:22selectable
  12386. 7:11:23and eventually we'll be able to submit a
  12387. 7:11:25choice so i have an idea
  12388. 7:11:27let's create an online menu like we're
  12389. 7:11:30ordering food from a restaurant
  12390. 7:11:32let's say it's bros italian restaurant
  12391. 7:11:34so let's add some food items to our menu
  12392. 7:11:36and somebody can submit an order so
  12393. 7:11:39we'll need to use the
  12394. 7:11:40insert function of the list box to add
  12395. 7:11:42some items so
  12396. 7:11:43list box dot insert we place
  12397. 7:11:47an index and a text item so let's say
  12398. 7:11:50for
  12399. 7:11:51index one we'll have pizza
  12400. 7:11:54and this is what we end up with we have
  12401. 7:11:56the item pizza
  12402. 7:11:58it's selectable and well that's really
  12403. 7:12:00it we'll need to create a submit button
  12404. 7:12:02and populate the rest of this list box
  12405. 7:12:04so let's add a few other items for our
  12406. 7:12:06menu so let's say we'll have five items
  12407. 7:12:10and we need an index for each so one two
  12408. 7:12:13three four and five so we have
  12409. 7:12:16pizza for the first item for the second
  12410. 7:12:18item let's have pasta
  12411. 7:12:20and third let's say garlic bread because
  12412. 7:12:23garlic bread
  12413. 7:12:24is awesome and restaurants usually have
  12414. 7:12:26appetizers so why not soup and salad
  12415. 7:12:30and that should be enough for this
  12416. 7:12:31example
  12417. 7:12:33and this is what we have we have our
  12418. 7:12:36menu and we can select
  12419. 7:12:37an item from this menu so let's
  12420. 7:12:40customize the appearance of this menu
  12421. 7:12:42well because we can so let's do that
  12422. 7:12:45let's change the background color so we
  12423. 7:12:46do that with the option
  12424. 7:12:48bg for background and you can set this
  12425. 7:12:50equal to a color of your choice
  12426. 7:12:52or a hex value so i have a hex color
  12427. 7:12:55value already picked out
  12428. 7:12:57it is f7fde and this has the appearance
  12429. 7:13:01of i would say
  12430. 7:13:02paper like it's kind of faded i think
  12431. 7:13:04that would fit some sort
  12432. 7:13:06of menu for a restaurant but pick
  12433. 7:13:08whatever background color you want
  12434. 7:13:10so let's change the font next so we can
  12435. 7:13:13do that with the option font
  12436. 7:13:16and i will pick the font
  12437. 7:13:22it's somewhat fancy i think it would fit
  12438. 7:13:24like a restaurant menu
  12439. 7:13:26and then a size of 35.
  12440. 7:13:29yeah we're getting somewhere we can also
  12441. 7:13:32change the width and the height
  12442. 7:13:35so width equals let's say 12.
  12443. 7:13:39that's not too bad for a width let's
  12444. 7:13:41also change the height
  12445. 7:13:43so we can do that with a function
  12446. 7:13:46of our list box so let's get the current
  12447. 7:13:49size of the list box
  12448. 7:13:50and let's actually do that after we
  12449. 7:13:53insert all of the items within our list
  12450. 7:13:55box
  12451. 7:13:55so to change the size of our list box
  12452. 7:13:58what we do
  12453. 7:13:59is type in list box
  12454. 7:14:02dot config you usually use config if you
  12455. 7:14:05need to change any uh
  12456. 7:14:06options and we want to change the
  12457. 7:14:09length of or the height of this list box
  12458. 7:14:13so we're going to say height equals
  12459. 7:14:17list box dot
  12460. 7:14:20size function so then this will adjust
  12461. 7:14:24the size
  12462. 7:14:25of our list box dynamically even if we
  12463. 7:14:28were to remove an item
  12464. 7:14:30then the size of our list box is going
  12465. 7:14:32to shrink
  12466. 7:14:34just like that but let's keep salad in
  12467. 7:14:37there for now because we need salad
  12468. 7:14:40all right so how do we actually submit
  12469. 7:14:43one of these options we'll need to
  12470. 7:14:44create a submit button
  12471. 7:14:46so let's do that after we create our
  12472. 7:14:48list box so near the bottom
  12473. 7:14:50we'll call this submit button
  12474. 7:14:53submit button equals button we're adding
  12475. 7:14:56this to our window
  12476. 7:14:58let's set the text to submit
  12477. 7:15:01and we'll need a command so we'll create
  12478. 7:15:03a function called
  12479. 7:15:05submit and then we need to pack
  12480. 7:15:08this submit button
  12481. 7:15:11submit button.pack now at the top of
  12482. 7:15:14this program
  12483. 7:15:15let's create a submit function
  12484. 7:15:19so at the top def submit
  12485. 7:15:24so to retrieve or get the current
  12486. 7:15:27selected item
  12487. 7:15:28of a list box there is a certain
  12488. 7:15:30function that you can use
  12489. 7:15:31so we'll begin by saying
  12490. 7:15:35list box dot get
  12491. 7:15:38what are we getting we want to get
  12492. 7:15:39anything that is currently selected and
  12493. 7:15:41there is a function
  12494. 7:15:43for that so what we're going to do
  12495. 7:15:45within the parentheses of the get
  12496. 7:15:47function is type in
  12497. 7:15:49listbox dot current selection
  12498. 7:15:54it's shortened to cur selection all
  12499. 7:15:57right and then we can either store this
  12500. 7:15:59within a variable or we can just print
  12501. 7:16:01this but i think we'll just print this
  12502. 7:16:02to keep it simple
  12503. 7:16:06now when we select an item and
  12504. 7:16:09click on the submit button it's going to
  12505. 7:16:11submit our order
  12506. 7:16:12actually better yet let's actually uh
  12507. 7:16:16print you have
  12508. 7:16:20ordered whatever item that
  12509. 7:16:25we select so garlic bread you have
  12510. 7:16:27ordered garlic bread
  12511. 7:16:29cool what we're going to work on now is
  12512. 7:16:32a way to submit
  12513. 7:16:33or insert an item to our list box after
  12514. 7:16:36this program is compiled and already
  12515. 7:16:38running
  12516. 7:16:39so we'll create an entry box within our
  12517. 7:16:42window
  12518. 7:16:42where we can submit a custom item to
  12519. 7:16:44this menu
  12520. 7:16:45so let's create an entry box and let's
  12521. 7:16:49do that before the submit button that
  12522. 7:16:50would be a good spot for this
  12523. 7:16:52so let's call this entry box where
  12524. 7:16:54somebody can
  12525. 7:16:55type in something to add to the menu so
  12526. 7:16:58entry box equals entry and we're adding
  12527. 7:17:01this
  12528. 7:17:02to our window and then entry box
  12529. 7:17:05dot pack then we're going to create a
  12530. 7:17:09button
  12531. 7:17:10called add and i'm just going to copy
  12532. 7:17:12everything from the
  12533. 7:17:13submit button and we'll change submit to
  12534. 7:17:16add
  12535. 7:17:17we're going to add an item to the menu
  12536. 7:17:20so we'll change the text
  12537. 7:17:21to add and command add
  12538. 7:17:24add button dot pack then we're going to
  12539. 7:17:28create
  12540. 7:17:29a function called add and we'll do that
  12541. 7:17:31at the top
  12542. 7:17:32def add parentheses
  12543. 7:17:35colon so to add an item to the list box
  12544. 7:17:39first we need to
  12545. 7:17:40get what is currently in the entry box
  12546. 7:17:43so we can do that by typing in
  12547. 7:17:47list box dot insert
  12548. 7:17:50then within the parentheses we need to
  12549. 7:17:52get
  12550. 7:17:53an index number as well as
  12551. 7:17:56a text item so for the number what we
  12552. 7:17:59can type here
  12553. 7:18:00is the listbox dot size
  12554. 7:18:03so this will give us the current
  12555. 7:18:05position of the
  12556. 7:18:07index that we're currently on for the
  12557. 7:18:08list box and then we need to get the
  12558. 7:18:11text
  12559. 7:18:12in the entry box so we type in the name
  12560. 7:18:14of the entry box
  12561. 7:18:15entry box dot get
  12562. 7:18:19and this will insert a new item within
  12563. 7:18:22our menu our list box so let's try it
  12564. 7:18:24again
  12565. 7:18:27so we can type in an item let's say soda
  12566. 7:18:29we want to add soda to the menu
  12567. 7:18:31and then we click add all right there's
  12568. 7:18:34our soda but we have to scroll down to
  12569. 7:18:36see it
  12570. 7:18:37so let's change the size of our list box
  12571. 7:18:41so there's actually a line of code that
  12572. 7:18:43we did that already
  12573. 7:18:44listbox.config height equals listbox
  12574. 7:18:47size so let's add that within the add
  12575. 7:18:50function at the very end
  12576. 7:18:52so then the height of our list box will
  12577. 7:18:55adjust
  12578. 7:18:56so currently there's no soda within our
  12579. 7:18:57menu let's type in soda
  12580. 7:18:59or some other item click add and then
  12581. 7:19:02that item will appear and the size of
  12582. 7:19:04the list box will change to accommodate
  12583. 7:19:06any new items
  12584. 7:19:07let's say now that we serve sushi we're
  12585. 7:19:10now an italian
  12586. 7:19:11and japanese restaurant now we have
  12587. 7:19:14sushi on the menu
  12588. 7:19:15you know what why stop there let's
  12589. 7:19:17create a delete button to delete an item
  12590. 7:19:19from this menu
  12591. 7:19:21so we'll create a delete button
  12592. 7:19:24i'll just copy everything for add and
  12593. 7:19:26change this to
  12594. 7:19:27delete so delete button text equals
  12595. 7:19:30delete
  12596. 7:19:32command equals delete as well and
  12597. 7:19:35delete button dot pack and then at the
  12598. 7:19:38very top we'll create a delete
  12599. 7:19:40function so we'll do that here
  12600. 7:19:44d e f delete
  12601. 7:19:48and to delete an item from a list box
  12602. 7:19:51there's actually a function to do that
  12603. 7:19:52so
  12604. 7:19:53list box dot delete
  12605. 7:19:56and then within the parenthesis of the
  12606. 7:19:58delete function we can pass
  12607. 7:20:00in list box dot current selection or
  12608. 7:20:04just
  12609. 7:20:04cur selection for short and then we want
  12610. 7:20:07to
  12611. 7:20:08readjust the height of this list box
  12612. 7:20:10after we delete an item
  12613. 7:20:12so we'll just add listbox.config height
  12614. 7:20:15equals listbox.size because the size
  12615. 7:20:18of all the items within the list box is
  12616. 7:20:20going to shrink after we delete an item
  12617. 7:20:23so let's try this so here's our menu
  12618. 7:20:26let's say we no longer want salad
  12619. 7:20:28because salad is healthy well we can
  12620. 7:20:30delete it
  12621. 7:20:31same thing with soup and you know what
  12622. 7:20:33let's also delete the
  12623. 7:20:34pasta and the pizza so we're only left
  12624. 7:20:37with sweet sweet garlic bread
  12625. 7:20:39well ladies and gentlemen we're going to
  12626. 7:20:40get into some more advanced stuff with
  12627. 7:20:42list boxes
  12628. 7:20:43what if you want to select more than one
  12629. 7:20:45item from this list box
  12630. 7:20:47we're going to need to change a few
  12631. 7:20:48things so when we create our list box
  12632. 7:20:51there is a select mode select mode
  12633. 7:20:54and we are going to set this equal to
  12634. 7:20:56multiple
  12635. 7:20:57so now we can actually select multiple
  12636. 7:21:00items
  12637. 7:21:01from this list box like i want to select
  12638. 7:21:03one of everything
  12639. 7:21:04but what happens when we submit this
  12640. 7:21:07well we're going to run into some
  12641. 7:21:08problems so we're going to need to
  12642. 7:21:09change this program around
  12643. 7:21:11so let's begin with the submit option so
  12644. 7:21:14what we're going to do within the submit
  12645. 7:21:16option
  12646. 7:21:16is first get rid of this line let's say
  12647. 7:21:20we
  12648. 7:21:20no longer need this anymore uh and then
  12649. 7:21:23before we print out the items that we
  12650. 7:21:26ordered
  12651. 7:21:26let's create a list called food
  12652. 7:21:30and we'll just leave this empty for now
  12653. 7:21:31we'll fill this later
  12654. 7:21:33so we'll create a for loop for index
  12655. 7:21:37in list box
  12656. 7:21:40dot current selection
  12657. 7:21:44what we're going to do after each
  12658. 7:21:46iteration
  12659. 7:21:48food dot insert
  12660. 7:21:52our index number as well as
  12661. 7:21:55listbox dot get
  12662. 7:21:59the item name at this index what this
  12663. 7:22:02for loop is doing is that it's going to
  12664. 7:22:04iterate once for each item that we
  12665. 7:22:07select
  12666. 7:22:08so since we have three items that are
  12667. 7:22:10currently selected this is going to
  12668. 7:22:12iterate three times
  12669. 7:22:13get the index number as well as the item
  12670. 7:22:15at that index number
  12671. 7:22:17and now we just need to display the
  12672. 7:22:18contents of what we have
  12673. 7:22:20within our list of food so we can do
  12674. 7:22:23that with
  12675. 7:22:23another for loop so let's add that after
  12676. 7:22:26you have ordered
  12677. 7:22:27then for
  12678. 7:22:30index in our list
  12679. 7:22:34food all we'll do is print
  12680. 7:22:39index so this will print out everything
  12681. 7:22:42that we ordered
  12682. 7:22:43so let's say that we want one of
  12683. 7:22:45everything and submit this
  12684. 7:22:46you have ordered pizza pasta garlic
  12685. 7:22:49bread soup and salad
  12686. 7:22:51now what happens if we try to delete
  12687. 7:22:53multiple items
  12688. 7:22:54so let's say we want to delete soup and
  12689. 7:22:56salad and we click delete
  12690. 7:22:58well we're going to run into an error
  12691. 7:23:00because our function is not set up to
  12692. 7:23:02accommodate for
  12693. 7:23:03multiple items so we'll need to change a
  12694. 7:23:05few things around so let's get rid of
  12695. 7:23:07this line for now
  12696. 7:23:08list box dot delete list box dot current
  12697. 7:23:10selection
  12698. 7:23:11and we're going to place a for loop here
  12699. 7:23:13so for
  12700. 7:23:14index in list box
  12701. 7:23:18dot current selection then after each
  12702. 7:23:21iteration
  12703. 7:23:22what we're going to do is use list box
  12704. 7:23:25dot
  12705. 7:23:26delete and we're deleting the item at
  12706. 7:23:29whatever index we're currently on so you
  12707. 7:23:32would think this would work
  12708. 7:23:33at first glance let's say we want to
  12709. 7:23:36delete pizza and pasta
  12710. 7:23:38we're going to click delete so pizza was
  12711. 7:23:40deleted but not
  12712. 7:23:41pasta that's because the indexes are
  12713. 7:23:44changing
  12714. 7:23:45after we delete an item so when we use
  12715. 7:23:48this for loop
  12716. 7:23:49we should actually reverse this so with
  12717. 7:23:53listbox.currentselection
  12718. 7:23:54we are going to enclose this with
  12719. 7:23:56reversed
  12720. 7:23:59so then we're going to start at the last
  12721. 7:24:01index and work our way to zero
  12722. 7:24:03in that order so now if we want to
  12723. 7:24:06delete
  12724. 7:24:06pizza and pasta i don't know why we
  12725. 7:24:09would because
  12726. 7:24:10they're amazing and we click delete it
  12727. 7:24:12deletes
  12728. 7:24:13pizza and pasta but we still have garlic
  12729. 7:24:16bread soup and salad then we can add an
  12730. 7:24:18item
  12731. 7:24:19let's say we want to add sushi against
  12732. 7:24:21the menu we can do that
  12733. 7:24:22and then we can order one of everything
  12734. 7:24:24because we're really hungry
  12735. 7:24:26you have ordered garlic bread soup salad
  12736. 7:24:28and sushi
  12737. 7:24:29so that's the basics of creating a list
  12738. 7:24:32box in python if you want a copy of all
  12739. 7:24:34this code
  12740. 7:24:35i'll post all of this in the comments
  12741. 7:24:36down below but yeah that's the basics
  12742. 7:24:38of list boxes in python
  12743. 7:24:41hey what's going on everybody it's your
  12744. 7:24:43bro here hope you're doing well and in
  12745. 7:24:45this video i'm going to teach you guys
  12746. 7:24:46how we can create
  12747. 7:24:47message boxes in python so sit back
  12748. 7:24:49relax and enjoy the show
  12749. 7:24:54okay people let's create some message
  12750. 7:24:56boxes but first we'll need an import so
  12751. 7:24:58from
  12752. 7:24:59tk enter import
  12753. 7:25:02message box and what this will do
  12754. 7:25:06is import the message box
  12755. 7:25:09library so it's not necessary to add
  12756. 7:25:12this comment uh so we'll need a window
  12757. 7:25:15window equals tk followed by window
  12758. 7:25:18dot main loop let's create a button
  12759. 7:25:22so button equals button we're going to
  12760. 7:25:26add this to the window
  12761. 7:25:27we're going to set the command to
  12762. 7:25:29execute a
  12763. 7:25:30click function that will have a message
  12764. 7:25:33box appear when we click on the button
  12765. 7:25:35and let's set the text so text equals
  12766. 7:25:39click me and then we will need to pack
  12767. 7:25:43this button
  12768. 7:25:43so button.pack now we'll need to create
  12769. 7:25:46a click function
  12770. 7:25:47so let's do that at the top def
  12771. 7:25:50click and what do we want to do
  12772. 7:25:54so there's a few different uh message
  12773. 7:25:56dialog boxes that you can use
  12774. 7:25:58so let's begin with a basic show info
  12775. 7:26:01message box
  12776. 7:26:02so message box dot and there's a few
  12777. 7:26:05options
  12778. 7:26:06let's begin with show info show
  12779. 7:26:09info this just displays a simple message
  12780. 7:26:12so if we were to run this we have a
  12781. 7:26:15button we click on the button and it's
  12782. 7:26:16going to create a message box for us
  12783. 7:26:19and it just shows some info but we don't
  12784. 7:26:21really have any info to show yet
  12785. 7:26:23so we can set a title and we can set a
  12786. 7:26:26message
  12787. 7:26:26so let's set the title title equals
  12788. 7:26:31this is an info
  12789. 7:26:34message box
  12790. 7:26:39although part of the title got cut off
  12791. 7:26:42let's also set the message message
  12792. 7:26:45equals
  12793. 7:26:47you are a person
  12794. 7:26:53click me you are a person thanks i
  12795. 7:26:56didn't know that so there's a few other
  12796. 7:26:59message boxes that we can use too
  12797. 7:27:01so for now i'm just going to turn this
  12798. 7:27:03into a comment and i'll just copy this
  12799. 7:27:05just to save some time
  12800. 7:27:09and the next one that we have is
  12801. 7:27:12show warning
  12802. 7:27:15so this will have a different icon so
  12803. 7:27:18the title
  12804. 7:27:19let's say warning and the message
  12805. 7:27:24you have a virus
  12806. 7:27:31so now we get this annoying warning
  12807. 7:27:33message but you can just click ok to get
  12808. 7:27:35rid of it
  12809. 7:27:36so one thing that you might see with
  12810. 7:27:38tech support scams
  12811. 7:27:39they'll have a message box like this
  12812. 7:27:42that shows a warning
  12813. 7:27:43but they'll put this within a while loop
  12814. 7:27:45that's set to true
  12815. 7:27:47so this will just continue on and on
  12816. 7:27:52kind of like this so if we were to try
  12817. 7:27:55and close out of this
  12818. 7:27:56it's just going to reappear while
  12819. 7:28:00true is true basically so that's just
  12820. 7:28:03something funny that i wanted to show
  12821. 7:28:05you
  12822. 7:28:05so let's move on i'm going to turn this
  12823. 7:28:07into a comment
  12824. 7:28:10then the next one we have is show error
  12825. 7:28:17so show error
  12826. 7:28:21title set it to whatever you want
  12827. 7:28:24error message
  12828. 7:28:27something went wrong
  12829. 7:28:36there's a few more sophisticated ones
  12830. 7:28:38too we can ask for some user input
  12831. 7:28:42so this next one is message box
  12832. 7:28:45dot ask and there's a few to choose from
  12833. 7:28:49let's go with ask okay cancel first
  12834. 7:28:53we'll set the title title equals
  12835. 7:28:56ask okay cancel
  12836. 7:29:00and a message message
  12837. 7:29:05do you want to
  12838. 7:29:08do the thing
  12839. 7:29:15so we can actually put this within an if
  12840. 7:29:18statement
  12841. 7:29:19so this returns either true or false
  12842. 7:29:21depending on what you click
  12843. 7:29:23so let's put this within an if statement
  12844. 7:29:26if this is true
  12845. 7:29:28print you did
  12846. 7:29:31a thing
  12847. 7:29:34if it returns false we can use an else
  12848. 7:29:36statement
  12849. 7:29:38else what else do we want to do we'll
  12850. 7:29:40say else
  12851. 7:29:42print you
  12852. 7:29:45cancelled a thing
  12853. 7:29:49and let's test this
  12854. 7:29:56but i am forgetting a colon there
  12855. 7:30:01do you want to do the thing okay you did
  12856. 7:30:04a thing
  12857. 7:30:04let's try it again do you want to do the
  12858. 7:30:07thing
  12859. 7:30:08cancel you cancelled a thing cool
  12860. 7:30:11there's a few others
  12861. 7:30:12so i'll turn all of these into comments
  12862. 7:30:17and the next one we can go over is ask
  12863. 7:30:20retry cancel
  12864. 7:30:22and this is fairly similar to what we
  12865. 7:30:23had before i'm going to copy this
  12866. 7:30:28let's change this to ask
  12867. 7:30:32retry cancel ask retry
  12868. 7:30:36cancel do you want to
  12869. 7:30:40retry the thing
  12870. 7:30:44you retried a thing
  12871. 7:30:48or you cancelled a thing so it's very
  12872. 7:30:51similar to what we had before
  12873. 7:30:52do you want to retry the thing retry you
  12874. 7:30:55retried a thing
  12875. 7:30:58and cancel you cancelled a thing
  12876. 7:31:03okay the next one we have is ask yes
  12877. 7:31:06no so we can get some user input
  12878. 7:31:11if it's a yes or a no
  12879. 7:31:14so this returns a boolean value a true
  12880. 7:31:17or false
  12881. 7:31:18value so message box
  12882. 7:31:21dot ask
  12883. 7:31:24yes no
  12884. 7:31:28and we'll set the title title equals
  12885. 7:31:31ask yes or no
  12886. 7:31:35and a message
  12887. 7:31:40do you like
  12888. 7:31:44cake
  12889. 7:31:47so let's put this within an if statement
  12890. 7:31:50because this returns
  12891. 7:31:51a true or false answer print
  12892. 7:31:57i like cake too
  12893. 7:32:01else they answered no then if this is
  12894. 7:32:05false
  12895. 7:32:06print why do you
  12896. 7:32:09not like cake
  12897. 7:32:15and i'm missing a colon there we go
  12898. 7:32:19so if they click yes this returns true
  12899. 7:32:22and we execute this line of code i like
  12900. 7:32:25cake
  12901. 7:32:25too if they answer no it's going to
  12902. 7:32:28execute this
  12903. 7:32:29else statement do you like cake no why
  12904. 7:32:32do you not like
  12905. 7:32:33cake now what we have next is ask
  12906. 7:32:36question and this doesn't return a
  12907. 7:32:38boolean value of true or false
  12908. 7:32:40it actually returns a string of yes or
  12909. 7:32:43no
  12910. 7:32:43so this is message box
  12911. 7:32:47dot ask question
  12912. 7:32:50and we'll set the title to ask
  12913. 7:32:54question and a message
  12914. 7:32:57message equals do
  12915. 7:33:00you like pi
  12916. 7:33:05so let's just print the result what this
  12917. 7:33:08returns
  12918. 7:33:09so print everything within here
  12919. 7:33:13so like i said this doesn't return it
  12920. 7:33:15true or false
  12921. 7:33:16but yes or no
  12922. 7:33:20so what we could do is assign the
  12923. 7:33:23response to
  12924. 7:33:24a variable such as answer
  12925. 7:33:27so answer equals whatever the answer is
  12926. 7:33:30whatever they
  12927. 7:33:31click whatever button they click on so
  12928. 7:33:33if
  12929. 7:33:34our answer is equal to yes
  12930. 7:33:38will print a message print
  12931. 7:33:42i like pie 2.
  12932. 7:33:47then else the answer is no
  12933. 7:33:51will print why do you
  12934. 7:33:54not like pie
  12935. 7:34:00so do you like pie yes i like pie too do
  12936. 7:34:03you like pie
  12937. 7:34:04no why do you not like pie
  12938. 7:34:07so this next one is probably the most
  12939. 7:34:10difficult
  12940. 7:34:10it's ask yes no cancel
  12941. 7:34:15so it's message box dot
  12942. 7:34:18ask yes no cancel
  12943. 7:34:21it's this last one so let's set the
  12944. 7:34:23title
  12945. 7:34:24title equals whatever you want yes
  12946. 7:34:28no cancel and a message message
  12947. 7:34:35equals do you
  12948. 7:34:38like to code
  12949. 7:34:42so let's just print the results what
  12950. 7:34:45this returns
  12951. 7:34:46so print everything just so we can take
  12952. 7:34:50a look
  12953. 7:34:51so the possible answers are
  12954. 7:34:55true false
  12955. 7:34:58or none so we can just run some if
  12956. 7:35:01statements for this
  12957. 7:35:04so if
  12958. 7:35:07answer is equal to true
  12959. 7:35:11what we can do is print
  12960. 7:35:16you like to code
  12961. 7:35:20oh but we need to assign this to an
  12962. 7:35:22answer so answer
  12963. 7:35:23equals all of this
  12964. 7:35:29else if
  12965. 7:35:33answer is equal to false
  12966. 7:35:37let's print then
  12967. 7:35:41why are you watching a video
  12968. 7:35:44on coding
  12969. 7:35:48and then else the response is none so we
  12970. 7:35:51can just use an
  12971. 7:35:52else statement so else
  12972. 7:35:56you have dodged the question
  12973. 7:36:02okay let's try this
  12974. 7:36:05do you like to code yes you like to code
  12975. 7:36:08uh but this should really be a smiley
  12976. 7:36:10face right
  12977. 7:36:12do you like to code no then why are you
  12978. 7:36:15watching a video on coding
  12979. 7:36:17do you like to code cancel you have
  12980. 7:36:20dodged the question
  12981. 7:36:22no one other option available to you is
  12982. 7:36:24that you can change the icon that you're
  12983. 7:36:26using
  12984. 7:36:27so let's say that we want to change this
  12985. 7:36:29icon so right now it is currently
  12986. 7:36:32this question mark so we can set this to
  12987. 7:36:34one
  12988. 7:36:35of a few preset icons so icon
  12989. 7:36:38equals and let's set this to warning
  12990. 7:36:42so instead of that question mark we get
  12991. 7:36:44this yellow exclamation point
  12992. 7:36:46and you can also use info
  12993. 7:36:54it's just an eye and error
  12994. 7:37:02so do it fits best for you all right
  12995. 7:37:05well
  12996. 7:37:06that's the basics on message boxes in
  12997. 7:37:08python if you'd like a copy of all this
  12998. 7:37:10code
  12999. 7:37:10i'll post all of this in the comments
  13000. 7:37:12down below but yeah that's the basics of
  13001. 7:37:14message boxes in python hey what's going
  13002. 7:37:18on everybody it's your bro here hope
  13003. 7:37:19you're doing well and in this video i'm
  13004. 7:37:21going to teach you guys about the color
  13005. 7:37:23chooser module in python so
  13006. 7:37:25sit back relax and enjoy the show
  13007. 7:37:29okay everybody so before we begin it
  13008. 7:37:31we'll need a second import so let's add
  13009. 7:37:33this at the top
  13010. 7:37:34so from tk enter import
  13011. 7:37:37color chooser and the reason that we
  13012. 7:37:40need a second import and it's not
  13013. 7:37:42included with this first import
  13014. 7:37:44is because this is a sub module
  13015. 7:37:47it's not necessary to add this comment i
  13016. 7:37:49just like to add comments for teaching
  13017. 7:37:51purposes
  13018. 7:37:52so make sure you have these two imports
  13019. 7:37:54before we begin
  13020. 7:37:55now we'll need a window and a button
  13021. 7:37:58that we can add to the window
  13022. 7:37:59and when we click on the button we're
  13023. 7:38:00going to select a color and change the
  13024. 7:38:03background color
  13025. 7:38:04of the window so let's create a window
  13026. 7:38:07window equals tk followed by window.main
  13027. 7:38:11loop and then let's set the geometry so
  13028. 7:38:14we're going to change
  13029. 7:38:15the background color of the window after
  13030. 7:38:17we select a color
  13031. 7:38:18so let's select a fairly large size for
  13032. 7:38:21a window
  13033. 7:38:22maybe 420 by 420 so this is the width
  13034. 7:38:25and the height
  13035. 7:38:26and then let's create a button so button
  13036. 7:38:29equals button
  13037. 7:38:31let's set some text text equals
  13038. 7:38:34click me and we're going to
  13039. 7:38:37set a command for this button that is
  13040. 7:38:39called so we're going to create a
  13041. 7:38:41function maybe called click
  13042. 7:38:43and then we just need to pack this
  13043. 7:38:45button so we can add this to the window
  13044. 7:38:47so button.pack
  13045. 7:38:48now we're going to need to create a
  13046. 7:38:50function and we'll call it click
  13047. 7:38:53so when we click on the button this
  13048. 7:38:55function is going to be called
  13049. 7:38:56so def click and what do we want to do
  13050. 7:39:00after clicking this button
  13051. 7:39:01what we're going to do is take color
  13052. 7:39:05chooser and we're going to use the ask
  13053. 7:39:09color function so currently this is what
  13054. 7:39:12this does
  13055. 7:39:13we can select a color after clicking
  13056. 7:39:15this button and pick whatever color you
  13057. 7:39:17want
  13058. 7:39:18then you hit ok however we need to store
  13059. 7:39:21this color within
  13060. 7:39:22let's say a variable so we'll create a
  13061. 7:39:23variable called color color equals
  13062. 7:39:26color chooser dot ask color and then we
  13063. 7:39:28can do something with this color
  13064. 7:39:30but for now let's just print this color
  13065. 7:39:32just to take a look to see what it
  13066. 7:39:33contains
  13067. 7:39:34so print color
  13068. 7:39:39so let's say that i want this green
  13069. 7:39:42color
  13070. 7:39:43click ok this is what this prints this
  13071. 7:39:46is our color
  13072. 7:39:47so this is the rgb values that we have
  13073. 7:39:51in this
  13074. 7:39:51first element so this is the amount of
  13075. 7:39:53red
  13076. 7:39:54then green and then blue and the second
  13077. 7:39:58element
  13078. 7:39:58is the hexadecimal representation of
  13079. 7:40:01these values
  13080. 7:40:03so what we want is this hexadecimal
  13081. 7:40:06value
  13082. 7:40:07so what we can do is create a second
  13083. 7:40:09variable to store this
  13084. 7:40:11so let's just call this color hex
  13085. 7:40:14color hex equals color
  13086. 7:40:18and we're going to get the first element
  13087. 7:40:21well technically it's the second element
  13088. 7:40:22but it's the
  13089. 7:40:24element at position one so this one
  13090. 7:40:27this is zero index zero so we're going
  13091. 7:40:30to
  13092. 7:40:31basically extract this hexadecimal value
  13093. 7:40:33and let's just print this
  13094. 7:40:35just to be sure that it's working fine
  13095. 7:40:36so print color
  13096. 7:40:38hex and let's try this again but select
  13097. 7:40:41a different color
  13098. 7:40:42uh maybe i want this purple color then
  13099. 7:40:45click ok
  13100. 7:40:46so this is cb42ff
  13101. 7:40:50and here we have cb42ff
  13102. 7:40:54now let's change the background color so
  13103. 7:40:56since we have this hex value that's
  13104. 7:40:58actually fairly simple
  13105. 7:40:59so we're going to take window dot config
  13106. 7:41:05bg for background color equals
  13107. 7:41:08color hex
  13108. 7:41:11and this will change background
  13109. 7:41:15color
  13110. 7:41:18and let's set this to a sky blue color
  13111. 7:41:22maybe like this click ok and it changes
  13112. 7:41:25the color
  13113. 7:41:26however it's not really necessary to
  13114. 7:41:28continue printing these values you can
  13115. 7:41:30take these out if you want
  13116. 7:41:33and this would work like just the same
  13117. 7:41:35then
  13118. 7:41:37now this part isn't necessary but if you
  13119. 7:41:40want
  13120. 7:41:40you can condense these three lines of
  13121. 7:41:42code that we have for our click function
  13122. 7:41:44into less lines of code so what you
  13123. 7:41:47could do
  13124. 7:41:48instead of assigning the value of color
  13125. 7:41:52at index 1
  13126. 7:41:53to a separate variable we can take color
  13127. 7:41:56at index 1
  13128. 7:41:57get rid of this line and assign bg
  13129. 7:42:00equals color
  13130. 7:42:01at index one so now this is done with
  13131. 7:42:03just
  13132. 7:42:04two lines of code and why stop there we
  13133. 7:42:06can write this with just
  13134. 7:42:08one line of code so now we're going to
  13135. 7:42:10take color chooser dot ask
  13136. 7:42:12color get rid of this line bg equals
  13137. 7:42:17color chooser dot ask color at index
  13138. 7:42:20one and now this is done
  13139. 7:42:24with just one line of code however it's
  13140. 7:42:27a
  13141. 7:42:27i would say a little more difficult to
  13142. 7:42:29teach
  13143. 7:42:30so that's why i kind of like to separate
  13144. 7:42:32this line by line
  13145. 7:42:34so if you want you can condense all of
  13146. 7:42:36this into just one line of code
  13147. 7:42:38so here's some ideas of where the color
  13148. 7:42:40chooser sub module could be helpful
  13149. 7:42:43so of course you can change the
  13150. 7:42:44background color too but let's say that
  13151. 7:42:46you're creating a game and you can
  13152. 7:42:48customize your character or avatar
  13153. 7:42:51well it would be kind of nice if you
  13154. 7:42:52could have some way for the user to
  13155. 7:42:54actually change maybe the character's
  13156. 7:42:56hair color eye color something like this
  13157. 7:42:58would actually work fairly well
  13158. 7:43:00so yeah that's the basics of color
  13159. 7:43:02chooser if you'd like a copy of all this
  13160. 7:43:04code
  13161. 7:43:04i'll post this in the comments down
  13162. 7:43:06below but that's the basics of
  13163. 7:43:08color chooser in python hey what's going
  13164. 7:43:11on everybody it's bro here hope you're
  13165. 7:43:12doing well and in this video i'm going
  13166. 7:43:14to teach you guys how we can create a
  13167. 7:43:15text area in python so sit back relax
  13168. 7:43:18and enjoy the show
  13169. 7:43:21all right my fellow bros let's discuss
  13170. 7:43:24the text
  13171. 7:43:24widget this functions like a text area
  13172. 7:43:27you can enter multiple lines of text i'm
  13173. 7:43:30not sure why
  13174. 7:43:31it's called a text widget instead of a
  13175. 7:43:33text area widget text area would make
  13176. 7:43:36more sense
  13177. 7:43:37but idk though so let's begin by
  13178. 7:43:39creating a window
  13179. 7:43:41window equals tk followed by window.main
  13180. 7:43:44loop
  13181. 7:43:46so to create a text widget also known as
  13182. 7:43:49a text area
  13183. 7:43:51let's call this text text equals text
  13184. 7:43:54and we're going to add this text to our
  13185. 7:43:56window and then we need to
  13186. 7:43:58pack this text text dot pack
  13187. 7:44:03so we should have a text area
  13188. 7:44:06and you can enter in
  13189. 7:44:09stuff like this
  13190. 7:44:13all right but what good is a text area
  13191. 7:44:16if you can't actually submit
  13192. 7:44:18some text so let's create a button to do
  13193. 7:44:20that for us
  13194. 7:44:21so button equals button
  13195. 7:44:24we're going to add this button to the
  13196. 7:44:26window
  13197. 7:44:28and button dot pack
  13198. 7:44:32so we need a command for this button
  13199. 7:44:36and a function as well so let's create a
  13200. 7:44:39function called submit it's going to be
  13201. 7:44:40a submit button
  13202. 7:44:42and let's change the text to because we
  13203. 7:44:44can
  13204. 7:44:45text equals submit
  13205. 7:44:50cool now let's create a function called
  13206. 7:44:53submit
  13207. 7:44:54and we'll do this at the top d e f
  13208. 7:44:56submit
  13209. 7:44:58and what is this going to do well it's
  13210. 7:45:01going to
  13211. 7:45:02get the text from the text area and
  13212. 7:45:05let's assign this to a variable and we
  13213. 7:45:07can call this maybe input or
  13214. 7:45:09whatever you want so to get the text
  13215. 7:45:12from the text area
  13216. 7:45:14let's say text that's the name of this
  13217. 7:45:16text
  13218. 7:45:17area dot get but we need
  13219. 7:45:20a starting index as well as an ending
  13220. 7:45:23index
  13221. 7:45:24so we're going to place 1.0 here
  13222. 7:45:27for the beginning index this is the
  13223. 7:45:29first line
  13224. 7:45:30we want to get everything so for the
  13225. 7:45:33second index we can just put
  13226. 7:45:34end then we can do anything we want with
  13227. 7:45:37this input so let's just print this to
  13228. 7:45:39the console window
  13229. 7:45:40so print input
  13230. 7:45:44and this should work
  13231. 7:45:49hey you drink plenty
  13232. 7:45:52of water love you
  13233. 7:45:55and click submit and then you can do
  13234. 7:45:58whatever you want with this
  13235. 7:45:59input well that's how a text area
  13236. 7:46:02functions but now let's customize the
  13237. 7:46:04appearance of this text area and i have
  13238. 7:46:06a few ideas that you might be interested
  13239. 7:46:08in
  13240. 7:46:09so let's change the background color of
  13241. 7:46:11this text area
  13242. 7:46:12so bg equals and i will pick
  13243. 7:46:15light yellow and you will see why in
  13244. 7:46:17just a second
  13245. 7:46:19so now this text area kind of resembles
  13246. 7:46:22maybe a piece of paper
  13247. 7:46:23a notebook a post-it note however
  13248. 7:46:27this font is really small so let's
  13249. 7:46:30change the font
  13250. 7:46:31styling and the font size so
  13251. 7:46:35font equals and i will pick the font
  13252. 7:46:39ink free this somewhat resembles
  13253. 7:46:42handwriting so
  13254. 7:46:43i kind of like this and it kind of fits
  13255. 7:46:45our notebook theme
  13256. 7:46:46and then a size maybe 25. one thing you
  13257. 7:46:49should know
  13258. 7:46:50the text area size corresponds directly
  13259. 7:46:53with
  13260. 7:46:53the font size so 25 is a fairly
  13261. 7:46:57large font so if we were to compile this
  13262. 7:47:00and run this
  13263. 7:47:01the size of our text area is now massive
  13264. 7:47:05because it corresponds directly with the
  13265. 7:47:07font size
  13266. 7:47:08so we might want to limit the width and
  13267. 7:47:10the height
  13268. 7:47:11so the height is the amount of
  13269. 7:47:14characters that this is tall
  13270. 7:47:16so let's say 8 and the width is the
  13271. 7:47:19amount of characters
  13272. 7:47:20that this is long and let's say 20.
  13273. 7:47:24and now this should be much smaller and
  13274. 7:47:27it kind of resembles a post-it note
  13275. 7:47:28which is kind of cool let's also change
  13276. 7:47:31the amount of padding that we have
  13277. 7:47:34we can do that with pad x and pad y so
  13278. 7:47:36pad x
  13279. 7:47:37let's say 20 and pad
  13280. 7:47:40y also 20. so the text shouldn't
  13281. 7:47:43actually touch any of the borders
  13282. 7:47:47this is some sample
  13283. 7:47:50text cool let's finish this by changing
  13284. 7:47:54the font color so that is
  13285. 7:47:56fg for foreground foreground equals
  13286. 7:47:59and i'll pick purple because purple is a
  13287. 7:48:02pretty color
  13288. 7:48:03and let's run this one last time
  13289. 7:48:07roses are red
  13290. 7:48:10violets are blue
  13291. 7:48:14booty booty booty booty
  13292. 7:48:19rockin everywhere
  13293. 7:48:25well that's the basics of the text
  13294. 7:48:27widget in python if you would like a
  13295. 7:48:29copy of all this code
  13296. 7:48:30i'll post all of this in the comments
  13297. 7:48:32down below but yeah that's the basics of
  13298. 7:48:34the text widget in python
  13299. 7:48:37hey what's going on everybody it's your
  13300. 7:48:39bro here hope you're doing well and in
  13301. 7:48:41this video i'm going to teach you guys
  13302. 7:48:42how we can use a file dialog
  13303. 7:48:44in python to open and read the contents
  13304. 7:48:46of a file so sit back
  13305. 7:48:47relax and enjoy the show
  13306. 7:48:51okay people so this is how you can use
  13307. 7:48:54file dialog
  13308. 7:48:55to open and read the contents of a file
  13309. 7:48:57but first we'll need a second import
  13310. 7:48:59along with this import at the top
  13311. 7:49:01so what we're going to be importing is
  13312. 7:49:03from tk
  13313. 7:49:05enter import file
  13314. 7:49:08dialog and then we'll need a window
  13315. 7:49:11like what we usually do so window equals
  13316. 7:49:15tk followed by window dot
  13317. 7:49:18main loop now we'll need a button that
  13318. 7:49:21we're going to add to the window and
  13319. 7:49:22when we click on this button
  13320. 7:49:23it's going to launch our file dialog so
  13321. 7:49:26we can select a file
  13322. 7:49:27so we'll call this button button equals
  13323. 7:49:30button
  13324. 7:49:31let's set some text maybe to just open
  13325. 7:49:34like we're opening a file and we'll
  13326. 7:49:36associate a function to our command
  13327. 7:49:39for this button so let's create a
  13328. 7:49:41function called
  13329. 7:49:43open file that's fine and then we need
  13330. 7:49:46to
  13331. 7:49:47pack this button so we can add it to the
  13332. 7:49:49screen so button.pack
  13333. 7:49:51now we need to create a function called
  13334. 7:49:53open file and we'll do this before we
  13335. 7:49:55create our window let's type
  13336. 7:49:57def open file
  13337. 7:50:00and when we call this function we're
  13338. 7:50:03going to
  13339. 7:50:04take file dialog and use the
  13340. 7:50:07ask open file name function
  13341. 7:50:11so this is going to return a string
  13342. 7:50:14and that string is the file path of
  13343. 7:50:16where your file is located
  13344. 7:50:18so what we could do is store this within
  13345. 7:50:21a variable
  13346. 7:50:22so let's store this within a variable
  13347. 7:50:24called file path
  13348. 7:50:26and to test this let's just print this
  13349. 7:50:29just to take a look at the contents of
  13350. 7:50:31this variable
  13351. 7:50:32so let's just print the file path now i
  13352. 7:50:35have a file on my desktop that i want to
  13353. 7:50:37use called test file
  13354. 7:50:39so let's click the open button and this
  13355. 7:50:42might not necessarily bring you to your
  13356. 7:50:44desktop it might bring you someplace
  13357. 7:50:45else within your
  13358. 7:50:46file directory so this just happens to
  13359. 7:50:49be
  13360. 7:50:50bring me to my desktop so i'm going to
  13361. 7:50:52click the test file that i have and
  13362. 7:50:54click
  13363. 7:50:54open so this prints the file path
  13364. 7:50:57so it's going to return a string and
  13365. 7:51:00it's the file path basically
  13366. 7:51:02so now we want to open and read the
  13367. 7:51:04contents of this file
  13368. 7:51:05so we don't really need this print line
  13369. 7:51:07anymore so
  13370. 7:51:09what we'll do is create a file
  13371. 7:51:12variable and we're going to use open
  13372. 7:51:16we're going to list the file path and
  13373. 7:51:18then we're going to use
  13374. 7:51:20r for read so the default is normally rt
  13375. 7:51:23for read text otherwise you can read
  13376. 7:51:25binary uh but we can just set this to r
  13377. 7:51:28r is the same as rt basically uh so then
  13378. 7:51:30we're going to open this file
  13379. 7:51:32and then we're going to read it so let's
  13380. 7:51:35just print this to the console window
  13381. 7:51:37so print the name of the file which is
  13382. 7:51:41file
  13383. 7:51:41where this is located file dot
  13384. 7:51:45read function and then it's a good idea
  13385. 7:51:47to close your files
  13386. 7:51:49after you're done with them so file dot
  13387. 7:51:51close
  13388. 7:51:52and let's test this so i'm going to
  13389. 7:51:55click
  13390. 7:51:56open find the file that i want
  13391. 7:52:00click it and then open
  13392. 7:52:05and that text file says my name is bro i
  13393. 7:52:07like pizza
  13394. 7:52:08now let's say you want to make this a
  13395. 7:52:10little more advanced so what i'm going
  13396. 7:52:12to do
  13397. 7:52:12is that i'm going to set the initial
  13398. 7:52:14directory of where we begin
  13399. 7:52:16looking for a file when we launch our
  13400. 7:52:18file dialog
  13401. 7:52:19so with that test file i'm going to move
  13402. 7:52:21this to my project folder so i'm just
  13403. 7:52:23going to paste it
  13404. 7:52:26so now this is within my project folder
  13405. 7:52:28now i'm going to set the initial
  13406. 7:52:30directory within
  13407. 7:52:31the function of ask open file name
  13408. 7:52:35so we're going to set the initial d
  13409. 7:52:38i r for initial directory and then we
  13410. 7:52:40can list a file path
  13411. 7:52:42so one thing you could do i want to get
  13412. 7:52:45this
  13413. 7:52:46path here this file path so if you're
  13414. 7:52:47using pycharm what you could do is right
  13415. 7:52:49click this
  13416. 7:52:50copy path and then get the absolute path
  13417. 7:52:53and then i'm just going to paste this
  13418. 7:52:55and then uh with these backslashes
  13419. 7:52:58you'll have to do double backslashes
  13420. 7:52:59because
  13421. 7:53:00this is the escape character for a
  13422. 7:53:02backslash
  13423. 7:53:04alright so this should open my project
  13424. 7:53:06folder
  13425. 7:53:08which it does and here's that test file
  13426. 7:53:12another thing we could do is that we can
  13427. 7:53:13change the title of our file dialog
  13428. 7:53:16right now the default is just open so we
  13429. 7:53:18can change that to something else
  13430. 7:53:20so i'm going to add a comma maybe put
  13431. 7:53:22this on a different line
  13432. 7:53:24uh so this is still within the function
  13433. 7:53:27of ask open file name
  13434. 7:53:28so i'm going to set the title to
  13435. 7:53:30something else i'll say
  13436. 7:53:32open file
  13437. 7:53:35okay so if we were to run this again
  13438. 7:53:40instead of just saying open it says open
  13439. 7:53:43file okay and we can also limit the
  13440. 7:53:46file types that we accept and look for
  13441. 7:53:49so we can use file types
  13442. 7:53:55so let's say that we want to initially
  13443. 7:53:57look for a
  13444. 7:53:58txt file so what we'll do is type in
  13445. 7:54:04like a name for the file that we're
  13446. 7:54:06looking for so this is what's going to
  13447. 7:54:07appear to the user
  13448. 7:54:09uh text files followed by an extension
  13449. 7:54:12so we want asterisk
  13450. 7:54:16dot txt
  13451. 7:54:19and then we can also have an option for
  13452. 7:54:21all files
  13453. 7:54:22so i'm going to add a comma for a second
  13454. 7:54:24option
  13455. 7:54:26and then let's say all
  13456. 7:54:29files and then an extension so comma
  13457. 7:54:34asterisk dot asterisk this will look for
  13458. 7:54:37all files
  13459. 7:54:38uh but i think i am missing a
  13460. 7:54:40parenthesis
  13461. 7:54:42there that should be good so then when
  13462. 7:54:44we open our file dialog
  13463. 7:54:47it's going to initially be looking for
  13464. 7:54:49plain text files
  13465. 7:54:50and with this drop down menu it says
  13466. 7:54:52text files and
  13467. 7:54:54all files so that's what we have listed
  13468. 7:54:56within our file types of what we want to
  13469. 7:54:58search for
  13470. 7:55:00so you can see that if i switch to all
  13471. 7:55:02files
  13472. 7:55:03our python file actually appears because
  13473. 7:55:06we don't have any limits on the files
  13474. 7:55:08that we're trying to open or search for
  13475. 7:55:10so if i change this back to text files
  13476. 7:55:13only folders and
  13477. 7:55:14plain text files are visible so then we
  13478. 7:55:16can open our test file again
  13479. 7:55:19but yeah that's the basics of file
  13480. 7:55:22dialog in the next video we're going to
  13481. 7:55:23be saving
  13482. 7:55:24a file so if you'd like a copy of all
  13483. 7:55:26this code i'll post
  13484. 7:55:28all of this in the comments down below
  13485. 7:55:29but yeah that's the basics of opening a
  13486. 7:55:31file
  13487. 7:55:32using python hey what's going on
  13488. 7:55:34everybody it's your bro here hope you're
  13489. 7:55:36doing well and in this video i'm going
  13490. 7:55:37to teach you guys how we can use
  13491. 7:55:39python's file dialog to save a file
  13492. 7:55:42someplace on your computer
  13493. 7:55:43so sit back relax and enjoy the show
  13494. 7:55:48okay everybody so this is how we can
  13495. 7:55:50save a file
  13496. 7:55:52we're going to be reusing a file dialog
  13497. 7:55:54again kind of like what we did with the
  13498. 7:55:56video on opening and reading a file so
  13499. 7:55:59from
  13500. 7:56:00tk enter import file dialog we're going
  13501. 7:56:03to be creating a window and adding a
  13502. 7:56:05button as well as a text area to this
  13503. 7:56:07window
  13504. 7:56:08so let's create our window window equals
  13505. 7:56:10tk
  13506. 7:56:11followed by window.main loop
  13507. 7:56:15let's create a button so button equals
  13508. 7:56:18button
  13509. 7:56:19let's set the text of the button so text
  13510. 7:56:22equals
  13511. 7:56:22save it's going to be a save button and
  13512. 7:56:25then a command
  13513. 7:56:26so we'll need to link a function to this
  13514. 7:56:28command so let's create a
  13515. 7:56:30save file function and then we need to
  13516. 7:56:34pack
  13517. 7:56:34this button so button dot pack
  13518. 7:56:38now let's create a text area this is
  13519. 7:56:40just called
  13520. 7:56:41text so text equals text we're going to
  13521. 7:56:44add this to the window
  13522. 7:56:46and we need to pack this text so text
  13523. 7:56:49dot pack and now let's create
  13524. 7:56:52a save file function so at the top of
  13525. 7:56:56our program
  13526. 7:56:57d e f save file
  13527. 7:57:02and then we're going to take file dialog
  13528. 7:57:05and use the
  13529. 7:57:06ask save as file function and we're
  13530. 7:57:09going to store
  13531. 7:57:11the contents within file file equals
  13532. 7:57:14file dialog dot ask save as file
  13533. 7:57:18let's run this just to test it out so we
  13534. 7:57:21have
  13535. 7:57:21our window a save button and this
  13536. 7:57:25is our text area
  13537. 7:57:28we can type in multiple
  13538. 7:57:32lines of text
  13539. 7:57:35yay and let's try and save this just to
  13540. 7:57:39see what we have so far
  13541. 7:57:41so this will bring me to my desktop it
  13542. 7:57:43might bring you someplace else for
  13543. 7:57:46your computer so i'm just going to save
  13544. 7:57:48this as
  13545. 7:57:50test file now with this drop down menu
  13546. 7:57:53save as type there's nothing set up so
  13547. 7:57:56we'll need to do that manually
  13548. 7:57:58uh if we were to save this and here's
  13549. 7:58:00that test file on my desktop
  13550. 7:58:02so let's take a look at the properties
  13551. 7:58:04we did not list a
  13552. 7:58:06file extension so the type of file just
  13553. 7:58:09says
  13554. 7:58:09file so we'll get to that later on in
  13555. 7:58:11this video
  13556. 7:58:12and if we were to open this with a
  13557. 7:58:14program like notepad
  13558. 7:58:16well it's currently empty that's because
  13559. 7:58:18we need to write
  13560. 7:58:19some text to this file so let's do that
  13561. 7:58:22next
  13562. 7:58:23let's store all of the text from our
  13563. 7:58:26text area to a variable
  13564. 7:58:28let's call this variable file text
  13565. 7:58:32file text equals and then we need to get
  13566. 7:58:35all of the text from our text area so
  13567. 7:58:38text dot get and we're going to list the
  13568. 7:58:42starting index as well as the ending
  13569. 7:58:44index
  13570. 7:58:45for this function so we're going to
  13571. 7:58:47begin at
  13572. 7:58:481.0 that is the beginning of this text
  13573. 7:58:51area
  13574. 7:58:51and the ending we can just put end and
  13575. 7:58:54then we need to convert
  13576. 7:58:55all of this into a string so let's
  13577. 7:58:57surround this with
  13578. 7:58:59str and then we can store all of the
  13579. 7:59:02text
  13580. 7:59:03from our text area within this variable
  13581. 7:59:06file text and now we just need to write
  13582. 7:59:09our text to this file so file dot
  13583. 7:59:12write and we're going to pass in file
  13584. 7:59:15text
  13585. 7:59:16and then it's a good idea to close your
  13586. 7:59:18files when you're done using them so
  13587. 7:59:20file
  13588. 7:59:21dot close and let's try this again
  13589. 7:59:26hello my name
  13590. 7:59:30is bro let's save this
  13591. 7:59:34i'll call this hello and click save
  13592. 7:59:38and we still can't save s-type so we'll
  13593. 7:59:40work on that next
  13594. 7:59:42let's go to my desktop here's the test
  13595. 7:59:44file that we created i'm going to open
  13596. 7:59:46this with notepad and it has all of the
  13597. 7:59:48text
  13598. 7:59:49that we added to that text area now
  13599. 7:59:52let's list
  13600. 7:59:53some available file extensions that we
  13601. 7:59:55can have the user
  13602. 7:59:56save this file as so let's begin by
  13603. 7:59:59setting
  13604. 8:00:00the default extension so default
  13605. 8:00:04extension and let's set this to
  13606. 8:00:07a text file that is dot
  13607. 8:00:11txt and then we can list
  13608. 8:00:14all of the available file types
  13609. 8:00:17so file types
  13610. 8:00:21and we're going to place these within a
  13611. 8:00:23set of straight brackets
  13612. 8:00:25and i'll just separate these line by
  13613. 8:00:26line so let's begin with a plain
  13614. 8:00:28text file so this will take two strings
  13615. 8:00:32the first is the message that will
  13616. 8:00:36appear within
  13617. 8:00:37that save as type drop down menu so
  13618. 8:00:39let's place
  13619. 8:00:41text file and the second string
  13620. 8:00:44the file extension and that is
  13621. 8:00:48dot txt
  13622. 8:00:51then let's add another let's say html
  13623. 8:00:54so i'm going to copy this and paste it
  13624. 8:00:57and
  13625. 8:00:58let's say html next
  13626. 8:01:02dot html and then lastly you can do
  13627. 8:01:06just all files so
  13628. 8:01:09all files and that is dot
  13629. 8:01:13asterisk and let's try this again
  13630. 8:01:17so hello my
  13631. 8:01:21name is bro i
  13632. 8:01:25like pizza and
  13633. 8:01:28ice cream and this time i think i'll
  13634. 8:01:32save this
  13635. 8:01:32as an html file
  13636. 8:01:35and this is we'll call this
  13637. 8:01:39pizza and ice cream i guess
  13638. 8:01:43and click save now let's go back to my
  13639. 8:01:46desktop
  13640. 8:01:47here's that file pizza and ice cream and
  13641. 8:01:49it is an
  13642. 8:01:51file let's just verify that yep.html for
  13643. 8:01:56the type of file
  13644. 8:01:57so we can open this with notepad
  13645. 8:02:01and it has all of the text i could even
  13646. 8:02:04open this with sublime text
  13647. 8:02:06as well kind of like that
  13648. 8:02:11all right let's change the initial
  13649. 8:02:14directory
  13650. 8:02:15for saving a file let's change it to
  13651. 8:02:18my project folder for uh this python
  13652. 8:02:22file that we have
  13653. 8:02:23so that is and i'm just going to
  13654. 8:02:25organize this a little bit
  13655. 8:02:28initial d i r
  13656. 8:02:31equals and then we can list a file path
  13657. 8:02:34so i'm just going to copy this
  13658. 8:02:37so copy path paste it
  13659. 8:02:42then i want to make sure that these are
  13660. 8:02:45double backslashes
  13661. 8:02:49and i need to add a comma at the end
  13662. 8:02:52okay let's try this again
  13663. 8:02:54so testing
  13664. 8:02:57one two three and this should go to my
  13665. 8:03:01project folder
  13666. 8:03:02which it does testing123
  13667. 8:03:07and let's save this as a plain text file
  13668. 8:03:10click save
  13669. 8:03:12and here's that plain text file
  13670. 8:03:15testing123
  13671. 8:03:16another option available to you is that
  13672. 8:03:18you don't necessarily need to use
  13673. 8:03:20a text area to get some text to write to
  13674. 8:03:23a file
  13675. 8:03:24you could use the console window to
  13676. 8:03:26accept some text
  13677. 8:03:28so to demonstrate this i'm going to turn
  13678. 8:03:30this line into a comment
  13679. 8:03:32and we're instead going to use the
  13680. 8:03:34console window this time
  13681. 8:03:36so we'll say file text equals
  13682. 8:03:40input and then let's just have a prompt
  13683. 8:03:43here
  13684. 8:03:44enter some text i
  13685. 8:03:47guess and then let's run this again so
  13686. 8:03:50we still need to
  13687. 8:03:51click this save button and select a file
  13688. 8:03:54location i'll just save over our file
  13689. 8:03:57testing123 i'll click save
  13690. 8:04:00yeah i'll replace it alright so we need
  13691. 8:04:02to enter some text
  13692. 8:04:04i guess so i will write a message
  13693. 8:04:08such as the cake
  13694. 8:04:12is a lie and hit enter
  13695. 8:04:16so then our file testing123 now says
  13696. 8:04:19the cake is a lie so you don't
  13697. 8:04:21necessarily need to use
  13698. 8:04:23the get function for a text area you can
  13699. 8:04:26use the console window as well
  13700. 8:04:29here's one last thing to consider before
  13701. 8:04:30you go so what if we begin the process
  13702. 8:04:33of
  13703. 8:04:34finding a file location to save a file
  13704. 8:04:37to
  13705. 8:04:37and then we exit out of this window well
  13706. 8:04:40we're going to encounter an exception
  13707. 8:04:42so one way that we can prevent this is
  13708. 8:04:45to add
  13709. 8:04:46this line if file is
  13710. 8:04:49none return
  13711. 8:04:52so then if we were to try this again
  13712. 8:04:56begin saving a file and then exit out of
  13713. 8:04:58this
  13714. 8:04:59there is no exception that we encounter
  13715. 8:05:02so that's just one little extra i
  13716. 8:05:04thought i would throw in
  13717. 8:05:05but yeah that's the basics of saving
  13718. 8:05:07files in python if you would like a copy
  13719. 8:05:09of all this code
  13720. 8:05:10i'll post all of this in the comments
  13721. 8:05:12down below but yeah that's how to save a
  13722. 8:05:15file using python
  13723. 8:05:17hey what's going on everybody it's your
  13724. 8:05:18bro here hope you're doing well and in
  13725. 8:05:20this video i'm going to teach you guys
  13726. 8:05:21how we can create a menu bar
  13727. 8:05:23in python so sit back relax and enjoy
  13728. 8:05:26the show
  13729. 8:05:28all right people let's create a menu bar
  13730. 8:05:31not too different from the menu bar that
  13731. 8:05:32you see at the top of many programs
  13732. 8:05:34there's usually a tab
  13733. 8:05:36for file edit view help stuff like that
  13734. 8:05:39except we're going to create one that
  13735. 8:05:41doesn't have as many options
  13736. 8:05:43or features it's going to be the generic
  13737. 8:05:45brand menu bar like the walmart variety
  13738. 8:05:48so let's begin by creating a window so
  13739. 8:05:50window equals tk followed by
  13740. 8:05:53window.mainloop then we're going to
  13741. 8:05:54create a menu bar
  13742. 8:05:56and add menus to the menu bar and each
  13743. 8:05:59menu is going to function like a
  13744. 8:06:01dropdown menu where you hover over one
  13745. 8:06:03of these tabs
  13746. 8:06:04and there's a drop down so we need a
  13747. 8:06:07menu bar
  13748. 8:06:08let's just call this menu bar menu bar
  13749. 8:06:10equals
  13750. 8:06:11menu and we're adding this to our window
  13751. 8:06:15so then we're going to take window dot
  13752. 8:06:17config
  13753. 8:06:18and we can set the menu of this window
  13754. 8:06:21to equal the menu bar that we just
  13755. 8:06:24created
  13756. 8:06:25with each of these tabs for your menu
  13757. 8:06:26bar you need to create a separate menu
  13758. 8:06:29and add each menu to your menu bar and
  13759. 8:06:31the menu bar is added to the window
  13760. 8:06:33there's different levels to the stuff so
  13761. 8:06:36let's create
  13762. 8:06:37a file menu because well this one has a
  13763. 8:06:40file menu
  13764. 8:06:40why not our program so file
  13765. 8:06:44menu equals menu
  13766. 8:06:47kind of like what we did with our menu
  13767. 8:06:49bar except instead of adding this to our
  13768. 8:06:52window we're going to add this
  13769. 8:06:53to our menu bar so menu
  13770. 8:06:57bar like that there's one other step two
  13771. 8:07:02we need to use this function menu
  13772. 8:07:05bar dot add
  13773. 8:07:08underscore cascade this will have a drop
  13774. 8:07:12down
  13775. 8:07:12menu sort of effect so let's create a
  13776. 8:07:16label
  13777. 8:07:16just called file to mimic the file tab
  13778. 8:07:19that you see at the top of many programs
  13779. 8:07:21so label file and we're going to set the
  13780. 8:07:25menu
  13781. 8:07:26to equal the file menu that we created
  13782. 8:07:30so file menu and let's take a look just
  13783. 8:07:33to see what we have
  13784. 8:07:35so here's our menu bar and here's our
  13785. 8:07:37menu there's a drop down
  13786. 8:07:39effect for this but we need to add
  13787. 8:07:41actual options
  13788. 8:07:42to this file menu so let's do that
  13789. 8:07:46so let's begin by creating let's see we
  13790. 8:07:49got
  13791. 8:07:50maybe open save and exit
  13792. 8:07:54that should be good so file
  13793. 8:07:58menu dot add
  13794. 8:08:01underscore command so this is a
  13795. 8:08:03clickable option
  13796. 8:08:05and let's create a label called open
  13797. 8:08:08and we can open a file and maybe two
  13798. 8:08:12more so we got open
  13799. 8:08:14save and exit open save
  13800. 8:08:17exit
  13801. 8:08:21and let's take a look at this
  13802. 8:08:25so here's the drop down menu we can open
  13803. 8:08:27save exit
  13804. 8:08:29now you might notice that there is this
  13805. 8:08:31annoying line
  13806. 8:08:32this is called a tear off you can
  13807. 8:08:34actually get rid of this if you want
  13808. 8:08:36so when we create this file menu we're
  13809. 8:08:39going to
  13810. 8:08:40set tear off to equal
  13811. 8:08:43zero and that should get rid of that
  13812. 8:08:47and it's gone so sometimes you might see
  13813. 8:08:51a line to separate sections well
  13814. 8:08:54you can do that with a separator so
  13815. 8:08:57let's separate
  13816. 8:08:58the exit command that we have
  13817. 8:09:01from open and save and to add that
  13818. 8:09:03separator we just type in the name of
  13819. 8:09:05our menu
  13820. 8:09:06file menu dot add
  13821. 8:09:10separator so this will separate your
  13822. 8:09:14different commands from each other
  13823. 8:09:16within a menu you can see that there is
  13824. 8:09:18a line between
  13825. 8:09:19save and exit because we placed a
  13826. 8:09:21separator here
  13827. 8:09:22now if you want each of these file
  13828. 8:09:24commands to actually do something you
  13829. 8:09:26need to associate
  13830. 8:09:27a command with each of these commands
  13831. 8:09:30it's a command within a command
  13832. 8:09:32so command equals for our open file
  13833. 8:09:35command
  13834. 8:09:36we'll create a function called open file
  13835. 8:09:40and we'll need to declare that function
  13836. 8:09:42and then let's create a command called
  13837. 8:09:45save file and then another called
  13838. 8:09:48exit actually one shortcut if you want
  13839. 8:09:51to exit something you can just say
  13840. 8:09:53quit all right let's create an
  13841. 8:09:56open file and a save file function so
  13842. 8:09:59def open file
  13843. 8:10:03let's just print something just to keep
  13844. 8:10:05this simple
  13845. 8:10:07i already have another video on opening
  13846. 8:10:09files so
  13847. 8:10:11print file has
  13848. 8:10:14been opened and let's do the same thing
  13849. 8:10:18for
  13850. 8:10:18save file
  13851. 8:10:25file has been saved
  13852. 8:10:28and then our last command exit we'll
  13853. 8:10:30just quit
  13854. 8:10:31so let's try this so let's go to file
  13855. 8:10:34here's the drop down menu we can
  13856. 8:10:36open a file file has been opened we can
  13857. 8:10:39save a file
  13858. 8:10:41and then exit and exit will close out of
  13859. 8:10:43our window
  13860. 8:10:44let's create a second menu called edit
  13861. 8:10:46to mimic the one that we have at the top
  13862. 8:10:48of our ide
  13863. 8:10:49so let's create a edit menu so it's the
  13864. 8:10:52same process that we did with our file
  13865. 8:10:54menu
  13866. 8:10:55so let's call this edit menu equals menu
  13867. 8:10:59we're adding this to our menu bar and i
  13868. 8:11:02do not want a tear
  13869. 8:11:03off so tear off equals zero then for the
  13870. 8:11:07next line we need to add
  13871. 8:11:09cascade so i'm going to copy this
  13872. 8:11:12menu bar dot add cascade the label name
  13873. 8:11:15will be edit and the file i mean the
  13874. 8:11:18menu
  13875. 8:11:19will be edit menu and we're setting
  13876. 8:11:22this menu that we're adding to edit menu
  13877. 8:11:26and let's try this so we have file and
  13878. 8:11:28edit
  13879. 8:11:29but we need to add commands to this edit
  13880. 8:11:31menu
  13881. 8:11:32so it's the same process that we did
  13882. 8:11:34with our file menu i'm just going to
  13883. 8:11:35copy this
  13884. 8:11:36change file to edit and let's
  13885. 8:11:40make a command called cut copy and paste
  13886. 8:11:44so the label will be cut we'll create a
  13887. 8:11:48command called
  13888. 8:11:49cut we'll copy this
  13889. 8:11:54and we'll create a another command
  13890. 8:11:56called
  13891. 8:11:58copy and then paste
  13892. 8:12:08all right then we just need to create
  13893. 8:12:10some functions for this so cut
  13894. 8:12:11copy paste let's do that at the top
  13895. 8:12:18so cut
  13896. 8:12:24you cut some text
  13897. 8:12:29so cut copy and then paste
  13898. 8:12:35you copied some text
  13899. 8:12:38and paste you
  13900. 8:12:42pasted some text
  13901. 8:12:45all right let's try this so here's our
  13902. 8:12:47edit menu
  13903. 8:12:49here's the drop down we can cut we can
  13904. 8:12:51copy
  13905. 8:12:52and we can paste not actually but it's
  13906. 8:12:55going to call a function that can
  13907. 8:12:57do something like that if you want to
  13908. 8:12:59set that up maybe that's a video for a
  13909. 8:13:01different day
  13910. 8:13:02let's customize these menus well because
  13911. 8:13:04we can
  13912. 8:13:05so let's begin by changing the font so
  13913. 8:13:08we can just type in
  13914. 8:13:09within each of these menus font equals
  13915. 8:13:12pick a font that you want and then a
  13916. 8:13:14size so i'm going to add this font
  13917. 8:13:16for both of these menus that we have and
  13918. 8:13:19then when we
  13919. 8:13:20hover over and click on these menus you
  13920. 8:13:23can see that the font has changed
  13921. 8:13:25did you guys know that you can actually
  13922. 8:13:27add images next to each of these
  13923. 8:13:28commands
  13924. 8:13:29well you can and this is how we can do
  13925. 8:13:31that first you'll need some images
  13926. 8:13:33here's a few that i'm using so add these
  13927. 8:13:35to your project folder i plan on using a
  13928. 8:13:38floppy disk for save
  13929. 8:13:39and open folder for open and for some
  13930. 8:13:42reason a stop sign for exit because i
  13931. 8:13:44couldn't really think of anything at the
  13932. 8:13:46moment
  13933. 8:13:46i guess an arrow would have worked too i
  13934. 8:13:48guess so let's create some photo images
  13935. 8:13:50out of these images
  13936. 8:13:53so i already did this i'm just going to
  13937. 8:13:54copy and paste the text that i had
  13938. 8:13:57so create a unique name for each image i
  13939. 8:14:00have
  13940. 8:14:00open image save image and exit image
  13941. 8:14:03they're all
  13942. 8:14:04photo images and here is the file path
  13943. 8:14:07or the file name for each of these
  13944. 8:14:09pictures that i'm using so to add an
  13945. 8:14:12image
  13946. 8:14:12to each of these commands when you add
  13947. 8:14:14the command we can just say
  13948. 8:14:16image and then the photo image that you
  13949. 8:14:18want to use
  13950. 8:14:19so with my open command i'm going to use
  13951. 8:14:22this
  13952. 8:14:22open image photo image that i have and
  13953. 8:14:25this is the
  13954. 8:14:26open folder so i'm going to do the same
  13955. 8:14:28thing for save
  13956. 8:14:29and exit so we have save image
  13957. 8:14:33and exit image
  13958. 8:14:38but when we run and compile this let's
  13959. 8:14:40take a look
  13960. 8:14:42yeah so all the text is gone that's
  13961. 8:14:44going to be a problem
  13962. 8:14:46so we'll need to compound views so with
  13963. 8:14:48compound we can add
  13964. 8:14:50an image on a certain side of the text
  13965. 8:14:53and keep the text so compound
  13966. 8:14:56let's say we want the image on the left
  13967. 8:14:59so
  13968. 8:15:00compound left and i'm just going to add
  13969. 8:15:02this to each of these commands
  13970. 8:15:06and now we should have an image as well
  13971. 8:15:09as some text
  13972. 8:15:11well that's the basics of creating a
  13973. 8:15:13menu bar in python if you want a copy of
  13974. 8:15:15all this code
  13975. 8:15:16i'll post all of this in the comments
  13976. 8:15:18down below but yeah that's how you can
  13977. 8:15:19create a simple
  13978. 8:15:20menu bar in python what's going on
  13979. 8:15:24everybody it's bro here hope you're
  13980. 8:15:25doing well and in this video i'm going
  13981. 8:15:27to teach you guys how
  13982. 8:15:28frames work in python so sit back relax
  13983. 8:15:31and enjoy the show
  13984. 8:15:34what's going on people we're going to be
  13985. 8:15:36discussing frames today
  13986. 8:15:38a frame is really just a rectangular
  13987. 8:15:40container to group and hold widgets
  13988. 8:15:42together
  13989. 8:15:43so we're going to practice using frames
  13990. 8:15:45today but first we'll need a window
  13991. 8:15:47we're going to create a frame and add a
  13992. 8:15:49frame to the window
  13993. 8:15:51so window equals tk followed by
  13994. 8:15:54window.main loop
  13995. 8:15:56well we're going to need some widgets to
  13996. 8:15:58add to our frame
  13997. 8:15:59so let's create a bunch of buttons
  13998. 8:16:01because why not
  13999. 8:16:03so i'm going to show you guys a shortcut
  14000. 8:16:05too so let's say
  14001. 8:16:06button equals button
  14002. 8:16:10and then for now we're going to add this
  14003. 8:16:12to our window but we're going to replace
  14004. 8:16:14this eventually with
  14005. 8:16:16frame when we create this and i'm just
  14006. 8:16:18going to set the text
  14007. 8:16:20to equal let's say w
  14008. 8:16:23we're going to have four different
  14009. 8:16:24buttons wasd
  14010. 8:16:26like the buttons on the keyboard if
  14011. 8:16:28you're playing a game and you want to
  14012. 8:16:29move forward
  14013. 8:16:30left back or right so we're adding this
  14014. 8:16:33to the window
  14015. 8:16:34for now let's set the text to w
  14016. 8:16:38and what else can we do uh let's change
  14017. 8:16:40the font so font
  14018. 8:16:41equals i'll pick the font consoles
  14019. 8:16:45because
  14020. 8:16:46that tends to be even uh with buttons
  14021. 8:16:50the size of the button adjusts to uh the
  14022. 8:16:52size of the
  14023. 8:16:54uh letter that you're using or the size
  14024. 8:16:56of the text
  14025. 8:16:57uh so let's set this to 25
  14026. 8:17:00and then a width so maybe
  14027. 8:17:04width equals three all right and then
  14028. 8:17:07button.pack and i just want to take a
  14029. 8:17:10look at this
  14030. 8:17:14so here's our button w so i'd like to
  14031. 8:17:16show you guys a
  14032. 8:17:17shortcut if you don't plan on using this
  14033. 8:17:20button by name
  14034. 8:17:22you don't really need to give it a name
  14035. 8:17:23you can just say button
  14036. 8:17:25and then have all your uh options here
  14037. 8:17:28and then instead of having button.pack
  14038. 8:17:31just add
  14039. 8:17:32dot pack to the end so this will do the
  14040. 8:17:34same thing
  14041. 8:17:35however you can't adjust this button by
  14042. 8:17:38its name though because it doesn't have
  14043. 8:17:39a name anymore
  14044. 8:17:40so let's create a few other buttons one
  14045. 8:17:42for a s
  14046. 8:17:44and d so we have w
  14047. 8:17:47a s d
  14048. 8:17:52and by default they're all top and
  14049. 8:17:55center
  14050. 8:17:56so with pack we can actually put these
  14051. 8:17:59on a certain side
  14052. 8:18:00so let's say we want uh w on the top
  14053. 8:18:04and then the other three will be all
  14054. 8:18:07left
  14055. 8:18:07so side equals left and then after i
  14056. 8:18:11show this to you
  14057. 8:18:12you'll see the benefits of using a frame
  14058. 8:18:15all right so right now they're all at
  14059. 8:18:18least it appears that they're all within
  14060. 8:18:19a container
  14061. 8:18:20so what if i were to expand this well w
  14062. 8:18:23is just drifting off
  14063. 8:18:24it's sailing away by w so
  14064. 8:18:27what if we could put these within a
  14065. 8:18:29frame or container
  14066. 8:18:30to actually contain all of these widgets
  14067. 8:18:33well we can
  14068. 8:18:34so let's create a frame frame
  14069. 8:18:38equals frame
  14070. 8:18:41and instead of adding these buttons to
  14071. 8:18:43the window we're going to add
  14072. 8:18:44these buttons to our frame
  14073. 8:18:51and now we need to add the frame to the
  14074. 8:18:54window
  14075. 8:18:56and this is what this looks like however
  14076. 8:18:59we need to
  14077. 8:19:00actually add this by using pack or place
  14078. 8:19:03so frame
  14079. 8:19:04dot pack so now what this looks
  14080. 8:19:08like is that all of these buttons all of
  14081. 8:19:10these widgets are now contained within a
  14082. 8:19:12frame
  14083. 8:19:13a container let's change the background
  14084. 8:19:16color of this frame just so that we can
  14085. 8:19:17see
  14086. 8:19:18the width and the height of this frame
  14087. 8:19:20so
  14088. 8:19:21when you create your frame there is an
  14089. 8:19:24option for background color it's just bg
  14090. 8:19:27and let's set this to a color we haven't
  14091. 8:19:29done pink yet
  14092. 8:19:30pink is a very masculine color so now
  14093. 8:19:33the background of our frame is pink and
  14094. 8:19:36that might be
  14095. 8:19:37a better visual cue to the balance of
  14096. 8:19:40the frame
  14097. 8:19:41let's also give this frame a border so
  14098. 8:19:43let's
  14099. 8:19:44set a width of 5 using bd
  14100. 8:19:47and then the type of border that we want
  14101. 8:19:50and we can
  14102. 8:19:51specify that with relief so relief
  14103. 8:19:53equals
  14104. 8:19:54let's say raised so this will have a 3d
  14105. 8:19:57pop effect
  14106. 8:19:58kind of like that let's try sunken
  14107. 8:20:00there's a few other ones as well but i
  14108. 8:20:02don't feel like it's necessary to
  14109. 8:20:04go over all the border types let's stick
  14110. 8:20:07with sunken for now that'll work
  14111. 8:20:09alright so with this frame we are
  14112. 8:20:12currently packing this
  14113. 8:20:13we can set this on a certain side within
  14114. 8:20:16our window
  14115. 8:20:17so currently it's at the top let's set
  14116. 8:20:19this to maybe the bottom so
  14117. 8:20:21side equals bottom
  14118. 8:20:25now this frame is sticking to the bottom
  14119. 8:20:28of the window
  14120. 8:20:29and it still contains all of the buttons
  14121. 8:20:31all of the widgets
  14122. 8:20:32that we added another function available
  14123. 8:20:34to you is
  14124. 8:20:35the place function we can place this
  14125. 8:20:38frame
  14126. 8:20:38at certain coordinates within our window
  14127. 8:20:41so let's set
  14128. 8:20:43x to equal zero and y to equal zero
  14129. 8:20:46so then when we place this frame within
  14130. 8:20:48our window and set some coordinates
  14131. 8:20:51this entire frame is going to stick at
  14132. 8:20:54these coordinates even if we were to
  14133. 8:20:56resize this window and we can change
  14134. 8:20:59these too
  14135. 8:21:00let's say x is now 100 and y is also 100
  14136. 8:21:05so the frame along with all of the
  14137. 8:21:08components all of the widgets that it
  14138. 8:21:10contains
  14139. 8:21:11are sticking within the container and
  14140. 8:21:13they move along with this
  14141. 8:21:15so that's the basics of frames it's
  14142. 8:21:17really just a rectangular container to
  14143. 8:21:19group and hold widgets together
  14144. 8:21:22so if you want a copy of all this code
  14145. 8:21:24i'll post all of this in the comments
  14146. 8:21:25down below
  14147. 8:21:26and well yeah that's how frames work in
  14148. 8:21:29python
  14149. 8:21:30hey what's going on everybody it's your
  14150. 8:21:32bro here hope you're doing well and in
  14151. 8:21:33this video i'm going to teach you guys a
  14152. 8:21:35few different ways in which we can
  14153. 8:21:36create new windows using python so sit
  14154. 8:21:38back relax and enjoy the show
  14155. 8:21:42let's get started everybody so we're
  14156. 8:21:44going to create a window and we'll add a
  14157. 8:21:45button to this window
  14158. 8:21:46when we click on this button it'll
  14159. 8:21:48create a new separate window for us
  14160. 8:21:50so window equals tk followed by
  14161. 8:21:52window.main loop
  14162. 8:21:54we'll need to add a button so let's do
  14163. 8:21:55that button we're adding this to the
  14164. 8:21:57window
  14165. 8:21:58let's add some text to the button let's
  14166. 8:22:00say create
  14167. 8:22:02new window and we'll set a command
  14168. 8:22:06command equals and we'll need to define
  14169. 8:22:08a function
  14170. 8:22:09let's create a function called create
  14171. 8:22:12underscore window
  14172. 8:22:14and let's pack this as well so we will
  14173. 8:22:16define
  14174. 8:22:17this function create window and let's do
  14175. 8:22:19so at the top
  14176. 8:22:21def create window and what do we want
  14177. 8:22:23this to do
  14178. 8:22:24well we're going to create a new window
  14179. 8:22:26and let's call this next window
  14180. 8:22:28new underscore window equals and there's
  14181. 8:22:31two popular ways of creating a window
  14182. 8:22:33one is we can use tk and the other is
  14183. 8:22:36that we can use
  14184. 8:22:37something called top level and let me
  14185. 8:22:40explain the difference between the two
  14186. 8:22:41here's a definition for a top level
  14187. 8:22:43window this is a
  14188. 8:22:45new window on top of other windows
  14189. 8:22:48and it's usually linked to a bottom
  14190. 8:22:51window
  14191. 8:22:52our main window is serving as the bottom
  14192. 8:22:54window
  14193. 8:22:55and our new window is serving as the top
  14194. 8:22:57window the top level window
  14195. 8:23:00so let's try this and just experiment
  14196. 8:23:02here is our main window
  14197. 8:23:03the bottom window and if we were to
  14198. 8:23:05click on this button it's going to
  14199. 8:23:07create a
  14200. 8:23:08new window for us a new top level window
  14201. 8:23:11if we were to close out of the bottom
  14202. 8:23:13level window
  14203. 8:23:14any top level windows associated with
  14204. 8:23:17this would also close
  14205. 8:23:18kind of like what you just saw now
  14206. 8:23:20however it's not the same case
  14207. 8:23:22the other way around if we have a bottom
  14208. 8:23:25window and a top level window
  14209. 8:23:26and we close out of the top level window
  14210. 8:23:29our bottom level window
  14211. 8:23:31is just fine it didn't close out of that
  14212. 8:23:33so think of these like
  14213. 8:23:34jenga pieces if you were to pull one of
  14214. 8:23:37the pieces
  14215. 8:23:38one of the windows out from underneath
  14216. 8:23:40the bottom any
  14217. 8:23:41pieces or windows on the top of it will
  14218. 8:23:44close out as well
  14219. 8:23:45or collapse kind of like in the game
  14220. 8:23:46jenga so
  14221. 8:23:48that's one way to think of top level
  14222. 8:23:50windows they're stacked on top of any
  14223. 8:23:52bottom level windows
  14224. 8:23:53if you remove a bottom piece it removes
  14225. 8:23:55any from the top
  14226. 8:23:56on the flip side we have tk this is a
  14227. 8:23:59new independent window and we've
  14228. 8:24:01actually been doing this since
  14229. 8:24:03day one of our gui programming tk is a
  14230. 8:24:06new independent window and our main
  14231. 8:24:09window and the new window that we create
  14232. 8:24:10are not linked whatsoever they're
  14233. 8:24:12completely independent
  14234. 8:24:13so if we were to create a new window
  14235. 8:24:16this is our new window and close out of
  14236. 8:24:17our old window
  14237. 8:24:18well this new window still persists so
  14238. 8:24:21this could be useful for
  14239. 8:24:22let's say a login form or a login screen
  14240. 8:24:25after you log in you'll be brought to
  14241. 8:24:26your main program which could be within
  14242. 8:24:28the new window
  14243. 8:24:29one other thing that you can do too you
  14244. 8:24:31can also destroy the old window after
  14245. 8:24:33creating a new window
  14246. 8:24:35so we need to type in the name of the
  14247. 8:24:37window that we want to destroy
  14248. 8:24:39actually let's call the main window just
  14249. 8:24:41old underscore window just to clarify
  14250. 8:24:43things
  14251. 8:24:44so let's rename that here here and here
  14252. 8:24:47as well
  14253. 8:24:48so we want to destroy our old window as
  14254. 8:24:51if we're closing out of it
  14255. 8:24:52so we type in the name of the window old
  14256. 8:24:55underscore window
  14257. 8:24:56and we use the destroy function and that
  14258. 8:24:58sounds pretty sweet
  14259. 8:25:00so we're going to destroy our old window
  14260. 8:25:02after creating
  14261. 8:25:03our new window so let me add a comment
  14262. 8:25:05that this will
  14263. 8:25:06close out of old window
  14264. 8:25:10and let's test it so we have our old
  14265. 8:25:13window
  14266. 8:25:14our main window we'll create a new
  14267. 8:25:16window it creates our new window and
  14268. 8:25:18then closes out of the old window
  14269. 8:25:20so that's one of a few ways in which you
  14270. 8:25:22can create a new window for your program
  14271. 8:25:24so if you want to copy of all this code
  14272. 8:25:26i'll post all of this in the comments
  14273. 8:25:28down below
  14274. 8:25:28but yeah that's how to create a new
  14275. 8:25:30window using python
  14276. 8:25:32hey what's going on everybody too bro
  14277. 8:25:34here hope you're doing well and in this
  14278. 8:25:36video i'm going to teach you guys how we
  14279. 8:25:37can create separate tabs for our gui
  14280. 8:25:39applications in python so
  14281. 8:25:41sit back relax and enjoy the show
  14282. 8:25:45okay let's begin so in order to create
  14283. 8:25:47tabs for our python gui application
  14284. 8:25:50we're going to need access to a widget
  14285. 8:25:52called notebook
  14286. 8:25:53and that's found in a different module
  14287. 8:25:56so this time we're going to need
  14288. 8:25:57two imports so at the top from tk enter
  14289. 8:26:00import
  14290. 8:26:01just everything but the notebook widget
  14291. 8:26:03is found within a separate module
  14292. 8:26:05so we'll need a second import from tk
  14293. 8:26:09enter
  14294. 8:26:10import ttk so this ttk import
  14295. 8:26:14gives us access to several different
  14296. 8:26:16widgets that are normally not available
  14297. 8:26:18to us
  14298. 8:26:18so once you have these two imports
  14299. 8:26:20you're ready to begin so we'll need a
  14300. 8:26:22window
  14301. 8:26:23window equals tk followed by window.main
  14302. 8:26:26loop
  14303. 8:26:26and we need to create a notebook widget
  14304. 8:26:29so
  14305. 8:26:30right after you create your window we're
  14306. 8:26:31going to create a notebook
  14307. 8:26:33we'll call this notebook equals ttk
  14308. 8:26:37dot notebook and we want to add
  14309. 8:26:40this notebook to our window so let me
  14310. 8:26:43add a comment here what this widget does
  14311. 8:26:45so this is a widget that manages
  14312. 8:26:49a collection of windows
  14313. 8:26:52and displays and that's really all there
  14314. 8:26:55is to it
  14315. 8:26:56so in order to create different tabs
  14316. 8:26:58we're going to create some frames
  14317. 8:27:00so for the first tab i'll call this tab
  14318. 8:27:021 equals frame
  14319. 8:27:04and we want to add this to the notebook
  14320. 8:27:08and with our notebook we're adding this
  14321. 8:27:10notebook to the window
  14322. 8:27:12all right so this will be a new frame
  14323. 8:27:16for tab one and let's create a second
  14324. 8:27:19tab
  14325. 8:27:19and we'll call this tab two and you can
  14326. 8:27:22create more tabs if you want but that
  14327. 8:27:24might be overkill
  14328. 8:27:25so this is a new frame for tab two
  14329. 8:27:28after you finish these two lines of code
  14330. 8:27:30we're going to follow this up with
  14331. 8:27:32notebook.ad so notebook.ad
  14332. 8:27:36what widget are we adding we're adding
  14333. 8:27:39our tab
  14334. 8:27:391 frame and we can set some text for
  14335. 8:27:43this tab so text equals and i'll type in
  14336. 8:27:46tab one and we'll repeat the process for
  14337. 8:27:49tab two
  14338. 8:27:51so notebook dot add tab two
  14339. 8:27:54and we'll change the text to tab two and
  14340. 8:27:57then we need to
  14341. 8:27:58pack this notebook notebook dot pack
  14342. 8:28:02and let's take a look to see what we
  14343. 8:28:04have all right we have
  14344. 8:28:06tab one and tab two but we'll probably
  14345. 8:28:09want to actually add something
  14346. 8:28:11to each of these tabs so that the
  14347. 8:28:13content is visible so let's create some
  14348. 8:28:15labels
  14349. 8:28:15so after we pack our notebook let's
  14350. 8:28:18create some labels
  14351. 8:28:19i don't really feel like giving these
  14352. 8:28:21names because we don't really plan on
  14353. 8:28:22using them for anything else
  14354. 8:28:24so we'll just say label tab
  14355. 8:28:27one that's going to be the parent widget
  14356. 8:28:30and let's set some text text equals
  14357. 8:28:33hello this is tab
  14358. 8:28:36number one then i think i'll give this a
  14359. 8:28:40width and a height too
  14360. 8:28:41just so it's more visible so for the
  14361. 8:28:44width i'll make this 50
  14362. 8:28:45and the height 25 so this will be
  14363. 8:28:48roughly a square shape
  14364. 8:28:51and then we need to pack this so i'm
  14365. 8:28:52just going to add pack
  14366. 8:28:55and let's create a second label for tab
  14367. 8:28:57two
  14368. 8:28:58so label tab two maybe i'll say
  14369. 8:29:02goodbye this is tab number two
  14370. 8:29:05okay we should have some actual content
  14371. 8:29:08for these labels now for these tabs
  14372. 8:29:10so here's the label this is tab one and
  14373. 8:29:13this is tab two and you can switch
  14374. 8:29:15between these and they display
  14375. 8:29:16each of these separate labels however
  14376. 8:29:19with the way that this window is
  14377. 8:29:20arranged
  14378. 8:29:21if we were to expand the size of this
  14379. 8:29:23window well
  14380. 8:29:25our frames and our tabs kind of stay in
  14381. 8:29:27the top
  14382. 8:29:28and in the center so i'm going to add a
  14383. 8:29:30few lines within
  14384. 8:29:32the pack function of our notebook the
  14385. 8:29:34first thing i'm going to do
  14386. 8:29:35is use the expand option and set this to
  14387. 8:29:38true
  14388. 8:29:40so what this does and i'm just going to
  14389. 8:29:41add a comment that expand
  14390. 8:29:44this will expand
  14391. 8:29:47to fill any space
  14392. 8:29:50not otherwise
  14393. 8:29:53used so what happens now
  14394. 8:29:57is that this will actually expand
  14395. 8:30:00and it's staying in the center of the
  14396. 8:30:02window but there's one more thing we're
  14397. 8:30:04going to add
  14398. 8:30:05fill equals both
  14399. 8:30:08normally this is x or y or you can do
  14400. 8:30:11both
  14401. 8:30:12so i'll add a comment as to what this
  14402. 8:30:14option does
  14403. 8:30:16so fill will fill
  14404. 8:30:19space on x and
  14405. 8:30:22y axis so if you combine
  14406. 8:30:26both of these together what you get is
  14407. 8:30:28that these tabs will stay
  14408. 8:30:30in the top left corner and if you were
  14409. 8:30:32to expand the size of this window
  14410. 8:30:34the tabs aren't going to move they're
  14411. 8:30:36going to stick to the top left
  14412. 8:30:38but the size of the window will expand
  14413. 8:30:41all right so that's the basics of
  14414. 8:30:42creating tabs in python if you want a
  14415. 8:30:44copy of all this code
  14416. 8:30:45i'll post all of this in the comments
  14417. 8:30:47down below but yeah that's the basics of
  14418. 8:30:49creating tabs in python
  14419. 8:30:52hey what's going on everybody it's your
  14420. 8:30:54bro here hope you're doing well and in
  14421. 8:30:56this video i'm going to teach you guys
  14422. 8:30:57all about the grid geometry manager in
  14423. 8:30:59python so sit back relax and enjoy the
  14424. 8:31:02show
  14425. 8:31:04all right everybody let's talk about the
  14426. 8:31:06grid geometry manager
  14427. 8:31:07well because we have to this is a useful
  14428. 8:31:10way to organize widgets within a
  14429. 8:31:12container
  14430. 8:31:12imagine our windows being arranged into
  14431. 8:31:14a series of
  14432. 8:31:16rows and columns similar to an excel
  14433. 8:31:18spreadsheet
  14434. 8:31:19i tend to use that example a lot the top
  14435. 8:31:21left most cell
  14436. 8:31:22is row zero column zero computers always
  14437. 8:31:25begin with zero so that's why our grid
  14438. 8:31:27doesn't begin with
  14439. 8:31:28row one column one and we can list which
  14440. 8:31:31row and column
  14441. 8:31:32we would like our widgets to be placed
  14442. 8:31:34within and by default we only have
  14443. 8:31:36one row and one column to work with at
  14444. 8:31:38the start but we can explicitly state
  14445. 8:31:41which row and column this widget should
  14446. 8:31:43be placed within
  14447. 8:31:44relative to the container that it's in
  14448. 8:31:46and python will create new relative rows
  14449. 8:31:48and columns for us if we state where
  14450. 8:31:50these widgets
  14451. 8:31:50should be placed so let's create a small
  14452. 8:31:53submission form
  14453. 8:31:54and then organize all of our widgets
  14454. 8:31:55using our new grid system that we have
  14455. 8:31:58so let's begin by creating a window
  14456. 8:32:00window equals tk
  14457. 8:32:01followed by window.mainloop now that we
  14458. 8:32:04have our window we'll need to create
  14459. 8:32:05some widgets to add to this window
  14460. 8:32:07so let's say that we want an entry box
  14461. 8:32:10and a label for a first name last name
  14462. 8:32:12and email address as well as a submit
  14463. 8:32:15button
  14464. 8:32:15so let's create a label and an entry box
  14465. 8:32:18for a first name just to demonstrate
  14466. 8:32:20this
  14467. 8:32:21so let's call this label first name
  14468. 8:32:24label equals label we're adding this to
  14469. 8:32:27the window
  14470. 8:32:27and let's set the text to first
  14471. 8:32:31name for now i'm just going to use that
  14472. 8:32:34pack to demonstrate the differences
  14473. 8:32:36between
  14474. 8:32:36pack and grid and let's create an entry
  14475. 8:32:40box
  14476. 8:32:41let's call this first name
  14477. 8:32:45entry equals entry
  14478. 8:32:48we are adding this to the window and for
  14479. 8:32:51now we're just going to
  14480. 8:32:52pack this and let's take a look
  14481. 8:32:57so here's our label on the top it says
  14482. 8:32:59first name and then it has
  14483. 8:33:01our entry box right underneath so what
  14484. 8:33:03if we want to have
  14485. 8:33:05this label on the left hand side and our
  14486. 8:33:08entry box on the right hand side of the
  14487. 8:33:09label
  14488. 8:33:10we're better off using the grid geometry
  14489. 8:33:13manager
  14490. 8:33:14so we're going to replace pack with grid
  14491. 8:33:21and we have to set which row and column
  14492. 8:33:23that we want each of these widgets to be
  14493. 8:33:24placed within
  14494. 8:33:26normally if you use the pack geometry
  14495. 8:33:29manager
  14496. 8:33:29as you create new widgets they're just
  14497. 8:33:31going to be placed directly underneath
  14498. 8:33:33in one long column so
  14499. 8:33:36let's say that for our name label we
  14500. 8:33:39want to place this where
  14501. 8:33:41the row is zero and the column is zero
  14502. 8:33:43that's the top left corner
  14503. 8:33:45so within the grid geometry manager when
  14504. 8:33:48we call it
  14505. 8:33:49we're going to set row to equal zero as
  14506. 8:33:52well as column
  14507. 8:33:53to equal zero and then for the first
  14508. 8:33:56name
  14509. 8:33:56entry we want this to be on the right
  14510. 8:33:58hand side of our label
  14511. 8:34:00so we'll place this within the same row
  14512. 8:34:03row
  14513. 8:34:03equals zero but in a different column
  14514. 8:34:06column
  14515. 8:34:07equals one so column one is directly to
  14516. 8:34:10the right of column zero
  14517. 8:34:12and then when we compile and run this we
  14518. 8:34:14now have our entry box
  14519. 8:34:16to the right of our label that we have
  14520. 8:34:18so let's repeat the process
  14521. 8:34:19for a last name an email address and
  14522. 8:34:22then a submit button
  14523. 8:34:23so i'm just going to copy what we have
  14524. 8:34:25here and change first name to
  14525. 8:34:27last name so last name label
  14526. 8:34:31and we'll change the text to last name
  14527. 8:34:35last name entry and we'll keep that the
  14528. 8:34:38same
  14529. 8:34:39now we want this last name label to be
  14530. 8:34:42underneath
  14531. 8:34:43the first name label because right now
  14532. 8:34:45they're going to overlap because we have
  14533. 8:34:47them in the same
  14534. 8:34:48row and columns so you can see that we
  14535. 8:34:50can no longer see
  14536. 8:34:51the first name label or the first name
  14537. 8:34:53entry
  14538. 8:34:54so let's place this directly underneath
  14539. 8:34:56we're going to put these underneath by
  14540. 8:34:58one row
  14541. 8:34:59so we'll keep the columns the same but
  14542. 8:35:01we're going to
  14543. 8:35:02put this underneath by one row so row
  14544. 8:35:05one
  14545. 8:35:05and row one for the last name entry and
  14546. 8:35:08what you get is that
  14547. 8:35:10these two labels and entry boxes are
  14548. 8:35:12arranged neatly into
  14549. 8:35:14rows and columns and let's do the same
  14550. 8:35:16thing for an email
  14551. 8:35:18so i'm going to copy what we have and
  14552. 8:35:20we're going to change
  14553. 8:35:22last name to email so
  14554. 8:35:25email label and email entry and then
  14555. 8:35:28change
  14556. 8:35:28for the text last name to just email
  14557. 8:35:31and we're going to place these where row
  14558. 8:35:34equals 2
  14559. 8:35:35and we'll keep the column the same and
  14560. 8:35:37for email entry
  14561. 8:35:39row 2 column one
  14562. 8:35:43so doesn't that look a lot better now
  14563. 8:35:45let's create a submit button
  14564. 8:35:48so let's call this submit button
  14565. 8:35:52equals button and then we're adding this
  14566. 8:35:54to the window
  14567. 8:35:56and we should set some text text equals
  14568. 8:35:59submit i guess dot grid
  14569. 8:36:03and we want to place this underneath
  14570. 8:36:06currently kind of just shoves it
  14571. 8:36:07underneath for now
  14572. 8:36:08let's say that we want this between
  14573. 8:36:11these two columns
  14574. 8:36:13so we'll want to set the row and the
  14575. 8:36:15column to begin with
  14576. 8:36:17row is going to equal three
  14577. 8:36:20and column we'll set this equal to zero
  14578. 8:36:22for now
  14579. 8:36:23there isn't going to be any apparent
  14580. 8:36:24change it's still going to be in the
  14581. 8:36:26same place
  14582. 8:36:27but one thing that we can do is use an
  14583. 8:36:29option called column
  14584. 8:36:30span we can have a widget take up more
  14585. 8:36:32than one column
  14586. 8:36:34and it's going to place this widget in
  14587. 8:36:36between both of these columns
  14588. 8:36:38in between the combined width of both of
  14589. 8:36:40them
  14590. 8:36:41so we'll add column span
  14591. 8:36:44equals two so this widget is going to
  14592. 8:36:47take up
  14593. 8:36:48the next two available columns including
  14594. 8:36:50the one that it's currently in and you
  14595. 8:36:52can see that our submit button
  14596. 8:36:54is now between these two columns that we
  14597. 8:36:56have
  14598. 8:36:57one thing that you should know is that
  14599. 8:36:59the column width is dependent on the
  14600. 8:37:01width
  14601. 8:37:01of the largest widget that is contained
  14602. 8:37:03within that particular column
  14603. 8:37:05let's say that we increase the width of
  14604. 8:37:07our first name
  14605. 8:37:08label well our column is going to expand
  14606. 8:37:11to actually fit
  14607. 8:37:12that widget that we have so for our
  14608. 8:37:14first name label
  14609. 8:37:15let's set the width to maybe 20
  14610. 8:37:19and this is going to increase the width
  14611. 8:37:22of column 0
  14612. 8:37:23and to even better demonstrate this
  14613. 8:37:25let's color
  14614. 8:37:26each label a certain color just for a
  14615. 8:37:28visual
  14616. 8:37:29for our first name label let's say this
  14617. 8:37:31is red and then for
  14618. 8:37:34our last name label let's set this to
  14619. 8:37:36green
  14620. 8:37:38and then for email maybe blue
  14621. 8:37:46now all of these are color coordinated
  14622. 8:37:48and you can see that
  14623. 8:37:49the background color of our first name
  14624. 8:37:51label
  14625. 8:37:52is taking up the entire column space now
  14626. 8:37:55what if we changed the size of our
  14627. 8:37:58email label to something even larger
  14628. 8:38:00like 30
  14629. 8:38:01so width equals 30.
  14630. 8:38:06and now the column 0 has expanded to
  14631. 8:38:09accommodate the size of our largest
  14632. 8:38:11widget which is our email
  14633. 8:38:13which has a width of 30 and before we
  14634. 8:38:15finish this video let's add a
  14635. 8:38:17title to the submission form right on
  14636. 8:38:19top of the first name label and the
  14637. 8:38:22entry label so let's call this title
  14638. 8:38:25label equals label writing this to the
  14639. 8:38:28window
  14640. 8:38:28let's set some text let's say enter
  14641. 8:38:32your info and let's pick font
  14642. 8:38:36font equals
  14643. 8:38:40what am i feeling like today maybe just
  14644. 8:38:42arial
  14645. 8:38:43and then a size maybe 25 we're going to
  14646. 8:38:46use the
  14647. 8:38:47grid geometry manager
  14648. 8:38:50and we want this at the top so we want
  14649. 8:38:53row zero at least
  14650. 8:38:55i want this to take up two columns so
  14651. 8:38:57we'll say column
  14652. 8:38:59equals zero and then set a column span
  14653. 8:39:02column span equals two
  14654. 8:39:05now a few things are going to overlap
  14655. 8:39:07here
  14656. 8:39:09so we're going to move everything down
  14657. 8:39:11by one row
  14658. 8:39:13besides our title label so we want
  14659. 8:39:16this first name label to be directly
  14660. 8:39:18underneath our title label
  14661. 8:39:20so let's move everything down by one row
  14662. 8:39:32and our title is going to be directly on
  14663. 8:39:34top of our first name label as well as
  14664. 8:39:37the first name
  14665. 8:39:38entry box that we have all right so
  14666. 8:39:40that's the very basics of the grid
  14667. 8:39:42geometry manager
  14668. 8:39:43if you want a copy of all this code i'll
  14669. 8:39:45post all of this in the comments down
  14670. 8:39:47below
  14671. 8:39:47but yeah that's the basics of the grid
  14672. 8:39:49geometry manager
  14673. 8:39:50in python hey what's going on everybody
  14674. 8:39:53it's your bro here hope you're doing
  14675. 8:39:55well and in this video i'm going to
  14676. 8:39:56teach you guys how we can create a
  14677. 8:39:57progress bar in python so
  14678. 8:39:59sit back relax and enjoy the show
  14679. 8:40:03okay let's create a progress bar but
  14680. 8:40:05we'll need a few imports to begin
  14681. 8:40:07the progress bar widget is found within
  14682. 8:40:09that ttk module so we'll need a separate
  14683. 8:40:12import from that
  14684. 8:40:13from tk enter dot ttk
  14685. 8:40:16import everything and we'll import
  14686. 8:40:19everything from time as well let's
  14687. 8:40:22create a window
  14688. 8:40:23window equals tk followed by
  14689. 8:40:25window.mainloop
  14690. 8:40:26we're going to add a progress bar and a
  14691. 8:40:28button to this window
  14692. 8:40:30when we click on the button it's going
  14693. 8:40:31to fill our progress bar
  14694. 8:40:33let's begin by creating a button to
  14695. 8:40:35begin with let's call this button
  14696. 8:40:37equals button we're adding this to the
  14697. 8:40:40window
  14698. 8:40:41let's set some text text equals maybe
  14699. 8:40:43download
  14700. 8:40:45and then a command maybe i'll call this
  14701. 8:40:48start
  14702. 8:40:50finish by adding this button to the
  14703. 8:40:52window and we'll just use pack to do
  14704. 8:40:54that
  14705. 8:40:55let's define what our start function
  14706. 8:40:57does
  14707. 8:40:58def start we'll fill this in later i'm
  14708. 8:41:01just going to
  14709. 8:41:02print nothing just as a placeholder and
  14710. 8:41:05we should just have a button
  14711. 8:41:07now let's add a progress bar before this
  14712. 8:41:09button
  14713. 8:41:11and we'll call this progress bar just
  14714. 8:41:12bar for short bar
  14715. 8:41:14equals progress bar
  14716. 8:41:18we're adding this to the window
  14717. 8:41:22we can set an orientation
  14718. 8:41:25this can be horizontal or vertical later
  14719. 8:41:28on in this video i'll show you the
  14720. 8:41:29differences between the two
  14721. 8:41:30but for now let's set this to horizontal
  14722. 8:41:34and then i'm going to pack this bar bar
  14723. 8:41:37dot pack
  14724. 8:41:38and add some padding pad y
  14725. 8:41:41equals 10 and let's see what we have
  14726. 8:41:46okay this is our progress bar at the top
  14727. 8:41:48and this is the button
  14728. 8:41:49when we click on this button it's going
  14729. 8:41:51to fill our progress bar
  14730. 8:41:53but i think i'm going to increase the
  14731. 8:41:55length of this progress bar
  14732. 8:41:57let's set this to maybe 300 i believe
  14733. 8:41:59100 is the default
  14734. 8:42:01okay that's not too bad of a size now
  14735. 8:42:04let's define what our start function
  14736. 8:42:06does
  14737. 8:42:07we can actually increase the value of
  14738. 8:42:10our progress bar by typing in the name
  14739. 8:42:11of this progress bar here
  14740. 8:42:14value plus equals some amount let's say
  14741. 8:42:1810. so what's going to happen right now
  14742. 8:42:22is that every time we click on this
  14743. 8:42:24button it's going to fill our progress
  14744. 8:42:26bar by
  14745. 8:42:2710 out of the total which is 100
  14746. 8:42:32let's pretend that we're waiting for a
  14747. 8:42:34certain amount of tasks
  14748. 8:42:35to complete so let's define a few
  14749. 8:42:38variables
  14750. 8:42:39let's say we have a variable called
  14751. 8:42:41tasks and let's set this to 10 just to
  14752. 8:42:43keep it simple
  14753. 8:42:45and x will represent the current task
  14754. 8:42:47that we're on
  14755. 8:42:48and we'll say 0 to begin with let's
  14756. 8:42:51create a while loop
  14757. 8:42:53while x is less than the amount of
  14758. 8:42:57tasks that we have we will
  14759. 8:43:00increment the value of our bar by 10.
  14760. 8:43:0310 10 for each test that we have which
  14761. 8:43:06is 10 for a total of 100
  14762. 8:43:09and we'll want to increment the task
  14763. 8:43:11that we're on as well
  14764. 8:43:12which is represented by x so let's
  14765. 8:43:15increment x by 1 after each iteration
  14766. 8:43:18of this while loop now when we run this
  14767. 8:43:21and click download this spills instantly
  14768. 8:43:25let's add a delay just to simulate
  14769. 8:43:29each task waiting to be completed so
  14770. 8:43:32let's add that here let's add a delay by
  14771. 8:43:35using time
  14772. 8:43:36dot sleep and let's set this to one
  14773. 8:43:38second
  14774. 8:43:40we're going to wait one second and then
  14775. 8:43:41complete a task
  14776. 8:43:45what ends up happening now is that
  14777. 8:43:46nothing appears to be updating
  14778. 8:43:48well what gives bro you lie to me well
  14779. 8:43:50that's because
  14780. 8:43:51the window is waiting to have this
  14781. 8:43:53progress bar complete
  14782. 8:43:55before it's actually going to display it
  14783. 8:43:57being filled
  14784. 8:43:58so we would want to refresh this window
  14785. 8:44:01after each iteration of the while loop
  14786. 8:44:04so at the end of this while loop let's
  14787. 8:44:06add one thing
  14788. 8:44:07window dot update underscore idle
  14789. 8:44:11tasks after each iteration of this while
  14790. 8:44:14loop it's going to update
  14791. 8:44:16the window that we have so then when we
  14792. 8:44:18download something
  14793. 8:44:20it's going to update our progress bar
  14794. 8:44:22after each iteration of this while loop
  14795. 8:44:24until it's completed
  14796. 8:44:26right about now let's take this a step
  14797. 8:44:29further by adding a label that displays
  14798. 8:44:31the current percent of the progress bar
  14799. 8:44:33that is complete
  14800. 8:44:34let's add this label after the progress
  14801. 8:44:36bar but before the button
  14802. 8:44:38sandwiched right between these two
  14803. 8:44:39widgets so let's add that here
  14804. 8:44:42we'll call this label percent label
  14805. 8:44:45equals label we'll add this to the
  14806. 8:44:47window
  14807. 8:44:48and we'll need a text variable text
  14808. 8:44:51variable equals let's call this percent
  14809. 8:44:54so the reason that we use a text
  14810. 8:44:56variable is so that we can update this
  14811. 8:44:58label with some text
  14812. 8:45:00after each iteration of our while loop
  14813. 8:45:02and we'll need to
  14814. 8:45:03pack this okay within the window
  14815. 8:45:07near the top let's define percent
  14816. 8:45:09percent
  14817. 8:45:10equals string var this allows us to
  14818. 8:45:14update percent with some new text and
  14819. 8:45:16set the percent label with the new text
  14820. 8:45:18that we have
  14821. 8:45:20within the while loop but before we
  14822. 8:45:22update idle tasks
  14823. 8:45:25let's set what percent is
  14824. 8:45:28percent dot set and let's say
  14825. 8:45:32x divided by tasks and then we'll
  14826. 8:45:35multiply this by
  14827. 8:45:36100 i'm just going to put this within
  14828. 8:45:39its own set of
  14829. 8:45:40parentheses times 100
  14830. 8:45:43and then we'll convert all of this to a
  14831. 8:45:44string
  14832. 8:45:46so we can display this along with some
  14833. 8:45:48text
  14834. 8:45:50there are a lot of parentheses here so
  14835. 8:45:51it might be difficult to keep track of
  14836. 8:45:53all this
  14837. 8:45:54all right and then we're just going to
  14838. 8:45:56tack on
  14839. 8:45:57percent and that's it let's try it
  14840. 8:46:03so we got 10 20 30
  14841. 8:46:0740 50. i'll shut up now one change that
  14842. 8:46:10i'm gonna make is that i'm going to cast
  14843. 8:46:13all of this
  14844. 8:46:13as an integer before we convert it to a
  14845. 8:46:15string
  14846. 8:46:18because i do not like that decimal at
  14847. 8:46:20the end of our percentage
  14848. 8:46:22so yeah it says 10 percent 20 so on and
  14849. 8:46:25so forth
  14850. 8:46:26let's add a label that displays the
  14851. 8:46:28current task that we're on
  14852. 8:46:29so let's copy what we have for our
  14853. 8:46:31percent label
  14854. 8:46:32and let's rename this as maybe task
  14855. 8:46:35label
  14856. 8:46:38task label text variable will equal
  14857. 8:46:41maybe text
  14858. 8:46:43and let's define what text is so text
  14859. 8:46:46is also a string var and within our
  14860. 8:46:50while loop
  14861. 8:46:51let's update the value of our text
  14862. 8:46:54text dot set
  14863. 8:46:57will display x plus maybe a forward
  14864. 8:47:01slash
  14865. 8:47:05plus our tasks
  14866. 8:47:08and since we're displaying integers
  14867. 8:47:10along with some strings
  14868. 8:47:12we need to convert these to strings and
  14869. 8:47:14we can use the str function to do that
  14870. 8:47:16for us
  14871. 8:47:19so surround x and tasks with str to
  14872. 8:47:22convert these to strings
  14873. 8:47:25and then we'll add just plus tasks
  14874. 8:47:28completed so the value of x will update
  14875. 8:47:33after each iteration of this while loop
  14876. 8:47:35let's try it
  14877. 8:47:36and after clicking download this will
  14878. 8:47:39display the percentage as well as the
  14879. 8:47:41amount of tasks that have been completed
  14880. 8:47:44all the way until it reaches the end
  14881. 8:47:49let's change this into a more practical
  14882. 8:47:51example
  14883. 8:47:52we'll pretend that we're downloading a
  14884. 8:47:53video game so let's replace or rename
  14885. 8:47:56tasks as maybe gb4 gigabytes
  14886. 8:48:00and this game will be 100 gigabytes
  14887. 8:48:03we'll rename x as well so let's refactor
  14888. 8:48:07rename and change x to maybe download to
  14889. 8:48:09represent
  14890. 8:48:10the amount of gigabytes that are
  14891. 8:48:12downloaded
  14892. 8:48:14and let's create a variable called speed
  14893. 8:48:17and let's set this to one
  14894. 8:48:19alright so while our download
  14895. 8:48:22is less than the amount of gigabytes for
  14896. 8:48:24this game
  14897. 8:48:25we'll sleep for maybe a portion of a
  14898. 8:48:28second
  14899. 8:48:28.05 should be good we'll increase
  14900. 8:48:32the value of our bar by
  14901. 8:48:35this amount speed divided by
  14902. 8:48:38gigabytes and then multiply this by 100
  14903. 8:48:44for the download we're going to
  14904. 8:48:46increment this by whatever
  14905. 8:48:47our speed is and right now it's one so
  14906. 8:48:49that's the same
  14907. 8:48:50we can keep percent the same for our
  14908. 8:48:53text
  14909. 8:48:53let's change tasks to gigabytes
  14910. 8:48:57and that's all the changes that we have
  14911. 8:48:58to make and
  14912. 8:49:00now when we download something we're
  14913. 8:49:02kind of pretending that we're
  14914. 8:49:04downloading
  14915. 8:49:05a large file or a game you can see that
  14916. 8:49:07once it reaches 100 it says 100
  14917. 8:49:10100 out of 100 gigabytes completed now
  14918. 8:49:13we can change
  14919. 8:49:14this to a different value let's say
  14920. 8:49:15maybe 50
  14921. 8:49:17and the speed will be maybe two
  14922. 8:49:21so this will adjust to accommodate
  14923. 8:49:23whatever the new file size is as well as
  14924. 8:49:25the speed
  14925. 8:49:26and before i forget you can change the
  14926. 8:49:28orientation to a
  14927. 8:49:30vertical bar if you so choose so we have
  14928. 8:49:33this vertical download bar now
  14929. 8:49:35which could be good for something all
  14930. 8:49:37right so that's the basics
  14931. 8:49:39of progress bars in python if you want
  14932. 8:49:41to copy of all this code
  14933. 8:49:42i'll post all of this in the comments
  14934. 8:49:44down below but yeah that's the basics of
  14935. 8:49:46progress bars
  14936. 8:49:47in python what's going on everybody it's
  14937. 8:49:50your bro here hope you're doing well and
  14938. 8:49:52in this video i'm going to teach you
  14939. 8:49:53guys how we can use the canvas widget to
  14940. 8:49:55draw some simple shapes so
  14941. 8:49:56sit back relax and enjoy the show
  14942. 8:50:01welcome back everyone well in this video
  14943. 8:50:03we're going to be creating a canvas
  14944. 8:50:05a canvas is a widget that is used to
  14945. 8:50:07draw simple graphs plots and images
  14946. 8:50:09within a window
  14947. 8:50:11we're going to be creating some simple
  14948. 8:50:12shapes in this video not anything too
  14949. 8:50:14sophisticated i might get more into the
  14950. 8:50:16canvas widget in a separate video
  14951. 8:50:18and at the end of this lesson we're
  14952. 8:50:20going to practice by creating a pokeball
  14953. 8:50:22so to begin we'll need a window window
  14954. 8:50:25equals tk
  14955. 8:50:26followed by window.main loop to create a
  14956. 8:50:28canvas we can just simply call this
  14957. 8:50:30canvas
  14958. 8:50:31canvas equals canvas and we're adding
  14959. 8:50:33this canvas
  14960. 8:50:34to our window and then we need to follow
  14961. 8:50:36this with canvas.pack or
  14962. 8:50:38place but i'm just going to use pack
  14963. 8:50:40because it's simple
  14964. 8:50:41this is what our canvas looks like it's
  14965. 8:50:43a rectangular area which we can draw
  14966. 8:50:45graphics on
  14967. 8:50:46we can also set the width and the height
  14968. 8:50:48of this canvas so when we create this
  14969. 8:50:50canvas
  14970. 8:50:51we're going to set the options for a
  14971. 8:50:53height and a width
  14972. 8:50:54so let's begin with the height height
  14973. 8:50:56equals let's say 500
  14974. 8:50:58and the width will be the same 500 and
  14975. 8:51:01now we have
  14976. 8:51:02a square canvas that we can draw
  14977. 8:51:04graphics on
  14978. 8:51:05let's begin by creating some simple
  14979. 8:51:07shapes maybe a line
  14980. 8:51:08i think a line is a shape maybe a
  14981. 8:51:10one-dimensional one i don't know
  14982. 8:51:12i don't geometry so let's create a
  14983. 8:51:14simple line
  14984. 8:51:16so we type the name of the canvas dot
  14985. 8:51:19create and there's a bunch of different
  14986. 8:51:20shapes that we can create
  14987. 8:51:21let's create a line
  14988. 8:51:24now i'm just going to turn this into a
  14989. 8:51:26comment for a moment we need to set some
  14990. 8:51:28coordinates
  14991. 8:51:28for this line this top left corner of
  14992. 8:51:31our canvas
  14993. 8:51:32is 0 0 and the bottom right corner of
  14994. 8:51:35this canvas is
  14995. 8:51:36500 by 500 depends on what you set the
  14996. 8:51:39dimensions of the canvas to
  14997. 8:51:41so when we create a line we need a
  14998. 8:51:43starting point and an ending point
  14999. 8:51:45so if we want this to start in the top
  15000. 8:51:47left corner we're going to pass in
  15001. 8:51:490 0 that is the starting position and we
  15002. 8:51:52can place the coordinates for the ending
  15003. 8:51:54position
  15004. 8:51:54if we want this line to end in the
  15005. 8:51:56bottom right corner that would be
  15006. 8:51:58where x is 500 and y is 500 and this is
  15007. 8:52:02what this looks like
  15008. 8:52:03we just have a simple line going across
  15009. 8:52:05the screen
  15010. 8:52:06but that's not all we can also change
  15011. 8:52:08the color of this line
  15012. 8:52:10and the thickness let's begin by
  15013. 8:52:11changing the color so
  15014. 8:52:13we can do that with fill fill equals
  15015. 8:52:16let's pick color blue
  15016. 8:52:20and now we have a blue line going across
  15017. 8:52:22the screen
  15018. 8:52:23but we can also change the width of this
  15019. 8:52:25as well so we can do that
  15020. 8:52:26using the width option and let's set
  15021. 8:52:28this to five
  15022. 8:52:30now we get an extra thick line across
  15023. 8:52:32the screen
  15024. 8:52:34now let's make a red line
  15025. 8:52:37so i'm just going to copy this
  15026. 8:52:40canvas.createline and let's change
  15027. 8:52:42some coordinates for this uh let's say
  15028. 8:52:45we want this to start in the bottom left
  15029. 8:52:47corner and go up to the top right
  15030. 8:52:50so the starting x position would be zero
  15031. 8:52:53y would actually be 500 x
  15032. 8:52:56for the ending position would be 500 and
  15033. 8:52:58y would be zero
  15034. 8:53:00let's set this to red and we'll keep the
  15035. 8:53:03width the same
  15036. 8:53:06okay so here's our red line and notice
  15037. 8:53:08that this red line
  15038. 8:53:10is overlapping the blue line so when you
  15039. 8:53:12create shapes
  15040. 8:53:13on a canvas if there's any that overlap
  15041. 8:53:16the one that you most recently created
  15042. 8:53:18will appear on the top one option
  15043. 8:53:20available to you is that you can
  15044. 8:53:22actually
  15045. 8:53:22give these graphics a unique name with
  15046. 8:53:25our blue line we could call this
  15047. 8:53:26blue line blue line equals all of this
  15048. 8:53:30and then we can call our red line
  15049. 8:53:32something different like
  15050. 8:53:34red line this would be useful if you
  15051. 8:53:35want to call or use a
  15052. 8:53:37graphic by its name if you want to like
  15053. 8:53:40move a graphic on the screen or have
  15054. 8:53:41something appear
  15055. 8:53:42you can use this by its name but we
  15056. 8:53:44don't really need it for this lesson so
  15057. 8:53:46i'll just revert this back to what we
  15058. 8:53:48had previously
  15059. 8:53:49i'm just letting you know that you can
  15060. 8:53:50do that alright what do we have up next
  15061. 8:53:52let's create a rectangle so canvas dot
  15062. 8:53:55create
  15063. 8:53:56rectangle we can set some starting
  15064. 8:53:59coordinates and ending coordinates
  15065. 8:54:01the starting coordinates are for the top
  15066. 8:54:03left of the rectangle
  15067. 8:54:04and the ending coordinates are for the
  15068. 8:54:05bottom right so for the starting
  15069. 8:54:07coordinates let's say
  15070. 8:54:08we're going to place this where x is 50
  15071. 8:54:11y is 50.
  15072. 8:54:12for the ending coordinates we want this
  15073. 8:54:14where
  15074. 8:54:15x is 250 and y is 250 i'm just making up
  15075. 8:54:18numbers
  15076. 8:54:19put it whatever you want really and this
  15077. 8:54:22is what this looks like
  15078. 8:54:24but i think i'm going to comment out
  15079. 8:54:26these lines for now
  15080. 8:54:29and here is our rectangle well i guess
  15081. 8:54:31it's also a square
  15082. 8:54:33we can actually give this a fill color
  15083. 8:54:35as well so we do that with the fill
  15084. 8:54:37option
  15085. 8:54:38fill equals purple
  15086. 8:54:41i guess and now we have a
  15087. 8:54:44purple rectangle next on our list
  15088. 8:54:47is create polygon we can create many
  15089. 8:54:50sorts of shapes using create polygon but
  15090. 8:54:52it really depends on how many
  15091. 8:54:53coordinates that we enter
  15092. 8:54:55let's create a simple triangle so we
  15093. 8:54:57need at least three coordinates for this
  15094. 8:54:59to work
  15095. 8:54:59so canvas dot create polygon
  15096. 8:55:02and i have some coordinates picked out
  15097. 8:55:04already so let's place the first set of
  15098. 8:55:06coordinates where
  15099. 8:55:07x is 250 y is zero for the second set
  15100. 8:55:12where x is 500 and y is 500
  15101. 8:55:15in the last set the third set we'll
  15102. 8:55:17place this where x
  15103. 8:55:18is zero and y is 500 and what this looks
  15104. 8:55:22like
  15105. 8:55:22is that we have this black triangle
  15106. 8:55:25that's taking up most of the canvas
  15107. 8:55:27we can also change the fill color as
  15108. 8:55:29well so we do that with
  15109. 8:55:31the option fill and we can set this to
  15110. 8:55:33whatever color you want
  15111. 8:55:35let's say yellow and we now have a
  15112. 8:55:38yellow triangle on our screen and it
  15113. 8:55:40kind of resembles the triforce from the
  15114. 8:55:42legend of zelda series one of my
  15115. 8:55:44personal favorites
  15116. 8:55:45let's also add a border this is actually
  15117. 8:55:48done with
  15118. 8:55:48outline outline equals and then pick a
  15119. 8:55:51color for the outline
  15120. 8:55:52let's say black and we have a black
  15121. 8:55:54outline around
  15122. 8:55:56this graphic and you can also change the
  15123. 8:55:58thickness of this too
  15124. 8:55:59with width and let's set the width to
  15125. 8:56:01five
  15126. 8:56:02and now we have an extra thick line
  15127. 8:56:04around our triangle
  15128. 8:56:06with these shapes you can pass in a list
  15129. 8:56:09of points too
  15130. 8:56:10so i'm going to get rid of all these and
  15131. 8:56:12we're going to pass in
  15132. 8:56:14a list so let's create a list called
  15133. 8:56:16points
  15134. 8:56:17points equals all the points that we
  15135. 8:56:20just entered in previously
  15136. 8:56:21and then instead of passing in all these
  15137. 8:56:24coordinates individually i'm just going
  15138. 8:56:25to pass in our list of points
  15139. 8:56:28and this will work exactly the same okay
  15140. 8:56:31let's create an arc now so canvas dot
  15141. 8:56:34create
  15142. 8:56:34arc and arc is really just a
  15143. 8:56:37curved line between at two points
  15144. 8:56:40however when we list some coordinates
  15145. 8:56:42we're not listing the coordinates of the
  15146. 8:56:44starting position and ending position
  15147. 8:56:45for this arc it's more or less for the
  15148. 8:56:48amount of space that we want to allocate
  15149. 8:56:50to actually draw this arc
  15150. 8:56:52so let's say that we want this arc to
  15151. 8:56:54take up the entire canvas we can pass in
  15152. 8:56:56the dimensions
  15153. 8:56:57for this canvas so the top left corner
  15154. 8:56:59is zero zero
  15155. 8:57:00and the height and the width are five
  15156. 8:57:02hundred
  15157. 8:57:07so this is our arc it resembles a pie
  15158. 8:57:09slice it's not starting in the top left
  15159. 8:57:12corner and ending down here
  15160. 8:57:13that's the amount of space that we're
  15161. 8:57:15giving this crate arc function to
  15162. 8:57:17actually draw
  15163. 8:57:18an arc it's really an entire circle but
  15164. 8:57:21only a portion of it is visible
  15165. 8:57:23let's also set a fill color for this so
  15166. 8:57:25fill equals
  15167. 8:57:26what color did we not pick yet let's say
  15168. 8:57:29green
  15169. 8:57:32and now we have a green pie slice
  15170. 8:57:35there's different styles you can do too
  15171. 8:57:38so we can change that with style equals
  15172. 8:57:40the default is pi slice
  15173. 8:57:42that's what we had previously let's
  15174. 8:57:44change this to chord
  15175. 8:57:46this will draw a line between these two
  15176. 8:57:48points
  15177. 8:57:49and this kind of resembles like a bow
  15178. 8:57:51like a ball with arrows
  15179. 8:57:52and there's also just arc
  15180. 8:57:56where it's simply just a curved line
  15181. 8:57:59let's change this back to pie slice well
  15182. 8:58:02because i like pie
  15183. 8:58:04we can change the starting position of
  15184. 8:58:06where this arc begins with the option
  15185. 8:58:08start
  15186. 8:58:09so start equals 0 by default so there's
  15187. 8:58:12going to be no apparent change
  15188. 8:58:14let's adjust this by 90 degrees so this
  15189. 8:58:17is in
  15190. 8:58:18degrees so this shifted
  15191. 8:58:22what is this direction counterclockwise
  15192. 8:58:24by 90 degrees
  15193. 8:58:26so 180 would theoretically flip this
  15194. 8:58:31and now this is on the other side of
  15195. 8:58:33what we had previously
  15196. 8:58:34and then 270.
  15197. 8:58:40all right we can also set an extent
  15198. 8:58:44so the default is normally 90 degrees
  15199. 8:58:46but let's change this to 180
  15200. 8:58:49and now we have a what is that
  15201. 8:58:51hemisphere half a circle
  15202. 8:58:54all right people let's practice by
  15203. 8:58:55creating a pokeball there's still one
  15204. 8:58:57more function that we have to cover it's
  15205. 8:58:59called create oval but we'll do that as
  15206. 8:59:01we're making this
  15207. 8:59:02so let's begin by creating the top red
  15208. 8:59:04hemisphere of this pokeball so that's
  15209. 8:59:06canvas
  15210. 8:59:07dot create arc and we're going to set
  15211. 8:59:11the extent of this
  15212. 8:59:13to 0 0 500 500
  15213. 8:59:16so that this is going to take up the
  15214. 8:59:18entire canvas
  15215. 8:59:20and then let's also set a fill color to
  15216. 8:59:22red
  15217. 8:59:23so fill equals red
  15218. 8:59:26we need to change the extent of how far
  15219. 8:59:28this extends
  15220. 8:59:29so we'll do that with the extent
  15221. 8:59:31function so extent
  15222. 8:59:32equals 180 so that this is a half circle
  15223. 8:59:36and let's also change the thickness of
  15224. 8:59:39this outline as well
  15225. 8:59:40using the width option so width equals
  15226. 8:59:4410 would be good and we have
  15227. 8:59:47a red hemisphere for the top of our
  15228. 8:59:49pokeball let's work on the bottom half
  15229. 8:59:51so that is a white hemisphere
  15230. 8:59:54so canvas dot create arc these are all
  15231. 8:59:57the same we'll change
  15232. 8:59:58the fill to white the extent will be the
  15233. 9:00:01same
  15234. 9:00:02but we need a different starting
  15235. 9:00:03position so start
  15236. 9:00:05equals 180. so this is going to begin
  15237. 9:00:08at the 180 degree mark so when you
  15238. 9:00:11combine these both together we get
  15239. 9:00:13two different colored hemispheres that
  15240. 9:00:14form a perfect circle
  15241. 9:00:16now let's create an oval within the
  15242. 9:00:18center of this pokeball
  15243. 9:00:21so we're going to do that with canvas
  15244. 9:00:24dot create
  15245. 9:00:26oval we're going to set the amount of
  15246. 9:00:29space that we want to give
  15247. 9:00:30the circle to be drawn so we'll place
  15248. 9:00:33this where
  15249. 9:00:33x is 190 where y is 190
  15250. 9:00:37where x 2 or the second x position is
  15251. 9:00:40310
  15252. 9:00:41and y2 is 310
  15253. 9:00:46let's set the fill color next so we do
  15254. 9:00:48that with fill
  15255. 9:00:49fill equals white
  15256. 9:00:53and lastly let's just give this a
  15257. 9:00:55thicker border
  15258. 9:00:56so that is width
  15259. 9:01:00equals 10 to match the width for the
  15260. 9:01:03two arcs that we have and there you have
  15261. 9:01:06it we have a simple
  15262. 9:01:07pokeball all right well that's the
  15263. 9:01:09basics of the canvas widget if you'd
  15264. 9:01:11like a copy of all this code i'll post
  15265. 9:01:13all of this in the comments down below
  15266. 9:01:15but yeah that's the basics of the canvas
  15267. 9:01:17widget in python
  15268. 9:01:18what's going on everybody it's your bro
  15269. 9:01:21here hope you're doing well and in this
  15270. 9:01:22video i'm going to teach you guys how we
  15271. 9:01:24can create key events in python so sit
  15272. 9:01:26back relax and enjoy the show
  15273. 9:01:30welcome back everyone in this video
  15274. 9:01:33we're going to be discussing at key
  15275. 9:01:34events
  15276. 9:01:35we can bind a key event and a function
  15277. 9:01:38to a widget or a window so that when we
  15278. 9:01:41press a certain key
  15279. 9:01:43or do something we can trigger a
  15280. 9:01:45function to be called that will perform
  15281. 9:01:47some sort of task for us
  15282. 9:01:48so we can actually do this by using the
  15283. 9:01:51bind function
  15284. 9:01:52so widgets and windows have access to a
  15285. 9:01:55bind function
  15286. 9:01:57so when our window or our widget is
  15287. 9:01:59selected
  15288. 9:02:00and then you press a certain key or do
  15289. 9:02:02something then it's going to call a
  15290. 9:02:04certain function to perform a task for
  15291. 9:02:06us
  15292. 9:02:06so there's two arguments within the bind
  15293. 9:02:08function this takes an event
  15294. 9:02:10as well as a function name let's say
  15295. 9:02:13that when we press the enter key we want
  15296. 9:02:15to
  15297. 9:02:16call this function to do something so
  15298. 9:02:18for this event
  15299. 9:02:20what we type for that is a set of quotes
  15300. 9:02:23angle brackets and the name of the
  15301. 9:02:25button
  15302. 9:02:26so for example if you want to press w to
  15303. 9:02:28move forward like you're playing a game
  15304. 9:02:30this would be w
  15305. 9:02:32a s d there's also up
  15306. 9:02:35down and then let's say we want to press
  15307. 9:02:38enter to do something that is actually a
  15308. 9:02:40return and then we will call
  15309. 9:02:42this function so let's create a function
  15310. 9:02:44called do something
  15311. 9:02:46and let's define this at the top def
  15312. 9:02:50do something now pay attention to this
  15313. 9:02:53part we need to set up
  15314. 9:02:54one parameter for this do something
  15315. 9:02:56function
  15316. 9:02:58this needs an event so be sure to
  15317. 9:03:00include that and it's easy to forget
  15318. 9:03:02so what do we want to do after we press
  15319. 9:03:04enter
  15320. 9:03:05let's just print a message you
  15321. 9:03:09did a thing and
  15322. 9:03:12this should work now so let's try it
  15323. 9:03:17so while this window has focus ability
  15324. 9:03:20we can actually uh have this respond to
  15325. 9:03:22events
  15326. 9:03:23so right now i'm pressing all sorts of
  15327. 9:03:24keys you can probably hear me right now
  15328. 9:03:26going crazy on my keyboard but now i'm
  15329. 9:03:28going to hit
  15330. 9:03:29enter and it says you did a thing and
  15331. 9:03:32you can bind all sorts of keys to
  15332. 9:03:33this window let's say that i want to
  15333. 9:03:35press q to quit
  15334. 9:03:38so we'll just replace this with
  15335. 9:03:39lowercase q
  15336. 9:03:42so i'm typing all sorts of keys but now
  15337. 9:03:44i'm going to press q
  15338. 9:03:45and it says you did a thing kind of like
  15339. 9:03:48we're quitting the game
  15340. 9:03:49you can actually have this respond to
  15341. 9:03:51all keys
  15342. 9:03:52well almost all of them so to do that
  15343. 9:03:55you just type in key with the capital k
  15344. 9:03:58so i can pretty much press on any key
  15345. 9:04:01then it's going to
  15346. 9:04:02trigger or call our function
  15347. 9:04:05kind of like that so one thing that we
  15348. 9:04:08could do
  15349. 9:04:09we can display the key event that was
  15350. 9:04:11pressed so i'm just going to add that
  15351. 9:04:13here
  15352. 9:04:14maybe i'll change this message u
  15353. 9:04:18pressed plus and to display
  15354. 9:04:21what key was pressed that is event
  15355. 9:04:24dot key sim i believe that's for
  15356. 9:04:28key symbol so this will actually display
  15357. 9:04:31what button you pressed
  15358. 9:04:32so w-a-s-d
  15359. 9:04:36here's a unique idea for practice let's
  15360. 9:04:39say that we want to add
  15361. 9:04:40a label to our window and we're going to
  15362. 9:04:42change the text on the label
  15363. 9:04:44to reflect whatever key that we press so
  15364. 9:04:47let's create a label
  15365. 9:04:48label equals label we're adding this to
  15366. 9:04:50our window
  15367. 9:04:52and let's change the font as well to
  15368. 9:04:54something massive
  15369. 9:04:55like helvetica
  15370. 9:04:59and a size of 100 that should be good
  15371. 9:05:02then we need to pack this label so
  15372. 9:05:04label dot pack then within
  15373. 9:05:07this function let's turn this into a
  15374. 9:05:09comment
  15375. 9:05:10and we're going to use label dot config
  15376. 9:05:14and we're going to update our text
  15377. 9:05:17and the text is going to be whatever key
  15378. 9:05:19that we press so we can do that with
  15379. 9:05:21event dot key sim so now
  15380. 9:05:25this label is going to display whatever
  15381. 9:05:28key that we press
  15382. 9:05:29so for example w a s
  15383. 9:05:32d backspace escape
  15384. 9:05:36up up down down left right left right b
  15385. 9:05:38a start
  15386. 9:05:39all right so that's the basics of key
  15387. 9:05:41events if you could do me a favor and in
  15388. 9:05:43the comments down below
  15389. 9:05:44just press f to pay respects so if you
  15390. 9:05:47want a copy of this code i'll post this
  15391. 9:05:49in the comments down below but yeah
  15392. 9:05:50that's the basics of key events in
  15393. 9:05:53python
  15394. 9:05:54hey what's going on everybody it's your
  15395. 9:05:55bro here hope you're doing well and in
  15396. 9:05:57this video i'm going to teach you guys
  15397. 9:05:58all about
  15398. 9:05:59mouse events in python so sit back relax
  15399. 9:06:01and enjoy the show
  15400. 9:06:05hey everyone we're going to be
  15401. 9:06:06discussing mouse events in python so
  15402. 9:06:08before we begin we'll need a window so
  15403. 9:06:10window equals tk followed by window.main
  15404. 9:06:13loop
  15405. 9:06:14and in the last video we learned that we
  15406. 9:06:16can bind
  15407. 9:06:17a widget or a window so that when a
  15408. 9:06:19certain event occurs we can call a
  15409. 9:06:21function
  15410. 9:06:22so we're going to do the same thing for
  15411. 9:06:24this video but we have
  15412. 9:06:25various mouse events that we can cover
  15413. 9:06:28so we're going to use the window.bind
  15414. 9:06:31function
  15415. 9:06:31and this takes two arguments an event as
  15416. 9:06:34well as a function
  15417. 9:06:36name so let's define a function let's
  15418. 9:06:38call it the
  15419. 9:06:39do something function so do something
  15420. 9:06:42and we'll need to define this so at the
  15421. 9:06:44top def
  15422. 9:06:46do something and remember that we need
  15423. 9:06:49to set up
  15424. 9:06:49one parameter for this function the
  15425. 9:06:52parameter
  15426. 9:06:53is an event and then we'll just print
  15427. 9:06:55something print
  15428. 9:06:57you did a thing
  15429. 9:07:01okay so there are various mouse events
  15430. 9:07:04for a
  15431. 9:07:04left button click within quotes and then
  15432. 9:07:08within angle brackets
  15433. 9:07:09a left button click is button dash one
  15434. 9:07:13so then if we were to click within this
  15435. 9:07:16window
  15436. 9:07:16it's going to call our function you did
  15437. 9:07:19a thing
  15438. 9:07:20but if i were to right click it doesn't
  15439. 9:07:22do anything
  15440. 9:07:24so there are other mouse events too so
  15441. 9:07:26button two
  15442. 9:07:27is that scroll wheel not if you scroll
  15443. 9:07:29up or down but if you press
  15444. 9:07:31in on it so right now i'm clicking left
  15445. 9:07:34i'm clicking right
  15446. 9:07:35now i'm going to press in on the mouse
  15447. 9:07:37wheel and it says you did a thing
  15448. 9:07:40button three is a right mouse click
  15449. 9:07:46so i'm clicking left nothing but i'm
  15450. 9:07:49gonna click right
  15451. 9:07:51and that seems to work all right so
  15452. 9:07:53button one i'm just going to add a note
  15453. 9:07:56left mouse click button two
  15454. 9:08:00is the scroll wheel
  15455. 9:08:04that's if you press in on it and then
  15456. 9:08:07button three
  15457. 9:08:08is a right mouse click
  15458. 9:08:11so did you guys know we can actually get
  15459. 9:08:13the x and y coordinates of where this
  15460. 9:08:15event occurred
  15461. 9:08:16so to do that we type in the name of the
  15462. 9:08:19event
  15463. 9:08:20dot x however since we're displaying
  15464. 9:08:23this along with some text
  15465. 9:08:24we'll need to convert this to a string
  15466. 9:08:28so string event dot x then i'll add the
  15467. 9:08:31y coordinates as well
  15468. 9:08:35event.y i think i'll just add a comma
  15469. 9:08:38between these
  15470. 9:08:39to separate them and we'll change our
  15471. 9:08:42text
  15472. 9:08:44so we'll say mouse coordinates
  15473. 9:08:48all right let's try this again so this
  15474. 9:08:51is going to
  15475. 9:08:52give us the coordinates of where we
  15476. 9:08:54click and right now i'm just left
  15477. 9:08:55clicking
  15478. 9:08:58so the top left corner is zero zero
  15479. 9:09:02well at least close to it not there
  15480. 9:09:04exactly
  15481. 9:09:06alright so here's a few other events
  15482. 9:09:08that we can cover
  15483. 9:09:11i'm going to copy this we have button
  15484. 9:09:14release
  15485. 9:09:15if we were to release a button it's
  15486. 9:09:18going to trigger our event
  15487. 9:09:21so right now i'm holding in on the left
  15488. 9:09:23mouse button but i'm going to let go
  15489. 9:09:25then it triggers our event if i were to
  15490. 9:09:28right click
  15491. 9:09:29and hold it it doesn't do anything but
  15492. 9:09:31as soon as i let go
  15493. 9:09:32then our event is triggered we also have
  15494. 9:09:35enter
  15495. 9:09:36not to be confused with the enter button
  15496. 9:09:39just going to get rid of that so that
  15497. 9:09:42is enter if we were to enter our binded
  15498. 9:09:46window or widget
  15499. 9:09:49so if i were to enter it's going to give
  15500. 9:09:51the coordinates of where i entered this
  15501. 9:09:56there is also leave
  15502. 9:09:59so i'm going to say that enter
  15503. 9:10:03the window
  15504. 9:10:07and leave is leave the window
  15505. 9:10:14so if i enter nothing happens but as
  15506. 9:10:16soon as i leave it gives the coordinates
  15507. 9:10:22and the last one is motion as long as
  15508. 9:10:24the cursor is in motion
  15509. 9:10:28so this one might be good for a game so
  15510. 9:10:31that is motion
  15511. 9:10:34where the mouse
  15512. 9:10:40moved
  15513. 9:10:42it's consistently giving me the
  15514. 9:10:43coordinates if i'm moving the mouse but
  15515. 9:10:45if i were
  15516. 9:10:45stationary or stop moving it stops
  15517. 9:10:50all right so that's the basics of mouse
  15518. 9:10:52events in python if you want to copy of
  15519. 9:10:54all this code
  15520. 9:10:54i'll post all of this in the comments
  15521. 9:10:56down below but yeah that's the basics of
  15522. 9:10:58mouse events in python hey what's going
  15523. 9:11:01on everybody it's bro here hope you're
  15524. 9:11:03doing well and in this video i'm going
  15525. 9:11:05to teach you guys how we can drag
  15526. 9:11:06and drop widgets in python so sit back
  15527. 9:11:09relax and enjoy the show
  15528. 9:11:12all right people let's just dive into
  15529. 9:11:15this so we'll need a window and then
  15530. 9:11:16we're going to be adding widgets to the
  15531. 9:11:18window
  15532. 9:11:19so window equals tk followed by
  15533. 9:11:21window.main loop
  15534. 9:11:23now you can drag and drop all sorts of
  15535. 9:11:25widgets but let's just stick with the
  15536. 9:11:26label because creating a label is simple
  15537. 9:11:29so let's define a label label equals
  15538. 9:11:32label we're going to be adding this
  15539. 9:11:35label to the window
  15540. 9:11:36and let's set a background color so we
  15541. 9:11:38can actually see this label
  15542. 9:11:40pick i don't know your favorite color
  15543. 9:11:42i'm going to pick red
  15544. 9:11:43and then i'm going to set a width and a
  15545. 9:11:45height because normally if you have a
  15546. 9:11:47label without any text it's really small
  15547. 9:11:50so let's set a width of maybe 10
  15548. 9:11:53and then a height of 5.
  15549. 9:11:57that should be good and we want to place
  15550. 9:11:59this label
  15551. 9:12:00someplace within the window so label dot
  15552. 9:12:03place and we can pass in some x
  15553. 9:12:05coordinates as well as y coordinates
  15554. 9:12:07so x equals zero and y equals zero
  15555. 9:12:11so that this is going to appear in the
  15556. 9:12:13top left corner
  15557. 9:12:14of our window so this is what we have
  15558. 9:12:17this
  15559. 9:12:18is our label and we want to be able to
  15560. 9:12:20drag and drop
  15561. 9:12:21this label someplace within the window
  15562. 9:12:23what we're going to need to do
  15563. 9:12:24is actually bind this label twice but
  15564. 9:12:27let's begin with just
  15565. 9:12:28the first binding so in order to bind a
  15566. 9:12:31widget you type in the name of the
  15567. 9:12:32widget
  15568. 9:12:33followed by calling the bind function
  15569. 9:12:36so this bind function can take two
  15570. 9:12:39arguments
  15571. 9:12:40it can take an event as well as a
  15572. 9:12:42function
  15573. 9:12:43name so for the function name let's call
  15574. 9:12:45this
  15575. 9:12:46drag underscore start but you don't
  15576. 9:12:48really need to
  15577. 9:12:49name it the same that i do and for the
  15578. 9:12:52event that occurs
  15579. 9:12:53this is going to be a left button click
  15580. 9:12:55with the mouse
  15581. 9:12:56so that event is within quotes and
  15582. 9:12:59within angle brackets
  15583. 9:13:01button dash one so if we were to click
  15584. 9:13:04someplace within the label using the
  15585. 9:13:06left mouse button
  15586. 9:13:07it's going to call our drag start
  15587. 9:13:10function but we need to define this
  15588. 9:13:12so at the top of our program let's
  15589. 9:13:13define this function
  15590. 9:13:16def drag start and this takes one
  15591. 9:13:19parameter
  15592. 9:13:20the parameter we have is our event so
  15593. 9:13:22what we're going to do
  15594. 9:13:23is actually get the coordinates of where
  15595. 9:13:26we click
  15596. 9:13:27within this label and we're going to
  15597. 9:13:29assign these
  15598. 9:13:30to a new variable or attribute of our
  15599. 9:13:33label
  15600. 9:13:34so label dot let's create an attribute
  15601. 9:13:37called
  15602. 9:13:37start x equals our event
  15603. 9:13:41dot x so this is where we click
  15604. 9:13:44within the label and not necessarily the
  15605. 9:13:47window
  15606. 9:13:48and let's repeat the process for y so
  15607. 9:13:50label
  15608. 9:13:51dot start y equals event dot
  15609. 9:13:54y so we're going to have some
  15610. 9:13:57coordinates of where we click
  15611. 9:13:58within this label and we can actually
  15612. 9:14:01use that for our next
  15613. 9:14:02function so we're going to bind this
  15614. 9:14:04label a second time
  15615. 9:14:06so i'm going to copy this line and paste
  15616. 9:14:08it
  15617. 9:14:09the second binding let's say is
  15618. 9:14:12drag underscore motion for the function
  15619. 9:14:15name
  15620. 9:14:16and this event is going to occur if we
  15621. 9:14:19were to hold down the left mouse button
  15622. 9:14:21and then drag so that is b
  15623. 9:14:25one dash motion
  15624. 9:14:28and then we need to define this function
  15625. 9:14:30so at the top
  15626. 9:14:32d e f drag underscore motion and this
  15627. 9:14:35takes our event as a parameter
  15628. 9:14:37so this part is going to be a little
  15629. 9:14:39funky we're going to
  15630. 9:14:41get or create new x and y
  15631. 9:14:44coordinates so let's begin with x x
  15632. 9:14:47equals the name of our label or widget
  15633. 9:14:50label dot w info
  15634. 9:14:54underscore x this is a function so add
  15635. 9:14:56some parentheses
  15636. 9:14:58this will get the top left x coordinate
  15637. 9:15:01of our label relative to the window that
  15638. 9:15:04we're in
  15639. 9:15:05minus label x well labeled.startx
  15640. 9:15:11plus event dot x
  15641. 9:15:14all right so we're going to do the same
  15642. 9:15:17thing for y
  15643. 9:15:19well we're going to replace x with y for
  15644. 9:15:21the second line
  15645. 9:15:25all right so this is the top
  15646. 9:15:29left corner the x coordinate of our
  15647. 9:15:32widget
  15648. 9:15:33relative to the window this
  15649. 9:15:37is the place where we click within
  15650. 9:15:40the label itself and this is where we
  15651. 9:15:43begin
  15652. 9:15:44dragging our widget to it's a little
  15653. 9:15:47difficult to explain but if you can find
  15654. 9:15:48combine
  15655. 9:15:49combine all of these together you'll get
  15656. 9:15:51the new coordinates
  15657. 9:15:52of where you want to drag this component
  15658. 9:15:56this widget to
  15659. 9:15:57so we need to replace this widget so
  15660. 9:16:00label dot place x equals x
  15661. 9:16:04and y equals y so then when you combine
  15662. 9:16:07all of these together
  15663. 9:16:09we have our widget and we can drag it
  15664. 9:16:11and drop it someplace else within the
  15665. 9:16:13window
  15666. 9:16:15now wait a second what if we have more
  15667. 9:16:17than one widget
  15668. 9:16:18well with the way that our functions are
  15669. 9:16:20written now they wouldn't be compatible
  15670. 9:16:22with more than one widget
  15671. 9:16:24just this label itself so what if we
  15672. 9:16:27were to create a
  15673. 9:16:28second label so i'm going to call the
  15674. 9:16:30second label just
  15675. 9:16:32label two so i'm just going to rename
  15676. 9:16:34them and
  15677. 9:16:35let's change the background color to
  15678. 9:16:37maybe
  15679. 9:16:38blue and i'm just going to place this
  15680. 9:16:40somewhere else within our window so
  15681. 9:16:41maybe where x
  15682. 9:16:42is 100 and y is 100 and we'll need to
  15683. 9:16:45bind
  15684. 9:16:46label two as well so
  15685. 9:16:49make sure to bind label two so we have
  15686. 9:16:53label one binded it's just called label
  15687. 9:16:55and the same thing with label two
  15688. 9:16:57all right let's see what happens i'm
  15689. 9:16:59going to attempt to drag
  15690. 9:17:01label 2. the label 1 is moving that's
  15691. 9:17:04because
  15692. 9:17:04with our functions we stated we're
  15693. 9:17:07getting the coordinates
  15694. 9:17:09and we're storing them within the
  15695. 9:17:10attributes of label
  15696. 9:17:121 and not necessarily label two so in
  15697. 9:17:14order to make
  15698. 9:17:15these functions compatible with all
  15699. 9:17:18widgets
  15700. 9:17:19we can actually add one line of code
  15701. 9:17:22widget equals event dot widget
  15702. 9:17:26so this is going to get the widget of
  15703. 9:17:28the event that we're dealing with
  15704. 9:17:30and we're going to temporarily rename
  15705. 9:17:32this as widget
  15706. 9:17:34so now we're going to replace all
  15707. 9:17:36instances of where we use
  15708. 9:17:37label and replace it with widget
  15709. 9:17:42be sure to do the same thing too with
  15710. 9:17:43drag motion
  15711. 9:17:46and then we will need to add this line
  15712. 9:17:48at the top of
  15713. 9:17:49drag motion so now
  15714. 9:17:53these functions are now compatible with
  15715. 9:17:55any widget that we create
  15716. 9:17:59and we should be able to drag and drop
  15717. 9:18:02both
  15718. 9:18:03widgets all right well that's the basics
  15719. 9:18:06of dragging and dropping widgets in
  15720. 9:18:08python if you want a copy of all this
  15721. 9:18:10code i'll post all of this in the
  15722. 9:18:11comments down below
  15723. 9:18:13but yeah that's the basics of dragging
  15724. 9:18:14and dropping widgets in python
  15725. 9:18:18hey what's going on everybody it's your
  15726. 9:18:20bro here hope you're doing well and in
  15727. 9:18:21this video i'm going to teach you guys
  15728. 9:18:22how we can move an image
  15729. 9:18:24on both a window and a canvas in python
  15730. 9:18:27so sit back relax and enjoy the show
  15731. 9:18:31all right my fellow bros i'm going to
  15732. 9:18:33divide this video into
  15733. 9:18:35two different sections in the first
  15734. 9:18:37section we're going to move a widget
  15735. 9:18:39within a window
  15736. 9:18:40and in the next section we're going to
  15737. 9:18:42move an image
  15738. 9:18:43on a canvas so let's begin by moving a
  15739. 9:18:46widget within a window
  15740. 9:18:47so we'll need a window window equals tk
  15741. 9:18:50followed by window.mainloop and we'll
  15742. 9:18:52also need to set some geometry for this
  15743. 9:18:54window
  15744. 9:18:55window dot geometry let's make this
  15745. 9:18:58500x500
  15746. 9:19:01500 x 500 and let's take a look
  15747. 9:19:06okay that should be a decent size let's
  15748. 9:19:08add a label to this window
  15749. 9:19:11so i actually have a race car image that
  15750. 9:19:13i want to use if you would like an image
  15751. 9:19:15feel free to pause the video and
  15752. 9:19:16download one
  15753. 9:19:17what i would like to do is create a
  15754. 9:19:19photo image from this png file that i
  15755. 9:19:21have
  15756. 9:19:22i'm just going to call this my
  15757. 9:19:25image equals photo
  15758. 9:19:28image file equals since this is within
  15759. 9:19:32my project folder i can just list the
  15760. 9:19:34file name
  15761. 9:19:36otherwise you might have to list the
  15762. 9:19:37file path and for me this is called
  15763. 9:19:40racecar.png
  15764. 9:19:42i want to add this image to my label not
  15765. 9:19:45necessary but
  15766. 9:19:46it'll be helpful for this example i'm
  15767. 9:19:48going to call my label just a label
  15768. 9:19:50label equals label i'm adding this to
  15769. 9:19:52the window and i'm going to set the
  15770. 9:19:54image to equal
  15771. 9:19:55my image that i created it's a photo
  15772. 9:19:57image
  15773. 9:19:58if you do not have an image that you
  15774. 9:20:00want to use you can simply
  15775. 9:20:02just change the background color you
  15776. 9:20:03could say bg
  15777. 9:20:05i don't know red
  15778. 9:20:08oh then we need to place this so be sure
  15779. 9:20:10to do that
  15780. 9:20:11label dot place and i'm going to place
  15781. 9:20:13this in the top left corner that is
  15782. 9:20:15where x
  15783. 9:20:15equals zero and y equals zero and then
  15784. 9:20:18we should have a race car
  15785. 9:20:20or whatever image you used and a red
  15786. 9:20:22background you can get rid of the
  15787. 9:20:24background color if you prefer
  15788. 9:20:25that's just if you don't have an image
  15789. 9:20:27to use you can set the background color
  15790. 9:20:29just for a
  15791. 9:20:30visual to show you where the label
  15792. 9:20:32currently is
  15793. 9:20:33let's set some key bindings for this
  15794. 9:20:35window and let's add them maybe here
  15795. 9:20:37before we create the label and our image
  15796. 9:20:39so let's find the w key for up s for
  15797. 9:20:43down
  15798. 9:20:43a for left and d for right so we do that
  15799. 9:20:46with
  15800. 9:20:47window dot bind and we pass in an event
  15801. 9:20:51as well as a function name so we want to
  15802. 9:20:54tie the w key to a function and we'll
  15803. 9:20:58call this
  15804. 9:20:58move underscore up and then we will need
  15805. 9:21:02to define this
  15806. 9:21:04so outside of the window let's define
  15807. 9:21:08move underscore up and this takes
  15808. 9:21:12an event so we'll place that as a
  15809. 9:21:14parameter
  15810. 9:21:15and to move our label up we're going to
  15811. 9:21:18take
  15812. 9:21:18label dot place
  15813. 9:21:22and we're going to place this at some
  15814. 9:21:23new coordinates
  15815. 9:21:25x equals the label's current position
  15816. 9:21:28relative to the window that it's in so
  15817. 9:21:31that is label
  15818. 9:21:32dot w info
  15819. 9:21:35underscore x
  15820. 9:21:38all right so then for y it's similar
  15821. 9:21:41y equals label dot w
  15822. 9:21:45info underscore y
  15823. 9:21:48but we're going to subtract maybe one
  15824. 9:21:52all right so this is all we need
  15825. 9:21:56although this is only going to move up
  15826. 9:21:58so i'm going to press
  15827. 9:21:59w and our label our race car or whatever
  15828. 9:22:03image you used is going to move
  15829. 9:22:04slowly up i'm going to increase this to
  15830. 9:22:0810 so the change is more drastic
  15831. 9:22:11so now our race car is drifting off into
  15832. 9:22:13space
  15833. 9:22:15let's find a few other keys s a
  15834. 9:22:18and d sad all right so
  15835. 9:22:21s will be down move underscore down
  15836. 9:22:27a for left move left
  15837. 9:22:31and d for right move
  15838. 9:22:35right we'll just copy what we have here
  15839. 9:22:38and make a few changes
  15840. 9:22:39let's begin with move down
  15841. 9:22:44the only thing we're going to change
  15842. 9:22:45within here is in place of subtracting
  15843. 9:22:4810
  15844. 9:22:48from the y coordinate we're going to add
  15845. 9:22:5010
  15846. 9:22:52and with our race car oh i guess we have
  15847. 9:22:54to define these so let's do that real
  15848. 9:22:56quick
  15849. 9:22:57uh so we have left
  15850. 9:23:00move left we're going to subtract
  15851. 9:23:0310 from the x coordinate and keep y the
  15852. 9:23:07same
  15853. 9:23:09and then the same thing for move right
  15854. 9:23:11move
  15855. 9:23:12underscore right and this time we are
  15856. 9:23:15going to add 10
  15857. 9:23:16to x and y will stay the same
  15858. 9:23:21okay now with our image we can move up
  15859. 9:23:25down right and then left
  15860. 9:23:29and y stop there we combine these to our
  15861. 9:23:32arrow keys
  15862. 9:23:36so up is just up with a capital u
  15863. 9:23:39down is down these are all capital by
  15864. 9:23:42the way
  15865. 9:23:43left is left and then right is right
  15866. 9:23:49and now i can use my arrow keys to move
  15867. 9:23:51this image
  15868. 9:23:58now for this part of the video i'm going
  15869. 9:24:00to teach you guys how we can move an
  15870. 9:24:02image on a canvas this time and not just
  15871. 9:24:04within a window
  15872. 9:24:05but we'll need a window to begin with
  15873. 9:24:07window equals tk followed by
  15874. 9:24:09window.mainloop
  15875. 9:24:10and we're going to create a canvas
  15876. 9:24:12canvas equals canvas
  15877. 9:24:15we're adding this canvas to the window
  15878. 9:24:17and let's set a width and height
  15879. 9:24:19for the width let's make this 500 and
  15880. 9:24:21the height 500 as well
  15881. 9:24:22so this is an even square and we'll need
  15882. 9:24:25to
  15883. 9:24:26pack or place this canvas canvas.pack
  15884. 9:24:28should take care of that
  15885. 9:24:30and let's run this and this is our
  15886. 9:24:32canvas
  15887. 9:24:33at this step we need to add an image to
  15888. 9:24:36our canvas and i have an image of a
  15889. 9:24:38racecar that i want to add
  15890. 9:24:39so i need to create a photo image from
  15891. 9:24:42this file
  15892. 9:24:43so we can do that let's give this photo
  15893. 9:24:46image a unique name i'm just going to
  15894. 9:24:47call this photo image to keep it simple
  15895. 9:24:50photo image equals photo image and i can
  15896. 9:24:53list the file path or the file name
  15897. 9:24:55since
  15898. 9:24:56this image is within my project folder i
  15899. 9:24:59only have to list the file name
  15900. 9:25:00and my file is called racecar.png
  15901. 9:25:04this next step we need to give our image
  15902. 9:25:07a unique name
  15903. 9:25:08the image that's being added to the
  15904. 9:25:09canvas i'll call this
  15905. 9:25:11my image equals
  15906. 9:25:15and we need to turn this photo image
  15907. 9:25:18into an image added to the canvas
  15908. 9:25:20so we type in the name of the canvas
  15909. 9:25:22canvas
  15910. 9:25:23dot create underscore image
  15911. 9:25:26we first need to list the coordinates of
  15912. 9:25:28where we want to place this photo image
  15913. 9:25:31so let's place this in the top left
  15914. 9:25:32corner that is where
  15915. 9:25:34x is 0 and y 0 and we need to set the
  15916. 9:25:37image of the image that we're creating
  15917. 9:25:40so we can do that with saying
  15918. 9:25:41image equals the name of our photo image
  15919. 9:25:46and let's see what we have so far so
  15920. 9:25:49this image is somewhat cut off in the
  15921. 9:25:51top left corner we can easily fix that
  15922. 9:25:53by anchoring
  15923. 9:25:54our image when we create it so let's at
  15924. 9:25:56the end add
  15925. 9:25:57anchor equals northwest
  15926. 9:26:00so that should fix that problem and we
  15927. 9:26:03now have our image that we want to use
  15928. 9:26:06and to move our image we can set some
  15929. 9:26:07key bindings
  15930. 9:26:10so we type in the name of the window dot
  15931. 9:26:12bind
  15932. 9:26:13and we pass in two arguments an event as
  15933. 9:26:16well as a function name
  15934. 9:26:18let's tie pressing the w key to moving
  15935. 9:26:21up
  15936. 9:26:21so the first argument is going to be w
  15937. 9:26:24and the second is going to be
  15938. 9:26:25a function name so let's say move
  15939. 9:26:28underscore up
  15940. 9:26:29and let's find some other keys
  15941. 9:26:33w for up s for down
  15942. 9:26:39a for left and
  15943. 9:26:42d for right
  15944. 9:26:45we'll need to define these now so at the
  15945. 9:26:48top outside of the window
  15946. 9:26:50let's begin by defining move up
  15947. 9:26:53def move up this has an event as a
  15948. 9:26:56parameter
  15949. 9:26:59and let's create the other definitions
  15950. 9:27:02here
  15951. 9:27:05so i'm just going to rename these so we
  15952. 9:27:08have move
  15953. 9:27:08up move down move left
  15954. 9:27:12and move right okay now this next step
  15955. 9:27:18we can use the canvases move function to
  15956. 9:27:20move an image
  15957. 9:27:21a certain amount of pixels on the x-axis
  15958. 9:27:24and
  15959. 9:27:24y-axis so let's begin with move up we
  15960. 9:27:27type in the name of the canvas
  15961. 9:27:29dot and use the move function and this
  15962. 9:27:32has
  15963. 9:27:32three arguments the image that we want
  15964. 9:27:34to use
  15965. 9:27:35and this image is called my image the
  15966. 9:27:38one with the race car
  15967. 9:27:41the next argument is the amount of
  15968. 9:27:43pixels we're moving this on the
  15969. 9:27:45x-axis with up we can keep this the same
  15970. 9:27:50but on the y-axis let's say we want to
  15971. 9:27:52move this
  15972. 9:27:53up 10 pixels so that would be -10
  15973. 9:27:57and let's repeat the process for move
  15974. 9:27:59down move left and move right
  15975. 9:28:01for move down we're going to change
  15976. 9:28:04y to 10 for move left
  15977. 9:28:08we'll keep y the same at zero but we're
  15978. 9:28:11going to subtract
  15979. 9:28:1210 and then move right
  15980. 9:28:16we are going to add 10 to our x's
  15981. 9:28:20and that should be it so we can move up
  15982. 9:28:24down right left
  15983. 9:28:28and if you want to bind these to some
  15984. 9:28:30arrow keys
  15985. 9:28:31all we have to do is just change the
  15986. 9:28:34event
  15987. 9:28:35so we can bind these to our wsad keys
  15988. 9:28:38as well as up
  15989. 9:28:42down left
  15990. 9:28:46and right and this time i'm going to use
  15991. 9:28:49my arrow keys
  15992. 9:28:50up down right and left
  15993. 9:28:56all right so that's a few of the basics
  15994. 9:28:58of moving either widgets or
  15995. 9:29:00images on a canvas or window if you
  15996. 9:29:02would like a copy of all this code for
  15997. 9:29:04both examples i'll post both of them in
  15998. 9:29:06the comments down below
  15999. 9:29:08but yeah that's the basics of moving
  16000. 9:29:10widgets and images
  16001. 9:29:11in python hey what's going on everybody
  16002. 9:29:14it's your bro here hope you're doing
  16003. 9:29:16well and in this video i'm going to
  16004. 9:29:17teach you guys how we can create some
  16005. 9:29:19simple 2d animations in python so sit
  16006. 9:29:22back relax and enjoy the show
  16007. 9:29:26alright my fellow bros in this video
  16008. 9:29:28we're going to be animating an image on
  16009. 9:29:30a canvas we're only going to be moving
  16010. 9:29:33this image
  16011. 9:29:33up down left and right feel free to
  16012. 9:29:36download an image that you want to use
  16013. 9:29:37i'm just going to use this
  16014. 9:29:39ufo emoji that i found so we'll need two
  16015. 9:29:42imports
  16016. 9:29:43at the top from tk enter import
  16017. 9:29:45everything that's what we usually have
  16018. 9:29:47we'll also need to import time as well
  16019. 9:29:50we'll need a window window equals tk
  16020. 9:29:53followed by window.main loop
  16021. 9:29:55we'll need a canvas i'm going to call
  16022. 9:29:57the canvas canvas
  16023. 9:29:58canvas equals canvas we're adding the
  16024. 9:30:01canvas
  16025. 9:30:01to the window i'm going to create
  16026. 9:30:03something that's called a
  16027. 9:30:05constant we're going to create a
  16028. 9:30:07constant for the width and the height
  16029. 9:30:09of our canvas so a constant is a
  16030. 9:30:12variable or value that you don't plan on
  16031. 9:30:14changing later at all
  16032. 9:30:16and a common naming convention for a
  16033. 9:30:18constant is that the name of the
  16034. 9:30:20constant is
  16035. 9:30:20all uppercase so i want the width and
  16036. 9:30:23the height
  16037. 9:30:24of this canvas to be constants so for
  16038. 9:30:27the width this is going to be
  16039. 9:30:28all uppercase it's not necessary to make
  16040. 9:30:31it all uppercase
  16041. 9:30:32but that's a common naming convention
  16042. 9:30:34for the width i'm going to set this to
  16043. 9:30:35500
  16044. 9:30:36and the height set to 500 as well
  16045. 9:30:39and for the width of this canvas i'm
  16046. 9:30:41going to set the width
  16047. 9:30:43to equal my constant which is 500
  16048. 9:30:46and the height to my constant height
  16049. 9:30:50so the reason that i'm making constants
  16050. 9:30:52is that we'll be reusing these a lot
  16051. 9:30:54later in this program so we might as
  16052. 9:30:56well just declare them now and get it
  16053. 9:30:58over with
  16054. 9:30:59so for the canvas we're adding this to
  16055. 9:31:01the window we're setting the width to
  16056. 9:31:03our constant of width and the height to
  16057. 9:31:05our constant of height
  16058. 9:31:07and we need to follow this by packing
  16059. 9:31:08this or place
  16060. 9:31:10canvas dot pack pack to make it simple
  16061. 9:31:13so we should have a canvas on our window
  16062. 9:31:17now if you have an image that you want
  16063. 9:31:18to use we're going to animate it
  16064. 9:31:20i need to create a photo image from this
  16065. 9:31:22file it's a png file it's called
  16066. 9:31:25ufo but yours is probably going to be
  16067. 9:31:27named something different so
  16068. 9:31:28don't copy me exactly i need to create a
  16069. 9:31:31photo image from this
  16070. 9:31:32i'm going to call this photo underscore
  16071. 9:31:35image but name it whatever you want this
  16072. 9:31:38equals
  16073. 9:31:39photo image and i need to list the file
  16074. 9:31:42path or the file name
  16075. 9:31:43since this file is within my project
  16076. 9:31:46folder i only have to list the file name
  16077. 9:31:48and for me this is called ufo.png
  16078. 9:31:53our next step is that we're going to
  16079. 9:31:54take our photo image and add this photo
  16080. 9:31:57image to the canvas
  16081. 9:31:58and we'll also give this image a unique
  16082. 9:32:00name i'm going to call this my
  16083. 9:32:03image equals we're going to use the
  16084. 9:32:06canvases
  16085. 9:32:07create image function so canvas dot
  16086. 9:32:11create image there's a few arguments
  16087. 9:32:13that we need to pass
  16088. 9:32:14in where we want this image to appear we
  16089. 9:32:17need a set of coordinates
  16090. 9:32:18if we want this to appear in the top
  16091. 9:32:20left corner that is where x
  16092. 9:32:22is zero and y is zero for the image
  16093. 9:32:25we're going to set this equal to
  16094. 9:32:27our photo image whatever you named this
  16095. 9:32:30and lastly we should anchor this i'm
  16096. 9:32:33going to anchor this in the northwest
  16097. 9:32:35corner to keep it simple
  16098. 9:32:37and we should have our image on our
  16099. 9:32:39canvas in the top left corner
  16100. 9:32:41and here is my ufo that's being piloted
  16101. 9:32:44by a green alien that's waving to me
  16102. 9:32:46aliens are usually not that friendly
  16103. 9:32:48unless they're abducting you our next
  16104. 9:32:49step is to create a
  16105. 9:32:50while loop so we'll say wow true this
  16106. 9:32:53will continue forever until we close out
  16107. 9:32:55of the program
  16108. 9:32:56if you're looking at the code for a game
  16109. 9:32:57this might say instead
  16110. 9:32:59while running running will be a boolean
  16111. 9:33:03variable that contains either true or
  16112. 9:33:05false so if this is true while the game
  16113. 9:33:07is running
  16114. 9:33:08they're going to continue moving all of
  16115. 9:33:10the characters on the screen
  16116. 9:33:11and then if somebody pauses the game or
  16117. 9:33:13closes out of the game
  16118. 9:33:14then they'll flip this running variable
  16119. 9:33:16to false but to keep things simple we'll
  16120. 9:33:19just say wow
  16121. 9:33:20true but that's something that you might
  16122. 9:33:21see what we'll want to do is get the
  16123. 9:33:24coordinates of our image
  16124. 9:33:25within the canvas and this will return a
  16125. 9:33:28list of coordinates
  16126. 9:33:29so let's create a list called
  16127. 9:33:31coordinates so
  16128. 9:33:33coordinates equals canvas
  16129. 9:33:37and we can use the chords function to
  16130. 9:33:40get the coordinates
  16131. 9:33:41i guess that's short for coordinates and
  16132. 9:33:44we're getting the coordinates of
  16133. 9:33:45my image so we'll pass this into this
  16134. 9:33:48function
  16135. 9:33:49and then let's print this just to see
  16136. 9:33:51what this returns
  16137. 9:33:52we'll print coordinates
  16138. 9:33:57okay now to update the window after each
  16139. 9:33:59cycle
  16140. 9:34:00of this while loop we can use the update
  16141. 9:34:02function of our window
  16142. 9:34:04so window.update will also have this
  16143. 9:34:07thread sleep for let's say 0.01 seconds
  16144. 9:34:10so time dot sleep 0.01 one
  16145. 9:34:15so here's a quick rundown of what we
  16146. 9:34:17have going on so far within this while
  16147. 9:34:19loop
  16148. 9:34:19we're going to first get the coordinates
  16149. 9:34:21of where our image
  16150. 9:34:23is located we'll print the coordinates
  16151. 9:34:26to the console window
  16152. 9:34:27this part isn't required but it's going
  16153. 9:34:29to help us for learning purposes
  16154. 9:34:32we'll update this window for any changes
  16155. 9:34:34and then our thread
  16156. 9:34:36which is in charge of running the
  16157. 9:34:37program is going to sleep
  16158. 9:34:39for i believe that's a hundredth of a
  16159. 9:34:41second
  16160. 9:34:43so when we run this our image is staying
  16161. 9:34:46in place
  16162. 9:34:47and we know that our while loop is
  16163. 9:34:48running because it's continuously
  16164. 9:34:50printing the coordinates
  16165. 9:34:51the first number is the x position
  16166. 9:34:55the second number is the y position and
  16167. 9:34:58now we're going to give our image a
  16168. 9:34:59speed
  16169. 9:35:00or velocity i like to call it velocity
  16170. 9:35:03and in fact i'm going to give this image
  16171. 9:35:05two velocities
  16172. 9:35:06one for how fast this image is moving on
  16173. 9:35:08the x-axis
  16174. 9:35:09and another for how fast it's moving on
  16175. 9:35:11the y axis
  16176. 9:35:13so i'm going to create two variables one
  16177. 9:35:15called x velocity
  16178. 9:35:17and i'm going to set this equal to one
  16179. 9:35:19and y velocity
  16180. 9:35:22and i'll set this to one as well okay
  16181. 9:35:26so what we need to do now is update
  16182. 9:35:29the position of our image so let's do
  16183. 9:35:32that before we update
  16184. 9:35:33our window so i'm going to type in the
  16185. 9:35:35name of the canvas
  16186. 9:35:36and use the move function this move
  16187. 9:35:39function takes three arguments
  16188. 9:35:41what you want to move i want to move my
  16189. 9:35:44image my ufo
  16190. 9:35:46and how far i want to move this image on
  16191. 9:35:49each axis each coordinate
  16192. 9:35:51so what i want to do is update the
  16193. 9:35:54x velocity the x position of my image
  16194. 9:35:58so i'm going to pass in my x velocity
  16195. 9:36:02variable and then y we'll just keep the
  16196. 9:36:04same for now so we'll just say
  16197. 9:36:06zero so after each iteration of this
  16198. 9:36:08while loop
  16199. 9:36:09my ufo is going to move one pixel to the
  16200. 9:36:11right
  16201. 9:36:12and it's just going to go off into the
  16202. 9:36:13void forever and now
  16203. 9:36:15if we replaced x velocity with zero
  16204. 9:36:19and for y we set this to our y velocity
  16205. 9:36:22this image is now going to travel down
  16206. 9:36:26and it will continue into the void
  16207. 9:36:27forever
  16208. 9:36:29and then if we combine both of these
  16209. 9:36:30together
  16210. 9:36:33we're going to move diagonally down and
  16211. 9:36:36to the right
  16212. 9:36:37so what if we want this image to bounce
  16213. 9:36:40off the walls we're going to add some
  16214. 9:36:41if statements we're going to begin by
  16215. 9:36:43just having this image bounce off the
  16216. 9:36:45left and right walls
  16217. 9:36:47for now let's set y velocity to zero so
  16218. 9:36:50that this image is only moving
  16219. 9:36:51left and right so before we move this
  16220. 9:36:55image we'll add an if statement we're
  16221. 9:36:56going to check to see
  16222. 9:36:58if the x position of our image
  16223. 9:37:01is either greater than the width of the
  16224. 9:37:03canvas
  16225. 9:37:04or if it's less than zero so we take
  16226. 9:37:07our coordinates and we're going to get
  16227. 9:37:09the first element from this list
  16228. 9:37:11so that is zero that is x the x value
  16229. 9:37:15if coordinates at element zero is
  16230. 9:37:18greater than or equal to
  16231. 9:37:20the width of this window and this is a
  16232. 9:37:22constant
  16233. 9:37:24so we can just place that here if
  16234. 9:37:26coordinates
  16235. 9:37:27well x within our coordinates is greater
  16236. 9:37:29than or equal to width
  16237. 9:37:32or coordinates at index
  16238. 9:37:360 is less than 0 then we want to reverse
  16239. 9:37:40the direction or flip the velocity
  16240. 9:37:43so we want to change our x velocity and
  16241. 9:37:45make this a negative number
  16242. 9:37:47one easy way to do that is that we can
  16243. 9:37:49take x velocity
  16244. 9:37:51equals x velocity times negative one
  16245. 9:37:55or another way of writing this is just
  16246. 9:37:57to say x velocity
  16247. 9:37:59minus negative velocity now what ends up
  16248. 9:38:02happening
  16249. 9:38:03is that when this image reaches the
  16250. 9:38:05right border it's going to bounce back
  16251. 9:38:08however it only bounces back after it
  16252. 9:38:10reaches
  16253. 9:38:11the top left corner of the image we need
  16254. 9:38:13to factor in
  16255. 9:38:14the width of the image as well so we're
  16256. 9:38:17going to create
  16257. 9:38:18two new variables and let's do this
  16258. 9:38:19right before the while loop
  16259. 9:38:21we'll want to determine what the height
  16260. 9:38:23and the width are for our photo image
  16261. 9:38:25that we have
  16262. 9:38:26so this is what we're going to write
  16263. 9:38:28let's call these two values
  16264. 9:38:29image underscore width and
  16265. 9:38:33image underscore height
  16266. 9:38:37so we need to determine what the width
  16267. 9:38:39and the height is
  16268. 9:38:40for our photo image and there is a
  16269. 9:38:42function to do that
  16270. 9:38:44type in the name of the photo image dot
  16271. 9:38:47and there is a width
  16272. 9:38:48function and the same thing for height
  16273. 9:38:53photo image dot height
  16274. 9:38:56and then we can reuse these so i'm just
  16275. 9:38:59going to add
  16276. 9:39:00one line to the sift statement if
  16277. 9:39:03the x position of our image is greater
  16278. 9:39:06uh greater than
  16279. 9:39:07or equal to the width of our window
  16280. 9:39:10minus the width of our image and i'm
  16281. 9:39:13just going to put these within
  16282. 9:39:15parentheses just to keep these more
  16283. 9:39:16organized
  16284. 9:39:17and what ends up happening now is that
  16285. 9:39:20when
  16286. 9:39:22the right portion of our image hits the
  16287. 9:39:25right wall it's going to bounce off
  16288. 9:39:27and let's do the same thing for our y
  16289. 9:39:29velocity
  16290. 9:39:32we're going to replace coordinates at
  16291. 9:39:35index 0 with 1 and change that here as
  16292. 9:39:38well
  16293. 9:39:39replace with with height
  16294. 9:39:43and this is image underscore height
  16295. 9:39:46and we're going to change the y velocity
  16296. 9:39:49this time
  16297. 9:39:54and when we move this image let's also
  16298. 9:39:56add the
  16299. 9:39:57y velocity
  16300. 9:40:00and now our image is just going to
  16301. 9:40:03bounce off the walls forever
  16302. 9:40:04but it's kind of predictable since we
  16303. 9:40:06set x and y
  16304. 9:40:08to equal one now let's change the speeds
  16305. 9:40:10to kind of spice things up
  16306. 9:40:12let's say for the x velocity this is
  16307. 9:40:14three and y velocity this is now two
  16308. 9:40:16this is going to go in a completely
  16309. 9:40:18different direction
  16310. 9:40:21so you can mess with the speeds however
  16311. 9:40:22you want so
  16312. 9:40:24guys how about we add a background image
  16313. 9:40:26to this well because we can
  16314. 9:40:28i'm going to reuse this image of space
  16315. 9:40:30from my java video on this same topic
  16316. 9:40:33i need to create a photo image from this
  16317. 9:40:35and i'm going to copy what we did for
  16318. 9:40:37our normal image that we were moving and
  16319. 9:40:40animating
  16320. 9:40:41so this is called space.png well that's
  16321. 9:40:44the name of my file
  16322. 9:40:46and i'm going to give this image a
  16323. 9:40:47different name maybe i'll call this
  16324. 9:40:50background underscore photo
  16325. 9:40:53and then i'm going to add this photo to
  16326. 9:40:55the canvas
  16327. 9:40:57and i'm going to call this a different
  16328. 9:40:59name
  16329. 9:41:01i'll just call this background all right
  16330. 9:41:04so
  16331. 9:41:04our window should have a background now
  16332. 9:41:08and then our image moves on top of the
  16333. 9:41:10background
  16334. 9:41:10so make sure you add and create the
  16335. 9:41:12background before you add any other
  16336. 9:41:14images
  16337. 9:41:15otherwise the background might overlap
  16338. 9:41:17your image
  16339. 9:41:18alright so that's the basics of some
  16340. 9:41:21simple animations in
  16341. 9:41:22python if you want to copy all this code
  16342. 9:41:24i'll post all of this in the comments
  16343. 9:41:26down below
  16344. 9:41:26but yeah that's the basics of some
  16345. 9:41:28animations in python
  16346. 9:41:31hey what's going on everybody it's your
  16347. 9:41:33bro here hope you're doing well and in
  16348. 9:41:35this video i'm going to teach you guys
  16349. 9:41:36how we can animate multiple objects in
  16350. 9:41:38python so
  16351. 9:41:39sit back relax and enjoy the show
  16352. 9:41:43welcome back my fellow bros in this
  16353. 9:41:45video we're going to be animating
  16354. 9:41:47multiple objects within a window
  16355. 9:41:49more specifically we'll animate some
  16356. 9:41:50circles and each of these circles will
  16357. 9:41:52have their own characteristics
  16358. 9:41:54speeds and directions so we'll need two
  16359. 9:41:57imports to begin
  16360. 9:41:58from ck enter import everything we'll
  16361. 9:42:01also be importing everything from the
  16362. 9:42:02time module as well
  16363. 9:42:04we'll need a window window equals tk
  16364. 9:42:06followed by window.main loop
  16365. 9:42:08i'm going to create two constants one
  16366. 9:42:10for the width and one for the height
  16367. 9:42:12let's set the width equal to 500 and the
  16368. 9:42:14height equal to
  16369. 9:42:15500 as well we'll create a canvas and
  16370. 9:42:17we'll be animating
  16371. 9:42:19the circles that we're going to create
  16372. 9:42:20on the canvas so let's create a canvas
  16373. 9:42:23canvas equals canvas
  16374. 9:42:27we're going to be adding the canvas to
  16375. 9:42:28the window and we'll set the width of
  16376. 9:42:30our canvas equal to our constant width
  16377. 9:42:33which is 500 and our height equal to our
  16378. 9:42:36constant of height
  16379. 9:42:37which is also 500 and we will add this
  16380. 9:42:40canvas by using the pack function
  16381. 9:42:42canvas.pack here's our canvas and we're
  16382. 9:42:45going to be creating multiple circles
  16383. 9:42:47let's pretend that they're all different
  16384. 9:42:48sports balls like a tennis ball baseball
  16385. 9:42:51volleyball bowling ball
  16386. 9:42:53they'll all be bouncing off of the
  16387. 9:42:55borders and once
  16388. 9:42:56each of them encounters a border they
  16389. 9:42:57will change direction
  16390. 9:42:59since we're going to be creating
  16391. 9:43:01multiple objects i think it's best if we
  16392. 9:43:03do this with another class
  16393. 9:43:05so let's create another class i'm going
  16394. 9:43:07to go to file
  16395. 9:43:08new python file and let's call this
  16396. 9:43:12class
  16397. 9:43:12ball and click python file and we need
  16398. 9:43:16to define
  16399. 9:43:17this class class ball
  16400. 9:43:22and we also need a constructor for this
  16401. 9:43:24ball class
  16402. 9:43:25we can create one with def two
  16403. 9:43:28underscores
  16404. 9:43:28i-n-i-t two underscores again
  16405. 9:43:32and we can set up the parameters of what
  16406. 9:43:34we want to receive if we were to create
  16407. 9:43:36a ball
  16408. 9:43:37object so let's pretend that
  16409. 9:43:40we're going to be creating a volleyball
  16410. 9:43:42to begin with so
  16411. 9:43:44let's call this ball volley
  16412. 9:43:47underscore ball equals
  16413. 9:43:50ball and we'll pass in some arguments so
  16414. 9:43:53let's set up the parameters first
  16415. 9:43:55what do we want to receive one we want
  16416. 9:43:57to receive
  16417. 9:43:58our canvas so that we can actually draw
  16418. 9:44:00this oval we can draw this ball
  16419. 9:44:03we should also have an x and y position
  16420. 9:44:07a diameter
  16421. 9:44:11x velocity y velocity
  16422. 9:44:15and a color all right so
  16423. 9:44:18when we create a ball like a volleyball
  16424. 9:44:21we're going to pass
  16425. 9:44:22in whatever the parameters require so we
  16426. 9:44:25need a canvas
  16427. 9:44:26x y a diameter an x velocity
  16428. 9:44:30y velocity and a color so within the
  16429. 9:44:32constructor for the ball
  16430. 9:44:34we're passing in our canvas it's kind of
  16431. 9:44:37like what we do with widgets
  16432. 9:44:38we're specifying what we're going to be
  16433. 9:44:40adding this widget to
  16434. 9:44:42we're going to be adding this ball to
  16435. 9:44:44our canvas we also need
  16436. 9:44:46x y and a diameter let's make this
  16437. 9:44:48simple by setting x and y to both zero
  16438. 9:44:51for the diameter let's set this to maybe
  16439. 9:44:54100 and we also need an x velocity and y
  16440. 9:44:58velocity
  16441. 9:44:59let's set this both to one and a color
  16442. 9:45:02so let's pass in a string
  16443. 9:45:04like white let's head back to our ball
  16444. 9:45:07class and finish assigning
  16445. 9:45:08all of these arguments that we're going
  16446. 9:45:10to receive the first step is that we'll
  16447. 9:45:12say
  16448. 9:45:13self dot canvas equals
  16449. 9:45:16the canvas that we receive when it's
  16450. 9:45:18passed to us as an argument
  16451. 9:45:21our next step is to draw this oval draw
  16452. 9:45:24this ball
  16453. 9:45:25and we'll say self dot image
  16454. 9:45:29equals canvas dot create
  16455. 9:45:33underscore oval and with creating ovals
  16456. 9:45:36we can pass in x y
  16457. 9:45:39and with circles the diameter is equal
  16458. 9:45:42for both the width and the height
  16459. 9:45:44so we'll pass in our diameter twice
  16460. 9:45:48and lastly a fill color
  16461. 9:45:52and we'll say this is equal to the color
  16462. 9:45:54that we receive
  16463. 9:45:55which is white
  16464. 9:45:58we're going to set the x velocity and
  16465. 9:46:00the y velocity
  16466. 9:46:02this is related to the direction that
  16467. 9:46:04it's initially going to head in
  16468. 9:46:06so self dot x velocity
  16469. 9:46:10equals the x velocity that we receive
  16470. 9:46:15and we'll do the same thing for y
  16471. 9:46:16velocity
  16472. 9:46:19self.y velocity equals y velocity
  16473. 9:46:23now since this is in another class we
  16474. 9:46:25need to import this class
  16475. 9:46:29from ball import
  16476. 9:46:32everything and you can see that this red
  16477. 9:46:34underline went away
  16478. 9:46:36so let's see what happens now
  16479. 9:46:39we have our volleyball but it's not
  16480. 9:46:42currently moving
  16481. 9:46:42so that's the next step so let's create
  16482. 9:46:45a function
  16483. 9:46:46within the ball class called move that
  16484. 9:46:48we can call upon
  16485. 9:46:49so we'll do this after the constructor
  16486. 9:46:51cef
  16487. 9:46:53move and this will take self as an
  16488. 9:46:56argument
  16489. 9:46:58the first thing that we should do is get
  16490. 9:47:00the coordinates
  16491. 9:47:02so we'll say coordinates and this is a
  16492. 9:47:04list coordinates equals
  16493. 9:47:06self dot canvas dot
  16494. 9:47:09c o o rds
  16495. 9:47:14and we'll pass in self dot image
  16496. 9:47:18and we will print these coordinates just
  16497. 9:47:20to see what's here
  16498. 9:47:26all right now we'll create a while loop
  16499. 9:47:29within our main class
  16500. 9:47:30so let's add that near the end and we'll
  16501. 9:47:33just say
  16502. 9:47:35while true so while this is running
  16503. 9:47:39let's move our volleyball so volleyball
  16504. 9:47:43dot move so this will call the move
  16505. 9:47:45function of our volleyball
  16506. 9:47:48and then we will update the window
  16507. 9:47:51window dot update so that this refreshes
  16508. 9:47:54and we will sleep for maybe a hundredth
  16509. 9:47:57of a second
  16510. 9:47:58time dot sleep 0.01 but you can put
  16511. 9:48:02whatever you want here
  16512. 9:48:04so this is going to at first just print
  16513. 9:48:06the coordinates of this ball
  16514. 9:48:09so it's not currently moving and this
  16515. 9:48:11time with our list we have
  16516. 9:48:13four coordinates zero zero that's the
  16517. 9:48:15top left corner and 100 100 that's the
  16518. 9:48:18bottom
  16519. 9:48:18right corner so this time we have four
  16520. 9:48:21coordinates that we have to deal with
  16521. 9:48:23the top left corner as well as the
  16522. 9:48:25bottom right corner
  16523. 9:48:26our next step is that we should move
  16524. 9:48:28this image
  16525. 9:48:30self dot canvas dot move
  16526. 9:48:34what object are we going to move we're
  16527. 9:48:36going to move self
  16528. 9:48:38dot image and in what directions
  16529. 9:48:41self.x velocity as well as
  16530. 9:48:44self dot y velocity now
  16531. 9:48:48our ball is going to move across the
  16532. 9:48:50window and it right now it's displaying
  16533. 9:48:52all of the coordinates
  16534. 9:48:53we don't have any bounds set up so it's
  16535. 9:48:55just going to continue into the void
  16536. 9:48:56forever so we should set up some if
  16537. 9:48:59statements to check to see if one of the
  16538. 9:49:01borders
  16539. 9:49:02touches one of the walls of the window
  16540. 9:49:04if so it's going to change
  16541. 9:49:05direction so let's check to see
  16542. 9:49:09if the left or right side of our ball
  16543. 9:49:11touches one of the left or right borders
  16544. 9:49:13so let's write an if statement if
  16545. 9:49:17and then let's take a look at these
  16546. 9:49:18coordinates if
  16547. 9:49:20coordinates at index 2
  16548. 9:49:24that is this one 0 1
  16549. 9:49:272. so this is the bottom right corner
  16550. 9:49:31with these two sets of coordinates if
  16551. 9:49:33coordinates at index 2
  16552. 9:49:35is greater than or equal to
  16553. 9:49:38i'm going to put these within
  16554. 9:49:39parentheses self
  16555. 9:49:41dot canvas and we need to get the width
  16556. 9:49:44of the canvas
  16557. 9:49:45one way which we can do that is to use
  16558. 9:49:47the w
  16559. 9:49:49info underscore with function
  16560. 9:49:53so this is going to get the width of the
  16561. 9:49:55canvas
  16562. 9:49:57and then let's add one more thing
  16563. 9:50:00or coordinates at index
  16564. 9:50:03zero is less than zero
  16565. 9:50:07so if this ball touches the left or
  16566. 9:50:10right border
  16567. 9:50:11we want to change direction so we can
  16568. 9:50:14flip
  16569. 9:50:14the x velocity self.x velocity
  16570. 9:50:18equals negative self x
  16571. 9:50:22velocity and let's do the same thing for
  16572. 9:50:24the top and bottom borders
  16573. 9:50:26so let's copy what we have
  16574. 9:50:31and we'll replace coordinates at index
  16575. 9:50:33two with
  16576. 9:50:34three and we'll also replace
  16577. 9:50:38w info underscore width with height
  16578. 9:50:43and coordinates at index one
  16579. 9:50:46change self dot x velocity with y
  16580. 9:50:49velocity
  16581. 9:50:50same thing here as well alright and
  16582. 9:50:54that should be everything let's try it
  16583. 9:50:58so let's see if this ball actually
  16584. 9:50:59bounces off the borders
  16585. 9:51:02which it does pretty cool so that's
  16586. 9:51:04everything we need to do within our ball
  16587. 9:51:06class
  16588. 9:51:06now if we need to create multiple balls
  16589. 9:51:09we can just construct them
  16590. 9:51:10so let's say this time we would like a
  16591. 9:51:12tennis ball
  16592. 9:51:14i'm going to name this volleyball as
  16593. 9:51:16tennis ball and
  16594. 9:51:18let's make the diameter smaller like
  16595. 9:51:20five but we'll make this a lot faster
  16596. 9:51:22like
  16597. 9:51:23for the velocity let's say four and
  16598. 9:51:25three and the color
  16599. 9:51:26let's make this yellow and we also need
  16600. 9:51:30to call the
  16601. 9:51:31move function of this tennis ball so
  16602. 9:51:33within the while loop
  16603. 9:51:34tennis ball dot move and then we should
  16604. 9:51:38also have a tennis ball moving across
  16605. 9:51:40the screen
  16606. 9:51:40in a different direction now technically
  16607. 9:51:43we don't need to print the coordinates
  16608. 9:51:44but i tend to do that just for
  16609. 9:51:46teaching purposes to better demonstrate
  16610. 9:51:48all of this
  16611. 9:51:49now our ball class is acting as a
  16612. 9:51:52blueprint
  16613. 9:51:53for how balls should be created and the
  16614. 9:51:55behavior that they exhibit
  16615. 9:51:57and if we need to create a ball we can
  16616. 9:51:59just come up with a name for the ball
  16617. 9:52:01and call the ball constructor to take
  16618. 9:52:03care of that for us
  16619. 9:52:05kind of like what we've been doing with
  16620. 9:52:06widgets so all we need to do to create
  16621. 9:52:09another ball
  16622. 9:52:10is come up with a unique name for the
  16623. 9:52:11ball and instantiate it using
  16624. 9:52:13our ball class that we created so this
  16625. 9:52:15time let's create
  16626. 9:52:17a basketball i'll rename this as
  16627. 9:52:19basketball
  16628. 9:52:20and we'll pass in different arguments
  16629. 9:52:22based on how we want to customize
  16630. 9:52:25this unique ball so for the diameter
  16631. 9:52:28let's say this is now 125
  16632. 9:52:30and the x and y velocity let's set this
  16633. 9:52:32to something very fast like
  16634. 9:52:35eight and seven and with the color let's
  16635. 9:52:38change this to orange
  16636. 9:52:40and make sure you also call the move
  16637. 9:52:42function
  16638. 9:52:44so basketball dot move and we should
  16639. 9:52:47have a basketball moving across the
  16640. 9:52:49screen
  16641. 9:52:50all right everybody so that's the basics
  16642. 9:52:53of animating multiple objects in python
  16643. 9:52:55if you want
  16644. 9:52:56a copy of all this code i'll post all of
  16645. 9:52:58this in the comments down below
  16646. 9:52:59but yeah that's how to animate multiple
  16647. 9:53:01objects in python
  16648. 9:53:04hey what's going on everybody it's ibro
  16649. 9:53:06here hope you're doing well and in this
  16650. 9:53:08video i'm going to teach you guys how we
  16651. 9:53:09can create a clock program in python so
  16652. 9:53:12sit back relax and enjoy the show
  16653. 9:53:16all right my fellow bros in this video
  16654. 9:53:18we're going to be creating a clock
  16655. 9:53:20gui program so we'll need a few imports
  16656. 9:53:22from
  16657. 9:53:23tk enter import everything
  16658. 9:53:27and from time import everything as well
  16659. 9:53:31we'll need a window window equals tk
  16660. 9:53:33followed by window.main loop
  16661. 9:53:35and in this program we're going to be
  16662. 9:53:37creating three separate labels
  16663. 9:53:39one for the time another for the day of
  16664. 9:53:41the week and another for the date
  16665. 9:53:43so let's begin with just displaying the
  16666. 9:53:45time and work on the day of the week and
  16667. 9:53:47the date later
  16668. 9:53:49so let's create a label for the time
  16669. 9:53:51called time
  16670. 9:53:52label equals label we're adding this to
  16671. 9:53:55the window
  16672. 9:53:56and let's set the font because we can
  16673. 9:53:59i'm going to choose
  16674. 9:54:00maybe ariel and then a size of 50 so
  16675. 9:54:03that this is somewhat large
  16676. 9:54:05i'm going to change the foreground color
  16677. 9:54:07that is the font color
  16678. 9:54:09and i will pick a hex value of green so
  16679. 9:54:12that is
  16680. 9:54:120 0 f 0 0 but feel free to pick whatever
  16681. 9:54:16color you want
  16682. 9:54:17and i'm also going to set the background
  16683. 9:54:19color and you can pick a hex value or a
  16684. 9:54:21color name i'm just going to say black
  16685. 9:54:24and then i'm going to pack this label
  16686. 9:54:27time label dot pack
  16687. 9:54:32now we're going to call a function and
  16688. 9:54:35we'll call this update
  16689. 9:54:37and we'll define what this does after
  16690. 9:54:39every second has passed
  16691. 9:54:40we will use a recursive function to keep
  16692. 9:54:43on updating the
  16693. 9:54:44time every one second or so so let's
  16694. 9:54:47define what this function does
  16695. 9:54:49def update
  16696. 9:54:52there is a function within the time
  16697. 9:54:54module called strf time
  16698. 9:54:57which we can use to return the current
  16699. 9:54:59time and format it however we want
  16700. 9:55:01so we're going to actually place this
  16701. 9:55:03within a string variable and we'll call
  16702. 9:55:05this time
  16703. 9:55:06string and this function is strf
  16704. 9:55:10time i'm actually going to go to
  16705. 9:55:12python's website just to give you an
  16706. 9:55:14example of how this works
  16707. 9:55:15here we are on python's website and this
  16708. 9:55:18is the
  16709. 9:55:18strf time function and this converts a
  16710. 9:55:22tuple representing a time as returned by
  16711. 9:55:26gm time or local time to a string
  16712. 9:55:29as specified by the format arguments
  16713. 9:55:32so when we call this function we can
  16714. 9:55:35pass in
  16715. 9:55:36directives as arguments based on what we
  16716. 9:55:39want to display
  16717. 9:55:40if we want to display the current time
  16718. 9:55:42there is a few directives that we can
  16719. 9:55:44pass in
  16720. 9:55:45so let's say that we want a 12 hour
  16721. 9:55:48clock as represented by a decimal number
  16722. 9:55:50between
  16723. 9:55:511 and 12 well we would pass in percent
  16724. 9:55:54i as an argument if we want the minutes
  16725. 9:55:57that would be
  16726. 9:55:58right here percent m and if we want the
  16727. 9:56:01seconds that is
  16728. 9:56:02percent s so we're going to pass in
  16729. 9:56:05these directives
  16730. 9:56:06as if they are arguments so within
  16731. 9:56:09this function we'll pass in those format
  16732. 9:56:13specifiers or directives so i want
  16733. 9:56:16percent i followed by
  16734. 9:56:20percent m and percent
  16735. 9:56:23s alright uh but these also have to be
  16736. 9:56:27within quotes so don't forget that
  16737. 9:56:28because i just did
  16738. 9:56:32okay this will display the current time
  16739. 9:56:35oh then we also have to update our time
  16740. 9:56:38label so
  16741. 9:56:39time label dot config
  16742. 9:56:44text equals our time string because
  16743. 9:56:47remember that this
  16744. 9:56:48returns a string of text
  16745. 9:56:52text equals time string so this will
  16746. 9:56:54display the current time
  16747. 9:56:56and right now it is six in the morning
  16748. 9:56:59or so
  16749. 9:56:59however we just get one long jumbled
  16750. 9:57:02number
  16751. 9:57:03so it would be a good idea if we
  16752. 9:57:05separated some of these numbers
  16753. 9:57:07so within our strf time function i'm
  16754. 9:57:10just going to add some colons to
  16755. 9:57:11separate these values
  16756. 9:57:15and this will display the current time
  16757. 9:57:17it is currently 6
  16758. 9:57:1846 in the morning let's add am or pm to
  16759. 9:57:22the end of this
  16760. 9:57:23so the directive for that is percent
  16761. 9:57:26lowercase p
  16762. 9:57:296 46 am now how do we update this label
  16763. 9:57:33every one second to reflect the current
  16764. 9:57:35time
  16765. 9:57:35well that's a good question and i'm glad
  16766. 9:57:37you asked it so we're going to use
  16767. 9:57:39the time labels after function
  16768. 9:57:42we pass in a delay and then a function
  16769. 9:57:45that we want to call
  16770. 9:57:46after that delay so let's say we want to
  16771. 9:57:49call this function update every 1000
  16772. 9:57:52milliseconds
  16773. 9:57:53or one second so we'll pass in the delay
  16774. 9:57:56as an argument
  16775. 9:57:571000 milliseconds and the function that
  16776. 9:57:59we want to call
  16777. 9:58:00and we're going to perform what is known
  16778. 9:58:02as a recursive
  16779. 9:58:04function we're going to call a function
  16780. 9:58:07within itself so after 1000 seconds
  16781. 9:58:10we're going to call
  16782. 9:58:11update again and it's going to repeat
  16783. 9:58:12the process it's going to return the
  16784. 9:58:14time
  16785. 9:58:15and format that time and update
  16786. 9:58:18our label with the new time so what ends
  16787. 9:58:21up happening now
  16788. 9:58:22is that our clock is going to update
  16789. 9:58:25every 1000 milliseconds
  16790. 9:58:28now let's add a few other labels let's
  16791. 9:58:30say the day of the week
  16792. 9:58:32i'm going to copy what we have here and
  16793. 9:58:34let's rename this as
  16794. 9:58:36day label
  16795. 9:58:40but i probably don't want this to be the
  16796. 9:58:42same size for the font let's say
  16797. 9:58:44ink free because i like that font and a
  16798. 9:58:48size of maybe 25 and i'll get rid of the
  16799. 9:58:50coloring
  16800. 9:58:53it's the same process as before really
  16801. 9:58:56so let's replace
  16802. 9:59:00time string with day string
  16803. 9:59:05the directive for strf time to display
  16804. 9:59:09the day of the week
  16805. 9:59:11is percent a
  16806. 9:59:14and we're going to replace time label
  16807. 9:59:17with
  16808. 9:59:18day label text equals day string
  16809. 9:59:23and this will display the day of the
  16810. 9:59:25week and it is currently
  16811. 9:59:27wednesday and if we want to display
  16812. 9:59:31the current date we can create a label
  16813. 9:59:34and we'll call this
  16814. 9:59:36date label
  16815. 9:59:40and i'll make the font slightly bigger
  16816. 9:59:41maybe 30.
  16817. 9:59:44we'll copy what we have for our day
  16818. 9:59:46string
  16819. 9:59:48and rename this as date string
  16820. 9:59:54so we have a few options for the date i
  16821. 9:59:56want to display
  16822. 9:59:58the name of the month the day and then
  16823. 10:00:00the year
  16824. 10:00:01so that would be percent capital b
  16825. 10:00:06percent lowercase d i'm going to add a
  16826. 10:00:09comma to separate the day and the year
  16827. 10:00:12and percent capital y
  16828. 10:00:15and then i want to set the text of my
  16829. 10:00:17date label
  16830. 10:00:18to my date string so that this will now
  16831. 10:00:22display the current date there's just
  16832. 10:00:24one quick change that i think we should
  16833. 10:00:26make
  16834. 10:00:26right now we have three different labels
  16835. 10:00:28a time label a day label
  16836. 10:00:30and a date label and right now we're
  16837. 10:00:33using the after function of the time
  16838. 10:00:35label
  16839. 10:00:36so since we have all of these different
  16840. 10:00:37labels i think we should replace time
  16841. 10:00:39label
  16842. 10:00:40with our window because our window has
  16843. 10:00:42an after function as well
  16844. 10:00:44so that after 1000 milliseconds has
  16845. 10:00:47passed
  16846. 10:00:47we can update the time the day of the
  16847. 10:00:50week
  16848. 10:00:50and the date all right so that's the
  16849. 10:00:53basics of creating a simple clock
  16850. 10:00:55program in python if you want a copy of
  16851. 10:00:57all this code
  16852. 10:00:58i'll post all of this in the comments
  16853. 10:00:59down below but yeah that's the basics of
  16854. 10:01:01creating a clock in python
  16855. 10:01:04hey what's going on everybody it's you
  16856. 10:01:06bro hope you're doing well
  16857. 10:01:07and in this video i'm going to show you
  16858. 10:01:08all how we can send an email using
  16859. 10:01:10python so
  16860. 10:01:11sit back relax and enjoy the show
  16861. 10:01:16oh a quick note for you all you're going
  16862. 10:01:17to need a gmail account to sign into
  16863. 10:01:20as well as your username and password so
  16864. 10:01:22once you have those set you're ready to
  16865. 10:01:24begin
  16866. 10:01:24all right let's begin by importing the
  16867. 10:01:27simple mail
  16868. 10:01:28transfer protocol library we'll need a
  16869. 10:01:31couple variables to hold strings
  16870. 10:01:32let's say we have a sender a receiver
  16871. 10:01:37a password
  16872. 10:01:40a subject for your email as well as
  16873. 10:01:44a body for the email okay sender is
  16874. 10:01:46going to be you
  16875. 10:01:47sending the email so enter your email in
  16876. 10:01:49here i'm just going to make up one
  16877. 10:01:51you at gmail.com
  16878. 10:01:54actually sender would be better i would
  16879. 10:01:56say let me change that sender at
  16880. 10:01:58gmail.com
  16881. 10:01:59let's say we have a receiver this is who
  16882. 10:02:01you're going to send the email to
  16883. 10:02:04receiver gmail.com
  16884. 10:02:07don't actually enter in these emails use
  16885. 10:02:09your owner the person that you intend to
  16886. 10:02:10send the email to for your test
  16887. 10:02:12because these will probably not work i'm
  16888. 10:02:14assuming
  16889. 10:02:15then enter the password for your gmail
  16890. 10:02:17account so let's say
  16891. 10:02:20password123 and a subject
  16892. 10:02:23uh let's say python email
  16893. 10:02:27test and a body what do you want to say
  16894. 10:02:30within your email
  16895. 10:02:32i wrote an email
  16896. 10:02:36okay so that should be good so remember
  16897. 10:02:38that you're going to want to change
  16898. 10:02:40these to your actual
  16899. 10:02:41gmail account and you'll want to send it
  16900. 10:02:43to a valid email address too
  16901. 10:02:45and make sure that you use your password
  16902. 10:02:47for your gmail account
  16903. 10:02:49okay now we're going to create a header
  16904. 10:02:51for our email and the syntax on this is
  16905. 10:02:53a little strange we're going to say
  16906. 10:02:55message
  16907. 10:02:56f and then triple quotes so what this
  16908. 10:02:59triple quotes is
  16909. 10:03:00is that a triple quote string can span
  16910. 10:03:03multiple lines of text so the first
  16911. 10:03:06thing we'll enter is
  16912. 10:03:07from within our header from and then
  16913. 10:03:10since we're using an f-string we can
  16914. 10:03:12insert
  16915. 10:03:13a variable at a given location when we
  16916. 10:03:16insert
  16917. 10:03:16a pair of these curly braces so from
  16918. 10:03:20will be sender the next line
  16919. 10:03:23to colon
  16920. 10:03:27receiver then the next line
  16921. 10:03:30subject colon
  16922. 10:03:34subject this will be the title of the
  16923. 10:03:36email then i'm going to add a new line
  16924. 10:03:39and then insert the body
  16925. 10:03:43and if you want you can give yourself a
  16926. 10:03:45name when you send this email
  16927. 10:03:47let's say maybe snoop dogg and if
  16928. 10:03:50there's somebody you want to send the
  16929. 10:03:51email to
  16930. 10:03:52and give them a name you can say like
  16931. 10:03:55nicholas
  16932. 10:03:56cage but these aren't necessary
  16933. 10:03:59okay so we have our header completed let
  16934. 10:04:02me just add a note that this is our
  16935. 10:04:03header
  16936. 10:04:05okay we're going to create a server
  16937. 10:04:07object
  16938. 10:04:08server equals smtplib.smtp
  16939. 10:04:17there are two arguments the first is
  16940. 10:04:20smtp
  16941. 10:04:25the other argument is the port number
  16942. 10:04:28and that is going to be
  16943. 10:04:29587 that is the default mail submission
  16944. 10:04:32port
  16945. 10:04:33587 now we're going to take our server
  16946. 10:04:36object
  16947. 10:04:37and start tls transport layer security
  16948. 10:04:42okay now we need to log in so server
  16949. 10:04:46dot login we will pass in
  16950. 10:04:50our sender email that's us as well as
  16951. 10:04:53our password
  16952. 10:04:56password and let's print
  16953. 10:05:02logged in
  16954. 10:05:06we'll eventually place all this within a
  16955. 10:05:08try and accept block later
  16956. 10:05:10all right and then to send an email type
  16957. 10:05:13server dot send mail
  16958. 10:05:17and we pass in our self sender
  16959. 10:05:20receiver and our message
  16960. 10:05:23sender comma receiver
  16961. 10:05:27comma message and let's print a
  16962. 10:05:31confirmation
  16963. 10:05:32print email has
  16964. 10:05:36been sent now if you were to run this
  16965. 10:05:39program
  16966. 10:05:40you may encounter this error smtp
  16967. 10:05:43authentication error that either means
  16968. 10:05:45your username
  16969. 10:05:46password combination was not correct or
  16970. 10:05:49you need to turn on
  16971. 10:05:50the less secure app access on your gmail
  16972. 10:05:53account
  16973. 10:05:54which you can find here however i would
  16974. 10:05:56recommend turning that back off when
  16975. 10:05:58you're done with this program just
  16976. 10:05:59because
  16977. 10:06:00your account will be less secure and i'm
  16978. 10:06:02not liable if you get hacked hey you
  16979. 10:06:03know what
  16980. 10:06:04let's place this code within a try block
  16981. 10:06:06we will try
  16982. 10:06:07all of this code and then if we
  16983. 10:06:10encounter
  16984. 10:06:10this exception we can catch and handle
  16985. 10:06:13it properly
  16986. 10:06:14so we will try these four lines of code
  16987. 10:06:17and we will handle
  16988. 10:06:18our exception of smt lib dot
  16989. 10:06:21smtp authentication error
  16990. 10:06:25that means we could not sign in so let's
  16991. 10:06:27print
  16992. 10:06:28unable to sign in
  16993. 10:06:32so i'm going to run this one more time
  16994. 10:06:34and instead of getting that ugly error
  16995. 10:06:36we should get a simple message that says
  16996. 10:06:38unable to sign in once you have the
  16997. 10:06:40proper username password combination for
  16998. 10:06:42your gmail account
  16999. 10:06:43and if you need to set less secure app
  17000. 10:06:46access to on
  17001. 10:06:47but make sure that you turn it back off
  17002. 10:06:49when you're done with this you should be
  17003. 10:06:50able to send an email
  17004. 10:06:51let's try it logged in email has been
  17005. 10:06:54sent
  17006. 10:06:55here is a snapshot of the email that i
  17007. 10:06:57received
  17008. 10:06:58i blurred out my actual email because i
  17009. 10:07:00don't want it public because
  17010. 10:07:01some of you guys are weird so we have
  17011. 10:07:04our sender
  17012. 10:07:05our receiver and i gave myself a
  17013. 10:07:07nickname of snoop dogg that's why it
  17014. 10:07:09says snoop
  17015. 10:07:10to nicholas the receiver but you don't
  17016. 10:07:12actually need a name for these as long
  17017. 10:07:14as you have the sender and receiver
  17018. 10:07:16fields filled in you should be good
  17019. 10:07:18we have our subject python email test
  17020. 10:07:21as well as our body i wrote an email
  17021. 10:07:24well everybody that's how to write and
  17022. 10:07:26send a simple email using python if you
  17023. 10:07:28would like a copy of this code i will
  17024. 10:07:30post this to the comment section down
  17025. 10:07:32below
  17026. 10:07:32but yeah that's how to send a simple
  17027. 10:07:34email using python
  17028. 10:07:37hey what's going on everybody it's you
  17029. 10:07:39bro hope you're doing well
  17030. 10:07:40and in this video i'm going to show you
  17031. 10:07:41all how we can run a python file using
  17032. 10:07:44command prompt so
  17033. 10:07:45sit back relax and enjoy the show
  17034. 10:07:50hey y'all in today's video i'm going to
  17035. 10:07:52show you all how we can run a python
  17036. 10:07:53file with command prompt
  17037. 10:07:55so first we'll need a script to work
  17038. 10:07:57with let's just make a very simple
  17039. 10:07:59program
  17040. 10:08:00let's say print hello world and then
  17041. 10:08:03maybe we will ask
  17042. 10:08:04for some user input name equals input
  17043. 10:08:08what's your name and then at the end
  17044. 10:08:12we will print the word hello
  17045. 10:08:16plus the user's name okay so
  17046. 10:08:20let's save this to a place that will
  17047. 10:08:22remember perhaps
  17048. 10:08:23my desktop now to save this file to a
  17049. 10:08:25separate location go to
  17050. 10:08:27file save as you can pick a name for
  17051. 10:08:30this python file
  17052. 10:08:32then to directory click these three dots
  17053. 10:08:34and pick a location you want to save
  17054. 10:08:36this to
  17055. 10:08:37to make this easy i'm going to save this
  17056. 10:08:39python file to my desktop
  17057. 10:08:40and then click ok so the name of this
  17058. 10:08:43python file of mine
  17059. 10:08:44is hello underscore world dot py then
  17060. 10:08:48click ok
  17061. 10:08:49now our second step is to open command
  17062. 10:08:51prompt to open command prompt just
  17063. 10:08:53search for
  17064. 10:08:54cmd if you're using mac i believe it's
  17065. 10:08:57terminal
  17066. 10:08:58step number three is that we need to
  17067. 10:09:00navigate to the directory that contains
  17068. 10:09:02our python file i saved this python file
  17069. 10:09:05to my desktop
  17070. 10:09:06so if you right click on your file and
  17071. 10:09:08go to location you can just copy this
  17072. 10:09:10address
  17073. 10:09:11now within command prompt type cd to
  17074. 10:09:13change directory
  17075. 10:09:15space and then paste that directory
  17076. 10:09:18that contains your python file hit enter
  17077. 10:09:21and for the last step we need to invoke
  17078. 10:09:23the python interpreter by typing
  17079. 10:09:25python space and then
  17080. 10:09:29the name of the script and the name of
  17081. 10:09:31my script is hello
  17082. 10:09:32underscore world dot p y hit enter to
  17083. 10:09:36run the script
  17084. 10:09:37hello world what is your name bro hello
  17085. 10:09:40bro
  17086. 10:09:41so that is how to run a python file with
  17087. 10:09:43command prompt i will post all of these
  17088. 10:09:45notes to the comments section down below
  17089. 10:09:47and well
  17090. 10:09:48yeah that's how to run a python file
  17091. 10:09:50with command prompt
  17092. 10:09:52it's you bro hope you're all doing well
  17093. 10:09:54and in this video i'm going to show you
  17094. 10:09:55all how we can use pip for python to
  17095. 10:09:58download packages and modules so sit
  17096. 10:10:00back
  17097. 10:10:01relax and enjoy the show
  17098. 10:10:05alright people pip for python pip is a
  17099. 10:10:08package manager for packages and modules
  17100. 10:10:10from the python package index if you're
  17101. 10:10:13using
  17102. 10:10:14python version 3.4 or above pip should
  17103. 10:10:17already be installed
  17104. 10:10:18if you're using a python version below
  17105. 10:10:213.4
  17106. 10:10:22one option is that you can always update
  17107. 10:10:24your version of python just click
  17108. 10:10:26download open when done
  17109. 10:10:30go to customize installation make sure
  17110. 10:10:32that pip
  17111. 10:10:33is checked go to next and add
  17112. 10:10:36python to environment variables then
  17113. 10:10:38install
  17114. 10:10:40now to use pip open up command prompt
  17115. 10:10:42and type pip
  17116. 10:10:43for a help menu all sorts of different
  17117. 10:10:47general options and commands will appear
  17118. 10:10:49if you need to check
  17119. 10:10:50your version of pip type this command
  17120. 10:10:53and here's a cheat sheet that i wrote
  17121. 10:10:55so to check the current version of pip
  17122. 10:10:57type pip
  17123. 10:10:58dash dash version enter
  17124. 10:11:02and currently the version that i'm using
  17125. 10:11:05is 21
  17126. 10:11:06if you need to upgrade pip use this
  17127. 10:11:08command pip
  17128. 10:11:10install dash dash up
  17129. 10:11:14grade pip mine should be the latest
  17130. 10:11:18version
  17131. 10:11:18yeah requirement already satisfied and
  17132. 10:11:21if you need a list of installed packages
  17133. 10:11:24type
  17134. 10:11:24pip list
  17135. 10:11:28so here's all of the packages that i
  17136. 10:11:31currently have installed
  17137. 10:11:32some of these you might recognize like
  17138. 10:11:34numpy
  17139. 10:11:35pie game pie installer those are just a
  17140. 10:11:38few to check to see if any of your
  17141. 10:11:40packages
  17142. 10:11:41are outdated type pip list
  17143. 10:11:45dash dash outdated
  17144. 10:11:49and this might take a second here are
  17145. 10:11:51four packages that are outdated on my
  17146. 10:11:53computer
  17147. 10:11:54babel pi game pi tz and setup tools
  17148. 10:11:58if you need to upgrade one of your
  17149. 10:11:59packages type pip
  17150. 10:12:01install the name of the package let's
  17151. 10:12:04say that i would like to update pygame
  17152. 10:12:06then dash dash upgrade
  17153. 10:12:10i just realized that in my notes i'm
  17154. 10:12:12missing updated package so i just added
  17155. 10:12:14that here
  17156. 10:12:15now you can see that pie game has
  17157. 10:12:17successfully
  17158. 10:12:18updated and let's just check to see if
  17159. 10:12:20it has by using
  17160. 10:12:22pip list outdated pip
  17161. 10:12:25list dash dash outdated
  17162. 10:12:29and pi game is no longer in here so it
  17163. 10:12:32is up to date
  17164. 10:12:33and if you would like to install a
  17165. 10:12:34package type hip
  17166. 10:12:36install and the name of the package you
  17167. 10:12:38would like to download
  17168. 10:12:39let's say i would like to download
  17169. 10:12:41pandas pip
  17170. 10:12:42install the name of the package in this
  17171. 10:12:44case pandas
  17172. 10:12:47two valley boring minutes later and it's
  17173. 10:12:50done
  17174. 10:12:50so i should now have pandas installed
  17175. 10:12:52and let me check
  17176. 10:12:54pip list
  17177. 10:12:57and here it is i have successfully
  17178. 10:12:59downloaded the package of pandas
  17179. 10:13:02so those are a few commands for pip and
  17180. 10:13:05if you're interested in downloading more
  17181. 10:13:06packages head to python's package index
  17182. 10:13:09on second thought i should probably show
  17183. 10:13:11you all where that is python's package
  17184. 10:13:13index
  17185. 10:13:14is located at pipi.org
  17186. 10:13:17and you can do a search for all sorts of
  17187. 10:13:19different projects and packages
  17188. 10:13:21so everybody that is pip4 python i will
  17189. 10:13:24post my notes in the comments section
  17190. 10:13:25down below
  17191. 10:13:26and well yeah that's how to use pip for
  17192. 10:13:29python
  17193. 10:13:30hey what's going on everybody it's bro
  17194. 10:13:32hope you're doing well
  17195. 10:13:33and in this video i'm going to show you
  17196. 10:13:34all how we can convert a python file to
  17197. 10:13:37an executable
  17198. 10:13:38so sit back relax and enjoy the show
  17199. 10:13:43here's a quick disclaimer before we
  17200. 10:13:45begin windows defender and other
  17201. 10:13:47anti-virus programs may prevent your
  17202. 10:13:49executable from running but if you head
  17203. 10:13:51to the settings section of your
  17204. 10:13:52anti-virus you can disable that so that
  17205. 10:13:54your python file can run
  17206. 10:13:56also please be sure that pip and pi
  17207. 10:13:58installer are both installed and up to
  17208. 10:14:00date
  17209. 10:14:01i have a separate video on that if
  17210. 10:14:03you're interested now the program that
  17211. 10:14:04i'm going to be converting to an
  17212. 10:14:06executable
  17213. 10:14:06has a gui portion to it i created a
  17214. 10:14:09clock program in a previous video
  17215. 10:14:11so i'm going to turn this clock program
  17216. 10:14:13into an executable
  17217. 10:14:15now i recommend creating a new folder on
  17218. 10:14:17your desktop just because this process
  17219. 10:14:19is normally somewhat messy so we'll
  17220. 10:14:21create a folder to contain it all
  17221. 10:14:23now copy any relevant python files and
  17222. 10:14:25or images and paste them to within
  17223. 10:14:27this folder if you have an image that
  17224. 10:14:29you would like to set as an icon for
  17225. 10:14:31your executable
  17226. 10:14:32i recommend moving that to the same
  17227. 10:14:33folder as well our next step is to open
  17228. 10:14:36command prompt
  17229. 10:14:38and we need to change the directory of
  17230. 10:14:40command prompt so that it's pointing to
  17231. 10:14:42that folder that contains our python
  17232. 10:14:44file
  17233. 10:14:44so on your python file right click on it
  17234. 10:14:46go to properties and copy
  17235. 10:14:48this location and in command prompt to
  17236. 10:14:52change directory type
  17237. 10:14:53cd space and then paste that location
  17238. 10:14:56now here's the cheat code to convert a
  17239. 10:14:58python file to an executable
  17240. 10:15:00make sure that pi installer and pip are
  17241. 10:15:02installed and both
  17242. 10:15:03up to date so type pi installer
  17243. 10:15:07dash capital f to make all of this one
  17244. 10:15:10file
  17245. 10:15:11if you need the terminal window in your
  17246. 10:15:14program
  17247. 10:15:14then omit this next part since i'm
  17248. 10:15:16running a gui application i do not need
  17249. 10:15:18that terminal window
  17250. 10:15:20so to remove that window type dash w
  17251. 10:15:23if you have an icon that you want to set
  17252. 10:15:26type dash
  17253. 10:15:27i and the name of your icon however it
  17254. 10:15:30should be an ico file
  17255. 10:15:32so if you have some other image that you
  17256. 10:15:34would like to convert to an ico file
  17257. 10:15:36here's how if you need to convert an
  17258. 10:15:39image to an ico file
  17259. 10:15:41you can just google convert to ico but
  17260. 10:15:43one website that i commonly use
  17261. 10:15:45is icoconvert.com just pick a file to
  17262. 10:15:48upload
  17263. 10:15:49so choose file i'm going to convert this
  17264. 10:15:52png file of an alarm clock
  17265. 10:15:54upload it there is a bunch of different
  17266. 10:15:57settings you can use
  17267. 10:15:58convert ico and then download my icon
  17268. 10:16:02and once your ico file is downloaded i
  17269. 10:16:04will drag and drop it to my folder
  17270. 10:16:06and i will rename this let's rename this
  17271. 10:16:09as
  17272. 10:16:09icon dot ico and here's that cheat code
  17273. 10:16:13one more time pi installer dash f
  17274. 10:16:16dash w if you do not need the terminal
  17275. 10:16:18window
  17276. 10:16:19dash i if you would like to add an icon
  17277. 10:16:22the name of your icon it should be an
  17278. 10:16:23ico file
  17279. 10:16:24followed by the name of the python file
  17280. 10:16:27and mine is named
  17281. 10:16:29clock.p.y then hit enter
  17282. 10:16:36alright looks like building our
  17283. 10:16:39executable has completed successfully so
  17284. 10:16:41open that folder back up
  17285. 10:16:43your executable is located within this
  17286. 10:16:45dist folder
  17287. 10:16:46i would recommend just dragging and
  17288. 10:16:48dropping it to your desktop
  17289. 10:16:50and we can get rid of this old folder
  17290. 10:16:53but close out a command prompt and by
  17291. 10:16:56clicking on this executable
  17292. 10:16:58this executable will now run my python
  17293. 10:17:00program
  17294. 10:17:01well everyone that is how to convert a
  17295. 10:17:03python file to
  17296. 10:17:04an exe file i will post my notes to the
  17297. 10:17:07comment section down below
  17298. 10:17:08and well yeah that's how to convert a
  17299. 10:17:10python file to an executable
  17300. 10:17:13what's going on everybody it's you bro
  17301. 10:17:15hope you're doing well
  17302. 10:17:16and in this video i'm going to show you
  17303. 10:17:17all how we can create a basic calculator
  17304. 10:17:19using python so
  17305. 10:17:21sit back relax and enjoy the show
  17306. 10:17:26let's create a calculator after
  17307. 10:17:27importing tkinter we'll need to define
  17308. 10:17:30three separate functions our first
  17309. 10:17:32function will be named
  17310. 10:17:33button press and there is one parameter
  17311. 10:17:37which we will call num we're not going
  17312. 10:17:39to fill this in quite yet we'll get back
  17313. 10:17:41to it later we'll use
  17314. 10:17:42pass as a placeholder we'll also need an
  17315. 10:17:45equals function to calculate our
  17316. 10:17:47expression
  17317. 10:17:50and a function to clear our calculator
  17318. 10:17:55okay that's it for our three functions
  17319. 10:17:57let's create a window
  17320. 10:17:58window equals tk and at the end of our
  17321. 10:18:02program
  17322. 10:18:02we'll write window.mainloop
  17323. 10:18:06let's set a title window
  17324. 10:18:09dot title calculator
  17325. 10:18:14programs good but make sure you spell
  17326. 10:18:17title right
  17327. 10:18:21and let's set a geometry of
  17328. 10:18:25500 500 should be decent
  17329. 10:18:29now we're going to create a string named
  17330. 10:18:31equation
  17331. 10:18:32text equals a set of quotes
  17332. 10:18:36this will effectively be empty for the
  17333. 10:18:38time being
  17334. 10:18:40and we'll need to create a string var
  17335. 10:18:42and i will call this
  17336. 10:18:44equation label equals string
  17337. 10:18:48var let's create a label
  17338. 10:18:51label equals label we're adding our
  17339. 10:18:54label
  17340. 10:18:54to our window i will set our text
  17341. 10:18:58variable equal to our equation label
  17342. 10:19:04you can pick a font if you wish
  17343. 10:19:11i'll set the background color equal to
  17344. 10:19:13white
  17345. 10:19:17a width of 24
  17346. 10:19:20and a height of two
  17347. 10:19:24and we need to finish this by using
  17348. 10:19:26label dot pack
  17349. 10:19:27and we should have a blank label within
  17350. 10:19:30our calculator
  17351. 10:19:31this will display our numbers that we're
  17352. 10:19:33entering in or our expression
  17353. 10:19:36okay this is the tedious part we have to
  17354. 10:19:38create all of the buttons and i think we
  17355. 10:19:40should place these all within a frame
  17356. 10:19:42frame equals frame we're adding our
  17357. 10:19:44frame to our window
  17358. 10:19:46frame dot pack
  17359. 10:19:50now let's create button one we'll do
  17360. 10:19:52some copying and pasting later
  17361. 10:19:53so it's not as tedious so button one
  17362. 10:19:57equals button we're adding our button to
  17363. 10:20:00our frame
  17364. 10:20:01i'll set the text equal to 1
  17365. 10:20:04the height equal to 4 and the width
  17366. 10:20:07equal to 9 and a font size
  17367. 10:20:11of 35
  17368. 10:20:15okay we'll need a command command
  17369. 10:20:18equals i'll write a lambda function here
  17370. 10:20:22lambda we will call button press
  17371. 10:20:28i think i'm going to put this on the
  17372. 10:20:29next line just so it's easier to read
  17373. 10:20:31button press and we will pass in the
  17374. 10:20:33number one
  17375. 10:20:35and we need to add our button to our
  17376. 10:20:37frame
  17377. 10:20:38button one dot grid
  17378. 10:20:42and i'm going to set a row equal to zero
  17379. 10:20:45and a column
  17380. 10:20:46equal to zero and let's just test it
  17381. 10:20:50okay let's add the other buttons i'm
  17382. 10:20:53going to copy and paste everything that
  17383. 10:20:54we have for button one
  17384. 10:20:56and change button one to button two
  17385. 10:21:00set the text equal to two pass into
  17386. 10:21:03to button press and for the row and
  17387. 10:21:06column
  17388. 10:21:06i'm going to set the column equal to one
  17389. 10:21:09but keep row the same
  17390. 10:21:11okay button three
  17391. 10:21:15it's gonna be the same process really so
  17392. 10:21:19text equals three
  17393. 10:21:20button press three row zero column two
  17394. 10:21:27and button four
  17395. 10:21:30text equals four button press
  17396. 10:21:34four column
  17397. 10:21:37actually let's set this to row one
  17398. 10:21:40column zero and i need to change that
  17399. 10:21:44too
  17400. 10:21:47okay let's just double check this
  17401. 10:21:52okay button five
  17402. 10:21:57so button five text equals five
  17403. 10:22:01button press five and
  17404. 10:22:05let's set column equal to one
  17405. 10:22:10okay number six i told you all this is
  17406. 10:22:14going to be tedious
  17407. 10:22:21okay so row one column two
  17408. 10:22:29button seven
  17409. 10:22:38okay so row two column two
  17410. 10:22:44actually let's change that to column
  17411. 10:22:46zero
  17412. 10:22:48much better okay
  17413. 10:22:51button eight
  17414. 10:22:55eight eight eight eight
  17415. 10:22:59row two column one
  17416. 10:23:04number nine
  17417. 10:23:14row two column two
  17418. 10:23:20and lastly zero
  17419. 10:23:26so button zero
  17420. 10:23:30text equals zero button press pass in
  17421. 10:23:33zero
  17422. 10:23:34and this will be row three column
  17423. 10:23:38zero okay
  17424. 10:23:41not too bad now let's fill in some of
  17425. 10:23:43the symbols
  17426. 10:23:45so we need plus
  17427. 10:23:49i'm just going to rename this as plus
  17428. 10:23:54the text will equal the plus sign put
  17429. 10:23:58that within quotes
  17430. 10:24:00and button press will be plus
  17431. 10:24:05oh and we should probably put this in a
  17432. 10:24:07row and column so let's say
  17433. 10:24:09row 0 column three
  17434. 10:24:12so it should be on the right hand side
  17435. 10:24:14here
  17436. 10:24:15okay we need minus
  17437. 10:24:25let's change plus to minus
  17438. 10:24:30and place this at row one column three
  17439. 10:24:36we need multiply
  17440. 10:24:45that is an asterisk
  17441. 10:24:51row two column three
  17442. 10:24:55and divide
  17443. 10:25:04that is a forward slash
  17444. 10:25:12row three column three
  17445. 10:25:18i promise we're near the end of creating
  17446. 10:25:20these buttons we'll need an equal sign
  17447. 10:25:23let's say equal
  17448. 10:25:27equal text
  17449. 10:25:31is the equal sign and our command
  17450. 10:25:34is going to be
  17451. 10:25:37equals the function
  17452. 10:25:41and this will be row 3 column 2.
  17453. 10:25:50we need a decimal
  17454. 10:26:02let's change the text to a decimal
  17455. 10:26:08row three column one
  17456. 10:26:13okay and lastly we just need a clear
  17457. 10:26:16button let's put this at the bottom
  17458. 10:26:24so clear clear
  17459. 10:26:28let's set the text equal to the word
  17460. 10:26:31clear
  17461. 10:26:33and the command will be our clear
  17462. 10:26:36function
  17463. 10:26:38and i think i'm going to pack this at
  17464. 10:26:40the end
  17465. 10:26:42but let's change frame to window
  17466. 10:26:46okay let's check out all these buttons
  17467. 10:26:50okay not too bad i think i'm going to
  17468. 10:26:51change the width on our clear button so
  17469. 10:26:53it's a little bit wider
  17470. 10:26:55let's change the width to 12.
  17471. 10:26:58that's a little bit better okay let's
  17472. 10:27:01fill in some of these functions
  17473. 10:27:03let's start with button press we're
  17474. 10:27:06going to state
  17475. 10:27:07global equation
  17476. 10:27:10text then our equation text
  17477. 10:27:16is equal to equation text plus
  17478. 10:27:20we're converting whatever number or
  17479. 10:27:22symbol that we receive
  17480. 10:27:24to a string and our equation
  17481. 10:27:29label which is a string var will be set
  17482. 10:27:32to our equation text
  17483. 10:27:36and let's test this
  17484. 10:27:39so we can type in any combination of
  17485. 10:27:42numbers or symbols
  17486. 10:27:47okay let's work on our equals
  17487. 10:27:50function next so let's say
  17488. 10:27:53global equation
  17489. 10:27:57text total
  17490. 10:28:01equals
  17491. 10:28:04eval now eval will parse the expression
  17492. 10:28:07that we pass in
  17493. 10:28:10equation
  17494. 10:28:14text
  17495. 10:28:17and our equation label
  17496. 10:28:20will be set to whatever our total is
  17497. 10:28:26and if we want to reuse our total let's
  17498. 10:28:28set
  17499. 10:28:29our equation text
  17500. 10:28:33equal to total 2 plus 2
  17501. 10:28:37equals four minus three equals one
  17502. 10:28:41times five equals five divided by two
  17503. 10:28:44equals two point five now what if we
  17504. 10:28:47divide by zero
  17505. 10:28:49well we'll run into that ugly zero
  17506. 10:28:51division error
  17507. 10:28:53so let's write our code within a try and
  17508. 10:28:55accept block
  17509. 10:28:56so i'm going to indent some of this
  17510. 10:29:01and let's write try all of this code
  17511. 10:29:04and we will catch a zero division error
  17512. 10:29:06exception
  17513. 10:29:08so accept zero division error
  17514. 10:29:15and i will set our equation
  17515. 10:29:19label set
  17516. 10:29:22let's say arithmetic
  17517. 10:29:26error equation text
  17518. 10:29:30equals a set of quotes okay so we should
  17519. 10:29:33be able to catch this exception now
  17520. 10:29:37five divided by zero equals arithmetic
  17521. 10:29:40error
  17522. 10:29:41now what if we have a syntax here
  17523. 10:29:45like we type in just a bunch of symbols
  17524. 10:29:46and hit equals
  17525. 10:29:49we'll probably want to catch the syntax
  17526. 10:29:51error as well
  17527. 10:29:52so let's write an additional accept
  17528. 10:29:55block before
  17529. 10:29:56zero division error or after i guess it
  17530. 10:29:59doesn't matter
  17531. 10:30:01accept syntax
  17532. 10:30:04error let's change arithmetic to
  17533. 10:30:09syntax and we can keep equation text the
  17534. 10:30:11same
  17535. 10:30:13so now our program won't crash if we
  17536. 10:30:15type in a bunch of symbols
  17537. 10:30:17syntax error now let's work on the clear
  17538. 10:30:20button
  17539. 10:30:22so that is within the clear function
  17540. 10:30:28so we will set global
  17541. 10:30:31equation text
  17542. 10:30:36equation label
  17543. 10:30:41dot set to a set of quotes to
  17544. 10:30:43effectively clear it
  17545. 10:30:46and our equation
  17546. 10:30:49text equal to a set of quotes as well
  17547. 10:30:54and that should be it for this program
  17548. 10:30:55let's run this one last time
  17549. 10:30:57let's say 3.14 times
  17550. 10:31:012.1 equals 6.594
  17551. 10:31:05divided by 2.1
  17552. 10:31:08equals 3.14 minus one i'm just making up
  17553. 10:31:12numbers here
  17554. 10:31:13plus ten equals twelve point one four
  17555. 10:31:16and let's clear this and start again one
  17556. 10:31:19plus
  17557. 10:31:19two plus three plus four equals ten
  17558. 10:31:23i think that's good enough all right
  17559. 10:31:24everybody so that's how to create a
  17560. 10:31:26basic
  17561. 10:31:27calculator in python i will post all of
  17562. 10:31:29my code in the comments section down
  17563. 10:31:31below if you would like a copy for
  17564. 10:31:33yourself
  17565. 10:31:34but yeah that's how to create a basic
  17566. 10:31:36calculator using python
  17567. 10:31:38hey what's going on everybody it's your
  17568. 10:31:40bro hope you're doing well and in this
  17569. 10:31:42video i'm going to explain how we can
  17570. 10:31:43build a basic text editor program in
  17571. 10:31:46python so
  17572. 10:31:47sit back relax and enjoy the show
  17573. 10:31:51okay people let's create a text editor
  17574. 10:31:53here's all the different imports that
  17575. 10:31:55you'll need and we'll begin by defining
  17576. 10:31:57all of the different functions that
  17577. 10:31:58we'll need
  17578. 10:31:59so let's say we have a change color
  17579. 10:32:01function to change the color
  17580. 10:32:03of our font and for all of these
  17581. 10:32:05functions i'm going to write pass as a
  17582. 10:32:07placeholder
  17583. 10:32:08so we have change font and
  17584. 10:32:12this function will accept a varying
  17585. 10:32:14amount of arguments
  17586. 10:32:15so we'll use that args keyword
  17587. 10:32:19we will need to open a file new
  17588. 10:32:23file
  17589. 10:32:27open file
  17590. 10:32:34save file
  17591. 10:32:40cut
  17592. 10:32:44copy
  17593. 10:32:49paste yeah there's a lot of functions
  17594. 10:32:52here
  17595. 10:32:54about
  17596. 10:32:57this is for an about section
  17597. 10:33:01and lastly quit
  17598. 10:33:05so how many do we have one two three
  17599. 10:33:09four five six seven eight
  17600. 10:33:12nine ten ten functions let's create the
  17601. 10:33:15main window that we're gonna work with
  17602. 10:33:17window equals tk
  17603. 10:33:20and at the very end window.main
  17604. 10:33:24loop
  17605. 10:33:27okay so let's create a title for this
  17606. 10:33:28window window
  17607. 10:33:30title notepad
  17608. 10:33:34program i guess or maybe text editor
  17609. 10:33:36program would be more appropriate
  17610. 10:33:39text editor program
  17611. 10:33:44okay and we will need a file to work
  17612. 10:33:46with and i'll declare that now
  17613. 10:33:48file equals none
  17614. 10:33:51we'll need a window width and height
  17615. 10:33:55window with equals 500 is good and a
  17616. 10:34:00height
  17617. 10:34:03okay so let's attempt to center this
  17618. 10:34:05window
  17619. 10:34:06because right now it just appears to the
  17620. 10:34:08side
  17621. 10:34:10so our next step is to get the screen
  17622. 10:34:12width
  17623. 10:34:16equals window dot w
  17624. 10:34:20info screen width
  17625. 10:34:24and i'll copy this for screen height
  17626. 10:34:35okay so we need to figure out how much
  17627. 10:34:37we're going to move our window on the x
  17628. 10:34:39and y axis so let's say x
  17629. 10:34:42equals screen
  17630. 10:34:46width divided by 2
  17631. 10:34:49minus
  17632. 10:34:52window width divided by
  17633. 10:34:56two and we're going to cast all of this
  17634. 10:34:58to an
  17635. 10:34:59end so add an inch cast around this
  17636. 10:35:04let's copy this do the same thing for y
  17637. 10:35:08except this will be screen height
  17638. 10:35:11and window height
  17639. 10:35:17okay let's set the geometry for the
  17640. 10:35:19window window
  17641. 10:35:21geometry i'm going to use a
  17642. 10:35:24string format method so let's say
  17643. 10:35:32these will be placeholders for our
  17644. 10:35:34values
  17645. 10:35:37so we have window width for the first
  17646. 10:35:40value
  17647. 10:35:42window height
  17648. 10:35:46x and y so this should be near the
  17649. 10:35:50middle
  17650. 10:35:51cool
  17651. 10:35:55we're going to create a string var
  17652. 10:35:57variable to hold the
  17653. 10:35:58font name font name
  17654. 10:36:02equals string var
  17655. 10:36:05and pass in your window and you can set
  17656. 10:36:08this to
  17657. 10:36:09what do you want your default font to be
  17658. 10:36:12so let's say font
  17659. 10:36:13name dot set and then pick a font
  17660. 10:36:16i'll use ariel i think
  17661. 10:36:19let's create a font size string bar as
  17662. 10:36:22well
  17663. 10:36:28we're going to add this to the window
  17664. 10:36:32and we can set a default size
  17665. 10:36:37perhaps 25.
  17666. 10:36:40okay let's create a text area
  17667. 10:36:43text area equals text
  17668. 10:36:48we're adding this to the window i'm
  17669. 10:36:51going to set a
  17670. 10:36:52font equal to font
  17671. 10:36:55name dot get the name of the font that
  17672. 10:36:59we want to use
  17673. 10:37:00will be the font that we set within our
  17674. 10:37:03font
  17675. 10:37:03name string var so now we'll use a size
  17676. 10:37:07of font size dot get
  17677. 10:37:11font size dot get
  17678. 10:37:16okay now we need a scroll bar
  17679. 10:37:20scroll bar equals
  17680. 10:37:23scroll bar
  17681. 10:37:26writing this to our text area so pass
  17682. 10:37:29that in
  17683. 10:37:31and i'm going to configure my window
  17684. 10:37:34using grid row configure
  17685. 10:37:39this will allow our text area to expand
  17686. 10:37:42but we're going to set a weight equal to
  17687. 10:37:441 so that it doesn't back expand
  17688. 10:37:47and then we need column configure
  17689. 10:37:55okay and text
  17690. 10:37:59area dot grid
  17691. 10:38:03i'm going to use the sticky keyword and
  17692. 10:38:07this will stick north plus east
  17693. 10:38:10plus south plus west
  17694. 10:38:14so my text area should take most of the
  17695. 10:38:16window
  17696. 10:38:19this is some sample text
  17697. 10:38:24hooray so it goes down to the next line
  17698. 10:38:27once it runs out of room on one line
  17699. 10:38:29unless you resize the window which is
  17700. 10:38:31exactly what we wanted
  17701. 10:38:33let's create a scroll bar while we
  17702. 10:38:37already created it we just have to
  17703. 10:38:39add a few details to it
  17704. 10:38:42so that will be near the bottom
  17705. 10:38:45and why don't we say scroll
  17706. 10:38:49bar dot pack
  17707. 10:38:53side equals right
  17708. 10:38:56fill equals y
  17709. 10:39:00and then we just have to configure our
  17710. 10:39:03text area
  17711. 10:39:04with the scroll bar so text
  17712. 10:39:08area dot config
  17713. 10:39:12y scroll command
  17714. 10:39:16equals scroll bar
  17715. 10:39:20dot set so that should work but
  17716. 10:39:24let's test it to be sure okay i'm just
  17717. 10:39:27going to type a bunch of f's
  17718. 10:39:31okay look at that our scroll bar works
  17719. 10:39:34cool okay let's head to this portion
  17720. 10:39:39and we'll create a bunch of buttons well
  17721. 10:39:42a button
  17722. 10:39:43an option menu and a spin box for the
  17723. 10:39:46font color the size and the font style
  17724. 10:39:49so i'm going to place all of these
  17725. 10:39:51different widgets within a frame
  17726. 10:39:53frame equals frame add it to the window
  17727. 10:39:58and then i'm going to use frame dot grid
  17728. 10:40:02okay let's create a color button to
  17729. 10:40:05change the fonts color
  17730. 10:40:07color button equals button
  17731. 10:40:10i'll add my button to my frame
  17732. 10:40:13i'll set the text of the button equal to
  17733. 10:40:16color
  17734. 10:40:18and i will associate a command of
  17735. 10:40:21change color which we have already
  17736. 10:40:23defined at the top
  17737. 10:40:25where is it at the top here change color
  17738. 10:40:32and then actually that's it for this
  17739. 10:40:35portion
  17740. 10:40:36so then we need to add this button color
  17741. 10:40:40button dot grid i'll set the row
  17742. 10:40:44equal to zero and the column equal to
  17743. 10:40:47zero
  17744. 10:40:48so let's be sure that this appears make
  17745. 10:40:51sure you spell grid right
  17746. 10:40:53okay and we have a color button but this
  17747. 10:40:56button doesn't do anything
  17748. 10:40:58so let's head to the change color
  17749. 10:41:00function
  17750. 10:41:02within the change color function we're
  17751. 10:41:04going to say color
  17752. 10:41:05equals color chooser
  17753. 10:41:10dot ask color
  17754. 10:41:14and you can set a title to
  17755. 10:41:17pick a color or
  17756. 10:41:21else you don't technically need a title
  17757. 10:41:25and then text area
  17758. 10:41:28dot config oh i should probably print
  17759. 10:41:32this color
  17760. 10:41:32so let me do that um i'm just gonna
  17761. 10:41:35explain something here
  17762. 10:41:36so this color is going to be i believe a
  17763. 10:41:39tuple
  17764. 10:41:39so let's print it so let's say that i
  17765. 10:41:43would like
  17766. 10:41:45white okay so we have a tuple we just
  17767. 10:41:49need
  17768. 10:41:50one of these values not both of them
  17769. 10:41:53so i'm going to say
  17770. 10:41:56text area dot config
  17771. 10:42:00foreground equals our color but we do
  17772. 10:42:03not want to assign a tuple
  17773. 10:42:05to our color we just need one of these
  17774. 10:42:07values so let's say i would like this
  17775. 10:42:09hex value
  17776. 10:42:11so i'm going to say at index 1
  17777. 10:42:15and let's try it color
  17778. 10:42:19perhaps i would like my text pink for
  17779. 10:42:22some reason so we're going with pink
  17780. 10:42:24this is some text i suppose
  17781. 10:42:28and we can change this back too by using
  17782. 10:42:30this color button now i would like
  17783. 10:42:32blue and we have blue text okay so that
  17784. 10:42:36is the change color function i'm going
  17785. 10:42:38to minimize this because we do not need
  17786. 10:42:40it anymore
  17787. 10:42:41okay we're going to change the font next
  17788. 10:42:44so
  17789. 10:42:44head to the portion of our code right
  17790. 10:42:48after the
  17791. 10:42:49color button and we'll create an option
  17792. 10:42:51menu
  17793. 10:42:52to list all of the different fonts
  17794. 10:42:53available to us so let's say
  17795. 10:42:55font box equals option
  17796. 10:42:59menu we're adding this to our frame
  17797. 10:43:04we need to set a variable a string var
  17798. 10:43:08so that will be our font name that we
  17799. 10:43:10declared up here
  17800. 10:43:12now here's a trick to add all of the
  17801. 10:43:14fonts as options to our option menu
  17802. 10:43:17we can use the unpacking operator which
  17803. 10:43:20is an asterisk
  17804. 10:43:21followed by font dot families
  17805. 10:43:25and then call this function so this will
  17806. 10:43:28return
  17807. 10:43:29all of the different fonts available to
  17808. 10:43:31us and we just need to set a command
  17809. 10:43:34and when we use our option menu we'll
  17810. 10:43:37call
  17811. 10:43:38change font and we need to add
  17812. 10:43:41our font box to our grid
  17813. 10:43:46row equals zero and column
  17814. 10:43:49equals one so this should be on the
  17815. 10:43:51right hand side of our color button
  17816. 10:43:55yeah it's going to look something like
  17817. 10:43:56that and you can see the default is
  17818. 10:43:58arial that we set
  17819. 10:43:59and we can change it to something else
  17820. 10:44:02like
  17821. 10:44:03comic sans or whatever
  17822. 10:44:06uh however it's not going to change it
  17823. 10:44:08quite yet so that'll be the change font
  17824. 10:44:10function
  17825. 10:44:11before we fill in the change font
  17826. 10:44:13function there's another widget that's
  17827. 10:44:15going to share the same command of
  17828. 10:44:17change font so we should create that
  17829. 10:44:18next
  17830. 10:44:19so our next widget is a spin box which i
  17831. 10:44:22will name
  17832. 10:44:23sizebox this will be in charge of
  17833. 10:44:26increasing or decreasing the font
  17834. 10:44:28so size box equals spin box
  17835. 10:44:32we're adding the spin box to our frame
  17836. 10:44:35and we'll set a range
  17837. 10:44:37so from underscore you have to add that
  17838. 10:44:39underscore it's kind of weird
  17839. 10:44:41this is the range of our spin box for
  17840. 10:44:43the font size
  17841. 10:44:44from 1 to let's say 100
  17842. 10:44:49the text variable will be
  17843. 10:44:53font size
  17844. 10:44:57and our command equals change
  17845. 10:45:01font so when
  17846. 10:45:04we use our spin box and call this
  17847. 10:45:08command of change font it's going to
  17848. 10:45:10pass in
  17849. 10:45:11our font size as an argument whereas
  17850. 10:45:13with option menu
  17851. 10:45:14it doesn't pass any arguments that's why
  17852. 10:45:17with our
  17853. 10:45:17change font function we're accepting a
  17854. 10:45:20varying amount of arguments
  17855. 10:45:21because one's going to pass in an
  17856. 10:45:23argument and the other isn't and we just
  17857. 10:45:26need to add our spin box
  17858. 10:45:28so that would be size box
  17859. 10:45:32dot grid row equals zero
  17860. 10:45:36and column equals two
  17861. 10:45:39so we should have a spin box right on
  17862. 10:45:42the bottom
  17863. 10:45:43and it's set to 25 that was our default
  17864. 10:45:46for
  17865. 10:45:47our font size string var which we set as
  17866. 10:45:50the text variable
  17867. 10:45:52okay so we can fill in this function of
  17868. 10:45:54change font now
  17869. 10:45:55which will apply both to our option menu
  17870. 10:45:58and our spin box
  17871. 10:46:00so that is all the way at the top within
  17872. 10:46:03the change font function
  17873. 10:46:04we're going to take our text area
  17874. 10:46:08dot config set
  17875. 10:46:11the font equal to font
  17876. 10:46:14name dot get
  17877. 10:46:18and the size will be size box
  17878. 10:46:22dot get and that's it for this function
  17879. 10:46:25i'll minimize it for now and let's test
  17880. 10:46:28it
  17881. 10:46:30this is some sample text
  17882. 10:46:34hey how you doing good
  17883. 10:46:38looking okay so let's change
  17884. 10:46:41the font uh let's pick comic sans or
  17885. 10:46:45whatever you want
  17886. 10:46:46and we can increase or decrease the size
  17887. 10:46:51and we can change the color how about
  17888. 10:46:54red this time
  17889. 10:46:56all right nice now let's add a menu bar
  17890. 10:46:59at the top so we can save this file or
  17891. 10:47:02open a file or create a new file and
  17892. 10:47:04some other stuff
  17893. 10:47:06on second thought i'm going to move
  17894. 10:47:08these two last lines of code
  17895. 10:47:09to this section here
  17896. 10:47:12where we create our scroll bar just so
  17897. 10:47:14that it's more organized
  17898. 10:47:16okay so at the bottom let's create
  17899. 10:47:19various drop-down menus so we'll need a
  17900. 10:47:21menu bar to begin with
  17901. 10:47:23so let's say menu bar
  17902. 10:47:27equals menu we're adding this to our
  17903. 10:47:30window
  17904. 10:47:31make sure that's capital by the way
  17905. 10:47:35and window dot config
  17906. 10:47:39menu equals menu bar
  17907. 10:47:44so we'll create a file menu edit menu
  17908. 10:47:46and
  17909. 10:47:47a help menu so let's begin with the file
  17910. 10:47:50menu to add to the menu bar file menu
  17911. 10:47:54equals menu we're adding this to our
  17912. 10:47:57menu
  17913. 10:47:58bar and set a tear off equal to zero
  17914. 10:48:06we'll need a cascade for it to actually
  17915. 10:48:09be a drop down menu
  17916. 10:48:10so that will be menu bar
  17917. 10:48:14dot add cascade
  17918. 10:48:21label equals file
  17919. 10:48:25comma menu equals
  17920. 10:48:28file menu
  17921. 10:48:32let's see if anything appears yet yep so
  17922. 10:48:35we have a file menu
  17923. 10:48:37but there's nothing to drop down yet
  17924. 10:48:40so we need some options
  17925. 10:48:43well commands technically file menu
  17926. 10:48:47dot add command
  17927. 10:48:54label equals new
  17928. 10:49:00and the command will equal our
  17929. 10:49:03new file function
  17930. 10:49:06actually i'll put these together okay so
  17931. 10:49:09let's copy this
  17932. 10:49:10and paste it we have open
  17933. 10:49:16the command will be open file
  17934. 10:49:22we have save
  17935. 10:49:25the command will be save
  17936. 10:49:28file and i'll add a separator
  17937. 10:49:33file menu dot add
  17938. 10:49:37separator and lastly
  17939. 10:49:40we will have exit
  17940. 10:49:44command quit
  17941. 10:49:47okay let me show you what this looks
  17942. 10:49:49like
  17943. 10:49:51so here's our drop down menu
  17944. 10:49:54that separator is this bold line going
  17945. 10:49:56across
  17946. 10:49:57so if i was to remove this then we
  17947. 10:50:00wouldn't have that bolt line
  17948. 10:50:01you can keep it in or get rid of it do
  17949. 10:50:03whatever you want
  17950. 10:50:05and this is the tear off so i'm going to
  17951. 10:50:07remove the tear off
  17952. 10:50:11and you get this like tear off line at
  17953. 10:50:13the top i guess it's the default
  17954. 10:50:15i usually don't like it so i set tear
  17955. 10:50:17off equal to zero
  17956. 10:50:18so that is our file menu let's work on a
  17957. 10:50:21edit menu
  17958. 10:50:24edit menu equals
  17959. 10:50:27menu menu
  17960. 10:50:32bar make sure that's capital two
  17961. 10:50:38tare off equals zero
  17962. 10:50:42we'll need a cascade menu bar
  17963. 10:50:46dot add cascade
  17964. 10:50:55label equals
  17965. 10:51:00edit
  17966. 10:51:02menu equals edit
  17967. 10:51:06menu
  17968. 10:51:10edit menu dot add
  17969. 10:51:14command
  17970. 10:51:19label equals cut to cut some text
  17971. 10:51:25command equals copy
  17972. 10:51:30actually that's cut my bad
  17973. 10:51:33okay next is copy so let's copy this
  17974. 10:51:36and paste it
  17975. 10:51:40cut copy
  17976. 10:51:44we have paste
  17977. 10:51:47command paste
  17978. 10:51:51all right let's take a look
  17979. 10:51:55oh make sure you spell label right
  17980. 10:51:58okay there we go okay so we can cut
  17981. 10:52:01copy and paste and lastly we should have
  17982. 10:52:05a help menu
  17983. 10:52:08help menu equals
  17984. 10:52:12menu menu
  17985. 10:52:15bar tear off
  17986. 10:52:19equals zero
  17987. 10:52:22i think i'll copy this for my cascade
  17988. 10:52:26so menu bar dot add cascade
  17989. 10:52:30help menu equals help
  17990. 10:52:33menu
  17991. 10:52:38what we'll be doing for a command is
  17992. 10:52:40that we'll be creating
  17993. 10:52:41an about section that will bring up a
  17994. 10:52:44pop-up window if you need to display
  17995. 10:52:46any like help information or anything
  17996. 10:52:49uh so let's say about and the command
  17997. 10:52:52will be
  17998. 10:52:54about okay let's take a look
  17999. 10:52:58so we have help and we'll have an about
  18000. 10:53:00section that will tell you more about
  18001. 10:53:02this program
  18002. 10:53:03but we need to work on some of these
  18003. 10:53:05functions let's head to the quit
  18004. 10:53:08function
  18005. 10:53:09this one's fairly easy this is found
  18006. 10:53:11within the file menu
  18007. 10:53:12window dot destroy that will close out
  18008. 10:53:16of the window
  18009. 10:53:18so go to file then exit to exit the
  18010. 10:53:22program
  18011. 10:53:23we don't need it anymore so let's
  18012. 10:53:25minimize this
  18013. 10:53:26okay let's go to the about function
  18014. 10:53:31let's bring up a message box show info
  18015. 10:53:36this first argument is the title so
  18016. 10:53:38let's say
  18017. 10:53:39about this program
  18018. 10:53:43and the text will be whatever you want
  18019. 10:53:45to
  18020. 10:53:46tell the user about this program this
  18021. 10:53:49is a program written
  18022. 10:53:53by you
  18023. 10:53:57okay so we should have a pop-up window
  18024. 10:53:59when you go to help
  18025. 10:54:00then about about this program this is a
  18026. 10:54:03program
  18027. 10:54:04written by you the next function is the
  18028. 10:54:07paste function so we can paste some text
  18029. 10:54:10text area dot event
  18030. 10:54:14generate and the event is going to be
  18031. 10:54:18within quotes
  18032. 10:54:19and double angle brackets
  18033. 10:54:22paste let's copy this
  18034. 10:54:26and change paste to copy
  18035. 10:54:31and then cut
  18036. 10:54:37okay so we should be able to cut copy
  18037. 10:54:39and paste
  18038. 10:54:44hey how's it
  18039. 10:54:47hanging okay
  18040. 10:54:50let's highlight this edit
  18041. 10:54:54copy let's move down to the next line
  18042. 10:54:58and paste and we should be able to cut
  18043. 10:55:01as well
  18044. 10:55:02edit cut nice
  18045. 10:55:06head to the new file function
  18046. 10:55:09and we'll set the title of our window to
  18047. 10:55:12maybe
  18048. 10:55:13untitled until we save something so
  18049. 10:55:16untitled
  18050. 10:55:20and i need to delete any text within our
  18051. 10:55:23text area
  18052. 10:55:24text area dot delete and we need to set
  18053. 10:55:28a range so this has to be a floating
  18054. 10:55:30point number
  18055. 10:55:31the first character until the end
  18056. 10:55:34let's try it so i'll write some sample
  18057. 10:55:40text
  18058. 10:55:42and we are going to go to file new
  18059. 10:55:45that changed the title and it deleted
  18060. 10:55:48our text
  18061. 10:55:49so that is the new file function
  18062. 10:55:52let's work on open file we are now
  18063. 10:55:55within
  18064. 10:55:56the open file function and we'll say
  18065. 10:55:58file
  18066. 10:55:59equals ask open
  18067. 10:56:03file name so we have a
  18068. 10:56:07default extension if you would like one
  18069. 10:56:11default i think i spelled that wrong
  18070. 10:56:14extension
  18071. 10:56:18equals let's say a txt file
  18072. 10:56:23comma what are the accepted file types
  18073. 10:56:28i'll set this equal to
  18074. 10:56:32all files
  18075. 10:56:35comma and then we need asterisk
  18076. 10:56:38dot asterisk for all file types
  18077. 10:56:43as well as text documents
  18078. 10:56:46so these are what is accepted when we
  18079. 10:56:49look for a file so we have either all
  18080. 10:56:52files
  18081. 10:56:53or all text documents text
  18082. 10:56:57documents comma
  18083. 10:57:02asterisk dot txt
  18084. 10:57:08okay so then let's try some code
  18085. 10:57:14we'll set our window title
  18086. 10:57:18equal to os dot path
  18087. 10:57:21dot base name of our file
  18088. 10:57:24so that we will change the title of the
  18089. 10:57:26window
  18090. 10:57:27to match whatever the file is that we
  18091. 10:57:29opened
  18092. 10:57:32then let's take our text
  18093. 10:57:35area delete what's currently here
  18094. 10:57:38from one until the end
  18095. 10:57:42take our file use the open function
  18096. 10:57:47open our file and read it so pass in our
  18097. 10:57:52text area dot insert
  18098. 10:57:58beginning at index of 1.0
  18099. 10:58:04file dot read
  18100. 10:58:07that you can catch any exceptions to
  18101. 10:58:11i'll just write accept exception but
  18102. 10:58:13it's better to name specific
  18103. 10:58:15exceptions i'll just print
  18104. 10:58:20couldn't read
  18105. 10:58:24file and we should close our file at the
  18106. 10:58:28end within a finally block
  18107. 10:58:31finally file dot close
  18108. 10:58:35we'll need a sample file to read so i'm
  18109. 10:58:37going to create a new file on my desktop
  18110. 10:58:40i'll call this test.txt
  18111. 10:58:44and let's write something whoa
  18112. 10:58:47you can actually read this
  18113. 10:58:53awesome okay let's save
  18114. 10:58:56close out of it add run our program
  18115. 10:59:01file open test.txt
  18116. 10:59:05open whoa you can actually read this
  18117. 10:59:08awesome nice so we know that the open
  18118. 10:59:12function is working
  18119. 10:59:13if we need to create a new file just go
  18120. 10:59:15to file new
  18121. 10:59:17and that will delete our old text
  18122. 10:59:20so lastly we have save file
  18123. 10:59:25underneath the save file function file
  18124. 10:59:28equals file dialog
  18125. 10:59:31dot ask save as
  18126. 10:59:34file name so this will be the default
  18127. 10:59:38name for the file but you can change
  18128. 10:59:40that
  18129. 10:59:41initial file equals
  18130. 10:59:45let's say untitled
  18131. 10:59:49dot
  18132. 10:59:53you txt set a default extension default
  18133. 10:59:57extension equals let's say txt
  18134. 11:00:05file types equals
  18135. 11:00:14all files
  18136. 11:00:18comma then asterisk dot asterisk
  18137. 11:00:22for all file types
  18138. 11:00:30and any text documents text
  18139. 11:00:34documents
  18140. 11:00:38asterisk dot txt
  18141. 11:00:46okay if file
  18142. 11:00:49is none that's if they close out of our
  18143. 11:00:52file dialog
  18144. 11:00:56we'll just return
  18145. 11:00:59else
  18146. 11:01:03so we'll set the window title first
  18147. 11:01:07window title os dot path
  18148. 11:01:11dot base name
  18149. 11:01:16file
  18150. 11:01:18file equals open open
  18151. 11:01:22our file and write
  18152. 11:01:27file dot right
  18153. 11:01:31take our text area and get
  18154. 11:01:34the text from
  18155. 11:01:37index one through the end
  18156. 11:01:42catch any exceptions it's better to name
  18157. 11:01:45them instead of just
  18158. 11:01:46doing a general accept exception
  18159. 11:01:49but i'm just going to print couldn't
  18160. 11:01:53save file
  18161. 11:01:57and finally close our file
  18162. 11:02:00file dot close all right so
  18163. 11:02:03let's test it so i'm going to change
  18164. 11:02:07the font i wonder if impact is in here
  18165. 11:02:11yes it is i'll change the font size
  18166. 11:02:15maybe 30 and i'll pick a color
  18167. 11:02:18how about this swamp green color okay so
  18168. 11:02:22write something perhaps some lyrics to a
  18169. 11:02:24song you like
  18170. 11:02:40i should probably test copying and
  18171. 11:02:42pasting
  18172. 11:02:43edit copy
  18173. 11:02:46edit paste
  18174. 11:02:50nice
  18175. 11:02:55all right let's actually try and save
  18176. 11:02:56this now i'll go to
  18177. 11:02:58file save the default file name is
  18178. 11:03:02untitled because we set it to that
  18179. 11:03:05i'll say all star
  18180. 11:03:09lyrics
  18181. 11:03:13and save now i should be able to open
  18182. 11:03:17this
  18183. 11:03:18so i'm going to go to my desktop here's
  18184. 11:03:21that text file
  18185. 11:03:21i'm going to open it and here are the
  18186. 11:03:23lyrics that i wrote within my text
  18187. 11:03:25editor
  18188. 11:03:26and let's try and read this so let's go
  18189. 11:03:29to
  18190. 11:03:29file open find your text document
  18191. 11:03:33open and would you look at that my
  18192. 11:03:35lyrics are still here
  18193. 11:03:37however this program doesn't save the
  18194. 11:03:39font
  18195. 11:03:40so i thought of a fun last minute
  18196. 11:03:42project that we can do we can actually
  18197. 11:03:44use our text editor to write a python
  18198. 11:03:46file much like what we do with pycharm
  18199. 11:03:49so let's write a program using our text
  18200. 11:03:51editor program
  18201. 11:03:53it'll be just a simple program let's
  18202. 11:03:56print
  18203. 11:03:58hello world and ask for some user input
  18204. 11:04:02let's say name equals input
  18205. 11:04:09what's your name
  18206. 11:04:14and we will print
  18207. 11:04:18hello plus our name
  18208. 11:04:21variable and you can change the font and
  18209. 11:04:24everything too if you want
  18210. 11:04:31okay so let's save this file save
  18211. 11:04:34i'll save this to my desktop but make
  18212. 11:04:36sure you save it as a
  18213. 11:04:38py file a python file i'll name this as
  18214. 11:04:42hello dot py and save
  18215. 11:04:47now to run a file using command prompt
  18216. 11:04:49we need to find
  18217. 11:04:50the directory that contains our python
  18218. 11:04:52file mine is hello.py and it's located
  18219. 11:04:56on my desktop
  18220. 11:04:57so i need the location right click go to
  18221. 11:05:00properties
  18222. 11:05:01you can copy the location then within
  18223. 11:05:03command prompt or terminal
  18224. 11:05:05you can change directory by typing cd
  18225. 11:05:08space
  18226. 11:05:09and then paste your new directory the
  18227. 11:05:11new location
  18228. 11:05:12enter then to run a python program
  18229. 11:05:15type python space and then the name of
  18230. 11:05:19the program
  18231. 11:05:20the python file mine is hello dot
  18232. 11:05:23py hit enter hello world what is your
  18233. 11:05:26name
  18234. 11:05:27bro hello bro i just thought that would
  18235. 11:05:31be a fun last minute project
  18236. 11:05:32that we can do you don't even
  18237. 11:05:33technically need an ide to write a
  18238. 11:05:36python program
  18239. 11:05:37screw you pycharm well everyone that is
  18240. 11:05:39a basic
  18241. 11:05:40text editor program i will post the code
  18242. 11:05:43for this program
  18243. 11:05:44in the comment section down below and
  18244. 11:05:46well yeah that's a basic text editor
  18245. 11:05:48program
  18246. 11:05:49using python all right
  18247. 11:05:52ladies and gentlemen let's create a game
  18248. 11:05:54of tic-tac-toe to begin we'll need to
  18249. 11:05:56import tkinter as well as random and
  18250. 11:05:59let's begin by defining
  18251. 11:06:00all of the different functions that
  18252. 11:06:02we'll need let's define a function named
  18253. 11:06:04next turn for the time being we'll just
  18254. 11:06:08write pass we'll fill this in later on
  18255. 11:06:10we'll need a function named check winner
  18256. 11:06:16a function named empty spaces to check
  18257. 11:06:21if there are any empty spaces left and
  18258. 11:06:24lastly a function named new
  18259. 11:06:26game that will launch a new game for us
  18260. 11:06:29okay we have our four functions now
  18261. 11:06:31let's create a window
  18262. 11:06:32let's create a window window equals
  18263. 11:06:35tk and at the end of our program we need
  18264. 11:06:39to use window
  18265. 11:06:40dot main loop and when we run this we
  18266. 11:06:43should have just a small
  18267. 11:06:45basic window let's set the title window
  18268. 11:06:49dot title let's set this to
  18269. 11:06:53tick tack toe
  18270. 11:06:57we'll need a list of players players
  18271. 11:07:00equals
  18272. 11:07:02x comma o
  18273. 11:07:05and with the way that we're writing this
  18274. 11:07:07program we can swap
  18275. 11:07:08these symbols with a different character
  18276. 11:07:10for example we could say
  18277. 11:07:12dollar sign is playing against the at
  18278. 11:07:15symbol too
  18279. 11:07:16but i'll demonstrate that later let's
  18280. 11:07:18keep it as x and o for now
  18281. 11:07:21now we need to select a random player to
  18282. 11:07:24begin
  18283. 11:07:25player equals random
  18284. 11:07:28dot choice and pass in our list of
  18285. 11:07:32players
  18286. 11:07:33now we'll need nine buttons i'm going to
  18287. 11:07:35create a 2d
  18288. 11:07:36list of buttons named buttons
  18289. 11:07:39and this will be the first row this will
  18290. 11:07:42be the second row
  18291. 11:07:44for the time being i'm just going to
  18292. 11:07:45initialize these with
  18293. 11:07:470 for everything and put these all
  18294. 11:07:50within
  18295. 11:07:51a list so we have a 2d list named
  18296. 11:07:54buttons
  18297. 11:07:56and to better visualize this i'm going
  18298. 11:07:58to place each row on a new line
  18299. 11:08:00so that's what our board is going to
  18300. 11:08:01look like we have a 2d list
  18301. 11:08:04named buttons we'll need a label to
  18302. 11:08:06display whose turn it is
  18303. 11:08:08label equals label
  18304. 11:08:11i'll set the text equal to
  18305. 11:08:15player plus the word
  18306. 11:08:18turn and i'll set the font to
  18307. 11:08:22font equals pick whatever font that you
  18308. 11:08:25want
  18309. 11:08:30and i am going to pack this label
  18310. 11:08:33label dot pack and set the side
  18311. 11:08:37equal to top and let's run this just to
  18312. 11:08:40test it
  18313. 11:08:42okay looks like it's x's turn let's try
  18314. 11:08:45it again
  18315. 11:08:46oh stern okay that's fine for now
  18316. 11:08:49let's create a reset button
  18317. 11:08:52reset underscore button
  18318. 11:08:55equals button
  18319. 11:08:59i'll set the text equal to
  18320. 11:09:02restart it's going to be a restart
  18321. 11:09:04button or reset button
  18322. 11:09:07pick a font
  18323. 11:09:14maybe i'll set the size to 20
  18324. 11:09:17and we'll need a command command
  18325. 11:09:21equals new game
  18326. 11:09:24when we click on this button it's going
  18327. 11:09:26to call this
  18328. 11:09:27new game function for us and we need to
  18329. 11:09:31pack this button reset
  18330. 11:09:34button dot pack
  18331. 11:09:37and i will set the side equal to top
  18332. 11:09:40okay
  18333. 11:09:41let's see if that appears alright so we
  18334. 11:09:43have our
  18335. 11:09:45turn order a label that will display
  18336. 11:09:47whose turn it is as well as our reset
  18337. 11:09:49button
  18338. 11:09:50now we'll need to create all of the
  18339. 11:09:51buttons to add to our 2d
  18340. 11:09:53list named buttons but i'm going to
  18341. 11:09:55place these all within
  18342. 11:09:57a frame frame equals frame
  18343. 11:10:00we're adding our frame to our window and
  18344. 11:10:03i'm going to
  18345. 11:10:04pack this frame frame dot pack
  18346. 11:10:08now we'll take our 2d list of buttons
  18347. 11:10:11and add a button to each spot
  18348. 11:10:13and i think the best way to do this
  18349. 11:10:14would be to use nested for loops
  18350. 11:10:17we'll have an outer for loop in charge
  18351. 11:10:19of the rows
  18352. 11:10:20four row in range three
  18353. 11:10:24remember we only have three rows and
  18354. 11:10:26three columns
  18355. 11:10:27the inner for loop will be in charge of
  18356. 11:10:29the columns
  18357. 11:10:30four column and range three
  18358. 11:10:34and inside the inner for loop we're
  18359. 11:10:36going to create a new button
  18360. 11:10:39we'll say buttons at index
  18361. 11:10:43row and column we have two indexes
  18362. 11:10:46because
  18363. 11:10:47we have a 2d list so buttons
  18364. 11:10:50at row whatever column whatever
  18365. 11:10:53depending on
  18366. 11:10:54what iteration we are within our for
  18367. 11:10:56loops
  18368. 11:10:57we'll create a new button
  18369. 11:11:00and we'll set the text oh first add this
  18370. 11:11:03to the frame
  18371. 11:11:04almost forgot about that we're adding
  18372. 11:11:06our buttons to our frame and adding the
  18373. 11:11:08frame to the window
  18374. 11:11:10now we'll set the text equal to a blank
  18375. 11:11:12set of quotes because we don't want any
  18376. 11:11:14text
  18377. 11:11:16we'll set the font i'll just copy what
  18378. 11:11:19we have here
  18379. 11:11:20for label
  18380. 11:11:26for a width let's say five and a height
  18381. 11:11:29let's say two i'm going to hit enter
  18382. 11:11:32just to move down to the next line
  18383. 11:11:33because we still have a few more things
  18384. 11:11:35to fill in
  18385. 11:11:36okay we'll need a command command
  18386. 11:11:39equals and i'm going to set this equal
  18387. 11:11:41to a lambda function
  18388. 11:11:42so we need some arguments actually i
  18389. 11:11:45think i'll pass in some keyword
  18390. 11:11:46arguments
  18391. 11:11:46row equals row and column
  18392. 11:11:50equals column then our expression is
  18393. 11:11:54next turn and we will pass in
  18394. 11:11:57row and column now we also need to add
  18395. 11:12:00our buttons to our
  18396. 11:12:02frame buttons at
  18397. 11:12:05index of row and column
  18398. 11:12:09we'll use the grid function for this
  18399. 11:12:12grid
  18400. 11:12:13row equals and
  18401. 11:12:16column equals column
  18402. 11:12:20and it's always a good idea to test your
  18403. 11:12:22program after making any major changes
  18404. 11:12:24so we should have our label our reset
  18405. 11:12:27button
  18406. 11:12:28and our grid of buttons now that the
  18407. 11:12:31main body of our program is complete
  18408. 11:12:33let's head to the next turn function
  18409. 11:12:36and remember that we're passing in row
  18410. 11:12:38and column as
  18411. 11:12:40arguments so we need to set up those
  18412. 11:12:42parameters within the next turn
  18413. 11:12:44function row and column and the first
  18414. 11:12:47line within here
  18415. 11:12:48we would like access to our player
  18416. 11:12:52so let's say global player so that we
  18417. 11:12:54have access to it
  18418. 11:12:56and we're first going to check to see if
  18419. 11:12:58the button
  18420. 11:12:59that we click on is empty so buttons
  18421. 11:13:03at index of row and index of column
  18422. 11:13:07to access the text of a button just add
  18423. 11:13:10a
  18424. 11:13:10third index operator and type in text
  18425. 11:13:14if the text of our button that we click
  18426. 11:13:17is equal to a set of quotes that means
  18427. 11:13:20it's empty
  18428. 11:13:21and when we call check winner
  18429. 11:13:25winner winner chicken dinner and it's
  18430. 11:13:28false
  18431. 11:13:29there is no winner then we will fill in
  18432. 11:13:32that button with our player our player's
  18433. 11:13:35character whatever it is
  18434. 11:13:37x or o or whatever we decide
  18435. 11:13:40so within here let's check to see if
  18436. 11:13:44player is equal to players
  18437. 11:13:47at index zero that is our first player
  18438. 11:13:51and remember that we created
  18439. 11:13:52a list of players and we're planning on
  18440. 11:13:55swapping these later
  18441. 11:13:56we could say like if player is equal to
  18442. 11:13:59x
  18443. 11:14:00but that would make our program less
  18444. 11:14:01flexible if we want to pick like a
  18445. 11:14:03different character instead of an x
  18446. 11:14:05so if player is equal to player at index
  18447. 11:14:08zero whatever symbol is there
  18448. 11:14:11then we are going to take buttons
  18449. 11:14:14at index of row index of column
  18450. 11:14:19take our text and set it equal to
  18451. 11:14:23our player
  18452. 11:14:27and we should check to see if there's a
  18453. 11:14:29winner after filling in this button
  18454. 11:14:32if check
  18455. 11:14:35winner and we still need to fill in this
  18456. 11:14:37function
  18457. 11:14:38returns false
  18458. 11:14:42then we will switch players player
  18459. 11:14:44equals
  18460. 11:14:45players at index of one and
  18461. 11:14:50label dot config
  18462. 11:14:55text equals
  18463. 11:14:59players at index 1
  18464. 11:15:02plus the word turn
  18465. 11:15:06so what we did here is that we're
  18466. 11:15:08checking to see
  18467. 11:15:09if after placing our text of our player
  18468. 11:15:13on that button that we click if there is
  18469. 11:15:16no winner
  18470. 11:15:17then we're going to swap players player
  18471. 11:15:20equals our next player
  18472. 11:15:21and we're configuring our label so that
  18473. 11:15:23it displays the next player's turn
  18474. 11:15:26players at index 1 turn
  18475. 11:15:29now what if there is a winner let's say
  18476. 11:15:32else if
  18477. 11:15:33check winner
  18478. 11:15:36is true
  18479. 11:15:40then we will take our label dot
  18480. 11:15:44config and set the text
  18481. 11:15:48equal to players
  18482. 11:15:51at index 0
  18483. 11:15:54plus the word wins
  18484. 11:15:59now let's add another else if statement
  18485. 11:16:02else if there's a tie let's say check
  18486. 11:16:06winner is equal to
  18487. 11:16:09the word tie because we are already
  18488. 11:16:12using
  18489. 11:16:12false and true then let's change our
  18490. 11:16:16label
  18491. 11:16:17and i'm just going to copy this
  18492. 11:16:20text equals the word
  18493. 11:16:23tie okay
  18494. 11:16:26so let's create an else block
  18495. 11:16:29so that goes right here
  18496. 11:16:32so if it's not player one's turn player
  18497. 11:16:35at index zero
  18498. 11:16:37then it's our other player's turn player
  18499. 11:16:39at index
  18500. 11:16:40of one so oh then so we just need to
  18501. 11:16:43mostly just copy this text and change a
  18502. 11:16:46few things around
  18503. 11:16:47so i'm going to copy all this
  18504. 11:16:52and within here let's set
  18505. 11:16:56player equals players at index
  18506. 11:16:59zero then players at
  18507. 11:17:02index zero's turn players
  18508. 11:17:06at index one wins
  18509. 11:17:09and that is it now our next turn
  18510. 11:17:11function is now complete let's head down
  18511. 11:17:13to the
  18512. 11:17:14check winner function and we need to
  18513. 11:17:16check all of the different win
  18514. 11:17:18conditions
  18515. 11:17:18and return it true if somebody won false
  18516. 11:17:22if there is yet no winner and the word
  18517. 11:17:24tie if it's a tie
  18518. 11:17:26so let's check all of the horizontal win
  18519. 11:17:28conditions
  18520. 11:17:29so we can do this using a for loop for
  18521. 11:17:32row in range three
  18522. 11:17:37we need to check the text of each button
  18523. 11:17:39in each row
  18524. 11:17:40if buttons at index of row
  18525. 11:17:44and column index of 0 we'll use the
  18526. 11:17:48index operator to check the text
  18527. 11:17:50is equal to the next button in our row
  18528. 11:17:54so i will copy this and change zero to
  18529. 11:17:57one
  18530. 11:17:58check to see if that is equal to buttons
  18531. 11:18:02at index of row index of two
  18532. 11:18:06and i'm going to check to see if this is
  18533. 11:18:08all not
  18534. 11:18:10equal let me move this a little bit
  18535. 11:18:13to a space
  18536. 11:18:17so if that is the case that means
  18537. 11:18:19somebody won
  18538. 11:18:20so if all of these buttons are the same
  18539. 11:18:23and they are not equal to
  18540. 11:18:25an empty space that means they're all
  18541. 11:18:27the same so let's return
  18542. 11:18:29true that means that somebody won
  18543. 11:18:32no we need to check the vertical win
  18544. 11:18:34conditions
  18545. 11:18:35if anybody has the same character all
  18546. 11:18:38the way down
  18547. 11:18:39a single column this next for loop is
  18548. 11:18:41going to be four column in range three
  18549. 11:18:44for the first button this will be button
  18550. 11:18:47at index of zero
  18551. 11:18:49and column then
  18552. 11:18:52one and column let me just paste that
  18553. 11:18:56and two and column
  18554. 11:19:01now we need to check the diagonal win
  18555. 11:19:03conditions
  18556. 11:19:04so if buttons at
  18557. 11:19:07index 0 0 that's the top left corner
  18558. 11:19:11if the text of that button is equal to
  18559. 11:19:14buttons at indexes of one one
  18560. 11:19:19is equal to the indexes of
  18561. 11:19:23button two two and if all of this
  18562. 11:19:27does not equal an empty space
  18563. 11:19:30then return true so there is a winner
  18564. 11:19:35and we have one last win condition to
  18565. 11:19:37check that is the other
  18566. 11:19:39diagonal win condition so we just need
  18567. 11:19:41to change some of these indexes around
  18568. 11:19:44so we have zero two
  18569. 11:19:47one one and two zero
  18570. 11:19:50i'm going to change this statement to an
  18571. 11:19:52else if statement
  18572. 11:19:54no we need to check to see if there's
  18573. 11:19:56any spaces remaining
  18574. 11:19:58else if we will call the empty spaces
  18575. 11:20:01function which we have yet to fill in if
  18576. 11:20:05this returns false then we will
  18577. 11:20:09return the word
  18578. 11:20:12tie
  18579. 11:20:15and lastly else else there is no winner
  18580. 11:20:19and
  18581. 11:20:20no tie so we will return false
  18582. 11:20:24and that is it for the check winner
  18583. 11:20:27function
  18584. 11:20:28let's test this
  18585. 11:20:31so this doesn't account for a tie quite
  18586. 11:20:33yet but we can fill in
  18587. 11:20:35some of these spaces looks like x wins
  18588. 11:20:37and we can no longer fill in buttons
  18589. 11:20:40and we cannot start a new game quite yet
  18590. 11:20:42either
  18591. 11:20:43within the empty spaces function let's
  18592. 11:20:45create a local
  18593. 11:20:47variable named spaces and set this equal
  18594. 11:20:49to nine whenever we call this function
  18595. 11:20:53four row in range
  18596. 11:20:56three and we'll create a nested for loop
  18597. 11:20:59for column in range
  18598. 11:21:04three we will check the text of each
  18599. 11:21:07button
  18600. 11:21:08if buttons at indexes of
  18601. 11:21:11row and column
  18602. 11:21:15and we will check the text to see
  18603. 11:21:21if it's not equal to an empty space
  18604. 11:21:26if that is the case spaces
  18605. 11:21:29minus equals one
  18606. 11:21:33then we'll write an if statement if
  18607. 11:21:36spaces
  18608. 11:21:36as in spaces remaining is equal to zero
  18609. 11:21:41that means we will return false and
  18610. 11:21:44there are no
  18611. 11:21:45spaces left else
  18612. 11:21:49we will return
  18613. 11:21:52true okay let's test it
  18614. 11:21:56so this time i'm trying to get a tie
  18615. 11:22:06yep looks like it's a tie this next
  18616. 11:22:10part's optional but i would like to
  18617. 11:22:11change
  18618. 11:22:12the color of each button for the winning
  18619. 11:22:14combination
  18620. 11:22:15so within our check winner function
  18621. 11:22:17underneath the first win condition
  18622. 11:22:20i'm going to take buttons at index
  18623. 11:22:23of row and index of zero
  18624. 11:22:29use the config method
  18625. 11:22:33and set the background color equal to
  18626. 11:22:36let's say green
  18627. 11:22:39and i'm going to repeat this process for
  18628. 11:22:41the other buttons within this win
  18629. 11:22:43condition
  18630. 11:22:44so that is row
  18631. 11:22:47zero row one and row two
  18632. 11:22:50i'll copy all of this paste it
  18633. 11:22:55and switch some of these around so these
  18634. 11:22:57should all be matching
  18635. 11:23:00so zero and column one and column and
  18636. 11:23:03two in column
  18637. 11:23:08okay so we have zero zero one
  18638. 11:23:11one and two two
  18639. 11:23:14and one more
  18640. 11:23:17zero two one one and
  18641. 11:23:20two zero now when we have a winning
  18642. 11:23:24combination
  18643. 11:23:26the color of the buttons involved in
  18644. 11:23:28that combination are going to change to
  18645. 11:23:30green
  18646. 11:23:30now if there is a tie let's change all
  18647. 11:23:33of the buttons to
  18648. 11:23:34let's say yellow so i'm going to write
  18649. 11:23:37nested for loops for this
  18650. 11:23:39four row in range
  18651. 11:23:43three for column
  18652. 11:23:47in range three
  18653. 11:23:51take our buttons at row
  18654. 11:23:54and column and change
  18655. 11:23:58the background color to yellow or some
  18656. 11:24:00other color of your choosing
  18657. 11:24:03so if there's a tie they should all be
  18658. 11:24:06yellow
  18659. 11:24:08i'm trying not to win here it's actually
  18660. 11:24:10more complex than what i thought
  18661. 11:24:16yep it looks like it's all a tie and the
  18662. 11:24:18last thing that we need to do is to fill
  18663. 11:24:20in
  18664. 11:24:20the new game function so that we can
  18665. 11:24:22begin a new game
  18666. 11:24:24so let's say global player we would like
  18667. 11:24:26access to our player
  18668. 11:24:27from inside this function and set player
  18669. 11:24:30equal
  18670. 11:24:31to a new random choice random
  18671. 11:24:35dot choice and pass in our list
  18672. 11:24:38of players and we will change our label
  18673. 11:24:41label dot config
  18674. 11:24:44and set the text equal to player
  18675. 11:24:48plus the word turn and we also need to
  18676. 11:24:52reset all of our buttons
  18677. 11:24:56for row in range
  18678. 11:25:00three for
  18679. 11:25:03column in range
  18680. 11:25:07three buttons
  18681. 11:25:10at index of row and index of column
  18682. 11:25:16dot config
  18683. 11:25:19text equals a set of quotes so it's
  18684. 11:25:22empty
  18685. 11:25:23and we'll change the background color
  18686. 11:25:26factors previously
  18687. 11:25:27so the default color for buttons is
  18688. 11:25:30actually hexadecimal
  18689. 11:25:32f 0 f 0 f 0
  18690. 11:25:35so this function will begin a new game
  18691. 11:25:37for us
  18692. 11:25:38and let's test it to be sure i'm going
  18693. 11:25:41to start a new game
  18694. 11:25:42reset reset
  18695. 11:25:46i'll actually try and win this time and
  18696. 11:25:49reset
  18697. 11:25:50okay so it looks like the new game
  18698. 11:25:52function is working
  18699. 11:25:54now another thing that you can do too is
  18700. 11:25:56that you can change
  18701. 11:25:57the players around let's say we would
  18702. 11:25:59like to play as
  18703. 11:26:01dollar sign and at sign so this program
  18704. 11:26:04is flexible enough
  18705. 11:26:06so that you can change the icons of the
  18706. 11:26:08players
  18707. 11:26:10all right everybody so that's a basic
  18708. 11:26:12game of tic-tac-toe for
  18709. 11:26:14python if you would like a copy of this
  18710. 11:26:16code i will post all of this to the
  18711. 11:26:17comments section down below
  18712. 11:26:19but yeah that's how to code a basic game
  18713. 11:26:21of tic-tac-toe
  18714. 11:26:23for python hey what's going on everybody
  18715. 11:26:26it's your bro hope you're doing well and
  18716. 11:26:28in this video we're going to create a
  18717. 11:26:30game of snake using python so
  18718. 11:26:32sit back relax and enjoy the show
  18719. 11:26:36first thing we should do is import from
  18720. 11:26:39tkinter as well as the random module
  18721. 11:26:41and let's define all of the different
  18722. 11:26:43classes and functions that we'll need
  18723. 11:26:45so let's create a class for our snake
  18724. 11:26:48object
  18725. 11:26:49as well as our food object so for the
  18726. 11:26:52time being i'm just going to write pass
  18727. 11:26:54for my classes we'll fill these in later
  18728. 11:26:56so we have class snake and
  18729. 11:26:58class food and
  18730. 11:27:01let's define all of the different
  18731. 11:27:02functions that we'll need so let's say
  18732. 11:27:04we have a function named next
  18733. 11:27:06turn
  18734. 11:27:10change direction
  18735. 11:27:18check collisions
  18736. 11:27:22and lastly game over
  18737. 11:27:29okay for change direction i'm going to
  18738. 11:27:32have one parameter
  18739. 11:27:34a new direction
  18740. 11:27:37now when i create a game i like to place
  18741. 11:27:39a bunch of constants at the top
  18742. 11:27:41of my game constants are variables that
  18743. 11:27:44you do not want to change
  18744. 11:27:45later they're kind of like the settings
  18745. 11:27:47however in python
  18746. 11:27:49there are no constants compared to other
  18747. 11:27:51programming languages
  18748. 11:27:52so we're just going to create a bunch of
  18749. 11:27:54variables that will behave like them
  18750. 11:27:56so constants are values we do not want
  18751. 11:27:58to change and they're kind of like the
  18752. 11:27:59game settings
  18753. 11:28:00and i placed them at the top for
  18754. 11:28:02convenience and the naming convention
  18755. 11:28:04for a constant is that
  18756. 11:28:05all the letters are uppercase so these
  18757. 11:28:08will be settings like
  18758. 11:28:09the game width the speed etc
  18759. 11:28:12so let's say the width of our game will
  18760. 11:28:14be 700
  18761. 11:28:15but feel free to take the liberty to
  18762. 11:28:17pick whatever size you want
  18763. 11:28:19so we have game width game height
  18764. 11:28:22i'll set this to 700 as well so it's a
  18765. 11:28:24square
  18766. 11:28:26let's say we have a speed the speed of
  18767. 11:28:28the snake how often will our canvas
  18768. 11:28:30update
  18769. 11:28:31so let's say 50 but the lower the number
  18770. 11:28:33the faster the game
  18771. 11:28:35and a space size how large are the items
  18772. 11:28:38in our game like
  18773. 11:28:39the food and body parts of the snake
  18774. 11:28:42so i'm going to pick 50 but you can
  18775. 11:28:44change this
  18776. 11:28:46and body parts how many body parts does
  18777. 11:28:50our snake have when we begin a game
  18778. 11:28:52let's say three how about a snake
  18779. 11:28:55color you can pick a color name
  18780. 11:28:58you can use rgb values or you can use a
  18781. 11:29:01hexadecimal value
  18782. 11:29:02so i'm going to pick green 0 0 ff00 but
  18783. 11:29:06you can pick any color you want
  18784. 11:29:09how about food color
  18785. 11:29:13equals red
  18786. 11:29:16and that is ff000
  18787. 11:29:20and what about a background color for
  18788. 11:29:22the canvas background
  18789. 11:29:25color i'll pick black so that is
  18790. 11:29:28six zeros
  18791. 11:29:31feel free to mess with some of these
  18792. 11:29:32colors too so we have a green snake
  18793. 11:29:35our food is going to be red and our
  18794. 11:29:37background is going to be black
  18795. 11:29:39so that is all of the constants for our
  18796. 11:29:41game but you can feel free to adjust
  18797. 11:29:43them if you like
  18798. 11:29:44let's head down to the bottom and make
  18799. 11:29:46our window so we have
  18800. 11:29:48window equals tk and at the very end we
  18801. 11:29:51should have
  18802. 11:29:52window dot main loop
  18803. 11:29:57let's set a title for this window window
  18804. 11:30:01dot title
  18805. 11:30:04snack game okay fine i'll spell it right
  18806. 11:30:08snake game and if you do not want your
  18807. 11:30:12window to be resizable
  18808. 11:30:14you can use resizable
  18809. 11:30:18and then you have to pass in false twice
  18810. 11:30:20it's kind of strange but it's how it is
  18811. 11:30:23okay we should have a small window and
  18812. 11:30:26we cannot resize this even if we tried
  18813. 11:30:30okay let's create a score label
  18814. 11:30:33but we probably need a score first so
  18815. 11:30:35let's say score equals zero
  18816. 11:30:37and an initial direction direction
  18817. 11:30:41equals let's say down
  18818. 11:30:45now let's create a score label so let's
  18819. 11:30:47say label
  18820. 11:30:49equals label we're adding our
  18821. 11:30:52label to our window let's set the text
  18822. 11:30:56equal to score
  18823. 11:30:59colon then i'm going to use the format
  18824. 11:31:02method
  18825. 11:31:04and we will pass in our score whatever
  18826. 11:31:08it is
  18827. 11:31:10and i'll set a font pick whatever font
  18828. 11:31:12you prefer
  18829. 11:31:17and 40 is a decent size
  18830. 11:31:21and then i'm going to pack this label
  18831. 11:31:25let's test it okay we're getting
  18832. 11:31:28somewhere
  18833. 11:31:29we'll need to create a canvas canvas
  18834. 11:31:33equals canvas we're adding it to our
  18835. 11:31:36window i'm going to set the background
  18836. 11:31:39color
  18837. 11:31:40equal to our background constant
  18838. 11:31:45i'm going to set our height equal to the
  18839. 11:31:47game
  18840. 11:31:48height and the width equal
  18841. 11:31:52to the game width that we set
  18842. 11:31:56and we need to pack this canvas dot pack
  18843. 11:32:02and we should have a game board
  18844. 11:32:06i'm going to try and center this window
  18845. 11:32:08when it appears
  18846. 11:32:09so we can do that using a few lines of
  18847. 11:32:11code the first thing we'll do is
  18848. 11:32:13update our window so that it renders and
  18849. 11:32:16then we need to find some dimensions
  18850. 11:32:18so let's say the window width
  18851. 11:32:22equals window dot w
  18852. 11:32:26info width
  18853. 11:32:31and window height
  18854. 11:32:35equals window
  18855. 11:32:38dot w info height
  18856. 11:32:43we'll need our screen width
  18857. 11:32:48equals window
  18858. 11:32:53dot screen width
  18859. 11:32:56and screen height
  18860. 11:33:00equals window dot w
  18861. 11:33:04info screen height
  18862. 11:33:07where is it there it is
  18863. 11:33:13okay then we need to see how much we're
  18864. 11:33:15going to adjust the position of our
  18865. 11:33:17window
  18866. 11:33:19so let's say x equals
  18867. 11:33:24screen width divided by 2
  18868. 11:33:28minus window
  18869. 11:33:32width divided by two
  18870. 11:33:36and do the same thing for y
  18871. 11:33:40except this will be screen height
  18872. 11:33:45and window height
  18873. 11:33:48and that will be why
  18874. 11:33:52okay then we need to set the geometry
  18875. 11:33:55window
  18876. 11:33:56dot geometry
  18877. 11:34:00we're going to use an f string
  18878. 11:34:04so let's set the window
  18879. 11:34:07width times
  18880. 11:34:12window height
  18881. 11:34:17and then add plus
  18882. 11:34:21x plus y
  18883. 11:34:24so when we pass in x and y
  18884. 11:34:27these cannot be floats they have to be
  18885. 11:34:30whole integers
  18886. 11:34:31so let's add a cast around x and y
  18887. 11:34:38okay now this should be fairly close to
  18888. 11:34:40the center
  18889. 11:34:42yeah that's not too bad now when we
  18890. 11:34:44begin a new game we should create a
  18891. 11:34:45snake object as well as a food object
  18892. 11:34:48snake equals snake and called the snake
  18893. 11:34:51constructor
  18894. 11:34:52and food equals food
  18895. 11:34:56okay let's fill in our food class first
  18896. 11:34:58because i think that'll be easier than
  18897. 11:35:00our snake class
  18898. 11:35:01so let's head to the food class
  18899. 11:35:04now let's create an init method so def
  18900. 11:35:08init this will construct a food object
  18901. 11:35:11for us
  18902. 11:35:12and we need to place our food object
  18903. 11:35:14randomly
  18904. 11:35:16so for the x coordinate let's say x
  18905. 11:35:18equals
  18906. 11:35:19random dot rand int and we need a range
  18907. 11:35:23the range is going to be zero comma now
  18908. 11:35:27with our game board i view it like a
  18909. 11:35:29chess board
  18910. 11:35:30there's a given amount of spaces so
  18911. 11:35:33700 divided by our space size
  18912. 11:35:37is 14 possible spots on the x-axis
  18913. 11:35:41and then 14 possible spots on the y-axis
  18914. 11:35:45so i need to pick one of these spots
  18915. 11:35:47randomly
  18916. 11:35:49so let's say game
  18917. 11:35:53width divided by our space
  18918. 11:35:56size so we'll get a random number
  18919. 11:35:58between 0 and 14
  18920. 11:36:00however this should be exclusive so
  18921. 11:36:02let's say -1
  18922. 11:36:05and for good measure i'm just going to
  18923. 11:36:07add a set of parentheses around this
  18924. 11:36:09okay and then we'll convert this to
  18925. 11:36:11pixels
  18926. 11:36:12so let's multiply all of this by our
  18927. 11:36:14space
  18928. 11:36:16size the size of each item in the game
  18929. 11:36:19and we'll do the same thing for
  18930. 11:36:20y but change game width to height
  18931. 11:36:30okay we should be good
  18932. 11:36:35all right now let's set the coordinates
  18933. 11:36:37let's say self dot coordinates
  18934. 11:36:41equals a list of x and y
  18935. 11:36:45and that's it but make sure you spell
  18936. 11:36:47coordinates right
  18937. 11:36:50now we need to draw our food object on
  18938. 11:36:52our canvas
  18939. 11:36:54canvas dot create
  18940. 11:36:58oval you can pick a square two if you
  18941. 11:37:00like
  18942. 11:37:01we need a starting corner that will be
  18943. 11:37:03where x
  18944. 11:37:05and y is and an ending coordinator
  18945. 11:37:08so that will be x and y plus
  18946. 11:37:11our space size the size of an object in
  18947. 11:37:14that game
  18948. 11:37:15so x plus space
  18949. 11:37:19size y plus
  18950. 11:37:22space size
  18951. 11:37:26and you can set a fill color too
  18952. 11:37:29fill will be our food color that we
  18953. 11:37:32declared
  18954. 11:37:34and i recommend adding a tag as well
  18955. 11:37:37that'll make it easy to delete this
  18956. 11:37:39object
  18957. 11:37:41so tag will equal the string food
  18958. 11:37:44now let's just test this so we should
  18959. 11:37:47have a random circle
  18960. 11:37:48or a square if you picked a square
  18961. 11:37:50appear someplace at some spot
  18962. 11:37:52on my game board cool seems like it's
  18963. 11:37:55working
  18964. 11:37:56let's work on the snake class next our
  18965. 11:37:59snake class we'll need a constructor
  18966. 11:38:06and we will set a body size
  18967. 11:38:10equal to our body parts
  18968. 11:38:16we'll need a list of coordinates
  18969. 11:38:24fill this in later and a list
  18970. 11:38:27of square graphics
  18971. 11:38:31so that will be a list okay
  18972. 11:38:34so we need to create a list of
  18973. 11:38:37coordinates
  18974. 11:38:38we can use a for loop for that so let's
  18975. 11:38:40say for
  18976. 11:38:41i in range zero
  18977. 11:38:45through body parts
  18978. 11:38:50we will take self dot coordinates
  18979. 11:38:54and append a new list
  18980. 11:38:59and the coordinates for each body part
  18981. 11:39:01at the start of the game will be
  18982. 11:39:030 0 so that our snake will appear in the
  18983. 11:39:06top left corner
  18984. 11:39:09now we'll need to create some squares
  18985. 11:39:11okay so
  18986. 11:39:12for x y
  18987. 11:39:15in self dot coordinates
  18988. 11:39:18and remember that we have a list of
  18989. 11:39:21lists so that's why we're using
  18990. 11:39:22x y in self coordinates
  18991. 11:39:27we'll create a square equals
  18992. 11:39:31canvas dot create
  18993. 11:39:35rectangle
  18994. 11:39:38so we need a starting corner that will
  18995. 11:39:40be x and y
  18996. 11:39:45then x plus our
  18997. 11:39:48space size the size of each object in
  18998. 11:39:51the game
  18999. 11:39:53let me use the constant though space
  19000. 11:39:57size y plus
  19001. 11:40:00space size
  19002. 11:40:06and let's set a fill color equal to
  19003. 11:40:09our snake color
  19004. 11:40:14all right and let's set a tag
  19005. 11:40:18for convenience tag equals
  19006. 11:40:22a string of snake okay so we have a
  19007. 11:40:25list of squares and we can append each
  19008. 11:40:28square into our list
  19009. 11:40:31self dot squares
  19010. 11:40:36dot append and pass in whatever square
  19011. 11:40:39that you create
  19012. 11:40:42so we have a snake that has a body size
  19013. 11:40:46a list of coordinates and a list of
  19014. 11:40:49square graphics
  19015. 11:40:50now after testing this we should have
  19016. 11:40:52the head of our snake in the top left
  19017. 11:40:54corner
  19018. 11:40:55and now we just need the snake to move
  19019. 11:40:57in a given direction every turn
  19020. 11:40:59so the initial direction is down but
  19021. 11:41:02we'll be able to change that later
  19022. 11:41:03so let's head to the next turn function
  19023. 11:41:06and there's actually two things we'll
  19024. 11:41:08need as parameters that i forgot to fill
  19025. 11:41:10in
  19026. 11:41:11snake as well as food and we will call
  19027. 11:41:14this function when we begin our game
  19028. 11:41:17so let's unpack the head of the snake so
  19029. 11:41:19that's
  19030. 11:41:20x comma y equals
  19031. 11:41:23snake dot coordinates
  19032. 11:41:26at index of zero so that's the head of
  19033. 11:41:30the snake
  19034. 11:41:31the coordinates will be stored in x and
  19035. 11:41:34y
  19036. 11:41:35so let's check to see if our direction
  19037. 11:41:39our initial direction equals
  19038. 11:41:42up and we'll need some else if
  19039. 11:41:46statements
  19040. 11:41:46we'll fill this in just a moment
  19041. 11:41:52else if our direction equals
  19042. 11:41:55down
  19043. 11:42:00then left
  19044. 11:42:06and then right
  19045. 11:42:11okay if our direction is up
  19046. 11:42:14then let's take our y coordinate for the
  19047. 11:42:17head of our snake
  19048. 11:42:18minus equals our space
  19049. 11:42:22size so that we move one space up
  19050. 11:42:26and then down is plus equals
  19051. 11:42:29space size left
  19052. 11:42:33is x minus equals
  19053. 11:42:36space size and lastly
  19054. 11:42:39right is x plus equals space size
  19055. 11:42:45okay then we need to call the next turn
  19056. 11:42:47function again
  19057. 11:42:48for the next turn so we can use window
  19058. 11:42:52dot after we need the time
  19059. 11:42:56so let's say our game speed
  19060. 11:42:59we're going to call the next turn
  19061. 11:43:01function and we need to pass in our
  19062. 11:43:03arguments of snake and food
  19063. 11:43:08snake food
  19064. 11:43:12oh and make sure you're not actually
  19065. 11:43:13calling the next turn function inside of
  19066. 11:43:15the after method you just have to write
  19067. 11:43:17the function name of next turn without
  19068. 11:43:19the parentheses
  19069. 11:43:21okay so let's update the coordinates for
  19070. 11:43:23the height for the snake
  19071. 11:43:24and write that before we move on to the
  19072. 11:43:26next turn
  19073. 11:43:28so snake dot coordinates
  19074. 11:43:34and we will insert a new set of
  19075. 11:43:36coordinates
  19076. 11:43:37after updating one of them
  19077. 11:43:41so zero will be the index the head of
  19078. 11:43:44the snake
  19079. 11:43:45and we will insert x and y coordinates
  19080. 11:43:48at
  19081. 11:43:48this new location now we're going to
  19082. 11:43:52create a new graphic for the head of the
  19083. 11:43:54snake
  19084. 11:43:55square equals canvas dot create
  19085. 11:43:59rectangle pass in x
  19086. 11:44:02and y for the starting corner of our
  19087. 11:44:05rectangle and
  19088. 11:44:06the ending corner will be x plus
  19089. 11:44:09our space size y
  19090. 11:44:14plus our space size
  19091. 11:44:17then i will add a fill color of
  19092. 11:44:22snake color and then we need to update
  19093. 11:44:26our snakes list of squares
  19094. 11:44:30so that will be snake dot squares
  19095. 11:44:35and insert at
  19096. 11:44:39index 0 a new square that we create
  19097. 11:44:44okay let's test this one last thing that
  19098. 11:44:46we'll need to get this program to run
  19099. 11:44:48is that after you create your snake and
  19100. 11:44:50food object we should call it the
  19101. 11:44:52next turn function and pass in our snake
  19102. 11:44:55and food object so we should be able to
  19103. 11:44:57test this
  19104. 11:44:59so our snake is going to move but we
  19105. 11:45:01need to delete the
  19106. 11:45:02last body part in our snake within the
  19107. 11:45:05next turn function
  19108. 11:45:07but before we update to the next turn
  19109. 11:45:10let's delete the last body part of our
  19110. 11:45:12snake so delete
  19111. 11:45:14snake dot coordinates
  19112. 11:45:18at negative index of one that is the
  19113. 11:45:21last set of coordinates
  19114. 11:45:24we will update our canvas canvas dot
  19115. 11:45:27delete
  19116. 11:45:31snake dot squares
  19117. 11:45:36an index of negative one
  19118. 11:45:40and lastly delete snake
  19119. 11:45:46the list of squares
  19120. 11:45:49at index of negative one
  19121. 11:45:52so it should appear that our snake is
  19122. 11:45:54moving cool
  19123. 11:45:56we need some controls for our snake so
  19124. 11:45:59at the bottom of our program
  19125. 11:46:01let's bind some keys let's do that here
  19126. 11:46:06so window dot bind
  19127. 11:46:10let's find the left arrow key
  19128. 11:46:16and we will use a lambda
  19129. 11:46:20the argument is event and we will call
  19130. 11:46:23the
  19131. 11:46:23change direction function and pass in
  19132. 11:46:27the word left
  19133. 11:46:31okay then we have to do the same thing
  19134. 11:46:33for the other directions
  19135. 11:46:40so we have right
  19136. 11:46:44pass in right
  19137. 11:46:48up pass in up
  19138. 11:46:53down pass in down
  19139. 11:46:59let's head to the change direction
  19140. 11:47:01function
  19141. 11:47:03we need to access our direction global
  19142. 11:47:07direction this is the old direction
  19143. 11:47:12if our new direction that is passed in
  19144. 11:47:16is equal to left
  19145. 11:47:22and if our old direction this direction
  19146. 11:47:26does not equal right because we do not
  19147. 11:47:28want to go backwards and do a 180 degree
  19148. 11:47:31turn
  19149. 11:47:34then we will set our direction
  19150. 11:47:37equal to our new
  19151. 11:47:40direction and we just need to repeat
  19152. 11:47:42this for the other
  19153. 11:47:44directions for new direction
  19154. 11:47:48and i'm going to change this to else if
  19155. 11:47:52else if new direction is right
  19156. 11:47:56and our direction is not already
  19157. 11:47:59left
  19158. 11:48:05then we have up
  19159. 11:48:09if our direction is not down
  19160. 11:48:16and down if our
  19161. 11:48:20direction is not up okay so we should be
  19162. 11:48:23able to change
  19163. 11:48:24the direction of our snake
  19164. 11:48:28sweet okay we need to eat that pesky
  19165. 11:48:31apple next so let's work on that
  19166. 11:48:34there's nothing else that we need to
  19167. 11:48:35change within the change direction
  19168. 11:48:37function so i'm going to minimize
  19169. 11:48:39this function and head to the
  19170. 11:48:42next turn function
  19171. 11:48:47we'll place an if statement here if
  19172. 11:48:50x remember that we unpacked the
  19173. 11:48:53coordinates
  19174. 11:48:54for the head of the snake if x
  19175. 11:48:57the x coordinate for the head of the
  19176. 11:48:59snake is equal to
  19177. 11:49:02our food objects coordinates
  19178. 11:49:08at index of zero that's the x-coordinate
  19179. 11:49:12for our food object and
  19180. 11:49:16y is equal to
  19181. 11:49:19food coordinates at index of one
  19182. 11:49:23that means they're overlapping
  19183. 11:49:27let's take our score
  19184. 11:49:31and increment it by one
  19185. 11:49:36and change our label label.config
  19186. 11:49:44the text will equal
  19187. 11:49:48score
  19188. 11:49:52and then i'm going to use the format
  19189. 11:49:54method
  19190. 11:49:55and pass in my new score
  19191. 11:50:01let's delete our food object
  19192. 11:50:04and we gave our food object a tag so we
  19193. 11:50:07can just use the name of the tag
  19194. 11:50:09to delete it and create a new food
  19195. 11:50:11object
  19196. 11:50:16then i'm going to write this part of our
  19197. 11:50:18program within an else statement
  19198. 11:50:23we will only delete the last body part
  19199. 11:50:26of our snake
  19200. 11:50:28if we did not eat a food object
  19201. 11:50:32okay let's test it again
  19202. 11:50:36so i have three body parts now i have
  19203. 11:50:38four
  19204. 11:50:39five
  19205. 11:50:43six seven eight nine
  19206. 11:50:47ten you can see that the score is going
  19207. 11:50:49up too
  19208. 11:50:51okay let's work on collisions next
  19209. 11:50:53because i should not be able to go off
  19210. 11:50:55screen like this
  19211. 11:50:56there's one thing that we're going to
  19212. 11:50:58change so let's add an
  19213. 11:51:00if statement that will check collisions
  19214. 11:51:04this will return true or false if we
  19215. 11:51:07detect a collision
  19216. 11:51:08and pass in our snake object
  19217. 11:51:13if there is a collision we'll call the
  19218. 11:51:16game over function
  19219. 11:51:20else we will update to the next turn
  19220. 11:51:25okay let's fill in the check collisions
  19221. 11:51:28function
  19222. 11:51:29and we no longer need our function of
  19223. 11:51:31next turn
  19224. 11:51:32actually we don't need these classes
  19225. 11:51:34either so i'll minimize them
  19226. 11:51:36okay check collisions so it looks like
  19227. 11:51:39we will need a parameter of snake
  19228. 11:51:41so be sure to set that
  19229. 11:51:44let's unpack the head of the snake x
  19230. 11:51:47y equals snake dot coordinates
  19231. 11:51:52at index of zero
  19232. 11:51:57and let's check to see if we cross the
  19233. 11:51:59left or
  19234. 11:52:00right border of the game if x
  19235. 11:52:03is less than zero or
  19236. 11:52:07x is greater than or equal to
  19237. 11:52:11our game width
  19238. 11:52:15then return true i think for testing
  19239. 11:52:19purposes i'm going to print something to
  19240. 11:52:21the console window
  19241. 11:52:24game over let's test it
  19242. 11:52:29okay i'm going to go over to the right
  19243. 11:52:30border game over and our game stopped
  19244. 11:52:34let's go over the left game over
  19245. 11:52:37cool so we know that it's working okay
  19246. 11:52:39let's do the same thing for y
  19247. 11:52:43we can use else if here
  19248. 11:52:47if y is less than zero or
  19249. 11:52:50y is greater than or equal to our game
  19250. 11:52:54height let's print game over
  19251. 11:52:59and return true and we should probably
  19252. 11:53:01test it
  19253. 11:53:03okay i'm going up game over and let's go
  19254. 11:53:07down
  19255. 11:53:08game over
  19256. 11:53:12so what if our snake touches its tail or
  19257. 11:53:15another body part
  19258. 11:53:17so let's say four i actually i
  19259. 11:53:20isn't too descriptive let's say for
  19260. 11:53:22every body part
  19261. 11:53:25in snake dot coordinates
  19262. 11:53:34we're going to set this to everything
  19263. 11:53:37after the head of the snake
  19264. 11:53:40we're going to check to see if any of
  19265. 11:53:41the coordinates are matching
  19266. 11:53:45if x is equal to
  19267. 11:53:48body part at index
  19268. 11:53:520 and
  19269. 11:53:55y is equal to body
  19270. 11:53:59part at index 1
  19271. 11:54:03then return true
  19272. 11:54:07and i'll print game over to test it
  19273. 11:54:10print game over
  19274. 11:54:15otherwise we can return false there are
  19275. 11:54:18no collisions
  19276. 11:54:20i think i'm going to change the size of
  19277. 11:54:23the snake
  19278. 11:54:24to 10. okay let's run into each other
  19279. 11:54:29cool game over and the last thing that
  19280. 11:54:32we need to do
  19281. 11:54:33is to fill in the game over function
  19282. 11:54:36because it looks like everything else is
  19283. 11:54:37fine
  19284. 11:54:38so head to the game over function take
  19285. 11:54:42your canvas and delete all
  19286. 11:54:49and we'll create some game over text
  19287. 11:54:52canvas
  19288. 11:54:53create text
  19289. 11:54:57i would like this in the center of my
  19290. 11:54:59canvas
  19291. 11:55:00i'll take canvas w info
  19292. 11:55:04width divided by two comma
  19293. 11:55:08canvas w info height
  19294. 11:55:12divided by two i'm going to put some of
  19295. 11:55:14this on the next line
  19296. 11:55:16for readability i'll set a font
  19297. 11:55:19pick whatever font that you want
  19298. 11:55:26we'll need some text
  19299. 11:55:30text equals game over
  19300. 11:55:36pick a color red's decent
  19301. 11:55:40i'll add a tag too for convenience game
  19302. 11:55:44over and that should be it for the game
  19303. 11:55:47over function
  19304. 11:55:49i'm going to change the body parts of
  19305. 11:55:51the snake back to what it was originally
  19306. 11:55:53and we should probably test that game
  19307. 11:55:55over screen
  19308. 11:55:58all right it appeared okay so let's
  19309. 11:56:01change some of these settings around
  19310. 11:56:03you can create a larger game board let's
  19311. 11:56:05say
  19312. 11:56:061000 by 700
  19313. 11:56:11that still works you can slow down the
  19314. 11:56:14speed or speed it up
  19315. 11:56:16so 100 will be about half as fast
  19316. 11:56:19it's going pretty slow now but what if
  19317. 11:56:22we set it to
  19318. 11:56:2320.
  19319. 11:56:28you can change the space size
  19320. 11:56:32so everything is a lot smaller now
  19321. 11:56:35including the food
  19322. 11:56:36object
  19323. 11:56:40you can change the body parts what about
  19324. 11:56:4220 to begin with
  19325. 11:56:46that's fairly excessive
  19326. 11:56:50i'll change that back to three you can
  19327. 11:56:53change the snake color
  19328. 11:56:54let's say we would like a blue snake
  19329. 11:56:57so that is for a hex color
  19330. 11:57:01four zeros and then two f's
  19331. 11:57:05you can change the food color so let's
  19332. 11:57:07say we would like a yellow food
  19333. 11:57:09object so that would be four f's and
  19334. 11:57:11then two zeros
  19335. 11:57:14kind of resembles the python logo that
  19336. 11:57:16color scheme
  19337. 11:57:20you can change the background color too
  19338. 11:57:23so let's say
  19339. 11:57:23all white that would be six apps
  19340. 11:57:28i do not like that go back delete delete
  19341. 11:57:32delete alright well
  19342. 11:57:33that should be everything let's run this
  19343. 11:57:35game one last time
  19344. 11:57:46[Music]
  19345. 11:58:14[Music]
  19346. 11:58:44well everybody that is a very basic game
  19347. 11:58:46of snake for beginners i will post all
  19348. 11:58:48of this code to the comment section down
  19349. 11:58:50below but yeah that's a basic game of
  19350. 11:58:53snake
  19351. 11:58:53using python hey you
  19352. 11:58:57yeah i'm talking to you if you learned
  19353. 11:58:58something new then help me
  19354. 11:59:00help you in three easy steps by smashing
  19355. 11:59:03that like button
  19356. 11:59:04drop a comment down below and subscribe
  19357. 11:59:06if you'd like to become a fellow bro
  19358. 11:59:11[Music]
  19359. 11:59:59you

About this transcript

This page contains the full transcript of Python Full Course for free ๐Ÿ by Bro Code, generated from the public captions YouTube serves with the video. The transcript has 111,769 words across 19,359 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator โ€” no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.