YouTube2Text

CS50P - Lecture 0 - Functions, Variables — Transcript

by CS50 · 21,070 words · 2,982 segments · language en · Watch on YouTube

Full transcript

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

What you can do with it

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

Free YouTube transcript tool

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