YouTube2Text

CS50P - Lecture 0 - Functions, Variables — Transcript

by CS50 · 21,072 words · 2,984 segments · language en · Watch on YouTube

Full transcript

  1. 0:11[music]
  2. 0:16[music]
  3. 0:24All right. This is CS50's introduction
  4. 0:26to programming with Python. My name is
  5. 0:28David Malan, and this is our week on
  6. 0:30functions and variables. But odds are
  7. 0:32many of you, most of you, have never
  8. 0:34actually programmed before. So, let's
  9. 0:36start by doing just that. Let me go
  10. 0:38ahead here and open up my computer and
  11. 0:40on it a program called Visual Studio
  12. 0:42Code or VS Code, which is just a very
  13. 0:44popular program nowadays for actually
  14. 0:46writing code. Now, you don't have to
  15. 0:47write code using this particular tool.
  16. 0:50In fact, all we need at the end of the
  17. 0:51day is a a so-called text editor, a
  18. 0:53program for writing text. And heck, if
  19. 0:55you really want, you could even use
  20. 0:56something like Google Docs or Microsoft
  21. 0:58Word. You'd have to save it in the right
  22. 1:00format, but really at the end of the
  23. 1:02day, all you need is a program for
  24. 1:03writing text because that's what code
  25. 1:05is, text. Now, within this particular
  26. 1:07program, I'm going to have the ability
  27. 1:08to create one or more files via this top
  28. 1:11portion of the screen. And I'm going to
  29. 1:12do so by diving right in and doing this
  30. 1:15at the bottom of my screen. At the
  31. 1:17bottom of my screen is a so-called
  32. 1:18terminal window, and this is a command
  33. 1:21line interface or CLI interface to the
  34. 1:24underlying computer, be it your Mac or
  35. 1:26your PC or even some server in the
  36. 1:28cloud. And what I'm going to do here is
  37. 1:30literally write code and then the name
  38. 1:32of the file that I want to code, for
  39. 1:34instance, hello.py.
  40. 1:36As we'll soon see, any program that you
  41. 1:38write in Python generally has a file
  42. 1:40name that ends in .py to indicate to the
  43. 1:42computer that it's indeed a program
  44. 1:44written in Python. Now, you'll see here
  45. 1:46at the top of my screen I have a
  46. 1:47blinking cursor at line one, which is
  47. 1:49where the very first line of my code is
  48. 1:51going to go, and then just a tab that
  49. 1:52reminds me of the name of this file,
  50. 1:54hello.py. And without even knowing much
  51. 1:57Python, I'm going to write my very first
  52. 1:59program here as follows. Print open
  53. 2:02parenthesis, quote hello, {comma} world,
  54. 2:06close quote, and close parenthesis. And
  55. 2:08you'll see that at my keyboard, some of
  56. 2:10my thoughts were finished for me. I only
  57. 2:11had to type one parenthesis and the
  58. 2:13other one automatically appeared. And
  59. 2:15that's just a feature that we'll see of
  60. 2:16tools like this tool here. Now, even if
  61. 2:19you've never programmed before, odds are
  62. 2:21you can guess and fur what this simple
  63. 2:24program is going to do. And it's only
  64. 2:26one line. Print open parenthesis, quote
  65. 2:28hello world, close quote, close
  66. 2:30parenthesis. Indeed, when I run this
  67. 2:33program, ultimately it's just going to
  68. 2:34say hello to the world. And in fact,
  69. 2:36this is a very famous, perhaps the most
  70. 2:37canonical program that you can write as
  71. 2:39your very first program in Python or any
  72. 2:41other language. And so that's what I've
  73. 2:43done here. But on my Mac, my PC, even my
  74. 2:46phone, I'm generally in the habit like
  75. 2:48you of running programs by double
  76. 2:50clicking an icon or just tapping on the
  77. 2:52screen. But I see no such icons here.
  78. 2:54And in fact, that's because my interface
  79. 2:57to at least my current Mac or PC or some
  80. 2:59server in the cloud is again only a CLI,
  81. 3:02command line interface, which even
  82. 3:04though it might feel like it's a step
  83. 3:05back from the menus and buttons and
  84. 3:07icons that you and I take for granted
  85. 3:08every day, you'll find we think that
  86. 3:10it's ultimately a much more powerful
  87. 3:12interface and incredibly popular to use
  88. 3:14among programmers in the real world. So
  89. 3:17to run this program, I'm going to have
  90. 3:18to use a command. And I'm going to move
  91. 3:20my cursor back down to the bottom of the
  92. 3:22screen here, where previously I already
  93. 3:24ran one command. The command code, which
  94. 3:27has the effect of opening VS Code in my
  95. 3:29computer, and then I passed in the name
  96. 3:31of the file that I wanted to code up.
  97. 3:33Now I have a chance to type a second
  98. 3:35command, and you'll see I see a second
  99. 3:37dollar sign. Now the dollar sign here
  100. 3:38doesn't indicate any kind of currency or
  101. 3:40money, it just is the symbol that's
  102. 3:42generally used to indicate your prompt,
  103. 3:44where the command line interface wants
  104. 3:46you to put those commands. Now the
  105. 3:48command I can run here is going to be
  106. 3:50this. I'm going to run python of
  107. 3:53hello.py.
  108. 3:55Now, why is that? Well, it turns out
  109. 3:57that when I actually write code in a
  110. 3:58language like python, it's of course
  111. 4:00stored in that file hello.py, but I need
  112. 4:04to interpret the code top to bottom left
  113. 4:07to right so that the computer knows what
  114. 4:08to do. Indeed, at the end of the day,
  115. 4:10even if you don't really know much about
  116. 4:11computers, you've probably heard that
  117. 4:13computers only understand zeros and
  118. 4:15ones, the so-called binary system. Well,
  119. 4:17if that's the case, then something that
  120. 4:19says print and parenthesis and quote
  121. 4:22unquote hello world is not surely zeros
  122. 4:24and ones. We have to somehow translate
  123. 4:26it into the zeros and ones that the
  124. 4:27computer understands. Now, fortunately,
  125. 4:30so long as you've installed such a
  126. 4:32program in advance, there's a program as
  127. 4:34well as a language called python. So,
  128. 4:36python is not only a language in which
  129. 4:38we're going to write code, it's also a
  130. 4:39program, otherwise known as an
  131. 4:41interpreter, that you install for free
  132. 4:43on your own Mac or PC or some server in
  133. 4:45the cloud, and you can then run that
  134. 4:47program, that interpreter, passing to it
  135. 4:50as input the name of your file like mine
  136. 4:52here, hello.py, and then that program,
  137. 4:54that interpreter, will handle the
  138. 4:56process of reading it top to bottom left
  139. 4:58to right and translating it effectively
  140. 5:00into those zeros and ones that the
  141. 5:02computer can understand. So, let's do
  142. 5:04just that. Let me go back to VS Code
  143. 5:06here. I already typed out python of
  144. 5:08hello.py, but I didn't yet hit enter.
  145. 5:11And that's what's now going to kick off
  146. 5:12this command and hopefully, if I didn't
  147. 5:14mess any of this up, I should see my
  148. 5:17very first program's output to the
  149. 5:19screen. And voilà, hello world. So, if
  150. 5:23you too have typed exactly that same
  151. 5:25code and have executed exactly that same
  152. 5:27command, you will have written your very
  153. 5:29first program, in this case, in python.
  154. 5:32Well, now let's take a step back and
  155. 5:34consider what is it that we actually
  156. 5:36just did and what is it we're looking
  157. 5:37here on the screen. Well, first and
  158. 5:39foremost, in most any programming
  159. 5:40language, you tend to have access to
  160. 5:42what are called functions. A function is
  161. 5:44like an action or a verb that lets you
  162. 5:46do something in the program. And
  163. 5:49generally speaking, any language comes
  164. 5:51with some predetermined set of
  165. 5:52functions, some very basic actions or
  166. 5:55verbs that the computer will already
  167. 5:57know how to do for you, that the
  168. 5:58language really will know how to do for
  169. 6:00you, and you, the programmer, the human,
  170. 6:02can use those functions that will to get
  171. 6:04the computer to do those things. Now,
  172. 6:07the program in question here, hello.py,
  173. 6:10is using one function, and you can
  174. 6:12perhaps guess what it is. That function
  175. 6:13is of course going to be this function
  176. 6:15print. And that print function, of
  177. 6:18course, doesn't print some preordained
  178. 6:20string of text, that is to say, it
  179. 6:23prints whatever it is you want it to
  180. 6:25print. And here, too, do we have another
  181. 6:28uh piece of terminology in the world of
  182. 6:29programming, namely arguments. An
  183. 6:31argument is an input to a function that
  184. 6:34somehow influences its behavior. The
  185. 6:36people who invented Python, of course,
  186. 6:38didn't necessarily know what it is you
  187. 6:40and I are going to want to print to the
  188. 6:41screen, so they designed this print
  189. 6:43function using these parentheses with
  190. 6:46the ability to take as input some string
  191. 6:48of text, be it in English or any other
  192. 6:50human language, that is what you want
  193. 6:53this function ultimately to print onto
  194. 6:55the screen.
  195. 6:56And what is it that the program's
  196. 6:58ultimately doing on the screen? Well,
  197. 6:59it's printing, of course. It's showing
  198. 7:01us "Hello, world" on the screen, and
  199. 7:03that's generally in programming known as
  200. 7:04a side effect. It can be visual, it can
  201. 7:06be audio. In this case, it's something
  202. 7:08that appears on the screen, and
  203. 7:09functions, therefore, can indeed have
  204. 7:11these side effects. One of the things
  205. 7:13they can do as this verb or action is to
  206. 7:15display on the screen as a side effect
  207. 7:17something like those world words that we
  208. 7:19wanted, "Hello, world."
  209. 7:21So, that's my first program. And, you
  210. 7:23know, I'm feeling pretty good.
  211. 7:24Everything worked as planned. I didn't
  212. 7:26make any mistakes, but honestly, when
  213. 7:28you're learning how to program, and even
  214. 7:30once you've learned how to program years
  215. 7:32later, you're going to make mistakes.
  216. 7:34And those mistakes, of course, are
  217. 7:36referred to a term you might already
  218. 7:37know, which is that of a bug. A bug is a
  219. 7:39mistake in a program, and they can take
  220. 7:40so many forms and take comfort perhaps
  221. 7:44in knowing that over the coming weeks
  222. 7:46you're going to make so many mistakes.
  223. 7:48You're going to have so many bugs in
  224. 7:49your code just as I did and just as I
  225. 7:52still do and those bugs themselves are
  226. 7:54just mistakes that are problems for you
  227. 7:56to solve. And over the weeks to come
  228. 7:58we're going to give you a lot of tools,
  229. 8:00both mental and technical via which you
  230. 8:02can solve those problems, but just don't
  231. 8:04get discouraged if when writing your
  232. 8:05program for the first time it doesn't
  233. 8:07even work that first time. It will with
  234. 8:10time, with practice, and with
  235. 8:11experience. So, let me deliberately now
  236. 8:14make a mistake that there was a non-zero
  237. 8:16chance I might have done accidentally
  238. 8:18already, but I got lucky. Let me go
  239. 8:20ahead and just suppose I forgot to
  240. 8:22include something like the closing
  241. 8:24parenthesis at the end of this line of
  242. 8:25code. You know, the code is almost
  243. 8:27correct. It's like 99% of the way there,
  244. 8:30but now that I've pointed it out it's
  245. 8:31pretty obvious that it's missing that
  246. 8:33close parenthesis, but even little,
  247. 8:35seemingly minor details like that that
  248. 8:38you and I as humans wouldn't really care
  249. 8:40about and if you're sending an email or
  250. 8:41a text message out if whatever, it's
  251. 8:43just a typo, it's not that big a deal.
  252. 8:45It is going to be a big deal to the
  253. 8:46computer. A computer is going to take
  254. 8:48you literally and if you don't finish
  255. 8:50your thought in the way the language
  256. 8:52expects, it's not going to necessarily
  257. 8:54run at all. So, let's do this. I'm going
  258. 8:56to go ahead here and clear my screen
  259. 8:57down at the bottom just so I can start
  260. 8:59fresh and I'm going to go ahead and run
  261. 9:01this version of my program after having
  262. 9:04made that change by deleting the
  263. 9:05parenthesis. I'm going to go ahead and
  264. 9:06type python again of hello.py
  265. 9:09and this time when I hit enter I'm
  266. 9:11hoping I'm going to see hello world, but
  267. 9:13here we have an error on the screen, a
  268. 9:15so-called syntax error, which refers to
  269. 9:18my having made a mistake at my keyboard.
  270. 9:21And if this one fortunately is pretty
  271. 9:23straightforward, it indeed says that
  272. 9:25this open parenthesis was never closed
  273. 9:28and so that's probably pretty intuitive
  274. 9:30now what I need to do. I need of course
  275. 9:32to close it. Unfortunately, sometimes
  276. 9:34the error messages we'll see in the
  277. 9:35coming weeks are not going to be nearly
  278. 9:37that user-friendly, but there too again,
  279. 9:39with experience, with practice, will you
  280. 9:41get better at debugging such programs.
  281. 9:44Let me now make sure that I indeed fixed
  282. 9:46it correctly. Let me go ahead and run
  283. 9:47now hello.py and hit enter, and voila,
  284. 9:50we're back in business. Well, let me
  285. 9:52pause here and see if we have any
  286. 9:54questions now about Python itself,
  287. 9:56writing, or running even the simplest
  288. 10:00of these programs.
  289. 10:02Uh could I write code inside of Word or
  290. 10:04for example Microsoft Excel, and what's
  291. 10:07the barrier to doing that?
  292. 10:10A really good question, and allow me to
  293. 10:11very explicitly say to the entire
  294. 10:14internet that you should not write code
  295. 10:16with Microsoft Word. I mentioned that
  296. 10:18only because it's a tool via which you
  297. 10:20can write text, and code is at the end
  298. 10:22of the day just text, but it's not the
  299. 10:23right tool for the job. We don't need
  300. 10:25bold facing, underlining, paragraphs,
  301. 10:27and the like. We generally want
  302. 10:28something much simpler than Microsoft
  303. 10:30Word or Google Docs, and so VS Code is
  304. 10:33an example of just a more general
  305. 10:34purpose text editor. Its purpose in life
  306. 10:36is to allow you, the human, to edit
  307. 10:38text. Nowadays, these text editors come
  308. 10:40with many more features. In fact, you'll
  309. 10:42notice that even in my code here, even
  310. 10:44though it's just one line, there's a bit
  311. 10:46of color to it. The word print for me is
  312. 10:48appearing in blue. The parentheses are
  313. 10:50black, and we'll see as we might write
  314. 10:52more lines of code, more and more of the
  315. 10:53lines will come to life in various
  316. 10:55colors. Now, that's just one feature of
  317. 10:58a text editor. We'll see too that it has
  318. 10:59features like this built-in terminal
  319. 11:01window. It's going to have a built-in
  320. 11:02tool for debugging or finding problems
  321. 11:05with code, and it's just a very popular
  322. 11:06tool nowadays. are many, many others out
  323. 11:09there. You're welcome to use them for
  324. 11:10this course and beyond. We just happen
  325. 11:12to use this one in large part too
  326. 11:14because you can also use VS Code
  327. 11:15nowadays uh for free in the cloud. I
  328. 11:18have one other question here on
  329. 11:20programming with Python or Hello World
  330. 11:23or syntax more generally.
  331. 11:24Okay, I was going to ask if
  332. 11:28is it not possible to run the computer
  333. 11:29using the terminal window?
  334. 11:31I think I heard is it not if it's
  335. 11:33possible to run the program without the
  336. 11:36terminal window?
  337. 11:37Are you
  338. 11:37Yes, sir.
  339. 11:38Okay, you froze for me again, but let me
  340. 11:40infer what the question is. So, in this
  341. 11:42environment, as I've configured my
  342. 11:44computer, I can only run these Python
  343. 11:46programs via the terminal window. Now,
  344. 11:48that's good for me, the programmer or
  345. 11:50the person who's trying to learn how to
  346. 11:52program, but it's not very good if you
  347. 11:53want to ship this software and have
  348. 11:55other people use your actual code. You
  349. 11:57can absolutely write programs and then
  350. 12:00allow other people to use not a command
  351. 12:02line interface, but a graphical user
  352. 12:03interface or GUI, GUI. This is just one
  353. 12:06mechanism and perhaps I think the best
  354. 12:09one with which to start writing code
  355. 12:10because eventually it's going to give us
  356. 12:12a lot more control. Allow me to forge
  357. 12:14ahead here, but please feel free to
  358. 12:16continue asking questions along the way
  359. 12:18if only via the chat. And let's consider
  360. 12:20now how we might go about improving this
  361. 12:23program. Let's go about improving this
  362. 12:24program to make it a little more
  363. 12:26interactive and not just assume that
  364. 12:28everyone is going to want to be greeted
  365. 12:30more generically as hello world. Let's
  366. 12:32see if I can't get this program to say
  367. 12:33something like hello David or hello
  368. 12:35Jeremiah or hello Horatio or whatever
  369. 12:37the actual user's name is. Well, to do
  370. 12:40this, I'm going to go back up to
  371. 12:42hello.py and I'm going to add another
  372. 12:44line of code at the very top that simply
  373. 12:46says, for instance, uh what's your name,
  374. 12:49quote unquote, with a little extra space
  375. 12:51at the end. So, I'm printing to the user
  376. 12:53asking them a question for some input,
  377. 12:55but now I need another function to
  378. 12:56actually get input from the user. And
  379. 12:59perfectly, enough Python comes with a
  380. 13:01function named input. So, here I'm going
  381. 13:03to go ahead and call a function input
  382. 13:05open paren close paren. And that's going
  383. 13:08to prompt the user with just a blinking
  384. 13:09cursor waiting for them to type
  385. 13:11something in. Now, it turns out if I
  386. 13:13read the documentation for the input
  387. 13:15function, it actually takes an argument
  388. 13:17itself. I don't need to use print
  389. 13:19separately and then prompt the user for
  390. 13:21input, so I can actually simplify this
  391. 13:23code before we even use it. I'm going to
  392. 13:25go ahead here and take that same string
  393. 13:27from print, put it as an argument to the
  394. 13:29input function, and get rid of the print
  395. 13:31altogether. And in fact, that print
  396. 13:33would have added a new line anyway, so
  397. 13:34now I've just got a prompt where the
  398. 13:36user's cursor is going to end up
  399. 13:37blinking at the end of the line asking
  400. 13:39them, "What's your name?" In my terminal
  401. 13:41window, I'm going to run python of
  402. 13:44hello.py.
  403. 13:45Enter. Okay, we're making progress. It
  404. 13:48seems that this new function input is
  405. 13:49indeed prompting me, the human, for
  406. 13:51input. So, I'm going to type in my name,
  407. 13:52David, and hit enter. Unfortunately, it
  408. 13:55doesn't really do anything with my name.
  409. 13:57It just outputs it immediately. All
  410. 13:59right, well, I could fix this, right? I
  411. 14:01could go up to line two, and I could
  412. 14:03change world to David, and then back in
  413. 14:06my terminal window here, I can do python
  414. 14:08of hello.py. Enter. What's your name?
  415. 14:11David. Enter. And there we go. All
  416. 14:13right, now I'm up and running. Now my
  417. 14:14program is working as intended. Of
  418. 14:17course, this isn't really working as
  419. 14:20intended here. Let me go ahead and try
  420. 14:22pretending to be my colleague Carter
  421. 14:24here. Well, Carter's name is this. I'm
  422. 14:26going to go ahead and hit enter, and
  423. 14:27I'll see of course hello Carter. Well,
  424. 14:29obviously not, because I've hard-coded,
  425. 14:31so to speak, I've written literally my
  426. 14:33name inside of the string. So, we need
  427. 14:35some way now of actually getting back
  428. 14:38what the user's input is and doing
  429. 14:40something with it ultimately. And for
  430. 14:41this, we're going to leverage another
  431. 14:43feature of programming, specifically a
  432. 14:45feature of some functions, which is that
  433. 14:47they can have return values as well. If
  434. 14:50you think of input as being again this
  435. 14:52action, this verb, you can actually
  436. 14:53personify it as maybe a person, like a
  437. 14:56friend of yours that you've asked a
  438. 14:57question of, and you've asked your
  439. 14:58friend to go get input from someone
  440. 15:00else. Go ask that person their name. And
  441. 15:02if your friend comes back knowing that
  442. 15:04person's name, well, wouldn't it be nice
  443. 15:06if they handed that name back to you?
  444. 15:08That's kind of what we need
  445. 15:09metaphorically the function to do is get
  446. 15:11the user's input and then hand it back
  447. 15:13to me so that I, the programmer, can do
  448. 15:15something with it. But if it's going to
  449. 15:17be handed back to me, I kind of want to
  450. 15:19put it somewhere so that I can then
  451. 15:22print it back on the screen. I need to
  452. 15:24do the equivalent of take out like a
  453. 15:25piece of paper or post-it note, write
  454. 15:27down on this piece of paper what it is
  455. 15:30the human has said so that I can then
  456. 15:32feed it into as input that print
  457. 15:34function. And to do that, we're going to
  458. 15:35need one more feature of programming,
  459. 15:37namely variables. And odds are most
  460. 15:40everyone's familiar with variables from
  461. 15:41math class way back when, X and Y and Z
  462. 15:44and the like. Well, programming has that
  463. 15:46same capability, this ability to create
  464. 15:48a variable, in this case in the
  465. 15:50computer's memory, not just on a piece
  466. 15:52of paper, and that variable can store a
  467. 15:54value, a number, some text, even an
  468. 15:57image or video or more. A variable is
  469. 15:59just a container for some variable
  470. 16:02a variable is just a container for some
  471. 16:04value inside of a computer or inside of
  472. 16:07your own program. So, how do I go about
  473. 16:09expressing myself in this way? Well, I
  474. 16:11think what I'm going to do is introduce
  475. 16:13a variable that's a little more
  476. 16:14interestingly named than X or Y. I could
  477. 16:17just say this, X equals input, but I'm
  478. 16:20going to use a better name than a
  479. 16:21typical mathematical variable here, and
  480. 16:23I'm going to literally call my variable
  481. 16:25name. Why? Well, in programming, because
  482. 16:27I have a whole keyboard in front of me,
  483. 16:28I can use more descriptive terms to
  484. 16:30describe what it is I'm writing. And now
  485. 16:32though, there's an opportunity to
  486. 16:34consider a specific piece of syntax.
  487. 16:36We've seen parentheses, we've seen
  488. 16:38quotes, all of which are necessary when
  489. 16:40passing inputs to a function, but this
  490. 16:42equal sign here, that's in between input
  491. 16:46on the right and name on the left, is
  492. 16:48actually important. And it's technically
  493. 16:50not an equal sign per se. It doesn't
  494. 16:52mean equality as much as it means
  495. 16:54assignment. So, in Python and many
  496. 16:57programming languages, a single equal
  497. 16:59sign is the assignment operator. And
  498. 17:01what that means specifically is that you
  499. 17:03want to assign from right to left
  500. 17:06whatever the user's input is. So, the
  501. 17:09equal sign copies from the right to the
  502. 17:12left whatever the return value of the
  503. 17:15function on the right is. So, again, the
  504. 17:18input function clearly gets input from
  505. 17:20the user. That's why I was able to type
  506. 17:21my name or Carter's, but it also, sort
  507. 17:24of behind the scenes, hands that value,
  508. 17:27that return value, back to me, the
  509. 17:29programmer. And if I use an equal sign
  510. 17:31and a variable, no matter what I call
  511. 17:33it, I can store that input in that
  512. 17:36variable so as to reuse it later. So,
  513. 17:38now sitting in the computer's memory
  514. 17:39somewhere is a container containing
  515. 17:42David, {quote} {unquote}, or Carter,
  516. 17:44{quote} {unquote}, or whatever the human
  517. 17:46has typed in.
  518. 17:47But here it's easy to make a mistake.
  519. 17:49Suppose I decide to try to print that
  520. 17:52name, and so I I kind of, on a hunch,
  521. 17:55type in this, "Hello, {comma} name."
  522. 17:57Just kind of plugging in the name of the
  523. 17:59variable. Well, let me go ahead here and
  524. 18:01run python of hello.py and hit enter.
  525. 18:05That's going to prompt me for my name,
  526. 18:06and let me type in my name, d a v i d,
  527. 18:08but I haven't hit enter yet. And perhaps
  528. 18:10via the chat, what's going to happen
  529. 18:12here when I now hit enter? I'm hoping it
  530. 18:16says, "Hello, David." I'd be okay if it
  531. 18:19says, "Hello, world." But I don't want
  532. 18:21it to say what it's actually going to
  533. 18:22say. And yep, what we're seeing in the
  534. 18:24chat is, well, it's probably going to
  535. 18:26say, literally, "Hello, {comma} name."
  536. 18:28So, that's not quite right. So, we need
  537. 18:30another way of printing out the value
  538. 18:33inside of that variable rather than just
  539. 18:35this word name. Well, let me try this in
  540. 18:37a couple of different ways. Let me try
  541. 18:39this as follows. Let me go ahead and
  542. 18:41maybe undo this because I've gotten
  543. 18:43pretty good already at saying hello. So,
  544. 18:45let's let, you know, let's draw that
  545. 18:46line in the sand and just say, "All
  546. 18:47right, let's get at least get hello,
  547. 18:48{comma}, out the door." Let's now print
  548. 18:51name, and just on a hunch, I'm going to
  549. 18:53try this. I'm going to use print again,
  550. 18:55cuz you can use this functions as many
  551. 18:56times as you need, and I'm going to pass
  552. 18:59to the name, to the print function, the
  553. 19:01variable called name. But notice, I'm
  554. 19:03being a little clever now. I'm not
  555. 19:05putting it in double quotes because
  556. 19:06we've seen already that double quotes
  557. 19:08means literally print out n a m e. I'm
  558. 19:10getting rid of the quotes this time in
  559. 19:12hopes that now by passing the variable
  560. 19:15called name to the function called
  561. 19:18print, it will in fact go about printing
  562. 19:20the contents of that variable, that is
  563. 19:23it's so-called value. All right, let's
  564. 19:25go ahead and do this here. Python of
  565. 19:27hello.py enter. What's your name? David.
  566. 19:30And now crossing my fingers still, I see
  567. 19:32hello, {comma} David. All right, so it's
  568. 19:36not the best program. I'm I'm kind of
  569. 19:38cutting some corners here so to speak.
  570. 19:40I'm saying hello David on two separate
  571. 19:43lines. So it's not as elegant, it's not
  572. 19:45as pretty, it's not as grammatically
  573. 19:46appropriate in English as just saying it
  574. 19:48all in one breath on one line, but at
  575. 19:50least I've solved the problem. Just not
  576. 19:52very well yet. But let me take a step
  577. 19:54back now and perhaps introduce a couple
  578. 19:56of other concepts with which we should
  579. 19:58be familiar, which is as our programs
  580. 20:00get longer and they're no longer just
  581. 20:02one line or two or even three,
  582. 20:04eventually our programs are going to
  583. 20:05become dozens of lines, maybe even
  584. 20:07hundreds of lines long. Let's set the
  585. 20:09stage for success moving forward. It
  586. 20:12turns out that Python and a lot of
  587. 20:13programming languages also support
  588. 20:15something called comments. Comments are
  589. 20:18notes to yourself in your code. And you
  590. 20:21include comments by way of a special
  591. 20:23symbol. In Python it's going to be the
  592. 20:24hash symbol typically, and that allows
  593. 20:26you to write the equivalent of a note to
  594. 20:28yourself, but in a way that's not going
  595. 20:30to break your code. The computer
  596. 20:31actually ignores your comment. It's just
  597. 20:33there for you. It's just there for your
  598. 20:35teacher. It's just there for your
  599. 20:36colleague with whom you're sharing
  600. 20:38ultimately that code. So if I go back to
  601. 20:40VS code here, and I just want to add
  602. 20:42some comments to this program to explain
  603. 20:44to my teacher, to myself, to my
  604. 20:47colleagues what this program is doing,
  605. 20:49well, let's go ahead and do that. I'm
  606. 20:50going to go at the very top of my
  607. 20:51program,
  608. 20:53and on line one now, I'm going to move
  609. 20:55that original line of code down a bit.
  610. 20:57I'm going to add a hash, and I'm going
  611. 20:58to say something like this, uh ask user
  612. 21:01for their name. Now, I don't have to use
  613. 21:04that language. I don't have to use that
  614. 21:05uh that text. I could use any human
  615. 21:07language whatsoever. It doesn't have to
  616. 21:09be English, but I'm going to now below
  617. 21:12that just say something like this. Say
  618. 21:14hello to user. And you'll notice that VS
  619. 21:16Code by default is kind of graying out
  620. 21:18my comments. They're no longer blue.
  621. 21:20There's no red. There's no color in
  622. 21:21them. And that's just because they're
  623. 21:22notes to myself and the computer
  624. 21:24ultimately is going to ignore them. But
  625. 21:26what we have now is two comments. Ask
  626. 21:29user for their name and then a second
  627. 21:30comment, say hello to user. And I've
  628. 21:32just kind of commented each chunk of
  629. 21:35code. Like each line or lines plural of
  630. 21:38code that are doing something
  631. 21:39noteworthy. Why? Well, tomorrow morning
  632. 21:41when I wake up having, you know,
  633. 21:44slept for quite some time, forgotten
  634. 21:46what it is I did the previous day, it's
  635. 21:47convenient with comments to just see in
  636. 21:49English or your own human language what
  637. 21:52it is this program is doing. So that you
  638. 21:54don't have to read the code itself. And
  639. 21:56better yet, if there's maybe a mistake
  640. 21:58down the road, you can read what your
  641. 22:00intention was and then you can look at
  642. 22:02the code and figure out if your code's
  643. 22:03now doing what you intended. So this
  644. 22:06isn't really necessary for a program
  645. 22:08this small. It's pretty obvious with
  646. 22:10just one or two or three lines what the
  647. 22:11program's doing. It's just as fast to
  648. 22:13read the code than the comment. But
  649. 22:14getting into this habit is generally a
  650. 22:16good thing to comment your code every
  651. 22:18one or few lines so as to remind
  652. 22:20yourself and others what it is your
  653. 22:22intent and your code is doing. What's
  654. 22:24nice about comments too is this.
  655. 22:26Comments can also serve to be sort of a
  656. 22:29to-do list for yourself. There's this
  657. 22:31notion in programming of pseudo code.
  658. 22:33Pseudo code isn't a formal thing. It's
  659. 22:35not one specific language. It's just
  660. 22:37using English or your own human language
  661. 22:40to express your thoughts succinctly,
  662. 22:42methodically, algorithmically so to
  663. 22:44speak. But pseudo code therefore,
  664. 22:46because it's not Python and it's not
  665. 22:48necessarily English, it just kind of
  666. 22:50allows you to outline your program even
  667. 22:52in advance. So for instance, if I wasn't
  668. 22:54sure today how I wanted to go about
  669. 22:57writing this program, but I did know
  670. 22:58what I want to do, I I have started
  671. 23:01today by just writing this in hello.py.
  672. 23:04No code. I could have written just a
  673. 23:06couple of comments to myself. Step one,
  674. 23:08ask user for their name. Step two, say
  675. 23:10hello to user. Then, once I've outlined
  676. 23:13my program in pseudo code, then I can go
  677. 23:15in there and say, all right, how do I
  678. 23:16ask the user for their name? Uh well, I
  679. 23:18can do input {quote} "What's your name?"
  680. 23:20{question mark} and then on the left
  681. 23:22here, I can maybe put a variable and
  682. 23:24assign it to that. Okay, how do I say
  683. 23:26hello to the user? Well, I know I can
  684. 23:28use print to say things on the screen.
  685. 23:30Let me say hello, {comma}
  686. 23:32uh and let me Okay, let me now print the
  687. 23:33person's name. So, again, pseudo code is
  688. 23:36a nice way of structuring your to-do
  689. 23:38list, especially if you have no idea how
  690. 23:40to write the code because it breaks a
  691. 23:42bigger program down into small bite-size
  692. 23:45tasks. All right, let me pause here to
  693. 23:47see if there are now any questions on
  694. 23:49comments, pseudo code, return values, or
  695. 23:53variables.
  696. 23:56Any questions we can clear up here?
  697. 23:59Yeah, my question is uh does the
  698. 24:00function input uh work for any type of
  699. 24:04of information or only for words?
  700. 24:07Yeah, good really good question. So,
  701. 24:08according to its documentation, and
  702. 24:10we'll look more at formal documentation
  703. 24:12soon, input is going to expect what's
  704. 24:13called a string. That is a sequence of
  705. 24:16text, be it in English or any other
  706. 24:18human language, but it's indeed going to
  707. 24:19be expecting text with which to prompt
  708. 24:22the user.
  709. 24:23A good question. How about another
  710. 24:24question from the group if we could?
  711. 24:26Uh I wanted to ask how I make several
  712. 24:29line comments.
  713. 24:31Oh, how do you do many lines of comments
  714. 24:32if I if I'm hearing you correctly? Sure,
  715. 24:35uh you would just keep doing them like
  716. 24:37this. You just prefix each of the lines
  717. 24:40with a hash symbol like I'm doing here.
  718. 24:43There is another technique for doing
  719. 24:45multi-line comments in Python that
  720. 24:46actually tend to have special meaning.
  721. 24:48You can do three double quotes like this
  722. 24:51and then anything in between here is a
  723. 24:54comment. That's That's technique or you
  724. 24:56can use single quotes as well, but more
  725. 24:57on those, I think, another time.
  726. 25:00All right. Well, if you don't mind, let
  727. 25:01me forge ahead here and see how we might
  728. 25:03improve this program further, and also
  729. 25:05introduce a few other features that we
  730. 25:07might want to take into account over
  731. 25:09time. So, it turns out that we can
  732. 25:11certainly improve on this program
  733. 25:13because it's a little disappointing that
  734. 25:16I'm cutting this corner and saying
  735. 25:17"Hello," {comma} and then on a new line
  736. 25:19printing out name. Like, we can do
  737. 25:21better. And most programs you use on
  738. 25:22your phone or your laptop certainly keep
  739. 25:24text together when people want. So, how
  740. 25:27can we go about doing that? Well,
  741. 25:28there's a few different ways. And, in
  742. 25:29fact, the goal here is not so much to
  743. 25:31solve this one problem, but to
  744. 25:33demonstrate and emphasize that in
  745. 25:34programming, Python, and other
  746. 25:36languages, there's so many ways,
  747. 25:37sometimes, to solve the same problem.
  748. 25:40And here's one way to solve this
  749. 25:42problem. Let me go in here, and let me
  750. 25:44go ahead now and say "Hello," {comma}
  751. 25:48and let me just add to the end of that
  752. 25:50the user's name. So, I'm using plus in
  753. 25:53kind of an interesting way. This is not
  754. 25:55addition, per se. I'm not adding
  755. 25:57numbers, obviously, but I do kind of
  756. 25:59want to add the person's name to the
  757. 26:02string of text "Hello," {comma}. Well,
  758. 26:05let me go now down to my terminal window
  759. 26:06and run python of hello.py again. Enter.
  760. 26:10What's your name? I'm going to type in
  761. 26:11David. Enter. Okay, it's better. It's
  762. 26:14better, but there's a minor bug, albeit
  763. 26:16aesthetic, here. There's missing space,
  764. 26:19but let's just use some intuition here.
  765. 26:20Well, if I'm missing the space after the
  766. 26:22comma, why don't I go ahead and just add
  767. 26:24it manually here? Let me now rerun the
  768. 26:26program, python of hello.py. Enter.
  769. 26:29David. Enter. And there we go. Now, we
  770. 26:31have something that looks a little
  771. 26:32prettier in terms of English grammar.
  772. 26:34Hello, {comma} {space} David. And now,
  773. 26:37if we rewind, you might have noticed
  774. 26:39before or wondered why I had this
  775. 26:42seemingly extra space after my question
  776. 26:44mark, namely here. There's a space after
  777. 26:46the question mark but before the double
  778. 26:48quote, and that was just for aesthetics,
  779. 26:49too. I wanted to move the user's cursor
  780. 26:51one space to the right so that when I
  781. 26:53type their name or they type their name,
  782. 26:55it's not immediately next to that same
  783. 26:58question mark there. But there's other
  784. 27:00ways we can do this. It turns out that
  785. 27:02some functions, print among them,
  786. 27:05actually take multiple arguments. And it
  787. 27:07turns out that if you separate the
  788. 27:10inputs to a function, the so-called
  789. 27:12arguments to a function with a comma,
  790. 27:14you can pass in not just one but two,
  791. 27:16three, four, five, onward. So let me go
  792. 27:19ahead and pass in not just hello,
  793. 27:21{comma} {space} but that
  794. 27:24followed by name. And this is a little
  795. 27:26confusing potentially at first glance
  796. 27:27cuz now I've got two commas, but it's
  797. 27:29important to note that the first comma
  798. 27:31is inside of my quotation marks, which
  799. 27:34is uh simply an English grammatical
  800. 27:36thing. The second comma here is outside
  801. 27:39of the quotes but between what are now
  802. 27:41two separate arguments to print. The
  803. 27:44first argument is hello, {comma}
  804. 27:45{space}. The second argument is the name
  805. 27:48variable itself. So let's see how this
  806. 27:50looks.
  807. 27:51Python of hello.py, enter. What's your
  808. 27:54name? David, enter. Okay, I've kind of
  809. 27:57overcorrected. Now I've got two spaces
  810. 27:59for some reason. Well, it turns out and
  811. 28:00this is subtle, when you pass multiple
  812. 28:03arguments to print, it automatically
  813. 28:06inserts a space for you. This was not
  814. 28:08relevant earlier because I was passing
  815. 28:10in one big argument to print all at once
  816. 28:14by using that plus operator. This time
  817. 28:16I'm passing in two because of the comma.
  818. 28:19So if I don't want that extra space, I
  819. 28:21don't need to pass in one myself. I can
  820. 28:23just do this and now notice if I run
  821. 28:25this program again, Python of hello.py,
  822. 28:28type in my name, David. Now it looks
  823. 28:30grammatically like I might want. Now
  824. 28:32which of these approaches is better?
  825. 28:34This approach uses a function print with
  826. 28:36two arguments, hello, {comma} and the
  827. 28:39name variable. The previous version,
  828. 28:41recall, technically used one argument
  829. 28:44even though it looked a little curious.
  830. 28:45It's one argument in the sense that the
  831. 28:47computer, just like mathematicians, are
  832. 28:49going to do what's inside of parentheses
  833. 28:51first. So, if inside of parentheses you
  834. 28:53have this string of text, "Hello,
  835. 28:55{comma} and a space", which I need to
  836. 28:57add back, then you have a plus, which
  837. 28:59means not addition per se, but
  838. 29:01concatenation, to join the thing on the
  839. 29:03left and the thing on the right. This
  840. 29:05ultimately becomes the English phrase
  841. 29:08"Hello, {comma} {space} David", and then
  842. 29:11what's being passed ultimately to the
  843. 29:13function is technically something like
  844. 29:16this, but it's doing it all dynamically.
  845. 29:18It's not me typing in David as I I
  846. 29:21secretly as I discreetly did earlier,
  847. 29:23it's figuring out dynamically what that
  848. 29:25value is after concatenating "Hello"
  849. 29:27with the value of name, and then passing
  850. 29:30that ultimately to print as the sole
  851. 29:32argument. Let me pause here to see if
  852. 29:34there's any questions on
  853. 29:36numbers of arguments now
  854. 29:39to functions.
  855. 29:41Can we use a function
  856. 29:43uh many times to solve a certain problem
  857. 29:45which we can encounter many times in our
  858. 29:47code?
  859. 29:48You can. You can use a function many
  860. 29:50different times to solve some problem.
  861. 29:52What we'll soon see though is if you
  862. 29:54find yourself as the programmer solving
  863. 29:56a problem the same way again and again
  864. 29:59and again, it turns out you'll be able
  865. 30:00to make your own function so that you
  866. 30:02don't have to keep reusing the basic
  867. 30:04ones that come with the language.
  868. 30:07I was curious about the {comma} and the
  869. 30:09plus sign. So, after plus sign, can we
  870. 30:12give just one variable and after
  871. 30:14{comma}, can we give multiple variables?
  872. 30:16Like, what is the difference?
  873. 30:18A good question. So, in the context of
  874. 30:20strings, and I keep using that term,
  875. 30:22string is a technical term in a
  876. 30:24programming language, and again it means
  877. 30:25a a sequence of text, a character, a
  878. 30:28word, a whole paragraph even. So, the
  879. 30:30plus operator is not just used, as we'll
  880. 30:33see, for addition of numbers in Python
  881. 30:36like we do on paper pencil, but it also
  882. 30:38is used for concatenation of strings on
  883. 30:40the left and the right. If you did want
  884. 30:42to combine not just two strings, left
  885. 30:44and right, but a third and a fourth, you
  886. 30:46can absolutely keep using plus, plus,
  887. 30:49plus, plus, and chain them together just
  888. 30:51like in math. Eventually, that's going
  889. 30:53to start to look a little ugly, I dare
  890. 30:55say, especially if your line of code
  891. 30:56gets long. So, there's better ways that
  892. 30:58we'll actually soon see. And a good
  893. 31:00question as well. Well, let me come back
  894. 31:02to the code here in question and see if
  895. 31:04we can't show you just a couple of other
  896. 31:06ways to solve the same problem, along
  897. 31:08the way emphasizing that what we're
  898. 31:09technically talking about here, yes, are
  899. 31:11strings, but there's even a technical
  900. 31:13term for these strings. In Python, it's
  901. 31:15just str, so to speak, str for short,
  902. 31:18for string. As you may know, if you
  903. 31:20program in other languages, people who
  904. 31:22invent programming languages like to be
  905. 31:24very uh succinct, to the point, so we
  906. 31:26tend to use fairly short phrases to
  907. 31:28describe things, not necessarily full
  908. 31:30words. So, while you might say string,
  909. 31:33technically in Python, what we're really
  910. 31:34talking about, these sequences of text,
  911. 31:36are technically strs. This is an actual
  912. 31:39type of data in a program. But, we'll
  913. 31:41soon see that there's other types of
  914. 31:43data in programs as well. In fact, let's
  915. 31:46see if we can't improve this in one
  916. 31:48other way. I like the progress we've
  917. 31:50made by keeping everything on the same
  918. 31:52line, "Hello, David."
  919. 31:54all on the same line. What more though
  920. 31:56could we do in terms of solving this
  921. 31:58problem? Well, it turns out that we
  922. 32:00didn't have to give up entirely with
  923. 32:02using print twice. Let me rewind a
  924. 32:04little bit and go back to that earlier
  925. 32:06version where I wasn't really sure how
  926. 32:08to solve this problem. So, I was using
  927. 32:10print once to print out just the hello
  928. 32:12and the space and the comma, and then I
  929. 32:14used print again to call uh to print
  930. 32:17name. That, strictly speaking, wasn't
  931. 32:19bad, but there was this visual side
  932. 32:21effect that I just didn't like. It just
  933. 32:23looked ugly to have these two lines of
  934. 32:25text separate from one another. But,
  935. 32:27there's another way to fix this.
  936. 32:29Clearly, it seems to be the case that
  937. 32:31the print function is automatically
  938. 32:34outputting a blank line. It's moving the
  939. 32:36cursor automatically for me to the next
  940. 32:38line because that's why I'm seeing hello
  941. 32:41on one line and David on the next and
  942. 32:43then my prompt, the dollar sign, on the
  943. 32:45line below that. So, print seems to be
  944. 32:47presuming automatically that you want it
  945. 32:49to move the cursor to the next line
  946. 32:51after you pass it some argument. But,
  947. 32:53you can override that behavior. Again,
  948. 32:55functions take arguments which influence
  949. 32:57their behavior. You just have to know
  950. 32:59what those arguments are. And it turns
  951. 33:01out that if we look at the documentation
  952. 33:04for Python's print function, we can
  953. 33:07actually look up at this URL here.
  954. 33:09docs.python.org is where all of Python's
  955. 33:12official documentation lies. If I poke
  956. 33:14around, I can find my way to more
  957. 33:16specifically this URL here where I can
  958. 33:18find all of the available functions in
  959. 33:20Python
  960. 33:22that and the documentation therefore.
  961. 33:24And if I go a little more precisely, I
  962. 33:26can even find specific documentation for
  963. 33:28the print function itself. And rather
  964. 33:30than pull that up in a browser, I'm
  965. 33:31going to go ahead and highlight just one
  966. 33:33line from that same URL
  967. 33:35which is this. And this is easily the
  968. 33:37most cryptic thing we've seen yet, but
  969. 33:40this is the official documentation for
  970. 33:42the print function. And one of the best
  971. 33:43things you can do when learning a
  972. 33:44programming language is honestly learn
  973. 33:46to read the documentation because truly
  974. 33:48all of the answers to your questions
  975. 33:50will in some way be there even though
  976. 33:53admittedly it's not always obvious. And
  977. 33:55I will say too, Python's documentation
  978. 33:57isn't necessarily the easiest thing
  979. 33:59especially for a first-time or novice
  980. 34:01programmer. It too just takes practice,
  981. 34:03so try not to get overwhelmed if you're
  982. 34:04not sure what you're looking at. But,
  983. 34:06let me walk you through this example.
  984. 34:08This again is a line of text from
  985. 34:09Python's official documentation for the
  986. 34:11print function. What this indicates as
  987. 34:13follows is this. The name of this
  988. 34:15function is of course print. Then
  989. 34:17there's a parenthesis over here and
  990. 34:18another close parenthesis way over
  991. 34:20there. Everything inside of those
  992. 34:22parenthesis are the arguments, the
  993. 34:24potential arguments to the function.
  994. 34:27However, when we're looking at these
  995. 34:29arguments in the documentation like
  996. 34:32this, there's technically a different
  997. 34:34term that we would use. These are
  998. 34:35technically the parameters to the
  999. 34:37function. So, when you're talking about
  1000. 34:39what you can pass to a function and what
  1001. 34:42those inputs are called, those are
  1002. 34:44parameters. When you actually use the
  1003. 34:46function and pass in values inside of
  1004. 34:49those parentheses, those inputs, those
  1005. 34:51values are arguments. So, we're talking
  1006. 34:54about the exact same thing. Parameters
  1007. 34:55and arguments are effectively the same
  1008. 34:57thing, but the terms you use from
  1009. 34:59looking at the problem from different
  1010. 35:00directions. When we're looking at what
  1011. 35:02the function can take versus what you're
  1012. 35:04actually passing into the function. So,
  1013. 35:06what does this imply? Well, this syntax
  1014. 35:08is pretty cryptic, but at the moment,
  1015. 35:10just know that an asterisk, a star, and
  1016. 35:13then the word objects means that the
  1017. 35:14print function can take any number of
  1018. 35:16objects. You can pass in zero strings of
  1019. 35:19text, one string like I did, two strings
  1020. 35:21like I did, or technically infinitely
  1021. 35:23many if you you really want, though that
  1022. 35:25code's not going to look very good.
  1023. 35:27After that, we see a comma, then we see
  1024. 35:29another parameter here called sep, short
  1025. 35:32for separator in English, and notice the
  1026. 35:34equal sign and the single quote space
  1027. 35:37single quote. So, quote unquote space. I
  1028. 35:40don't know what that is yet, but I I I
  1029. 35:42think we've seen a hint about it. Let's
  1030. 35:44focus though for a moment on this. The
  1031. 35:46print function takes another parameter
  1032. 35:48called end, and the default value of
  1033. 35:51that parameter is apparently, based on
  1034. 35:53this equal sign and these quotes,
  1035. 35:55backslash n.
  1036. 35:57And what is backslash n? If you'd like
  1037. 35:59to chime in in the chat. Anyone who's
  1038. 36:00programmed before has probably seen
  1039. 36:02this, though if you've never programmed
  1040. 36:03before, this might look quite cryptic.
  1041. 36:07Backslash n
  1042. 36:09means new line, and it's a way textually
  1043. 36:12of indicating if and when you want the
  1044. 36:14computer effectively to move the cursor
  1045. 36:16to the next line, create a new line of
  1046. 36:18text. And so, technically, if we read
  1047. 36:21into the documentation, we'll see more
  1048. 36:22detail on this,
  1049. 36:24the fact that there's a parameter called
  1050. 36:26end in the documentation for the print
  1051. 36:28function just means that by default this
  1052. 36:30print function is going to end every
  1053. 36:33line with backslash n. You don't
  1054. 36:35literally see backslash n, you see a new
  1055. 36:37line, you see the cursor moving to the
  1056. 36:39next line. Now, by that logic, let's
  1057. 36:41move backwards. Sep for separator. The
  1058. 36:44default value of separator is apparently
  1059. 36:47a single blank space. Well, where have
  1060. 36:50we seen that? Well, recall in earlier
  1061. 36:52example when I passed in not just one
  1062. 36:54but two arguments to the print function,
  1063. 36:57recall that they magically had a space
  1064. 36:59between them. In fact, they had that
  1065. 37:01space plus my own space and that's why I
  1066. 37:02deleted my space because at that point
  1067. 37:04it was extra. So, this just means that
  1068. 37:06when you pass multiple arguments to
  1069. 37:08print, by default they're going to be
  1070. 37:09separated by a single space. By default
  1071. 37:12when you pass arguments to print, it's
  1072. 37:14the whole thing is going to be ended
  1073. 37:16with a new line. Now, just by knowing
  1074. 37:18this and let me literally wave my hand
  1075. 37:20at the rest of the documentation for
  1076. 37:22another day. There's more things that
  1077. 37:24print can do, but we're going to focus
  1078. 37:25just on sep and on end, let's see if we
  1079. 37:28can't leverage this now to solve that
  1080. 37:29original problem. The original problem
  1081. 37:31was this, I don't like how hello {comma}
  1082. 37:33David is on two different lines. Well,
  1083. 37:35that's happening again because print is
  1084. 37:37automatically printing out a new line,
  1085. 37:39so let's tell it not to do that. Let's
  1086. 37:42tell it by passing a second argument to
  1087. 37:45the first use of print to say end equals
  1088. 37:49{quote} unquote not backslash n, which
  1089. 37:52is the default automatically, let's make
  1090. 37:55it {quote} unquote nothing else. Let's
  1091. 37:58override the default value, so there is
  1092. 38:00no new line, there's literally nothing
  1093. 38:02there. And let's see what happens. Let
  1094. 38:05me now go down to my terminal window and
  1095. 38:07clear it, and I'm going to run python of
  1096. 38:09hello.py enter. I'm going to type in my
  1097. 38:11name David and I think now everything's
  1098. 38:14going to stay on the same line because,
  1099. 38:16and it did,
  1100. 38:17this line here five is is to print out
  1101. 38:20hello {comma} space, but then nothing at
  1102. 38:23the end of it because I changed it to be
  1103. 38:25{quote unquote}. The second line is
  1104. 38:27going to print the name, David, or
  1105. 38:29whatever the human's name is, and it
  1106. 38:31will move the cursor to the next line
  1107. 38:32because I didn't override the value of
  1108. 38:35end there. Just to see this more
  1109. 38:37explicitly, if you do something cryptic
  1110. 38:39like, "Well, I have no idea what's going
  1111. 38:41on. Let me just put in temporarily three
  1112. 38:42question marks here." We'll see the
  1113. 38:44results of this, too. Let me go back
  1114. 38:46down to my terminal window, run python
  1115. 38:48of hello.py What's your name, David? And
  1116. 38:50now you see literally really ugly
  1117. 38:53output, but you see literally what's
  1118. 38:55going on. Hello, {comma} space, then
  1119. 38:57three question marks, and that print
  1120. 39:00statement, and then you see d a v i d.
  1121. 39:03So, not a good outcome, but it
  1122. 39:06demonstrates just how much control we
  1123. 39:07have here, too. And let me rewind
  1124. 39:09further. Recall that in our other
  1125. 39:11version of this,
  1126. 39:13when I passed in hello, {comma} and
  1127. 39:15name, they were separated by a single
  1128. 39:18space. So, python of hello.py
  1129. 39:20d a v i d {enter} That just worked.
  1130. 39:23Well, what if we override the value of
  1131. 39:26sep for separator instead of being one
  1132. 39:29space, we could say something like uh
  1133. 39:33{question mark} {question mark}
  1134. 39:34{question mark} just to wrap our minds
  1135. 39:35around what's going on there. Let me now
  1136. 39:37do python of hello.py d a v i d {enter}
  1137. 39:41and you see two. These two inputs,
  1138. 39:43hello, {comma} and the name are now
  1139. 39:46separated in an ugly way by three
  1140. 39:48question marks because I've overridden
  1141. 39:50the default behavior of sep. And even
  1142. 39:52though the documentation uses single
  1143. 39:54quotes, I've been in the habit of using
  1144. 39:55double quotes. In Python, you can use
  1145. 39:57either. Strictly speaking, it doesn't
  1146. 39:59matter, but you should be consistent,
  1147. 40:01and I generally always use double
  1148. 40:02quotes. Python's documentation, though,
  1149. 40:04always uses single quotes.
  1150. 40:08Questions now on these types of
  1151. 40:11parameters. And allow me to propose that
  1152. 40:13we give these an official name. Up until
  1153. 40:15now, when we've been passing values to
  1154. 40:18print, those are called positional
  1155. 40:20parameters. Positional in the sense that
  1156. 40:22the first thing you pass to print gets
  1157. 40:23printed first. The second thing you pass
  1158. 40:25to print after a comma gets printed
  1159. 40:27second, and so forth. But, there's also
  1160. 40:29these things we've now seen called named
  1161. 40:31parameters, named SEP, separator, or
  1162. 40:35end, END, for the line ending. Those are
  1163. 40:37named parameters because, one, they're
  1164. 40:39optional and you can pass them in at the
  1165. 40:42end of your print statement, but you can
  1166. 40:44also call uh use them by name.
  1167. 40:48This may be a weird question, but I was
  1168. 40:49wondering uh what if someone wants to uh
  1169. 40:52like add actually quote quotation marks
  1170. 40:56within the quotation marks?
  1171. 40:57Yeah, I like how you think. This is what
  1172. 41:00we would call a corner case, right? Just
  1173. 41:02when we've made Right, this is This is
  1174. 41:03all sounding great, at least as as
  1175. 41:05programming goes, but wait a minute,
  1176. 41:06what if you want to print a quote?
  1177. 41:08That's a really good question. Well,
  1178. 41:09let's see if we can't figure this out.
  1179. 41:11Suppose that I want to print out not
  1180. 41:14just the user's name. Let me simplify
  1181. 41:16this further. Let me go ahead and get
  1182. 41:17rid of all of this, and let me just say
  1183. 41:19something like hello, um
  1184. 41:24maybe I'm being a little sarcastic here.
  1185. 41:26Hello, friend, you know, in that kind of
  1186. 41:29tone. Well, this is not going to work,
  1187. 41:31actually, because you are trying to use
  1188. 41:33quotes to be like friend in finger
  1189. 41:35quotes, but you're also trying to end
  1190. 41:37the sentence. And if I try running this,
  1191. 41:38let's do this. python ifhello.py, you'll
  1192. 41:41see that this is just invalid syntax.
  1193. 41:43Perhaps you forgot a comma. And this is
  1194. 41:44actually a bit annoying. Sometimes the
  1195. 41:46error messages you see are misleading.
  1196. 41:48Like, the computer the language doesn't
  1197. 41:50really know what's going on, so it gives
  1198. 41:52its best guess, but it's not necessarily
  1199. 41:54correct. But, I can solve this problem
  1200. 41:56in a couple of ways. I can do this. I
  1201. 41:59can change my outermost quotes to single
  1202. 42:01quotes, cuz recall a moment again I said
  1203. 42:03you could use double quotes or single
  1204. 42:05quotes so long as you're consistent. So,
  1205. 42:07that's fine. If you use single quotes on
  1206. 42:08the outside, you can then use double
  1207. 42:10quotes on the inside, and you'll see
  1208. 42:12them literally. So, for instance, if I
  1209. 42:14run python of hello.py, there we go.
  1210. 42:16Hello, friend. But, there's another way.
  1211. 42:19If you insist on using double quotes, as
  1212. 42:22you might want to just be consistent,
  1213. 42:24you can also use that backslash
  1214. 42:26character again. We saw the backslash n
  1215. 42:29a moment ago, and that meant we don't
  1216. 42:31want a literal n to be in the output. We
  1217. 42:33wanted a new line. So, the backslash
  1218. 42:35actually represents what's called an
  1219. 42:36escape character. An escape character is
  1220. 42:39one that you can't just type necessarily
  1221. 42:41once on your keyboard. You need to
  1222. 42:43express it with multiple characters. So,
  1223. 42:45I can actually put backslashes in front
  1224. 42:47of these inner
  1225. 42:49double quotes, so that the computer
  1226. 42:51realizes, "Oh, wait a minute. Those
  1227. 42:52aren't literal Those aren't quotes that
  1228. 42:54finish or start the thought. They're
  1229. 42:56literal quotes." So, now let me go back
  1230. 42:58to my terminal window, run python of
  1231. 43:00hello.py, enter, and now it's working as
  1232. 43:03well. So, escaping is a general
  1233. 43:05technique that allows us to do that,
  1234. 43:07too.
  1235. 43:08And if I may, let me rewind now on these
  1236. 43:11examples and go back to where we left
  1237. 43:13off with my code. I'm just undoing all
  1238. 43:15of that because I want to get back to
  1239. 43:17the point, ultimately, of specifying now
  1240. 43:20a a final way of solving this problem.
  1241. 43:23Well, it turns out that we have yet
  1242. 43:25another way we can solve this problem,
  1243. 43:27which is perhaps the most frequently
  1244. 43:30done now, or at least the most elegant
  1245. 43:33when it comes to setting us up for
  1246. 43:35longer and longer uh uses of strings.
  1247. 43:38You can use a relatively new feature of
  1248. 43:41Python that allows you to do this. You
  1249. 43:43can literally put not the name of the
  1250. 43:45variable like that in your string,
  1251. 43:47because we already saw this is wrong,
  1252. 43:48right? If you do this, you will
  1253. 43:50literally see hello {comma} name. But,
  1254. 43:52what if I do this? What if I put curly
  1255. 43:54braces or curly brackets around the
  1256. 43:57variable's name? Notice VS Code is
  1257. 43:58actually very subtly changing the color
  1258. 44:00of it. So, VS Code knows something
  1259. 44:01interesting is going on here. Let me run
  1260. 44:04this program, but I'm I'm done yet.
  1261. 44:06Python of hello.py, enter. d a v i d,
  1262. 44:08enter. Okay, obviously not what I want,
  1263. 44:11but I need to tell Python that this is a
  1264. 44:13special string. This is what we're going
  1265. 44:15to call a format string or an f string,
  1266. 44:18a relatively new feature of Python in
  1267. 44:20the past few years that tells Python to
  1268. 44:23actually format stuff in the string in a
  1269. 44:25special way. And the symbol by which you
  1270. 44:28do this, this is a little weird, but
  1271. 44:29this is what the world chose, if you put
  1272. 44:31a f at the beginning of the string right
  1273. 44:35before the first quote mark, that's a
  1274. 44:37clue to Python that ooh, this is a
  1275. 44:40special string. Let me format this in a
  1276. 44:42special way for you. Let me now rerun
  1277. 44:44the program. Python hello.py, enter. d a
  1278. 44:47v i d, enter. And now we see the goal
  1279. 44:50this whole time, hello, {comma} David.
  1280. 44:52We don't start with this way because I
  1281. 44:54think if we did this the first way,
  1282. 44:55you'd be like, why are we doing this?
  1283. 44:57What are all these magical symbols? But
  1284. 44:59this is just yet another way to solve
  1285. 45:01the same problem. But let me propose
  1286. 45:03that we consider now yet other things we
  1287. 45:05can do with strings. And it turns out
  1288. 45:07that even as we've been doing
  1289. 45:09some relatively simple operations here,
  1290. 45:11we've generally been trusting that the
  1291. 45:13user's going to cooperate. And that is
  1292. 45:15to say that they're going to actually
  1293. 45:16type in what we want them to type. Now,
  1294. 45:18just because they type a string though,
  1295. 45:20doesn't mean it's going to look the way
  1296. 45:21we want. You and I honestly as humans
  1297. 45:23are actually in the habit on websites
  1298. 45:25and apps of like accidentally hitting
  1299. 45:27the space bar a lot, either at the
  1300. 45:29beginning of our input or at the end,
  1301. 45:31maybe because the space bar tends to be
  1302. 45:32so big. It's pretty common to get
  1303. 45:34accidental spaces before or after some
  1304. 45:37user's input. You and I are definitely
  1305. 45:39in the habit of not necessarily
  1306. 45:40capitalizing words like we should. If
  1307. 45:42we're sending text messages, we're
  1308. 45:43probably being a little quick and just
  1309. 45:45sending everything in lower case, for
  1310. 45:46instance, if that's your style, if your
  1311. 45:48phone's not fixing it for you. Maybe in
  1312. 45:50a formal letter you would capitalize
  1313. 45:51things properly, but you and I as
  1314. 45:53humans, we can't really be trusted to
  1315. 45:54type things in a nice way necessarily
  1316. 45:56when using some piece of software, be it
  1317. 45:58an app or a website or something else.
  1318. 46:00But it turns out that strings themselves
  1319. 46:04come with a lot of built-in
  1320. 46:05functionality, and you can see all of
  1321. 46:07that in Python's own documentation here.
  1322. 46:09The string data type that we've been
  1323. 46:11talking about comes with a lot of
  1324. 46:13functionality built in. That means that
  1325. 46:15we can manipulate the user's input to do
  1326. 46:17more than just join it with something
  1327. 46:19else like hello. We can actually clean
  1328. 46:21it up or reformat it in a way that
  1329. 46:24hopefully looks a little better for us.
  1330. 46:26So, let me go back to my code here, and
  1331. 46:28let me just demonstrate what might
  1332. 46:30happen if a user doesn't cooperate. If I
  1333. 46:32go ahead here and run python of
  1334. 46:33hello.py, enter, let me just sloppily
  1335. 46:36hit the spacebar a few too many times.
  1336. 46:38Why? I just wasn't paying attention. And
  1337. 46:41now I'm going to type in my name d a v i
  1338. 46:42d, and I don't know. I hit the spacebar
  1339. 46:44a couple more times. Like it's kind of a
  1340. 46:46mess. It's all lowercase. That's not
  1341. 46:48going to necessarily look grammatically
  1342. 46:49right. It's got spaces here and here.
  1343. 46:51The program is going to print exactly
  1344. 46:53that, and that looks really bad. At
  1345. 46:55least if we're prioritizing aesthetics
  1346. 46:57and grammar, like why are there so many
  1347. 46:59spaces after the comma? This is not a
  1348. 47:00very nice way to greet your users. But,
  1349. 47:03we can clean this up. It turns out that
  1350. 47:05built into strings, which again is this
  1351. 47:09data type, so to speak, this type of
  1352. 47:11data in Python, is the ability to
  1353. 47:13actually do things to that string. So,
  1354. 47:15let me do this. I can actually go ahead
  1355. 47:18and do something like this.
  1356. 47:19Uh name equals name.strip.
  1357. 47:23And what does this do? Remove white
  1358. 47:26space from string.
  1359. 47:30And what do I mean by this? Well, on the
  1360. 47:32right-hand side, notice I've written the
  1361. 47:34variable name called name.
  1362. 47:36I've then used a period or a dot, and
  1363. 47:39then I seem to be doing what's a
  1364. 47:41function, right? Anytime we've seen this
  1365. 47:43function thus far, we see it's the the
  1366. 47:45function's name, print or input, then we
  1367. 47:47see a parenthesis, then another
  1368. 47:48parenthesis, and that's exactly what I
  1369. 47:49see here. But, I'm using this function a
  1370. 47:51little differently. Technically, this
  1371. 47:53function is in this context called a
  1372. 47:55method. And what do I mean by that?
  1373. 47:57Well, if name is a string, aka stir,
  1374. 48:01well, it turns out according to the
  1375. 48:02documentation, there's a lot of
  1376. 48:03functions that come with strings in
  1377. 48:07Python. And you can access that
  1378. 48:08functionality by using the name of a
  1379. 48:10string, like literally name here, then a
  1380. 48:13period, then the name of the function,
  1381. 48:15and then an open parenthesis and a close
  1382. 48:17parenthesis. Maybe some arguments inside
  1383. 48:19of those parentheses, but in this case,
  1384. 48:21it doesn't need any arguments. I just
  1385. 48:23want to strip the space from the left
  1386. 48:25and the space from the right of the
  1387. 48:27user's input. But, that's not enough. I
  1388. 48:29want to remember that I've stripped off
  1389. 48:31that white space on the left and the
  1390. 48:32right. So, I'm going to use the equal
  1391. 48:34sign again here. And notice that just as
  1392. 48:36before, this doesn't mean equality, this
  1393. 48:38means assignment from right to left. So,
  1394. 48:41when this line of code here, name.strip,
  1395. 48:44returns to me,
  1396. 48:45aka a return value, it will return the
  1397. 48:48same thing that the user typed in, but
  1398. 48:50with no more white space to the left or
  1399. 48:51to the white
  1400. 48:53to the right. So, then the equal sign
  1401. 48:55assignment is going to copy that value
  1402. 48:57from the right to the left, thereby
  1403. 49:00updating the value inside of my name
  1404. 49:03variable. So, you can not only assign
  1405. 49:06values to variables, you can absolutely
  1406. 49:08change the value of variables by just
  1407. 49:10using the assignment operator, the equal
  1408. 49:12sign again and again and again. And it
  1409. 49:14will just keep copying from right to
  1410. 49:15left whatever the new value should be.
  1411. 49:17So, now, if I rerun this program, python
  1412. 49:20of hello.py,
  1413. 49:22enter,
  1414. 49:23I have d a v i Whoop, let's do it again.
  1415. 49:25Space space space space space d a v i d
  1416. 49:28in all lower case, space space, enter,
  1417. 49:30it's better. It hasn't fixed my
  1418. 49:32capitalization, so I'm still being a
  1419. 49:34little sloppy with the first d, but it
  1420. 49:36has stripped off all of that extra
  1421. 49:38space. Super minor detail, right? Like,
  1422. 49:41this isn't all that exciting, but it
  1423. 49:43just speaks to the power of what you can
  1424. 49:45do with with just a single line of code.
  1425. 49:47Now, what else can I do here? Well, I
  1426. 49:49could capitalize the user's input. Let
  1427. 49:51me go ahead and try this. It turns out
  1428. 49:54that I could also
  1429. 49:55uh do this. name.capitalize.
  1430. 49:59So, let me go ahead and capitalize
  1431. 50:02uh user's name. And again, I'm making
  1432. 50:04comments, and there's no one right way
  1433. 50:05to write the comments. I'm just using
  1434. 50:07some short English phrases here to
  1435. 50:08remind myself of what I'm doing. What's
  1436. 50:10now going on here? Well, let me go ahead
  1437. 50:12and run python of hello.py. Enter. Uh
  1438. 50:15space space space space space d a v i d
  1439. 50:17space space enter.
  1440. 50:19Okay. Now it's looking prettier, right?
  1441. 50:21No matter how the user typed in their
  1442. 50:22name, even a little sloppily, I'm now
  1443. 50:25fixing that. But, let's let's try
  1444. 50:27something. I'm getting a little curious
  1445. 50:28here. How about this? Uh space space
  1446. 50:30space space space d a v i d space
  1447. 50:33mailin. I'll use my last name now.
  1448. 50:35Enter.
  1449. 50:36Okay. So, ironically, capitalize is not
  1450. 50:39really capitalizing everything we want.
  1451. 50:42It's clearly capitalizing what? Just the
  1452. 50:44very first letter. So, it turns out that
  1453. 50:46again, there's other functions in Python
  1454. 50:48that come with strings. And if we poke
  1455. 50:50around the documentation, scrolling
  1456. 50:52through a URL like that, I bet we'll
  1457. 50:55find another solution, one of which is
  1458. 50:57actually this.
  1459. 50:58Let's actually change this to title.
  1460. 51:01There's yet another function that come
  1461. 51:03with strings called title that do
  1462. 51:05title-based capitalization, just like a
  1463. 51:07book or a person's name, capitalizing
  1464. 51:09the first letter of each word. And this
  1465. 51:12is just going to do a little more work
  1466. 51:13for us. So, let's go ahead and run this.
  1467. 51:15And as an aside, I'm kind of tired now
  1468. 51:17at this point of typing python python
  1469. 51:20python all the time. It turns out that
  1470. 51:22when using a command line interface like
  1471. 51:25this, you can actually go back through
  1472. 51:26all of your old commands. What I just
  1473. 51:28did a moment ago was I hit the up arrow.
  1474. 51:30That immediately goes back through my
  1475. 51:32history of all of the commands I've ever
  1476. 51:34typed. So, this is just a faster way now
  1477. 51:36for me to repeat myself than typing
  1478. 51:38everything manually. Let me go ahead and
  1479. 51:40hit enter. Uh space space space space
  1480. 51:42space d a v i d mailin space space all
  1481. 51:45lower case. Enter. Now it's it's looking
  1482. 51:49better. Now I've capitalized things and
  1483. 51:51cleaned things up. But what about my
  1484. 51:52code? I've got like eight lines of code
  1485. 51:54now, four of which are comments, four of
  1486. 51:56which are actual code. Do I really need
  1487. 51:58this much? Well, not necessarily. Watch
  1488. 52:01what I can also do in Python. Let me not
  1489. 52:04bother capitalizing the user's name
  1490. 52:05separately. Let me say this, and capital
  1491. 52:10capitalize user's name. I can chain
  1492. 52:13these functions together. I can add
  1493. 52:15title to the end of this. And now what's
  1494. 52:18happening? Well, again, with a line of
  1495. 52:20code like this, you first focus on
  1496. 52:22what's to the right of the equal sign.
  1497. 52:24Then we'll get to the left of the equal
  1498. 52:26sign. What's on the right of the equal
  1499. 52:27sign? This line here. Well, what does
  1500. 52:29this mean? Get the value of the name
  1501. 52:32variable, like d a v i d space m a l a
  1502. 52:34n,
  1503. 52:35then strip off the white space on the
  1504. 52:37left and the right. That is going to
  1505. 52:39return a value. It's going to return d a
  1506. 52:41v i d space m a l a n without any white
  1507. 52:44space to the left or right. What do you
  1508. 52:46want to do with that return value? You
  1509. 52:48want Python to title case it. That is go
  1510. 52:50through every word in that resulting
  1511. 52:53string and fix the first letter of the
  1512. 52:55first word, the first letter of the
  1513. 52:57second word, and so forth. And then, now
  1514. 52:59we can finish our thought, copy the
  1515. 53:00whole thing
  1516. 53:02from right to left into that same name
  1517. 53:05variable. And you know what? I can take
  1518. 53:06this even one step further. Why don't we
  1519. 53:09go ahead and do this if we want? Let me
  1520. 53:11get rid of all that, and let me just do
  1521. 53:14strip and title all on that first line.
  1522. 53:18And now we've gone from like eight lines
  1523. 53:19of code to four. It's a lot tighter,
  1524. 53:22it's a lot neater, and even though
  1525. 53:24reasonable people might disagree, it's
  1526. 53:26arguably better
  1527. 53:28because it's just easier to read. Fewer
  1528. 53:30lines of code, fewer opportunities for
  1529. 53:31mistakes. It just allows me to move on
  1530. 53:34with my next problem to solve.
  1531. 53:37All right, let me pause here and see if
  1532. 53:38there's any questions on these methods.
  1533. 53:41A method is a function that's built in
  1534. 53:44to a a type of value like these
  1535. 53:46functions are
  1536. 53:47or on F strings, which we saw a moment
  1537. 53:49ago.
  1538. 53:50Yes, hi. Thanks, David. Um so, is there
  1539. 53:52a way to remove the spaces between
  1540. 53:55the spaces that I might have added?
  1541. 53:57Short answer, no. If you read the
  1542. 53:59documentation at that same URL earlier,
  1543. 54:00you'll see that strip removes from the
  1544. 54:02left and the right, but not in between.
  1545. 54:04In fact, there's two other functions
  1546. 54:06that come with strings. One's called L
  1547. 54:08strip, the other's called R strip that
  1548. 54:10allow you to do one or the other. If we
  1549. 54:12want to start getting rid of space in
  1550. 54:14the middle, we're going to have to do a
  1551. 54:15different trick altogether.
  1552. 54:17How many functions can we combine like
  1553. 54:20this? {dot} strip {dot} title you have
  1554. 54:22combined. So, how many we can combine?
  1555. 54:24Yeah, a really good question.
  1556. 54:27Technically, as many as you want, but at
  1557. 54:29some point your code's going to start to
  1558. 54:31look really, really bad, right? Because
  1559. 54:32the line of code's going to get really,
  1560. 54:34really long. It's eventually going to
  1561. 54:35maybe wrap around again and again. So,
  1562. 54:38at some point you just kind of say like,
  1563. 54:40"Uh-uh, that's too many." and you start
  1564. 54:41breaking it up into multiple lines like
  1565. 54:43I did, maybe reassigning the value to
  1566. 54:46the variable as needed. And this is
  1567. 54:48actually a good question. If I can pivot
  1568. 54:49Divya off your question, I mean, what do
  1569. 54:52people think? If we could go ahead and
  1570. 54:53put everyone's hands down for a moment,
  1571. 54:55let me ask this. Is the way I've done
  1572. 54:59this now with strip and title and input
  1573. 55:03all in the same line better than my
  1574. 55:06previous approach? In Zoom, you can use
  1575. 55:08the yes icon or the no icon. If you
  1576. 55:11think this version is better, say yes.
  1577. 55:14If you think this previous version was
  1578. 55:16better,
  1579. 55:18for instance, this one here, where we
  1580. 55:20had everything broken out, say no.
  1581. 55:23And then we'll see why in just a moment.
  1582. 55:26I proposed earlier that reasonable
  1583. 55:28people can disagree, and that's
  1584. 55:30absolutely the case.
  1585. 55:32Doing it one way or the other isn't
  1586. 55:34necessarily best, at least if you can
  1587. 55:36justify it. Let me go back to the most
  1588. 55:38recent version here.
  1589. 55:41All right. So, we're seeing a lot of
  1590. 55:43yeses and a lot of no's. Why don't we go
  1591. 55:45ahead and and call on one of the yeses
  1592. 55:47if we could? Someone who's voting yes,
  1593. 55:50why do you think the current version of
  1594. 55:52this code is indeed better than the
  1595. 55:54previous longer version of the code?
  1596. 55:57Uh I think it's more readable, so I can
  1597. 55:59say, "Hey, this is the name fun This is
  1598. 56:01the name variable.
  1599. 56:03It gets some input and then remove the
  1600. 56:05space and give it a title and there you
  1601. 56:07go. You have a hello name."
  1602. 56:09Yeah, I think that's pretty reasonable.
  1603. 56:10It's very readable, at least if you're
  1604. 56:12in the habit, as you are in English, of
  1605. 56:13reading left to right. It just kind of
  1606. 56:15flows very naturally as a result. The
  1607. 56:17line is not really that long. It's
  1608. 56:19certainly fitting nicely onto the
  1609. 56:20screen, so I think that's a good
  1610. 56:21argument. How about a counterpoint
  1611. 56:22though? Someone who voted no, if we
  1612. 56:24could call on someone who thinks this is
  1613. 56:26worse.
  1614. 56:27Um because it's not uh readable at all.
  1615. 56:31I seems like
  1616. 56:32uh
  1617. 56:33uh
  1618. 56:34It's a very long line, so I think it's
  1619. 56:37better to separate.
  1620. 56:39Yeah, I I think that's persuasive, too,
  1621. 56:41right? It's getting a little longer and
  1622. 56:43even though my sentence here, "What's
  1623. 56:45your name?" is relatively short, you
  1624. 56:47could imagine that this could get even
  1625. 56:48uglier quickly. If I were asking a
  1626. 56:50longer question of the user, that's
  1627. 56:52going to make this line of code even
  1628. 56:53longer and therefore less readable. It
  1629. 56:56might be less obvious to me or my
  1630. 56:57colleagues that I am calling strip or
  1631. 57:00that I am calling title. It might be
  1632. 57:01kind of a unexpected surprise. So, I
  1633. 57:04think that's reasonable, too. In short,
  1634. 57:05there is no right answer here and in
  1635. 57:07fact, part of the process of getting
  1636. 57:09better at programming is getting your
  1637. 57:11own sense of style or working for a
  1638. 57:13company where they might prescribe which
  1639. 57:15way is better than the other because
  1640. 57:16they just want everyone doing the same
  1641. 57:18thing, even though reasonable people
  1642. 57:20might uh
  1643. 57:21disagree. Ultimately though, so long as
  1644. 57:23you have what's a pretty good argument
  1645. 57:25in favor of one way or the other. Like
  1646. 57:28ultimately, that's what's important. If
  1647. 57:29you're just doing things cuz you don't
  1648. 57:30really know which one's better, that's
  1649. 57:32not great. But if if and when you start
  1650. 57:34to acquire opinions and if your boss, if
  1651. 57:36your teacher, if your colleague, your
  1652. 57:38friend can challenge you and say, "Wait,
  1653. 57:39why did you do it like this?" They might
  1654. 57:41not agree with you, but at least have an
  1655. 57:43answer and that should be sufficiently
  1656. 57:45persuasive in general. Now, strings come
  1657. 57:47with a whole bunch of other methods as
  1658. 57:49well, among which is one called split,
  1659. 57:51which can, as the name suggests, split a
  1660. 57:53string into multiple smaller substrings,
  1661. 57:56so to speak. For instance, if the human
  1662. 57:58here is in the habit of typing in their
  1663. 57:59first name, then a space, and then their
  1664. 58:01last name, and you want to go ahead and
  1665. 58:03greet them only by first name, well, we
  1666. 58:05could actually leverage that single
  1667. 58:06space between the first name and last
  1668. 58:08name and split that string into two
  1669. 58:10smaller substrings. How can we do this?
  1670. 58:12Well, let me go ahead and in between
  1671. 58:14these lines proactively comment that
  1672. 58:15we're about to split user's name into
  1673. 58:19first name and last name, and then let's
  1674. 58:23go ahead and take that name variable,
  1675. 58:24which currently contains something like,
  1676. 58:26presumably, David space Maylan, and let
  1677. 58:28me go ahead and call split and pass in
  1678. 58:31as the argument to split a single white
  1679. 58:33space, thereby indicating that I indeed
  1680. 58:35want to split on that character. Now, it
  1681. 58:37turns out split's going to return a
  1682. 58:39sequence of values, ideally a first name
  1683. 58:41and then a last name, and we can
  1684. 58:43actually in Python assign both of those
  1685. 58:44values from that sequence at once to
  1686. 58:47some variables. For instance, first
  1687. 58:49{comma} last equals, and that's going to
  1688. 58:51have the effect from right to left of
  1689. 58:53putting the first such value in the
  1690. 58:54first variable, the second such value in
  1691. 58:56the second variable. So, now on my last
  1692. 58:59line of code, I can go in and say hello
  1693. 59:01not to the full name, something like
  1694. 59:03David Maylan, I can just say hello
  1695. 59:05{comma} first. All right, let's go ahead
  1696. 59:07and clear my terminal window, run Python
  1697. 59:09of hello.py and hit enter. I won't
  1698. 59:11bother with any leading white space this
  1699. 59:12time, but let me go ahead and type in
  1700. 59:14David space Maylan, and crossing my
  1701. 59:17fingers as usual, hello David is what we
  1702. 59:20now see.
  1703. 59:21All right. So, we've seen so much so
  1704. 59:23many examples thus far involving
  1705. 59:25strings, but certainly programs uh and
  1706. 59:28programming languages can manipulate
  1707. 59:30other types of data as well. Let's go
  1708. 59:32ahead and transition then to another
  1709. 59:34very common type of data in Python and
  1710. 59:36programming more generally, namely
  1711. 59:38integers, otherwise known in Python as
  1712. 59:40int, i n t. So, just as str, s t r is
  1713. 59:43short for string, so is int in Python
  1714. 59:46short for integer. Well, what's an
  1715. 59:47integer? Well, just like in math, it's a
  1716. 59:50number like -2, -1, 0, 1, 2 and all the
  1717. 59:54way toward negative infinity, all the
  1718. 59:55way toward positive infinity, but
  1719. 59:56there's no decimal point in an integer.
  1720. 59:59It's just a number like -2, -1, 0, 1,
  1721. 1:00:02and 2 onward. That's an int. Of course,
  1722. 1:00:05in the world of mathematics, there's
  1723. 1:00:08lots of symbols that we use and we've
  1724. 1:00:09seen plus before, although we used it
  1725. 1:00:11for a different purpose, but Python
  1726. 1:00:13supports these symbols and more. And
  1727. 1:00:15Python allows you to add numbers
  1728. 1:00:17together, plus, subtract numbers,
  1729. 1:00:19multiply numbers, divide numbers, and
  1730. 1:00:21the only one here that might look a
  1731. 1:00:22little strange to people or unfamiliar
  1732. 1:00:25is this percent sign, but it doesn't
  1733. 1:00:27mean percent in this context. If you use
  1734. 1:00:29a single percent sign in a Python
  1735. 1:00:31program, that's actually the so-called
  1736. 1:00:33modulo operator, the operator that
  1737. 1:00:35allows you to take the remainder after
  1738. 1:00:37dividing one number by another. So,
  1739. 1:00:40we'll see examples of that before long,
  1740. 1:00:41but the first four of these are perhaps
  1741. 1:00:43quite quite familiar. Well, it turns out
  1742. 1:00:46that in Python, you cannot necessarily
  1743. 1:00:49you don't necessarily have to keep
  1744. 1:00:50writing code in a file like hello.py and
  1745. 1:00:54then running it in a terminal window.
  1746. 1:00:56One of the features that many people
  1747. 1:00:58like about Python is that it supports
  1748. 1:01:00this so-called interactive mode. Like,
  1749. 1:01:02you can start writing Python code and
  1750. 1:01:04immediately execute each of those lines
  1751. 1:01:07interactively. If especially if you
  1752. 1:01:09don't care about saving all of your
  1753. 1:01:10lines of code, you just want to execute
  1754. 1:01:12code and get back some answers. So, for
  1755. 1:01:14instance, let me go back to VS Code here
  1756. 1:01:16and let me close hello.py and let me
  1757. 1:01:18click on the little triangle over here
  1758. 1:01:20in my terminal window just to make it
  1759. 1:01:22much bigger just temporarily for a
  1760. 1:01:24moment. So, I'm not creating any dot py
  1761. 1:01:26file now. I'm just going to run Python
  1762. 1:01:29by itself at my prompt. And you'll see
  1763. 1:01:32when I do this, I get some cryptic
  1764. 1:01:34looking output and the date and time at
  1765. 1:01:36which the program was last updated and
  1766. 1:01:38so forth, but I ultimately get three
  1767. 1:01:40triple uh brackets like this. This is
  1768. 1:01:43the interactive mode for Python. So, I'm
  1769. 1:01:47running the Python interpreter and
  1770. 1:01:49anytime I type
  1771. 1:01:51uh a a line of code in the interpreter,
  1772. 1:01:52it's going to execute it immediately. I
  1773. 1:01:54don't have to keep running Python again
  1774. 1:01:56and again. It's as though in the human
  1775. 1:01:57world, if you were standing next to a
  1776. 1:01:59human who speaks some other language and
  1777. 1:02:01you're just having a conversation with
  1778. 1:02:03them back and forth, it's all happening
  1779. 1:02:05the translation immediately. So, what
  1780. 1:02:07might I do in interactive mode? Well, I
  1781. 1:02:09could do something like 1 + 1, enter.
  1782. 1:02:11That's actually code. All right, you
  1783. 1:02:13might not think of it as code, but if
  1784. 1:02:15you know a bit of arithmetic and you
  1785. 1:02:16know uh numbers and you know plus,
  1786. 1:02:19that's valid Python code. And you can
  1787. 1:02:21use Python really as a fancy calculator.
  1788. 1:02:23But I could do other things too. If I
  1789. 1:02:24want to print to myself, "Hello, {comma}
  1790. 1:02:27world." I can also print out that line
  1791. 1:02:29of code there too. Hello, world. So,
  1792. 1:02:31it's interactive in the sense that the
  1793. 1:02:33moment you execute a line of code, boom,
  1794. 1:02:35you see the result. We're generally not
  1795. 1:02:37going to do that because at least when
  1796. 1:02:39teaching the language, we tend to want
  1797. 1:02:41to do things incrementally and we want
  1798. 1:02:43you to be able to see where it is we
  1799. 1:02:45came from and we want to be able to try
  1800. 1:02:46things again and again, especially if we
  1801. 1:02:48make mistakes. But know that this is
  1802. 1:02:50indeed a feature of Python, this
  1803. 1:02:51so-called interactive mode. But let's
  1804. 1:02:54focus for a moment now not just on that
  1805. 1:02:56interactivity, but really on the fact
  1806. 1:02:57that Python apparently supports integers
  1807. 1:03:00and mathematics and some of those basic
  1808. 1:03:02operations. And let's see if we can't
  1809. 1:03:03make maybe our our own little
  1810. 1:03:05calculator. So, let me go ahead and open
  1811. 1:03:07up VS Code again and I'm going to shrink
  1812. 1:03:09down my terminal window and I'm going to
  1813. 1:03:11create a new file called calculator.py.
  1814. 1:03:14So, to do that recall, I can type code
  1815. 1:03:16down here and the name of the file I
  1816. 1:03:19want to create {dot} py. Enter. That
  1817. 1:03:21gives me a new tab up top, so I have
  1818. 1:03:24already closed hello.py. I'm now in
  1819. 1:03:25calculator.py and let's just make a
  1820. 1:03:27simple calculator that does some
  1821. 1:03:29addition for me. But, I'm going to do it
  1822. 1:03:31in a file so that we can iterate on this
  1823. 1:03:33and make changes for better or for worse
  1824. 1:03:35over time. Let me go ahead and first
  1825. 1:03:37declare a couple of variables. I'm going
  1826. 1:03:39to do the mathematical thing of calling
  1827. 1:03:41my first variable X,
  1828. 1:03:42my second variable Y, and then I'm going
  1829. 1:03:45to give myself a third variable Z equals
  1830. 1:03:47X plus Y, and then I'm going to go ahead
  1831. 1:03:49and print out Z. Now, this program,
  1832. 1:03:52admittedly, not very exciting or
  1833. 1:03:54interesting. In fact, it's a little less
  1834. 1:03:56interesting than printing stuff on the
  1835. 1:03:57screen like before with strings, but
  1836. 1:04:00we'll build on this and see what other
  1837. 1:04:01features exist in Python that we can
  1838. 1:04:03leverage. So, hopefully, if Python knows
  1839. 1:04:05its math as well as I do, when I run
  1840. 1:04:07Python of calculator.py,
  1841. 1:04:10I should see, hopefully, that 1 + 2
  1842. 1:04:13equals
  1843. 1:04:14indeed three. All right. So, not that
  1844. 1:04:16surprising and not that interesting. And
  1845. 1:04:18honestly, this isn't the most useful
  1846. 1:04:20program because it's always going to
  1847. 1:04:21calculate 1 + 2 = 3. Let's at least make
  1848. 1:04:25this program say a little more
  1849. 1:04:27interactive, right? We already know from
  1850. 1:04:28previous examples how we can get input
  1851. 1:04:30from the user. Let's bring back that
  1852. 1:04:32input function. And let's do this. Let
  1853. 1:04:34me go ahead now and at the top of my
  1854. 1:04:36code, let's change X to not be the
  1855. 1:04:39number one always. Let's change it to be
  1856. 1:04:41whatever the return value is of asking
  1857. 1:04:43the user for X. And I can use any
  1858. 1:04:46English or human language I want here.
  1859. 1:04:48I'm going to say, "What's X?" Just like
  1860. 1:04:50I asked before, "What's your name?" And
  1861. 1:04:52I'm going to do the same thing for Y.
  1862. 1:04:54I'm going to use input again, but this
  1863. 1:04:56time change the question to be, "What's
  1864. 1:04:57Y?"
  1865. 1:04:58All right. At this point, I think I'm
  1866. 1:05:00going to leave the rest of the code the
  1867. 1:05:01same. Z equals X plus Y, and then print
  1868. 1:05:04Z. But, what's nice now is that I think
  1869. 1:05:07I have a nice interactive calculator.
  1870. 1:05:10Right now, it's not going to do 1 + 2
  1871. 1:05:11all the time. It's going to do whatever
  1872. 1:05:13the user types plus whatever the user
  1873. 1:05:15types. So, let's try this. Let me go
  1874. 1:05:17ahead and run the program. All right,
  1875. 1:05:19let's do it. 1 is going to be X, 2 is
  1876. 1:05:22going to be Y, and of course, everyone
  1877. 1:05:25in agreement, 1 + 2 equals 3.
  1878. 1:05:31Huh, what's going on there?
  1879. 1:05:35Either your math class misled you or I
  1880. 1:05:38have misled you.
  1881. 1:05:40Why don't we call on someone here to see
  1882. 1:05:42if you can't help us reason through what
  1883. 1:05:44the bug is. What's the mistake? Uh
  1884. 1:05:46Anjali, if I'm saying it right?
  1885. 1:05:48I think the issue is is that it's
  1886. 1:05:51concatenating strings because you use
  1887. 1:05:53the plus operator instead of adding.
  1888. 1:05:55Perfect. So, perfect intuition. We've
  1889. 1:05:57seen that plus is used a little
  1890. 1:05:59differently in the context of strings
  1891. 1:06:00because it concatenates, that is it
  1892. 1:06:02joins the two strings, and that seems to
  1893. 1:06:04indeed be what's happening here even
  1894. 1:06:06though the user typed a number. But, the
  1895. 1:06:08interesting thing here is that when you
  1896. 1:06:10get user input, because they're using a
  1897. 1:06:12keyboard on their Mac or PC or their
  1898. 1:06:14phone, it is always going to be text. It
  1899. 1:06:16might look like a number, but by
  1900. 1:06:18default, it's coming from the keyboard
  1901. 1:06:20as a string. That is, as text. And so,
  1902. 1:06:24how do we go about re- resolving this if
  1903. 1:06:27ultimately we don't want to treat those
  1904. 1:06:28inputs as strings, we want to treat them
  1905. 1:06:31as actual numbers? Well, we need another
  1906. 1:06:32function. And it turns out in Python
  1907. 1:06:34that you can convert sometimes from one
  1908. 1:06:37type of data to another type of data,
  1909. 1:06:39for instance, from string to int by
  1910. 1:06:42doing something like this. Let me go
  1911. 1:06:44back into my code, and let me change X
  1912. 1:06:47before adding it to Y to be whatever the
  1913. 1:06:50integer version of X is plus whatever
  1914. 1:06:53the integer version of Y is. So, it
  1915. 1:06:56turns out that int is not only a type of
  1916. 1:06:58data in Python, it's also a function.
  1917. 1:07:01And it's a function that if you pass in
  1918. 1:07:03an input like a string, so long as that
  1919. 1:07:06string looks like a number like one or
  1920. 1:07:08like two, it will convert it to an
  1921. 1:07:11actual number that you can perform
  1922. 1:07:12mathematics on instead. So if I now go
  1923. 1:07:15back to my terminal window and run
  1924. 1:07:17Python and let me show you another
  1925. 1:07:19trick. Calculator's kind of a long word,
  1926. 1:07:21it's a little tedious to type. Notice
  1927. 1:07:23what I can do in my terminal window and
  1928. 1:07:25a command line interface in general. If
  1929. 1:07:27I start typing c a l for calculator, I
  1930. 1:07:30can actually hit tab to finish my
  1931. 1:07:33thought. So autocomplete is possible in
  1932. 1:07:35a terminal window like this. Type the
  1933. 1:07:36first letter or few letters and then
  1934. 1:07:38boom, with tab it'll finish your thought
  1935. 1:07:40for you. Or you can go back in your
  1936. 1:07:42history like I did with the up and down
  1937. 1:07:44arrows. Let me go ahead and execute
  1938. 1:07:45this. What's x1? What's x2? And there we
  1939. 1:07:49go. Now we have a general purpose
  1940. 1:07:51calculator that's going to support not
  1941. 1:07:53just
  1942. 1:07:54uh addition of one and two, but now any
  1943. 1:07:56two integers that the user types. And
  1944. 1:07:59let me now improve this, right? We've
  1945. 1:08:00seen how we can make improvements to
  1946. 1:08:02code and I I don't know if it's going to
  1947. 1:08:04necessarily be better, but let's try
  1948. 1:08:06this.
  1949. 1:08:07Do I really need the z variable?
  1950. 1:08:09It's worth noting that I'm creating a
  1951. 1:08:11variable called c z and then I'm
  1952. 1:08:14immediately using it on the next line of
  1953. 1:08:16code. Now, that's not that compelling
  1954. 1:08:19because if you're creating a variable
  1955. 1:08:20and then immediately using it but never
  1956. 1:08:22again using it, did you really need to
  1957. 1:08:24take the time to introduce another
  1958. 1:08:26symbol and another variable just to use
  1959. 1:08:28it once and only once? Well, maybe not.
  1960. 1:08:31Maybe we don't really need z in this
  1961. 1:08:33way. Maybe I should go and do something
  1962. 1:08:36uh like this. Maybe I should get rid of
  1963. 1:08:39z here.
  1964. 1:08:41Maybe I should change this to be int up
  1965. 1:08:44here, change this to be int up here.
  1966. 1:08:47Doing something that's pretty
  1967. 1:08:49interesting now. Even though it's a bit
  1968. 1:08:50of new syntax, notice that you can nest
  1969. 1:08:54functions so to speak. You can put one
  1970. 1:08:57function call, that is the use of a
  1971. 1:09:00function, inside of the use of another
  1972. 1:09:02function so that the return value of the
  1973. 1:09:05inner function
  1974. 1:09:07becomes the argument to or the input to
  1975. 1:09:10the outer function. So, just like in
  1976. 1:09:11math, if you have parentheses,
  1977. 1:09:13parentheses, parentheses, your teacher
  1978. 1:09:14probably taught you to focus on what's
  1979. 1:09:16inside the innermost parentheses first
  1980. 1:09:18and then work your way out. Same thing
  1981. 1:09:20with programming. That's what Python's
  1982. 1:09:21going to do. It's going to look at
  1983. 1:09:22what's inside of the parentheses first,
  1984. 1:09:24it's going to get the answer, and then
  1985. 1:09:26it's going to pass the return value to
  1986. 1:09:29the outermost function. So, what happens
  1987. 1:09:31on line one now is that the input
  1988. 1:09:32function gets called first, then the
  1989. 1:09:35result of that, quote unquote one,
  1990. 1:09:38becomes the input to the int function,
  1991. 1:09:41and same on line two. The output of
  1992. 1:09:43what's Y becomes the input to this int
  1993. 1:09:46function. And now, there is no Z. I
  1994. 1:09:49could just do print X + Y. And because
  1995. 1:09:52I've taken the time to convert each of
  1996. 1:09:55those strings to an integer, I think
  1997. 1:09:58we're okay. So, let me try this. Python
  1998. 1:10:00calculator.py, enter. One and two, and
  1999. 1:10:03we're still getting three. Not 12 or not
  2000. 1:10:061212,
  2001. 1:10:08we're indeed getting three. And we've
  2002. 1:10:09additionally gotten rid of the variable
  2003. 1:10:11because we didn't necessarily need it,
  2004. 1:10:13it seems, especially if only using it
  2005. 1:10:14once. Well, here too, let me put
  2006. 1:10:16everyone's hands down for just a moment
  2007. 1:10:18and let me ask, as before, this version
  2008. 1:10:20now, which uses int around the
  2009. 1:10:24invocations of input, and does not use
  2010. 1:10:27Z, is this better than the previous
  2011. 1:10:30version? If you want to vote yes, go
  2012. 1:10:31ahead, or if you prefer the old way,
  2013. 1:10:34vote no. The old way, I'll undo all of
  2014. 1:10:36this as we vote, instead looked like
  2015. 1:10:40this.
  2016. 1:10:43All right, and let me go back to now the
  2017. 1:10:44newest version. Let's take a hand of the
  2018. 1:10:46yeses. Someone who thinks this latest
  2019. 1:10:49version is better.
  2020. 1:10:51I think this way is a better because it
  2021. 1:10:54uh allows us to immediately see what the
  2022. 1:10:57X and Y variables are with integers and
  2023. 1:11:02so we know what to expect from them. And
  2024. 1:11:05also the print argument is more
  2025. 1:11:08intuitive. We avoid too much clutter in
  2026. 1:11:11the code.
  2027. 1:11:12I think those are all good reasons. It's
  2028. 1:11:14nice and succinct. The lines of code are
  2029. 1:11:16not very long.
  2030. 1:11:17I don't need to know what Z is cuz it
  2031. 1:11:19doesn't exist. It just see print X plus
  2032. 1:11:21Y. I like that. But someone who prefers
  2033. 1:11:23the older way where we did have Z and we
  2034. 1:11:26more explicitly passed individual
  2035. 1:11:29variables to the int function.
  2036. 1:11:30Yeah, hi.
  2037. 1:11:32I think the earlier version is better
  2038. 1:11:34because when
  2039. 1:11:36I mean if user input something else
  2040. 1:11:38other than in let's say I mean let's say
  2041. 1:11:40data type one and two like
  2042. 1:11:43so will be it will be easier to debug.
  2043. 1:11:46This version or this version here or the
  2044. 1:11:48old version?
  2045. 1:11:50The old version.
  2046. 1:11:50Okay, that's fair. And in fact I'm I'm
  2047. 1:11:52being very careful today as best I can
  2048. 1:11:55not to mess up. I have thus far only
  2049. 1:11:58inputted integers when I'm expecting
  2050. 1:12:00integers and Rose actually pointed to
  2051. 1:12:02something we'll come back to in the
  2052. 1:12:03coming weeks. How do we actually handle
  2053. 1:12:05errors? What if the user doesn't type in
  2054. 1:12:07the number one or the number two or a
  2055. 1:12:09number at all? What if they type in a
  2056. 1:12:10word like cat c a t? That's not a number
  2057. 1:12:14and I bet I can't convert it to an
  2058. 1:12:15integer. But for today I'm not going to
  2059. 1:12:17focus on that. I'm just going to hope
  2060. 1:12:18that the user cooperates but that's not
  2061. 1:12:20going to be the case and so perhaps one
  2062. 1:12:22way would set us up for more success
  2063. 1:12:24when it comes to handling those errors.
  2064. 1:12:26Now for today's purposes which is
  2065. 1:12:27better?
  2066. 1:12:28I mean I like both and I think both of
  2067. 1:12:30you made very valid arguments in there
  2068. 1:12:32too. So long as you have a justification
  2069. 1:12:34that feels pretty reasonable. I mean
  2070. 1:12:36that's what ultimately matters. But
  2071. 1:12:38acquiring again a sense of the
  2072. 1:12:40trade-offs here. Well, is this way
  2073. 1:12:42better? If so, why or why not? Just
  2074. 1:12:45understanding what those trade-offs are.
  2075. 1:12:46But generally speaking prioritizing
  2076. 1:12:49readability is a very good thing. Making
  2077. 1:12:51your code readable for someone else is a
  2078. 1:12:53very good thing and very good for you,
  2079. 1:12:56too. So, that when you wake up the next
  2080. 1:12:57morning or you come back the next week
  2081. 1:12:58or the next year, you too can read your
  2082. 1:13:00own code without having to waste time
  2083. 1:13:02trying to remember what you did. And
  2084. 1:13:04simplicity tends to be a good thing,
  2085. 1:13:06too. Keeping your code simple. So, is as
  2086. 1:13:09you get more comfortable with
  2087. 1:13:10programming, you might be tempted to try
  2088. 1:13:11to like combine an entire program into
  2089. 1:13:14one long line. For instance, let me do
  2090. 1:13:16right just that. Don't Technically
  2091. 1:13:18speaking, we don't really need X in a
  2092. 1:13:19variable. We don't really need Y in a
  2093. 1:13:21variable. We could also do this. I could
  2094. 1:13:24just get rid of X and Y altogether. I
  2095. 1:13:28could then now eliminate that and make
  2096. 1:13:30it just one line of code. Okay, so on
  2097. 1:13:32some sense you might be inclined to
  2098. 1:13:33think, "Wow, that's really nice. You
  2099. 1:13:35made it one simple line of code." I
  2100. 1:13:38would argue this actually isn't that
  2101. 1:13:39simple. Now, I think I'm starting to
  2102. 1:13:41nest too many things. I have to think
  2103. 1:13:43about print and int and input. I then
  2104. 1:13:46have to notice that, "Okay, I've opened
  2105. 1:13:48two parentheses. I've closed two of
  2106. 1:13:49them. There's a plus." You're making me
  2107. 1:13:51think too much. And anytime you make me
  2108. 1:13:53think, you're wasting time. And anytime
  2109. 1:13:55you complicate the look of the code like
  2110. 1:13:57this, you're just going to increase the
  2111. 1:13:59probability of mistakes, syntactical
  2112. 1:14:01mistakes, or logical errors in your
  2113. 1:14:03code. So, of all the things we've done,
  2114. 1:14:05this is the only one that I would argue,
  2115. 1:14:07"Yes, it's one line and it's nice and
  2116. 1:14:10compact. It's just not readable enough."
  2117. 1:14:12I would shy away from doing this,
  2118. 1:14:13especially since two of those function
  2119. 1:14:15calls are getting input from the user.
  2120. 1:14:17But there, too, reasonable people might
  2121. 1:14:19disagree. But that's the kind of like
  2122. 1:14:21visceral reaction you should have
  2123. 1:14:22sometimes when code starts getting a
  2124. 1:14:24little too complicated, a little too
  2125. 1:14:27clever perhaps for its own good.
  2126. 1:14:31All right. Well, it's not just integers
  2127. 1:14:33we have access to. Let me propose that
  2128. 1:14:35we transition from integers to one more
  2129. 1:14:38data type here, namely a float. So,
  2130. 1:14:41again, a string is a sequence of text.
  2131. 1:14:43An int is an integer like -1, 0, and 1.
  2132. 1:14:46A float is a number with a decimal
  2133. 1:14:48point, properly called a floating point
  2134. 1:14:50value, and you can think of the floating
  2135. 1:14:52point as being the decimal that might be
  2136. 1:14:54over here or over here with some number
  2137. 1:14:56of digits to the left or the right.
  2138. 1:14:57Mathematically, it's a real number, a
  2139. 1:14:59number that has a decimal point in it.
  2140. 1:15:01So, that's a third type of data that
  2141. 1:15:03Python supports. Right now, our
  2142. 1:15:05calculator is somewhat naively assuming
  2143. 1:15:07that the user's only going to type in
  2144. 1:15:08integers, but if I want to support
  2145. 1:15:10floating point values, too, I think I
  2146. 1:15:13can just make a couple of tweaks. So,
  2147. 1:15:15I'm going to go back to VS Code here,
  2148. 1:15:17and instead of just converting the
  2149. 1:15:18user's input X and Y to integers on line
  2150. 1:15:21one and two, let's just make a simple
  2151. 1:15:23change. Let's actually convert it to a
  2152. 1:15:25float on the first line and a float on
  2153. 1:15:29the second line here. Now, I think if I
  2154. 1:15:31go down to my terminal window and run
  2155. 1:15:33python of calculator.py, let's type in a
  2156. 1:15:36number like 1.2 with a decimal point and
  2157. 1:15:393.4 with a decimal point, and there we
  2158. 1:15:41go. We have 4.6 as the final answer. So,
  2159. 1:15:44that wouldn't have worked before if I
  2160. 1:15:46was only expecting integers from the
  2161. 1:15:48user, but now that I'm support expecting
  2162. 1:15:50floating point values and accommodating
  2163. 1:15:52it, I can actually now do floating point
  2164. 1:15:54arithmetic as well. But, suppose that I
  2165. 1:15:56don't really want the final answer to be
  2166. 1:16:00a floating point value like 4.6. I would
  2167. 1:16:03be happy if we just round to the nearest
  2168. 1:16:05integer. So, I want to support the user
  2169. 1:16:07typing in floating point values with
  2170. 1:16:09decimal points, but at the end of the
  2171. 1:16:10day, I just want to round the result to
  2172. 1:16:12the nearest possible integer for
  2173. 1:16:15instance. Well, it turns out that here,
  2174. 1:16:17too, Python comes with some
  2175. 1:16:19functionality built in. And in fact, if
  2176. 1:16:21we return to this URL from earlier,
  2177. 1:16:23wherein all of the Python built-in
  2178. 1:16:25functions are listed, there's one called
  2179. 1:16:27round, which does exactly as we would
  2180. 1:16:29expect. It takes as input a number and
  2181. 1:16:31then rounds it for us, for instance, to
  2182. 1:16:34the nearest digit to the nearest
  2183. 1:16:35integer.
  2184. 1:16:36But, if we look a little closer at that
  2185. 1:16:38documentation, as we can here, I'll
  2186. 1:16:40provide an excerpt, this is what the
  2187. 1:16:42function looks like in the
  2188. 1:16:44documentation. And recall that earlier
  2189. 1:16:46we looked at the documentation for
  2190. 1:16:47print, and this is similar in spirit
  2191. 1:16:50that this shows us not just the name of
  2192. 1:16:51the function, but it's available
  2193. 1:16:53parameters. That is inputs that we can
  2194. 1:16:55provide when using this function. But
  2195. 1:16:57this is a little cryptic too, just like
  2196. 1:16:59print was, and it adds some syntax. So
  2197. 1:17:01let's see. The name of this function
  2198. 1:17:03here is of course round, and its first
  2199. 1:17:05argument is a number. Notice this times
  2200. 1:17:08there's no star. There's no star objects
  2201. 1:17:10like there was for print. The round
  2202. 1:17:12function takes just one number as its
  2203. 1:17:14first argument, period. That's its
  2204. 1:17:16positional parameter. But notice this
  2205. 1:17:19syntax, and this is a convention in
  2206. 1:17:21programming or technology more
  2207. 1:17:22generally. Generally speaking, when you
  2208. 1:17:24see square brackets in documentation
  2209. 1:17:27like this, this means that you're about
  2210. 1:17:28to see something optional. And so what
  2211. 1:17:30this means is that if you want to
  2212. 1:17:32specify more precisely the number of
  2213. 1:17:35digits that you want the round function
  2214. 1:17:37to round to, you can specify it here by
  2215. 1:17:41adding a comma and then that number. So
  2216. 1:17:43if we read the documentation, if you
  2217. 1:17:45don't specify a number of digits, you
  2218. 1:17:48just specify the number to round, it
  2219. 1:17:50rounds to the nearest integer. But
  2220. 1:17:52suppose you want to round to the tenths
  2221. 1:17:54place or the hundredths place, that is
  2222. 1:17:56one or two digits after the decimal
  2223. 1:17:57point, you could additionally pass in
  2224. 1:17:59{comma} one or {comma} two to be more
  2225. 1:18:03precise. So that's what the
  2226. 1:18:04documentation there is saying. Let's see
  2227. 1:18:06if we can't then translate this to
  2228. 1:18:08some actual code for us. So if I go back
  2229. 1:18:11now to VS Code, and I consider that I
  2230. 1:18:13want to go ahead and round X and Y, I
  2231. 1:18:16can do this in a couple of ways. I could
  2232. 1:18:18do round X + Y, but you know, I'd
  2233. 1:18:22actually kind of prefer to break this
  2234. 1:18:23now out into two lines. I don't have to,
  2235. 1:18:26and reasonable people here might
  2236. 1:18:27disagree, but I'd like to revert to a
  2237. 1:18:29scenario where I'm printing Z so that I
  2238. 1:18:31can just a little more clearly to
  2239. 1:18:33myself, to others say Z equals the
  2240. 1:18:35rounded result of X plus Y. It's not
  2241. 1:18:39necessarily the better way to do it, but
  2242. 1:18:41I'm a little more comfortable with
  2243. 1:18:42breaking out my thoughts one at a time,
  2244. 1:18:44especially if I want to start commenting
  2245. 1:18:46each of these chunks of code. All right,
  2246. 1:18:48let me go down to my terminal window now
  2247. 1:18:50and run python of calculator.py. What's
  2248. 1:18:52X? Let's do 1.2 again. Then let's do
  2249. 1:18:553.4. And now it was previously 4.6, but
  2250. 1:18:58now it's been rounded up to the nearest
  2251. 1:19:01integer, which of course is going to be
  2252. 1:19:03five. All right, what if I wanted to
  2253. 1:19:06change this a little further? What if I
  2254. 1:19:09wanted to support maybe really big
  2255. 1:19:11numbers? Big numbers irrespective of
  2256. 1:19:13rounding. Let's just do something like
  2257. 1:19:14this.
  2258. 1:19:15Let me go ahead and run python of
  2259. 1:19:16calculator.py again. And let me just add
  2260. 1:19:19999
  2261. 1:19:20plus one. And notice I don't have to
  2262. 1:19:23type decimal points, even though I'm
  2263. 1:19:25converting to float. My program will
  2264. 1:19:27just allow me to type decimal points,
  2265. 1:19:29but I don't need to oblige. The answer
  2266. 1:19:31of course here it should be and is in
  2267. 1:19:33fact 1,000, whether or not we round. So
  2268. 1:19:35that's just arithmetic with integers
  2269. 1:19:37here. But in
  2270. 1:19:39the US, we tend to format long numbers
  2271. 1:19:42by putting commas
  2272. 1:19:44uh after or before every triple of
  2273. 1:19:46digits. Other countries flip it and they
  2274. 1:19:48use periods and commas instead. That's a
  2275. 1:19:50system setting. You can change that on
  2276. 1:19:52your own Mac or PC or device for python
  2277. 1:19:55or any language. But for me, I'm using
  2278. 1:19:57the uh US approach here, which is
  2279. 1:19:59periods for decimal points and commas
  2280. 1:20:01for separators. What if I wanted this to
  2281. 1:20:03be outputted as 1 comma 000?
  2282. 1:20:07Just to make it a little more clear that
  2283. 1:20:09it's 1,000 and not something like 100.
  2284. 1:20:12That's even more useful when it's like 1
  2285. 1:20:14million. 1 comma 000 comma 000. Wouldn't
  2286. 1:20:18it be nice if we could automatically
  2287. 1:20:19output those numbers as well? Well, it
  2288. 1:20:21turns out that we can. There is a way
  2289. 1:20:24using python
  2290. 1:20:26to actually specify that we want to
  2291. 1:20:28include commas like this. And here we
  2292. 1:20:31have an opportunity to bring back our
  2293. 1:20:32old friend, the F string.
  2294. 1:20:35First, let me do something that's not
  2295. 1:20:36that productive. First, let me do this.
  2296. 1:20:38Let me print out the value of Z, but
  2297. 1:20:41wait a minute, I can't just say {quote}
  2298. 1:20:42Z cuz that's literally going to print Z
  2299. 1:20:44on the screen. So, let me wrap it with
  2300. 1:20:46those curly braces like I did before,
  2301. 1:20:48but that too was not enough. I literally
  2302. 1:20:51needed to add an F at the beginning of
  2303. 1:20:53my string to tell Python that this is an
  2304. 1:20:55F string, a format string. That now is
  2305. 1:20:57going to print out, not very
  2306. 1:20:59interestingly, just the value of Z
  2307. 1:21:01itself. So, that I'm going to great
  2308. 1:21:03lengths just to print Z when really I
  2309. 1:21:05could have just passed Z as the sole
  2310. 1:21:07argument. But, just to ensure that I
  2311. 1:21:09haven't broken it, let's do this again.
  2312. 1:21:11999 plus 1 enter. Okay, it's still a
  2313. 1:21:14thousand. So, I didn't make anything
  2314. 1:21:16worse. But, notice and this syntax is
  2315. 1:21:18unfortunately a bit cryptic. Notice that
  2316. 1:21:21I can actually do this. I can put a
  2317. 1:21:22colon after the Z and I can put a comma
  2318. 1:21:26thereafter. This looks very cryptic
  2319. 1:21:28admittedly and even I have to constantly
  2320. 1:21:30look things like this up in the
  2321. 1:21:31documentation to remember the syntax.
  2322. 1:21:33But, here let me run it again. Python of
  2323. 1:21:35calculator.py
  2324. 1:21:37999
  2325. 1:21:391 and now notice that the number's been
  2326. 1:21:42automatically formatted for me. If I
  2327. 1:21:44were in a different country or locale, I
  2328. 1:21:45could absolutely override this to use
  2329. 1:21:47periods instead of commas or vice versa.
  2330. 1:21:50But, in this case here, it's just
  2331. 1:21:51happening for me automatically. So,
  2332. 1:21:52there too, we see a hint of what it
  2333. 1:21:54means to really format a string. There's
  2334. 1:21:56even more power, more powerful
  2335. 1:21:58capabilities built into that.
  2336. 1:22:01All right, let me pause here to see if
  2337. 1:22:02there's any questions now on floats,
  2338. 1:22:05on rounding,
  2339. 1:22:06or on this use of F strings.
  2340. 1:22:10Yes, so I have a question. So, when
  2341. 1:22:12using floats, um is there like a cap you
  2342. 1:22:14have on the decimal points you can have?
  2343. 1:22:16Really good question. So, floats, yes,
  2344. 1:22:18and this is a problem we'll revisit
  2345. 1:22:20before long. Floats cannot represent
  2346. 1:22:23numbers infinitely precisely in a
  2347. 1:22:25nutshell because computers only have so
  2348. 1:22:27much memory. They only have a finite
  2349. 1:22:29amount of memory. You and I only have a
  2350. 1:22:30finite amount of hardware inside of the
  2351. 1:22:33computer. So, at some point they're
  2352. 1:22:35going to have to round. Right now, I'm
  2353. 1:22:37rounding automatically. Effectively,
  2354. 1:22:38computers will eventually have to do
  2355. 1:22:39that for us, but we'll see that as a
  2356. 1:22:41fundamental problem before long.
  2357. 1:22:44Allow me to turn back just for a few
  2358. 1:22:45final examples on float before we
  2359. 1:22:47introduce a few final examples that
  2360. 1:22:49allow us not just to use functions, but
  2361. 1:22:51to make our own. Let me propose that we
  2362. 1:22:53also try our hand at a bit of division
  2363. 1:22:56here. Let me propose that we modify this
  2364. 1:22:58calculator now to still take a couple of
  2365. 1:23:00floats, but let's now just do something
  2366. 1:23:02a little simpler than
  2367. 1:23:03a little different from this, just doing
  2368. 1:23:05X divided by Y. And let me go ahead and
  2369. 1:23:07get rid of my format string and just
  2370. 1:23:09keep it simple for now, printing out Z
  2371. 1:23:11instead. And what are we going to see
  2372. 1:23:13here? Well, just some simple division.
  2373. 1:23:14So, Python of calculator.py. Let's do
  2374. 1:23:17something like 2 divided by 3. And of
  2375. 1:23:20course, I get .66666. And to Ethan's
  2376. 1:23:23question a moment ago, it does seem to
  2377. 1:23:24be finite. It's not rounding in a weird
  2378. 1:23:26way here, but I only seem to see so many
  2379. 1:23:29digits. That's a
  2380. 1:23:31inevitability of using a float in this
  2381. 1:23:33way. By contrast, just so you know,
  2382. 1:23:35integers nowadays in Python can be as
  2383. 1:23:38big as you want them to be. Unlike other
  2384. 1:23:40languages, there is no upper bound on
  2385. 1:23:41how big an int can be now in Python, but
  2386. 1:23:44there is a bound on just how precise a
  2387. 1:23:46floating point value can be.
  2388. 1:23:48All right. Now that I've got some simple
  2389. 1:23:49division working here, let's go ahead
  2390. 1:23:51and round this. It would be nice to
  2391. 1:23:53round this really long number .666666
  2392. 1:23:56and so forth to maybe just two decimal
  2393. 1:23:58places. We've seen how to do this with
  2394. 1:24:00round though, at least in its
  2395. 1:24:01documentation. Let's just round this not
  2396. 1:24:03to the nearest int by passing in just X
  2397. 1:24:06divided by Y, which is one argument once
  2398. 1:24:09the math is done inside of the
  2399. 1:24:10parentheses. I don't want to pass in
  2400. 1:24:12just one argument. I want to pass in two
  2401. 1:24:14so that I can specify in digits number
  2402. 1:24:17of digits, which recall was the second
  2403. 1:24:19parameter for round. Let me go ahead and
  2404. 1:24:21run pythonofcalculator.py. I'll do the
  2405. 1:24:23same thing, two and then three, 0.67.
  2406. 1:24:27So, here too, we see a way of rounding
  2407. 1:24:29now, not just to a nearest integer, but
  2408. 1:24:31to a nearest number of digits. But,
  2409. 1:24:34there's another way to do this here. And
  2410. 1:24:36in fact, this evokes our our F F-string
  2411. 1:24:39example again. Let me go ahead and
  2412. 1:24:41change this. Suppose that you didn't
  2413. 1:24:43remember the round function or for some
  2414. 1:24:45reason you didn't want to use it, you
  2415. 1:24:47instead want to just use a format
  2416. 1:24:48string. Well, let's go there. Let me do
  2417. 1:24:51{quote} Z, but let me surround it with
  2418. 1:24:54those curly braces. Let me add the F at
  2419. 1:24:56the beginning. And again, this is not
  2420. 1:24:57interesting yet. This is just going to
  2421. 1:24:59print out Z, but I'm adding a lot more
  2422. 1:25:01complexity to turn it into an F-string.
  2423. 1:25:04But, notice I can do something else
  2424. 1:25:05after my variable name, after the colon,
  2425. 1:25:08if this were going to be a big integer,
  2426. 1:25:11I might want to use a comma, like
  2427. 1:25:12before, to separate each triple of
  2428. 1:25:14numbers with commas, but I don't. I'm
  2429. 1:25:16going to use a different sequence
  2430. 1:25:18uh of characters. I'm going to say
  2431. 1:25:20{quote} 2F. And this too is one of these
  2432. 1:25:23very cryptic things I have to constantly
  2433. 1:25:25look up cuz I forget if I don't use it
  2434. 1:25:27that often. So, don't be intimidated if
  2435. 1:25:29this looks especially weird, but this
  2436. 1:25:31is, according to the documentation, the
  2437. 1:25:33way you specify using an F-string how
  2438. 1:25:36many digits you want to print. So, let
  2439. 1:25:38me run this version of the calculator,
  2440. 1:25:39type in two and then three, we get the
  2441. 1:25:41exact same thing. But again, this is
  2442. 1:25:44just consistent with my claim that in
  2443. 1:25:45programming we can so very often solve
  2444. 1:25:48the same problem in multiple ways. This
  2445. 1:25:51is just now the F-string approach to
  2446. 1:25:53that very same problem.
  2447. 1:25:56All right, which one is better? It
  2448. 1:25:57depends. In this case, they're pretty
  2449. 1:25:59equivalent. You could imagine though it
  2450. 1:26:01being useful to use a function sometimes
  2451. 1:26:03so that you can pass in an argument like
  2452. 1:26:05N digits as that second argument, or you
  2453. 1:26:07can imagine just deciding in advance
  2454. 1:26:09that you want {quote} 2 and then writing
  2455. 1:26:11it like this. Let's transition now from
  2456. 1:26:14focusing on strings and on integers and
  2457. 1:26:17on floats to focusing now on functions
  2458. 1:26:19themselves. We began today by focusing
  2459. 1:26:21on how you can use functions that come
  2460. 1:26:23with Python. But, wouldn't it be nice if
  2461. 1:26:25you could invent your own functions,
  2462. 1:26:26especially if, to our point earlier, you
  2463. 1:26:29find yourself solving the same kind of
  2464. 1:26:30problem again and again? It's nice that
  2465. 1:26:33Python comes with the print function cuz
  2466. 1:26:34it's really useful to be able to print
  2467. 1:26:36things on the screen, but wouldn't it be
  2468. 1:26:37nice if you could print specific things
  2469. 1:26:39on the screen by just calling your own
  2470. 1:26:41function? Well, let me propose that we
  2471. 1:26:43do this. Let me go back to VS Code here,
  2472. 1:26:46and let me propose that we go back to
  2473. 1:26:48hello.py. I'm going to reopen hello.py
  2474. 1:26:51where we left it before, and I'm going
  2475. 1:26:53to go ahead now and propose that we
  2476. 1:26:54consider how we can start improving this
  2477. 1:26:57further by making our own function. I
  2478. 1:26:59have written so many programs today that
  2479. 1:27:02just say hello, and each time I'm using
  2480. 1:27:04print, but wouldn't it have been nice if
  2481. 1:27:07from the beginning of today we could
  2482. 1:27:08just call a function called hello that
  2483. 1:27:11just says hello for us. Now, the authors
  2484. 1:27:13of Python years ago didn't think that we
  2485. 1:27:15need a special function just to say
  2486. 1:27:17hello, but I would like that to exist.
  2487. 1:27:19I'm saying hello so many times, I just
  2488. 1:27:21want to be able to call a function
  2489. 1:27:22hello. So, I'm going to start from
  2490. 1:27:24scratch here. I'm going to delete all of
  2491. 1:27:26my code from earlier, and I'm going to
  2492. 1:27:28pretend for the moment that a function
  2493. 1:27:30called hello exists. And I'm going to do
  2494. 1:27:33just as I did before, I'm going to get
  2495. 1:27:34the user's name with the input function
  2496. 1:27:36asking, "What's your name?" {question
  2497. 1:27:38mark} And now, I'm going to call a
  2498. 1:27:40function hello, and then I'm going to
  2499. 1:27:43print out the user's name.
  2500. 1:27:45Now, I will admit hello doesn't exist,
  2501. 1:27:48so bad things are about to happen, but
  2502. 1:27:50let's see what. Let me go down to my
  2503. 1:27:52terminal window. Let me run python of
  2504. 1:27:54hello.py.
  2505. 1:27:55I think the first line's going to be
  2506. 1:27:57okay cuz that worked before, and indeed
  2507. 1:27:59it's prompting me for my name. So, let
  2508. 1:28:01me type in David. The second line of
  2509. 1:28:03code is apparently calling a function
  2510. 1:28:05that looks like it's called hello
  2511. 1:28:07because why is it a function? It has a
  2512. 1:28:09parenthesis and a close parenthesis
  2513. 1:28:10immediately after it, and that's what
  2514. 1:28:12every function we've used has looked
  2515. 1:28:13like, but Python's not going to
  2516. 1:28:15recognize this one. When I hit enter
  2517. 1:28:16now, I get a name error. Name hello is
  2518. 1:28:19not defined. Did you mean help? I
  2519. 1:28:22didn't, although it's opportune. That's
  2520. 1:28:24what I need at this point is some help,
  2521. 1:28:26but I am encountering this error because
  2522. 1:28:28why? The function just doesn't exist.
  2523. 1:28:31So, how do I make this function exist?
  2524. 1:28:33Well, I need to create it myself using
  2525. 1:28:35this keyword def. Def for define. So,
  2526. 1:28:39here too, just as str is short for
  2527. 1:28:41string and int is short for integer, def
  2528. 1:28:44is short for define. If and when you
  2529. 1:28:46want to define, create, invent your own
  2530. 1:28:49functions, you can do so using now this
  2531. 1:28:52keyword in Python. So, let me go back to
  2532. 1:28:54my code here, and let me propose that we
  2533. 1:28:57define this perhaps in this way.
  2534. 1:28:59At the very top of my file, I'm going to
  2535. 1:29:01first take a moment to define a function
  2536. 1:29:04called hello using def
  2537. 1:29:06hello open parenthesis close parenthesis
  2538. 1:29:09colon. What this means now is that
  2539. 1:29:11Python is going to treat every line of
  2540. 1:29:14code that I indent underneath this one
  2541. 1:29:17as the meaning of this new function
  2542. 1:29:19hello. So, def is important, as is the
  2543. 1:29:22space. I get to choose the name of the
  2544. 1:29:24function, and I'm choosing to call it
  2545. 1:29:26hello. The parentheses with nothing
  2546. 1:29:27inside means that this function at the
  2547. 1:29:29moment it's not going to take any
  2548. 1:29:30inputs, no arguments there, too. The
  2549. 1:29:32colon means stay tuned for some
  2550. 1:29:34indentation. Everything that's indented
  2551. 1:29:37beneath this line of code is going to be
  2552. 1:29:39part of this function. It's going to be
  2553. 1:29:40a super short function, one line of
  2554. 1:29:42code. It's just going to print out
  2555. 1:29:44{quote} hello.
  2556. 1:29:46But now, on lines one and two, I have
  2557. 1:29:48invented my own function hello. Notice
  2558. 1:29:52these dots that have now magically
  2559. 1:29:53appeared here. This is just a setting of
  2560. 1:29:55my text editor, VS Code in this case,
  2561. 1:29:57that's just making super explicit to me
  2562. 1:30:00that I've hit the space bar four times,
  2563. 1:30:02or equivalently the tab key once, which
  2564. 1:30:04is converted automatically to four
  2565. 1:30:06spaces. Generally speaking, I'm going to
  2566. 1:30:08need to make sure that all of my
  2567. 1:30:09indented code lines up now so that
  2568. 1:30:11Python knows that it's all part of the
  2569. 1:30:13same thing. But it's easy in this case
  2570. 1:30:15cuz it's just a single line. But now,
  2571. 1:30:17thanks to lines one and two, the
  2572. 1:30:19function hello will absolutely exist
  2573. 1:30:22when I'm ready to use it on line six. So
  2574. 1:30:25let me go down to my terminal window and
  2575. 1:30:26run python of hello.py enter.
  2576. 1:30:29Here comes my name again. And now, when
  2577. 1:30:31I hit enter, I now see hello David. All
  2578. 1:30:35right, we've kind of regressed though,
  2579. 1:30:36right? This is not nearly as pretty as
  2580. 1:30:38it once was. I think we can probably do
  2581. 1:30:41better than this by improving things
  2582. 1:30:44further. Why don't we consider though
  2583. 1:30:46how we might, say, parameterize the same
  2584. 1:30:49function? That is to say, can we
  2585. 1:30:51customize hello to maybe take the user's
  2586. 1:30:53name as input so that we can say not
  2587. 1:30:55only hello, but the person's name all on
  2588. 1:30:58one line, all in one breath. Well, I
  2589. 1:31:01think we can do this. Let me propose
  2590. 1:31:03that we do this as follows. Let me go
  2591. 1:31:05ahead and up in my code, let me inside
  2592. 1:31:08of these parentheses, let me come up
  2593. 1:31:10with my own parameter name. I have
  2594. 1:31:12complete choice here, and I'm going to
  2595. 1:31:14say that the name of my parameter will
  2596. 1:31:16be the word to. Why? Because I want my
  2597. 1:31:19function to sound like the verb I it
  2598. 1:31:22represents, hello, but who do you want
  2599. 1:31:24to say hello to? Well, I'm going to call
  2600. 1:31:26my parameter for this function to, just
  2601. 1:31:28cuz in English it kind of sounds nice to
  2602. 1:31:30me. Hello to. Who do you want to say
  2603. 1:31:32hello to? That's why I'm calling this
  2604. 1:31:33parameter to instead of something
  2605. 1:31:35simpler like X or Y or Z.
  2606. 1:31:38All right, well what do I want to do
  2607. 1:31:39with the word to? Well, I can do a
  2608. 1:31:41couple of different things. We've seen
  2609. 1:31:42like so many different ways to implement
  2610. 1:31:44hello. Let me just add a comma there for
  2611. 1:31:46grammar's sake, and then let me put the
  2612. 1:31:48word to after that as the second
  2613. 1:31:51argument to the function hello. There's
  2614. 1:31:54other ways we can do this, and we've
  2615. 1:31:55seen so many, but this one looks a
  2616. 1:31:56little clearer to me, I'll say. What's
  2617. 1:31:59going to happen next? Well, I don't
  2618. 1:32:00think I need this extra print line here,
  2619. 1:32:03I think what I'm going to do is this.
  2620. 1:32:05I'm going to go ahead here and print out
  2621. 1:32:08not the person's name manually, I'm
  2622. 1:32:11going to instead instead say hello
  2623. 1:32:13parentheses name. So, what am I now
  2624. 1:32:16doing? On lines one and two, I'm
  2625. 1:32:18defining my very own function called
  2626. 1:32:20hello, but this time that function has
  2627. 1:32:22been designed to take a parameter, a
  2628. 1:32:24single parameter as input, and I'm using
  2629. 1:32:27the value of that parameter which I
  2630. 1:32:29called two to plug into print so that I
  2631. 1:32:32see not only hello, but also that
  2632. 1:32:34person's name. What am I doing on line
  2633. 1:32:36five? Same as always, I'm just getting
  2634. 1:32:38the user's name. Line six, I'm not only
  2635. 1:32:40calling hello, I'm passing as input the
  2636. 1:32:43name variable as an argument so that
  2637. 1:32:47that's what gets passed into hello. And
  2638. 1:32:49what's happening here is essentially
  2639. 1:32:50this. Even though the variable is called
  2640. 1:32:52name here, when the function itself is
  2641. 1:32:55called, the computer assumes that that
  2642. 1:32:58same value is now called two. So, name
  2643. 1:33:01is essentially copied to another
  2644. 1:33:03variable called two, so that in the
  2645. 1:33:06context of hello, I can say hello to
  2646. 1:33:09that variable instead. And we'll see in
  2647. 1:33:11a moment what happens if we don't keep
  2648. 1:33:13those uh straight. Let me go ahead and
  2649. 1:33:15run python if hello.py, enter. What's
  2650. 1:33:17your name? And now I'm crossing my
  2651. 1:33:19fingers. Enter. There we go. We're back
  2652. 1:33:22in business, but now I have my own
  2653. 1:33:23custom function called hello that's
  2654. 1:33:25allowing me to say hello to a specific
  2655. 1:33:27person. And here's where now things can
  2656. 1:33:29get really fancy. What if you wanted
  2657. 1:33:31your hello function to say hello to
  2658. 1:33:33someone specific, but you know what, if
  2659. 1:33:34you don't know who you want to say hello
  2660. 1:33:36to, you want to say hello to the whole
  2661. 1:33:38world? You can give parameters default
  2662. 1:33:40values. We've seen that. Recall that
  2663. 1:33:42with print, there was a default value
  2664. 1:33:44for sep, for the separator. There was a
  2665. 1:33:46default value for end, the line ending.
  2666. 1:33:49We can do that, too, and here's the
  2667. 1:33:51syntax. If you want the value of this
  2668. 1:33:54parameter by default, if not provided by
  2669. 1:33:57the programmer to be equal to {quote}
  2670. 1:33:59world, you literally do that in the same
  2671. 1:34:03line you're defining the function. And
  2672. 1:34:05I'll admit it's starting to look more
  2673. 1:34:06cryptic, but I'm still just defining a
  2674. 1:34:08function called hello. It takes a
  2675. 1:34:10parameter called two, but I'm assigning
  2676. 1:34:13it with the equal sign a default value
  2677. 1:34:16of {quote} world just in case the
  2678. 1:34:18programmer doesn't call hello with an
  2679. 1:34:21argument. And we can see this here. Let
  2680. 1:34:23me change my code to use hello in two
  2681. 1:34:26ways. On line five, I'm going to very
  2682. 1:34:28simply call hello no arguments. Then on
  2683. 1:34:32line six, I'm going to get the name.
  2684. 1:34:33Line seven, I'm going to call hello with
  2685. 1:34:35an argument. So, you'll see hello now
  2686. 1:34:37being used in two ways. Let me go ahead
  2687. 1:34:39and run python of hello.py.
  2688. 1:34:41I'll type in my name. Oh, interesting.
  2689. 1:34:44Notice, I already see hello world, but
  2690. 1:34:46that's expected cuz line five happens
  2691. 1:34:49before line six. But once I type my
  2692. 1:34:51name, now the program's going to be a
  2693. 1:34:53little more polite and say hello to me
  2694. 1:34:56personally. So, there too, we see with
  2695. 1:34:59relatively simple but new syntax how you
  2696. 1:35:01can implement functionality very similar
  2697. 1:35:04in spirit to what the print function
  2698. 1:35:06gave us automatically. Now you have
  2699. 1:35:08control over doing that yourself. But
  2700. 1:35:11let me now make this point too. One of
  2701. 1:35:13the whole points of defining your own
  2702. 1:35:14functions is one, just to avoid having
  2703. 1:35:16to repeat yourself again and again. You
  2704. 1:35:18don't have to actually keep reinventing
  2705. 1:35:21the wheel and keep using the print
  2706. 1:35:23function again and again and again if
  2707. 1:35:24you just want to say hello. Wouldn't it
  2708. 1:35:26be nice now if I could kind of move this
  2709. 1:35:28code that I wrote for defining the hello
  2710. 1:35:31function and just to be dramatic, I'm
  2711. 1:35:34going to hit enter a whole lot of times,
  2712. 1:35:3650 lines down, and put my definition of
  2713. 1:35:39hello way further down in this file.
  2714. 1:35:41Why? Well, just for in the spirit of out
  2715. 1:35:43of sight, out of mind. Because if I now
  2716. 1:35:45rewind to the start of my program, now
  2717. 1:35:48you can sort of take for granted that
  2718. 1:35:50oh, hello is a function. Why? Because
  2719. 1:35:51it's there on line one and it has an
  2720. 1:35:53open parenthesis and a close
  2721. 1:35:54parenthesis, which up until now has
  2722. 1:35:56meant call this function. And then on
  2723. 1:35:58line two, we're getting a variable from
  2724. 1:35:59the user by typing in their name and
  2725. 1:36:01then we're calling hello, passing in
  2726. 1:36:03that value. Well, at this point, I can
  2727. 1:36:05just take for granted that hello exists,
  2728. 1:36:07even if it's way down further in the
  2729. 1:36:09file or as we'll see in future weeks,
  2730. 1:36:11even if it's in a different file
  2731. 1:36:12altogether. But there's a problem here,
  2732. 1:36:15and let me go ahead and run this version
  2733. 1:36:17of hello.py.
  2734. 1:36:19Notice that as soon as I run the
  2735. 1:36:21interpreter, python of hello.py, I see a
  2736. 1:36:23name error, name hello is not defined.
  2737. 1:36:26Again, did you mean help? Well, again
  2738. 1:36:29fitting, I do need some help here, but I
  2739. 1:36:30didn't mean to call the function help.
  2740. 1:36:33The problem here though is that python
  2741. 1:36:35is just taking me literally. I have
  2742. 1:36:36defined my function hello all the way
  2743. 1:36:39down here, but I'm trying to use it way
  2744. 1:36:42up here, and that's not allowed.
  2745. 1:36:43Python's interpreter's going to take you
  2746. 1:36:45literally, and if you use a function, it
  2747. 1:36:46must already exist by the time you are
  2748. 1:36:49calling it. So, how do I fix this? Well,
  2749. 1:36:51apparently, I can't do that. I have to
  2750. 1:36:53define any functions I want at the very
  2751. 1:36:56top of my file, but that too could get
  2752. 1:36:58me into a bit of trouble eventually
  2753. 1:37:00because if I constantly have to define a
  2754. 1:37:03function above where I want to use it,
  2755. 1:37:05you're kind of writing code in reverse.
  2756. 1:37:06You're constantly writing functions up
  2757. 1:37:08here, up here, up here as opposed to
  2758. 1:37:09like writing your code logically, top to
  2759. 1:37:11bottom. So, let me fix this in a more
  2760. 1:37:13standard way, which is to do this.
  2761. 1:37:16Generally speaking, you do want to put
  2762. 1:37:18the main part of your code at the top of
  2763. 1:37:21your file. And in fact, I'm going to go
  2764. 1:37:23so far as to define my function called
  2765. 1:37:26main. It's not a requirement, but it's
  2766. 1:37:28indeed a convention, and this just
  2767. 1:37:30connotes to the reader that this is the
  2768. 1:37:32main part of my program. I'm going to
  2769. 1:37:33get rid of my empty hello call now and
  2770. 1:37:36only pass in one version with hello
  2771. 1:37:38name, and then down here, a couple lines
  2772. 1:37:41further down, I'll actually define my
  2773. 1:37:43hello function. Unfortunately, now that
  2774. 1:37:45I've reordered the functions in this way
  2775. 1:37:47by putting the main part of my code at
  2776. 1:37:49the top and hello at the bottom so that
  2777. 1:37:52my logic kind of flows top to bottom, if
  2778. 1:37:54I go ahead and run python of hello.py
  2779. 1:37:56enter,
  2780. 1:37:57nothing whatsoever happens. If I do it
  2781. 1:38:00again, nothing whatsoever happens. Well,
  2782. 1:38:02why in the world is this? Well, just
  2783. 1:38:05because I've defined a function called
  2784. 1:38:06main and I've defined a function called
  2785. 1:38:08hello doesn't mean that I've actually
  2786. 1:38:10called, that is used either of them.
  2787. 1:38:13Yes, I'm using hello inside of main, but
  2788. 1:38:16no one is telling python to actually use
  2789. 1:38:18or call main. So, in order to tidy this
  2790. 1:38:22up, the last thing I need to do in this
  2791. 1:38:24file it seems is actually call my main
  2792. 1:38:27function. And in fact, by calling my
  2793. 1:38:29main function in this way, it gets me
  2794. 1:38:31out of trouble because now I'm defining
  2795. 1:38:33main first, but I'm not calling hello
  2796. 1:38:34yet. I'm defining hello next, but I'm
  2797. 1:38:37not calling hello next. I only at the
  2798. 1:38:39very end of this file call main, which
  2799. 1:38:42has the effect of running this code up
  2800. 1:38:44here, which has the effect of running
  2801. 1:38:46this code down here, and it allows me
  2802. 1:38:48therefore to organize my file and order
  2803. 1:38:51my functions in any way I want including
  2804. 1:38:53main at the very top, and solving
  2805. 1:38:56ultimately that problem of python not
  2806. 1:38:58knowing what's going on. Now, it's
  2807. 1:39:00important to note that I defined my
  2808. 1:39:02function hello as taking an argument
  2809. 1:39:04two, and then I passed into that
  2810. 1:39:06function the value of the variable that
  2811. 1:39:08I wanted to say hello to, that is the
  2812. 1:39:10variable called name. Because suppose I
  2813. 1:39:12had done something a little bit
  2814. 1:39:13differently. Suppose that I hadn't hello
  2815. 1:39:16as taking an argument, so I just remove
  2816. 1:39:18mention of two and its default value
  2817. 1:39:20world, and I go back up to my main
  2818. 1:39:23function and I just call hello itself
  2819. 1:39:26without passing in any argument. And
  2820. 1:39:28now, let me go ahead and make one more
  2821. 1:39:29change, one more mistake technically.
  2822. 1:39:32Let me go ahead and just try to naively
  2823. 1:39:34print out the value of name in the hello
  2824. 1:39:37function. So, now to be clear, in my
  2825. 1:39:38main function on line two, I'm defining
  2826. 1:39:40my variable called name and assigning it
  2827. 1:39:42the return value of the input function
  2828. 1:39:44from the user. I'm then just calling
  2829. 1:39:46hello. In my hello function, which now
  2830. 1:39:48no longer takes any arguments, I am
  2831. 1:39:51calling print, passing in hello,
  2832. 1:39:53{comma}, and then immediately passing in
  2833. 1:39:55name, the variable into which I got the
  2834. 1:39:57user's input. But the catch is that name
  2835. 1:40:00exists now only in main. And so watch
  2836. 1:40:02what happens when I try to run this
  2837. 1:40:04version of the program with Python
  2838. 1:40:05hello.py. I hit enter. I'm prompted for
  2839. 1:40:08my name, d a v i d, enter, and ah, a
  2840. 1:40:11name error. Name name, {quote} is not
  2841. 1:40:14defined. So it turns out that this is
  2842. 1:40:16actually an issue of what's called
  2843. 1:40:18scope. Scope refers to a variable only
  2844. 1:40:21existing in the context in which you
  2845. 1:40:23defined it. So in so far as I define
  2846. 1:40:25this variable name in my main function,
  2847. 1:40:28I can only use that variable in my main
  2848. 1:40:30function. I can't use it, as I've tried
  2849. 1:40:32to here, in my hello function. It
  2850. 1:40:34doesn't exist in that so-called scope.
  2851. 1:40:36And so this is why now, if I rewind and
  2852. 1:40:39undo all of those changes, you'll see
  2853. 1:40:42that I'm deliberately passing name from
  2854. 1:40:44my main function into my hello function.
  2855. 1:40:47And now in the hello function, it
  2856. 1:40:48technically has a different name. It's
  2857. 1:40:50called two in that context, but that's
  2858. 1:40:51fine. It's completely up to each
  2859. 1:40:53individual function to name its own
  2860. 1:40:55variables or name its own arguments. But
  2861. 1:40:57this is a way now that I'm handing to
  2862. 1:40:59the hello function the value of that
  2863. 1:41:01variable so it can be printed by hello
  2864. 1:41:03as well.
  2865. 1:41:04And there's one final flourish we can
  2866. 1:41:06add here. Now that we've implemented
  2867. 1:41:08hello, you'll notice that hello only has
  2868. 1:41:10a so-called side effect. It only prints
  2869. 1:41:12out something to the screen. Well, what
  2870. 1:41:15if I also want my function
  2871. 1:41:17to not have a side effect per se, but
  2872. 1:41:20actually hand me back a value. Recall
  2873. 1:41:22that the input function returns a value,
  2874. 1:41:25the string that the user typed in.
  2875. 1:41:27Recall that the int function returns a
  2876. 1:41:29value, the float function returns a
  2877. 1:41:31value that was passed into it. Well, you
  2878. 1:41:34can use one final keyword here,
  2879. 1:41:36literally return to return a value
  2880. 1:41:39explicitly yourself. In fact, let me go
  2881. 1:41:41back to VS Code here, and I think we'll
  2882. 1:41:44return our attention to calculator.py
  2883. 1:41:47and see if we can't implement one other
  2884. 1:41:49version of calculate. calculator.py that
  2885. 1:41:52actually has our own function that even
  2886. 1:41:54returns a value. So, I'm going to go
  2887. 1:41:56ahead and open up calculator.py,
  2888. 1:41:59and I think this time I'm going to throw
  2889. 1:42:01everything away as before, and I'm just
  2890. 1:42:03going to start practicing what we're
  2891. 1:42:05preaching here. Define a function called
  2892. 1:42:07main, which is now going to be the main
  2893. 1:42:09part of my function. Let's go ahead and
  2894. 1:42:11now declare a variable called X and
  2895. 1:42:14assign it to the converted version of
  2896. 1:42:17the user's input after asking them
  2897. 1:42:19what's X. So, again, a line of code
  2898. 1:42:21quite like we've done before. And
  2899. 1:42:23suppose now that what I want to do is
  2900. 1:42:25square this value. I want to take the
  2901. 1:42:27number that the user's typed in and
  2902. 1:42:29raise it to the power of two. So, 2
  2903. 1:42:31squared would be 4, 3 squared would be
  2904. 1:42:339, 4 squared would be 16, and so forth.
  2905. 1:42:36Well, how do I go about implementing a
  2906. 1:42:38function literally called square, which
  2907. 1:42:40actually doesn't come with Python built
  2908. 1:42:42in? Well, let me assume for the moment
  2909. 1:42:44that it does exist, and let me say
  2910. 1:42:45something like this.
  2911. 1:42:47Let me go ahead and say that printing,
  2912. 1:42:50how about X squared is
  2913. 1:42:54comma
  2914. 1:42:55square of X. So, what have I done? I've
  2915. 1:42:59defined a function called main, and I've
  2916. 1:43:01implemented two lines. The first of
  2917. 1:43:03these lines prompts the user for a value
  2918. 1:43:05X and converts it to an int and stores
  2919. 1:43:07it in a variable called X. On line
  2920. 1:43:09three, I then say X squared is and then
  2921. 1:43:12I pass a second argument to the print
  2922. 1:43:14function, whatever the return value is
  2923. 1:43:17of a square function. But square doesn't
  2924. 1:43:20exist, and I'll show you this here. If I
  2925. 1:43:22now call main at the bottom, and I run
  2926. 1:43:25python of calculator.py,
  2927. 1:43:30I'll see that X is two and then I see a
  2928. 1:43:33whole bunch of errors. A name error,
  2929. 1:43:35name square is not defined. So this
  2930. 1:43:37isn't a typo here, it's just the
  2931. 1:43:38function doesn't exist, but I think I
  2932. 1:43:40can make it exist here. Let me go ahead
  2933. 1:43:42and define another function called
  2934. 1:43:44square. This one's going to take in a
  2935. 1:43:46number and I'm going to call it
  2936. 1:43:47generically N as many a programmer would
  2937. 1:43:50just to represent any old number and
  2938. 1:43:52then what do I want to do in order to
  2939. 1:43:54square N? Well, a number squared is
  2940. 1:43:57really just itself times itself, so I'm
  2941. 1:43:59going to do this N times N. But it's not
  2942. 1:44:03enough just to do the math yourself, N
  2943. 1:44:05times N. You're going to have to return
  2944. 1:44:07the actual value N times N and that's
  2945. 1:44:10our new keyword here. When I now do
  2946. 1:44:12this, watch what happens. Python of
  2947. 1:44:14calculator.py enter, X say shall be two,
  2948. 1:44:18X squared is four. Let me go ahead now
  2949. 1:44:21and say X is now three, X squared is now
  2950. 1:44:25nine. So I've implemented my very own
  2951. 1:44:27function that returns the square of a
  2952. 1:44:29value and because I'm using the return
  2953. 1:44:31keyword, that ensures that I can pass
  2954. 1:44:34the return value of this, just like the
  2955. 1:44:36return value of input or int or float to
  2956. 1:44:39another function like print instead. And
  2957. 1:44:41here too, there's going to be so many
  2958. 1:44:42ways to solve the same problem. I can
  2959. 1:44:44actually raise N to the power of two.
  2960. 1:44:47We've not seen this syntax before, but
  2961. 1:44:49if you use two asterisks like this, two
  2962. 1:44:51stars, that raises the thing on the left
  2963. 1:44:54to the power on the right. Or it turns
  2964. 1:44:56out there is in Python a function called
  2965. 1:44:58pow for raising something to the power
  2966. 1:45:00that takes two arguments, the first of
  2967. 1:45:02which is the number, the second of which
  2968. 1:45:04is the exponent. So there too, there's
  2969. 1:45:05just so many ways to actually solve that
  2970. 1:45:08same problem as well.
  2971. 1:45:11So ultimately, what have we done here?
  2972. 1:45:12We first introduced functions, these
  2973. 1:45:14actions or verbs, many of which come
  2974. 1:45:16built into Python that you can just use
  2975. 1:45:17in your own code. We then introduced
  2976. 1:45:19variables by which you could store those
  2977. 1:45:21return values and then maybe do
  2978. 1:45:22something more with it. At the end of
  2979. 1:45:24the day too, you now have the ability to
  2980. 1:45:26create to invent your own functions to
  2981. 1:45:28solve simple problems like hello or in
  2982. 1:45:30the weeks to come much more
  2983. 1:45:32sophisticated, more challenging, more
  2984. 1:45:33fun problems as well.

About this transcript

This page contains the full transcript of CS50P - Lecture 0 - Functions, Variables by CS50, generated from the public captions YouTube serves with the video. The transcript has 21,072 words across 2,984 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 choose a caption track available on the video.

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, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.