-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py.txt
2227 lines (2222 loc) · 108 KB
/
test.py.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
[GENTLE ORCHESTRAL MUSIC PLAYING]
DAVID MALAN: All right, this is CS50's Introduction
to Programming with Python.
My name is David Malan and this is our week on functions and variables.
But odds are, many of you, most of you, have never actually programmed before.
So let's start by doing just that.
Let me go ahead here and open up my computer
and on it, a program called Visual Studio Code or VS
Code, which is just a very popular program nowadays
for actually writing code.
Now, you don't have to write code using this particular tool.
In fact, all we need at the end of the day
is a so-called text editor, a program for writing text.
And, heck, if you really want, you could even use something
like Google Docs or Microsoft Word.
You'd have to save it in the right format,
but really, at the end of the day, all you need is a program for writing text
because that's what code is-- text.
Now within this particular program, I'm going
to have the ability to create one or more files
via this top portion of the screen.
And I'm going to do so by diving right in
and doing this at the bottom of my screen.
At the bottom of my screen is a so-called terminal window
and this is a command line interface or CLI interface to the underlying
computer, be it your Mac or your PC or even some server in the cloud.
And what I'm going to do here is literally write, "code" and then
the name of the file that I want to code, for instance, Hello.py.
As we'll soon see, any program that you write in Python generally has a file
name that ends in .py to indicate to the computer that it's indeed a program
written in Python.
Now you'll see here at the top of my screen, I have a blinking cursor,
a line one-- which is where the very first line of my code is going to go--
and then just a tab that reminds me of the name of this file Hello.py.
And without even knowing much Python, I'm
going to write my very first program here as follows--
print("hello,world").
And you'll see that, at my keyboard, some of my thoughts
were finished for me.
I only had to type one parenthesis and the other one automatically appeared,
and that's just the feature that we'll see of tools like this tool here.
Now even if you've never programmed before,
odds are you can guess, infer what this simple program is going to do.
And it's only one line. print("hello,world").
Indeed, when I run this program ultimately,
it's just going to say, Hello to the world.
And, in fact, this is a very famous--
perhaps the most canonical-- program that you
can write as your very first program in Python or any other language,
and so that's what I've done here.
But on my Mac, my PC, even my phone, I'm generally in the habit,
like you, of running programs by double clicking an icon
or just tapping on the screen.
But I see no such icons here.
And in fact, that's because my interface to at least my current Mac
or PC or some server in the cloud, is again only a CLI--
command line interface-- which even though it
might feel like it's a step back from the menus and buttons and icons
that you and I take for granted every day, you'll find, we think,
that it's ultimately a much more powerful interface and incredibly
popular to use among programmers in the real world.
So to run this program, I'm going to have to use a command
and I'm going to move my cursor back down to the bottom of the screen
here where previously I already ran one command--
the command Code which has the effect of opening VS Code in my computer,
and then I passed in the name of the file that I wanted to code up.
Now I have a chance to type a second command,
and you'll see I see a second dollar sign.
Now the dollar sign here doesn't indicate any kind of currency or money,
it just is the symbol that's generally used
to indicate your prompt-- where the command line interface wants
you to put those commands.
Now the command I can run here is going to be this--
I'm going to run Python of Hello.py.
Now why is that?
Well, it turns out that when I actually write code in a language like Python,
it's of course stored in that file--
Hello.py, but I need to interpret the code top to bottom,
left to right so that the computer knows what to do.
Indeed, at the end of the day, even if you don't really
know much about computers, you've probably
heard the computers only understand zeros and ones, the so-called binary
system.
Well if that's the case, then something that says "print" and parentheses
and quote unquote hello world, is not surely zeros and ones.
We have to somehow translate it into the zeros and ones
that the computer understands.
Now fortunately, so long as you've installed such a program in advance,
there's a program as well as a language called Python.
So Python is not only a language in which we're going to write code,
it's also a program otherwise known as an interpreter
that you install for free on your own Mac or PC or some server in the cloud
and you can then run that program, that interpreter, passing to it as input
the name of your file like mine here, Hello.py.
And then that program, that interpreter, will
handle the process of reading it top to bottom, left to right
and translating it effectively into those zeros and ones
that the computer can understand.
So let's do just that.
Let me go back to VS Code here.
I already typed out Python of Hello.py but I didn't get hit Enter.
And that's what's now going to kick off this command
and hopefully, if I didn't mess any of this up,
I should see my very first program's output to the screen.
And voila, hello, world.
So if you too have typed exactly that same code
and have executed exactly that same command,
you will have written your very first program in this case in Python.
Well now let's take a step back and consider,
what is it that we actually just did and what
is it we're looking here on the screen?
Well first and foremost, in most any programming language,
you tend to have access to what are called functions.
A function is like an action or a verb that
lets you do something in the program.
And generally speaking, any language comes with some predetermined set
of functions-- some very basic actions or verbs that the computer will already
know how to do for you, that the language, really,
will know how to do for you.
And you, the programmer, the human, can use those functions at will
to get the computer to do those things.
Now the program in question here, Hello.py is using one function.
And you could perhaps guess what it is.
That function is, of course, going to be this function print
and that print function, of course, doesn't
print some preordained string of text.
That is to say, it prints whatever it is you want it to print.
And here too, do we have another piece of terminology in the world
of programming, namely arguments.
An argument is an input to a function that somehow influences its behavior.
The people who invented Python, of course,
didn't necessarily know what it is you and I
are going to want to print to the screen, so they designed this print
function using these parentheses with the ability
to take as input some string of text, be it
in English or any other human language, that is
what you want this function ultimately to print onto the screen.
And what is it that the program is ultimately doing on the screen?
Well, it's printing, of course.
It's showing us hello, world on the screen.
And that's generally in programming known as a side effect.
It can be visual, it can be audio.
In this case, it's something that appears on the screen
and functions, therefore, can indeed have these side effects.
One of the things they can do as this verb or action is
to display on the screen as a side effect, something like those words
that we wanted--
hello, world.
So that's my first program and, you know, I'm feeling pretty good.
Everything worked as planned.
I didn't make any mistakes, but, honestly,
when you're learning how to program and even
once you've learned how to program years later, you're going to make mistakes.
And those mistakes, of course, are referred
to a term you might already know, which is that of a bug.
A bug is a mistake in a program and they can take so many forms.
And take comfort, perhaps, in knowing that over the coming weeks,
you're going to make so many mistakes.
You're going to have so many bugs in your code, just like I did
and just as I still do.
And those bugs themselves are just mistakes
that are problems for you to solve.
And over the weeks to come, we're going to give you
a lot of tools both mental and technical via which you can solve those problems.
But just don't get discouraged if when writing your program for the first time
it doesn't even work that first time, it will with time, with practice,
and with experience.
So let me deliberately now make a mistake
that there was a non-zero chance I might have done accidentally already,
but I got lucky.
Let me go ahead and just suppose I forgot
to include something like the closing parenthesis
at the end of this line of code.
The code is almost correct, it's like 99% of the way there,
but now that I've pointed it out, it's pretty obvious
that it's missing that closed parenthesis.
But even little, seemingly minor details like that, that you and I as humans
wouldn't really care about.
And if you're sending an email or a text message, whatever, it's just a typo.
It's not that big a deal.
It is going to be a big deal to the computer.
A computer is going to take you literally,
and if you don't finish your thought in the way the language expects,
it's not going to necessarily run at all.
So let's do this.
I'm going to go ahead here and clear my screen down at the bottom
just so I can start fresh, and I'm going to go ahead and run
this version of my program after having made that change by deleting
the parenthesis.
I'm going to go ahead and type Python again of Hello.py and this time
when I hit Enter, I'm hoping I'm going to see hello, world,
but here we have an error on the screen, a so-called syntax
error, which refers to my having made a mistake at my keyboard.
And this one, fortunately, is pretty straightforward.
It indeed says that this open parenthesis was never closed.
And so that's probably pretty intuitive.
Now what I need to do, I need to, of course, to close it.
Unfortunately, sometimes the error messages we'll see in the coming weeks
are not going to be nearly that user-friendly.
But there too again, with experience, with practice, will you
get better at debugging such programs.
Let me now make sure that I indeed fixed it correctly.
Let me go ahead and run now Hello.py and hit Enter and, voila,
we're back in business.
Well let me pause here and see if we have any questions now
about Python itself, writing, or running even the simplest of these programs.
AUDIENCE: Could I write code inside a word or, for example, Microsoft Excel?
And what's the barrier to doing that?
DAVID MALAN: A really good question, and allow
me to very explicitly say to the entire internet
that you should not write code with Microsoft Word.
I mentioned that only because it's a tool via which
you can write text and code is, at the end of the day, just text.
But it's not the right tool for the job.
We don't need bold facing, underlining, paragraphs and the like.
We generally want something much simpler than Microsoft Word or Google Docs.
And so VS Code is an example of just a more general purpose text editor.
Its purpose in life is to allow you, the human, to edit text.
Nowadays these text editors come with many more features.
In fact, you'll notice that even in my code here,
even though it's just one line, there's a bit of color to it.
The word "print" for me is appearing in blue.
The parentheses are black.
And we'll see as we write more lines of code, more and more of the lines
will come to life in various colors.
Now that's just one feature of a text editor.
We'll see too that it has features like this built-in terminal window.
It's going to have a built-in tool for debugging or finding problems
with code.
And it's just a very popular tool nowadays,
but there are many, many others out there.
You're welcome to use them for this course and beyond.
We just happen to use this one in large part
too because you can also use VS Code nowadays for free in the cloud.
How about one other question here on programming with Python
or hello, world or syntax more generally?
AUDIENCE: Yeah I was trying to ask if it's not
possible to run the computer using the terminal window?
DAVID MALAN: I think I heard is it not--
if it's possible to run the program without the terminal window?
Are you--
AUDIENCE: Yes, sir.
DAVID MALAN: OK, you froze for me again.
But let me infer what the question is.
So in this environment, as I've configured my computer,
I can only run these Python programs via the terminal window.
Now that's good for me, the programmer, or the person who's
trying to learn how to program, but it's not very good
if you want to ship this software and have other people use your actual code.
You can absolutely write programs and then allow other people to use,
not a command line interface, but a graphical user interface or GUI--
G-U-I. This is just one mechanism and perhaps, I think,
the best one with which to start writing code
because eventually it's going to give us a lot more control.
Allow me to forge ahead here, but please feel
free to continue asking questions along the way if only via the chat.
Let's consider now how we might go about improving this program.
Let's go about improving this program to make it a little more interactive
and not just assume that everyone is going to want to be greeted
more generically as hello, world.
Let's see if I can't get this program to say something like Hello, David
or Hello, Jeremiah or Hello, Horatio or whatever the actual user's name is.
Well to do this I'm going to go back up to Hello to pi
and I'm going to add another line of code at the very top that simply says,
for instance, what's your name, quote unquote with an extra space at the end.
So I'm printing to the user asking them a question for some input,
but now I need another function to actually get input from the user.
And, perfectly, enough Python comes with a function named input.
So here I'm going to go ahead and call a function input, open paren,
close paren.
And that's going to prompt the user with just a blinking cursor waiting
for them to type something in.
Now it turns out, if I read the documentation for the input function,
it actually takes an argument itself.
I don't need to use "print" separately and then prompt the user for input.
So I can actually simplify this code before we even use it.
I'm going to go ahead here and take that same string from print,
put it as an argument to the input function,
and get rid of the "print" altogether.
And, in fact, that "print" would have added a new line anyway.
So now I've just got a prompt where the user's cursor is
going to end up blinking at the end of the line asking them, what's your name?
In my terminal window I'm going to run Python of Hello.py, Enter.
OK.
We're making progress.
It seems that this new function input is indeed prompting me, the human,
for input.
So I'm going to type in my name--
David-- and hit Enter.
Unfortunately, it doesn't really do anything with my name,
it just outputs it immediately.
All right, well, I could fix this, right?
I could go up to line 2 and I could change "world" to "David,"
and then back in my terminal window here I can do Python of Hello.py, Enter.
What's your name?
David, Enter.
And there we go.
All right now I'm up and running.
Now my program is working as intended.
Of course, this isn't really working as intended here.
Let me go ahead and try pretending to be my colleague, Carter here.
Well Carter's name is this.
I'm going to go ahead and hit Enter and I'll see, of course, Hello, Carter--
well, obviously not, because I've hardcoded, so to speak,
I've written literally my name inside of the string.
So we need some way now of actually getting back what the user's input is
and doing something with it ultimately.
And for this we're going to leverage another feature of programming,
specifically a feature of some functions, which is that they
can have return values as well.
If you think of input as being, again, this action,
this verb-- you can actually personify it as maybe a person,
like a friend of yours that you've asked a question of
and you've asked your friend to go get input from someone else--
go ask that person their name.
And if your friend comes back knowing that person's name, well,
wouldn't it be nice if they handed that name back to you?
That's kind of what we need metaphorically the function to do,
is get the user's input and then hand it back to me so that I, the programmer,
can do something with it.
But if it's going to be handed back to me,
I kind of want to put it somewhere so that I can then print it back
on the screen.
I need to do the equivalent of take out like a piece of paper or Post-It note,
write down on this piece of paper what it is the human has said,
so that I can then feed it into as input that print function.
And to do that, we're going to need one more feature of programming, namely
variables.
And odds are, most everyone's familiar with variables from math class
way back when-- x and y and z and the like.
Well, programming has that same capability, this ability
to create a variable-- in this case, in the computer's memory--
not just on a piece of paper.
And that variable can store a value--
a number, some text, even an image or video or more.
A variable is just a container for some value inside of a computer
or inside of your own program.
So how do I go about expressing myself in this way?
Well I think what I'm going to do is introduce
a variable that's a little more interestingly named than x or y.
I could just say this-- x = input.
But I'm going to use a better name than a typical mathematical variable here
and I'm going to literally call my variable "name," why?
Well in programming, because I have a whole keyboard in front of me,
I can use more descriptive terms to describe what it is I'm writing.
And now, though, there's an opportunity to consider a specific piece of syntax.
We've seen parentheses, we've seen quotes, all of which
are necessary when passing inputs to a function,
but this equal sign here that's in between input on the right and name
on the left is actually important.
And it's technically not an equal sign per se.
It doesn't mean equality as much as it means assignment.
So in Python and many programming languages,
a single equal sign is the assignment operator
and what that means specifically is that, you
want to assign from right to left whatever the user's input is.
So the equal sign copies from the right to the left whatever the return
value of the function on the right is.
So again, the input function clearly gets input from the user.
That's why I was able to type my name or Carter's, but it also
sort of behind the scenes hands that value, that return value, back to me
the programmer.
And if I use an equal sign and a variable, no matter what I call it,
I can store that input in that variable so as to reuse it later.
So now, sitting in the computer's memory somewhere,
is a container containing "David" quote, unquote or "Carter" quote,
unquote, or whatever the human has typed in.
But here it's easy to make a mistake.
Suppose I decide to try to print that name and so
I kind of on a hunch type in this, hello, name.
Just kind of plugging in the name of the variable.
Well let me go ahead here and run Python of Hello.py and hit Enter.
That's going to prompt me for my name.
And let me type in my name--
D-A-V-I-D. But I haven't hit Enter yet.
And perhaps via the chat, what's going to happen here when I now hit Enter?
I'm hoping it says, Hello, David.
I'd be OK if it says Hello, world, but I don't want it to say what it's actually
going to say and, yep, what we're seeing in the chat is,
well, it's probably going to say literally Hello, name.
So that's not quite right.
So we need another way of printing out the value inside of that variable
rather than just this word "name."
Well let me try this in a couple of different ways.
Let me try this as follows--
let me go ahead and maybe undo this because I've gotten pretty good already
at saying Hello.
So let's draw that line in the sand and just say, all right,
let's get at least get Hello comma out the door.
Let's now print name and just on a hunch, I'm going to try this.
I'm going to use print again--
because you can use these functions as many times as you need--
and I'm going to pass to the print function the variable called name.
But notice I'm being a little clever now.
I'm not putting it in double quotes because we've seen already that double
quotes means literally print out N-A-M-E.
I'm getting rid of the quotes this time in hopes that,
now by passing the variable called name to the function called print it will,
in fact, go about printing the contents of that variable that is,
its so-called value.
All right, let's go ahead and do this here.
Python of Hello.py, Enter.
What's your name?
David.
And now, crossing my fingers still, I see Hello comma David.
All right.
So it's not the best program.
I'm kind of cutting some corners here, so to speak.
I'm saying Hello, David on two separate lines.
So it's not as elegant, it's not as pretty,
it's not as grammatically appropriate in English as just saying it
all in one breath on one line, but at least I've solved the problem, just not
very well yet.
But let me take a step back now and perhaps introduce
a couple of other concepts with which we should be familiar,
which is as our programs get longer and they're no longer just one line or two
or even three, eventually our programs are going to become dozens of lines,
maybe even hundreds of lines long.
Let's set the stage for success moving forward.
It turns out that Python and a lot of programming languages
also support something called comments.
Comments are notes to yourself in your code
and you include comments by way of a special symbol-- in Python
it's going to be the hash symbol, typically--
and that allows you to write the equivalent
of a note to yourself but in a way that's not going to break your code.
The computer actually ignores your comment.
It's just there for you, it's just there for your teacher,
it's just there for your colleague with whom
you're sharing ultimately that code.
So if I go back to VS Code here and I just
want to add some comments to this program to explain to my teacher,
to myself, to my colleagues what this program is doing,
well, let's go ahead and do that.
I'm going to go at the very top of my program and on line 1
now I'm going to move that original line of code down a bit,
I'm going to add a hash, and I'm going to say something like this,
ask user for their name.
Now, I don't have to use that language.
I don't have to use that text.
I could use any human language whatsoever.
It doesn't have to be English.
But I'm going to now, below that, just say something like this--
say Hello to user.
And you'll notice that VS Code by default
is kind of graying out my comments.
They're no longer blue, there's no red, there's no color in them.
And that's just because they're notes to myself
and the computer ultimately is going to ignore them.
But what we have now is two comments-- ask user
for their name and then a second comment, say Hello to user.
And I've just kind of commented each chunk of code,
like each line or lines plural of code, that are doing something noteworthy.
Why?
Well, tomorrow morning when I wake up having
slept for quite some time, forgotten what it is I did the previous day,
it's convenient with comments to just see
in English or your own human language what
it is this program is doing so that you don't have to read the code itself
and, better yet, if there's maybe a mistake down the road,
you can read what your intention was and then you can look at the code
and figure out if your code is now doing what you intended.
So this isn't really necessary for a program this small.
It's pretty obvious with just one or two or three lines
what the program is doing.
It's just as fast to read the code than the comments,
but getting into this habit is generally a good thing--
to comment your code every one or a few lines so as to remind yourself
and others what it is your intent and your code is doing.
What's nice about comments too is this--
comments can also serve to be sort of a to-do list for yourself.
There's this notion in programming of pseudocode.
Pseudocode isn't a formal thing.
It's not one specific language.
It's just using English or your own human language
to express your thoughts succinctly, methodically, algorithmically,
so to speak.
But pseudocode, therefore, because it's not Python
and it's not necessarily English, it just kind of allows
you to outline your program even in advance.
So for instance, if I wasn't sure today how
I wanted to go about writing this program
but I didn't know what I wanted to do, I could have started today
by just writing this in Hello.py.
No code, I could have written just a couple of comments to myself--
Step 1, ask user for their name.
Step 2, say Hello to user.
Then once I've outlined my program in pseudocode, then I can go in there
and say, all right, how do I ask the user for their name?
Well, I can do input "what's your name" question?
And then on the left here, I can maybe put a variable and assign it to that.
OK, how do I say Hello to the user?
Well, I know I can use print to say things on the screen.
Let me say hello, and let me--
OK, let me now print the person's name.
So again, pseudocode is a nice way of structuring your to-do list,
especially if you have no idea how to write the code,
because it breaks a bigger program down into small bite-sized tasks.
All right, let me pause here to see if there are now
any questions on comments, pseudocode, return values, or variables.
Any questions we can clear up here?
AUDIENCE: Yeah, my question is does the function input
work for any type of information, or only for words?
DAVID MALAN: Yeah, really good question.
So according to its documentation, and we'll look more at formal documentation
soon, input is going to expect what's called
a string, that is a sequence of text, be it
in English or any other human language.
But it's indeed going to be expecting text with which to prompt the user.
A good question.
How about another question from the group, if we could?
AUDIENCE: I wanted to ask how would I make a several line comment?
DAVID MALAN: Oh, how do you do many lines of comments,
if I'm hearing you correctly?
AUDIENCE: Yes.
DAVID MALAN: Sure.
You would just keep doing them like this.
You just prefix each of the lines with a hash symbol, like I'm doing here.
There is another technique for doing multi-line comments in Python that
actually tend to have special meaning.
You can do three double quotes like this and then anything in between here
is a comment.
That's another technique.
Or you can use single quotes as well.
But more on those, I think, another time.
All right, well, you don't mind, let me forge ahead here
and see how we might improve this program further
and also introduce a few other features that we might
want to take into account over time.
So it turns out that we can certainly improve on this program
because it's a little disappointing that I'm cutting this corner
and saying Hello comma and then on a new line printing out name.
Like we can do better.
And most programs you use on your phone or your laptop certainly
keep text together when people want.
So how can we go about doing that?
Well there's a few different ways.
And in fact, the goal here is not so much to solve this one problem,
but to demonstrate and emphasize that in programming--
Python and other languages-- there's so many ways
sometimes to solve the same problem.
And here's one way to solve this problem.
Let me go in here and let me go ahead now and say hello,
and let me just add to the end of that the user's name.
So I'm using + in kind of an interesting way.
This is not addition, per se.
I'm not adding numbers, obviously, but I do
kind of want to add the person's name to the string of text Hello comma.
Well let me go now down to my terminal window and run Python of Hello.py
again, Enter.
What's your name?
I'm going to type in David.
Enter.
OK, it's better.
It's better, but there's a minor bug, albeit aesthetic here.
There's missing space, but let's just use some intuition here.
Well, if I'm missing the space after the comma,
why don't I go ahead and just add it manually here.
Let me now rerun the program Python of Hello.py, Enter, David, Enter.
And there we go.
Now we have something that looks a little prettier
in terms of English grammar.
Hello, comma, space, David.
And now if we rewind, you might have noticed before
or wondered why I had this seemingly extra space after my question mark,
namely here.
There's a space after the question mark but before the double quote
and that was just for aesthetics too.
I wanted to move the user's cursor one space to the right
so that when I typed their name or they typed their name,
it's not immediately next to that same question mark there.
But there's other ways we can do this.
It turns out that some functions, print among them,
actually take multiple arguments.
And it turns out that if you separate the inputs
to a function-- the so-called arguments to a function-- with a comma,
you can pass in not just one, but two, three, four, five, onward.
So let me go ahead and pass in not just hello, comma, space, but that followed
by name.
And this is a little confusing potentially at first glance
because now I've got two commas but it's important to note
that the first comma is inside of my quotation marks, which is simply
an English grammatical thing.
The second comma here is outside of the quotes,
but between what are now two separate arguments to print.
The first argument is hello, comma, space.
The second argument is the name variable itself.
So let's see how this looks.
Python of Hello.py, Enter.
What's your name?
David.
Enter.
OK, I've kind of over-corrected.
Now I've got two spaces for some reason.
Well, it turns out, and this is subtle, when
you pass multiple arguments to print, it automatically inserts a space for you.
This was not relevant earlier because I was
passing in one big argument to print all at once by using that + operator.
This time I'm passing in two because of the comma.
So if I don't want that extra space, I don't need to pass in one myself,
I can just do this and now notice, if I run this program again--
Python of Hello.py, type in my name David,
now it looks grammatically like I might want.
Now which of these approaches is better?
This approach uses a function print with two arguments--
Hello, comma and the name variable.
The previous version, recall, technically used one argument,
even though it looked a little curious.
It's one argument in the sense that the computer, just like mathematicians,
are going to do what's inside of parentheses first.
So if inside of parentheses you have this string of text--
hello, comma, and a space, which I need to add back.
Then you have a +, which means not addition, per se,
but concatenation-- to join the thing on the left and the thing on the right.
This ultimately becomes the English phrase--
Hello, comma, space, David.
And then what's being passed ultimately to the function
is technically something like this, but it's doing it all dynamically.
It's not me typing in David as I discreetly did earlier.
It's figuring out dynamically what that value is after concatenating Hello
with the value of name and then passing that ultimately
to print as the sole argument.
Let me pause here to see if there's any questions on numbers of arguments
now to functions.
AUDIENCE: Can we use a function many times
to solve a certain problem which we can encounter many times in our code?
DAVID MALAN: You can.
You can use a function many different times to solve some problem.
What we'll soon see, though, is if you find yourself
as the programmer solving a problem the same way again,
and again, and again, it turns out you'll
be able to make your own function so that you
don't have to keep reusing the basic ones that come with the language.
AUDIENCE: I was curious about the comma and the + sign.
So after + sign, can we give just one variable and after comma again we
give multiple variable status?
What is the difference?
DAVID MALAN: A good question.
So in the context of strings--
and I keep using that term.
"String" is a technical term in a programming language
and again, it means a sequence of text-- a character, a word, a whole paragraph,
even.
So the + operator is not just used, as we'll
see, for addition of numbers in Python, like we do on paper pencil,
but it also is used for concatenation of strings on the left and the right.
If you did want to combine not just two strings--
left and right-- but a third and a fourth,
you can absolutely keep using +, +, +, +, and chain them together
just like in math.
Eventually that's going to start to look a little ugly,
I dare say, especially if your line of code gets long.
So there's better ways that we'll actually soon see.
And a good question as well.
Well let me come back to the code here in question
and see if we can show you just a couple of other ways
to solve the same problem, along the way emphasizing that what we're technically
talking about here, yes, are strings, but there's even
a technical term for these strings in Python,
it's just STR, so to speak, S-T-R for short, for string.
As you may know if you programmed in other languages,
people who invent programming languages like to be very succinct, to the point,
so we tend to use fairly short phrases to describe things,
not necessarily full words.
So while you might say "string," technically
in Python what we're really talking about, these sequences of text,
are technically STRs.
This is an actual type of data in a program.
But we'll soon see that there's other types of data in programs as well.
In fact, let's see if we can't improve this in one other way.
I like the progress we've made by keeping everything on the same line--
Hello, David all on the same line.
What more though could we do in terms of solving this problem?
Well, it turns out that we didn't have to give up entirely
with using print twice.
Let me rewind a little bit and go back to that earlier version
where I wasn't really sure how to solve this problem so I was using print once
to print out just the Hello and the space and the comma.
And then I used print again to print name.
That's, strictly speaking, wasn't bad, but there was this visual side effect
that I didn't like.
It just looked ugly to have these two lines of text separate
from one another.
But there's another way to fix this.
Clearly it seems to be the case that the print function is automatically
outputting a blank line.
It's moving the cursor automatically for me to the next line
because that's why I'm seeing Hello on one line and David
on the next and then my prompt--
the dollar sign-- on the line below that.
So print seems to be presuming automatically
that you want it to move the cursor to the next line
after you pass it some argument.
But you can override that behavior.
Again, functions take arguments which influence their behavior,
you just have to know what those arguments are.
And it turns out that if we look at the documentation for Python's print
function, we can actually look up at this URL here--
docs.python.org is where all of Python's official documentation lies.
If I poke around, I can find my way to more specifically this URL
here where I can find all of the available functions in Python
and the documentation there for.
And if I go a little more precisely, I can even
find specific documentation for the print function itself.
And rather than pull that up in a browser,
I'm going to go ahead and highlight just one line from that same URL, which
is this, and this is easily the most cryptic thing we've seen yet,
but this is the official documentation for the print function.
And one of the best things you can do when learning a programming language
is, honestly, learn to read the documentation,
because truly, all of the answers to your questions will in some way
be there, even though, admittedly, it's not always obvious.
And I will say too, Python's documentation
isn't necessarily the easiest thing, especially for a first time or novice
programmer.
It too just takes practice, so try not to get overwhelmed if you're not
sure what you're looking at.
But let me walk you through this example.
This again is a line of text from Python's official documentation
for the print function.
What this indicates as follows is this--
the name of this function is, of course print.
Then there's a parenthesis over here and another close parenthesis way
over there.
Everything inside of those parentheses are
the arguments, the potential arguments, to the function.
However, when we're looking at these arguments
in the documentation like this, there's technically a different term
that we would use.
These are technically the parameters to the function.
So when you're talking about what you can pass to a function
and what those inputs are called, those are parameters.
When you actually use the function and pass
in values inside of those parentheses, those inputs,
those values are arguments.
So we're talking about the exact same thing-- parameters and arguments are
effectively the same thing, but the terms you
use from looking at the problem from different directions.
When we're looking at what the function can take versus what
you're actually passing into the function.
So what does this imply?
Well this syntax is pretty cryptic, but at the moment,
just know that an asterisk, a star, and then the word "objects"
means that the print function can take any number of objects.
You can pass in 0 strings of text, one string
like I did, two strings like I did, or, technically, infinitely many
if you really want, though that code is not going to look very good.
After that we see a comma, then we see another parameter here
called SEP, short for separator in English.
And notice the equal sign and the single quote, space, single quote.
So 'space' I don't know what that is yet,
but I think we've seen a hint about it.
Let's focus though for a moment on this--
the print function takes another parameter called end and the default
value of that parameter is apparently, based on this equal sign and these
quotes, /n.
And what is /n, if you'd like to chime in in the chat?
Anyone who's program before has probably seen this,
though if you've never programmed before, this might look quite cryptic.
Backslash n means new line, and it's a way textually
of indicating if and when you want the computer effectively
to move the cursor to the next line, create a new line of text.
And so technically, if we read into the documentation,
we'll see more detail on this.
The fact that there's a parameter called end in the documentation for the print
function, just means that by default this print function is going to end
every line with /n.
You don't literally see /n, you see a new line.
You see the cursor moving to the next line.
Now by that logic, let's move backwards.
SEP four separator-- the default value of separator
is apparently a single blank space.
Well where have we seen that?
Well recall in an earlier example when I passed in not just one but two
arguments to the print function.
Recall that they magically had a space between them.
In fact, they had that space + my own space
and that's why I deleted my space because at that point it was extra.
So this just means that when you pass multiple arguments to print, by default
they're going to be separated by a single space.
By default, when you pass arguments to print,
it's the whole thing is going to be ended with a new line.
Now just by knowing this, and let me literally
wave my hand at the rest of the documentation for another day,
there's more things that print can do, but we're
going to focus just on SEP and on END, let's see if we can't leverage this now
to solve that original problem.
The original problem was this, I don't like how Hello, David
is on two different lines.
Well that's happening again because print is automatically
printing out a new line, so let's tell it not to do that.
Let's tell it by passing a second argument to the first use of PRINT
to say END = quote, unquote--
not /n, which is the default automatically.
Let's make it quote, unquote nothing else.
Let's override the default value so there is no new line.
There's literally nothing there.
And let's see what happens.
Let me now go down to my terminal window and clear it.
And I'm going to run Python of Hello.py, Enter.
I'm going to type in my name, David and I
think now everything is going to stay on the same line because--
and it did-- this line here, 5, is going to print out Hello, comma, space,
but then nothing at the end of it because I
changed it to be quote, unquote.
The second line is going to print the name, David,
or whatever the human's name is, and it will move the cursor to the next line
because I didn't override the value of END there.
Just to see this more explicitly, if you do something cryptic like,
well I have no idea what's going on.
Let me just put in temporarily three question marks here.
We'll see the results of this too.
Let me go back down to my terminal window, run Python of hello. py,
what's your name?
David.
And now you see literally really ugly output,
but you see literally what's going on.
Hello, comma, space, then three question marks, END, that print statement,
and then you see D-A-V-I-D. So not a good outcome,
but it demonstrates just how much control we have here too.
And let me rewind further.
Recall that in our other version of this,
when I passed in Hello, comma and name, they were separated by a single space.
So Python of Hello.py, D-A-V-I-D, Enter.
That just worked.
Well what if we override the value of SEP for separator?
Instead of being one space, we could say something
like, question mark, question mark, question mark just to wrap
our minds around what's going on there.
Let me now do Python of Hello.py, D-A-V-I-D, Enter.
And you see two.
These two inputs--
Hello, comma and the name are now separated in an ugly way
by three question marks because I've overridden the default behavior of SEP.
And even though the documentation uses single quotes,
I've been in the habit of using double quotes.
In Python you can use either.
Strictly speaking, it doesn't matter, but you should be consistent
and I generally always use double quotes.
Python's documentation, though, always uses single quotes.
Questions now on these types of parameters?
And allow me to propose that we give these an official name.
Up until now, when we've been passing values to print,
those are called positional parameters-- positional in the sense
that the first thing you pass to print gets printed first.
The second thing you pass to print after a comma gets printed second.
And so forth.
But there's also these things we've now seen called named parameters.
Named SEP, separator, or END, E-N-D for the line ending.
Those are named parameters because one, they're optional
and you can pass them in at the end of your print statement,
but you can also use them by name.
AUDIENCE: This may be a weird question, but I was wondering,
what if someone wants to add actually quotation marks within the quotation
marks?
DAVID MALAN: Yeah, I like how you think.
This is what we would call a corner case, right?
Just when we've made-- right, this is all
sounding great, at least as programming goes, but, wait a minute,
what if you want to print a quote?
That's a really good question.
Well, let's see if we can't figure this out.
Suppose that I want to print out not just the user's name.
Let me simplify this further.
Let me go ahead and get rid of a lot of this,
and let me just say something like, Hello--
maybe I'm being a little sarcastic here.
Hello, (sarcastically) "friend."
You know, in that kind of tone.
Well this is not going to work, actually,
because you are trying to use quotes to be like "friend" in finger quotes,
but you're also trying to end the sentence.
And if I try running this, let's do this.
Python of Hello.py, you'll see that this is just invalid syntax.
Perhaps you forgot a comma.
And this is actually a bit annoying.
Sometimes the error messages you see are misleading.
Like the computer, the language, doesn't really
know what's going on so it gives its best
guess, but it's not necessarily correct.
But I can solve this problem in a couple of ways.
I can do this--
I can change my outermost quotes to single quotes.
Because recall a moment again, I said you
could use double quotes or single quotes so long as you're consistent.
So that's fine.
If you use single quotes on the outside, you
can then use double quotes on the inside and you'll see them literally.
So for instance, if I run Python of Hello.py, there we go.
Hello, "friend."
But there's another way.
If you insist on using double quotes as you might want to,
just to be consistent, you can also use that backslash character again.
We saw the /n a moment ago.
And that meant we don't want a literal n to be in the output,
we wanted a new line.
So the backslash actually represents what's called an escape character.
An escape character is one that you can't just
type necessarily once on your keyboard.
You need to express it with multiple characters.
So I can actually put backslashes in front of these inner double quotes
so that the computer realizes, Oh wait a minute,
those aren't quotes that finish or start the thought, they're literal quotes.
So now let me go back to my terminal window, run Python of Hello.py, Enter.
And now it's working as well.
So escaping is a general technique that allows us to do that too.
And if I may, let me rewind now on these examples
and go back to where we left off with my code,
I'm just undoing all of that, because I want to get back to the point
ultimately of specifying now a final way of solving this problem.
Well, it turns out that we have yet another way
we can solve this problem, which is perhaps the most frequently done
now or at least the most elegant when it comes to setting us up
for longer and longer uses of strings.
You can use a relatively new feature of Python that allows you to do this.
You can literally put, not the name of the variable like that in your string,
because we already saw this is wrong, right?
If you do this, you will literally see Hello, comma, name.
But what if I do this?
What if I put curly braces or curly brackets around the variable's name?
Notice VS Code is actually very subtly changing the color of it.
So VS Code knows something interesting is going on here.
Let me run this program, but I'm not done yet Python of Hello.py, Enter.
D-A-V-I-D, Enter.
OK, obviously not what I want, but I need to tell Python
that this is a special string.
This is what we're going to call a format string or an F string,
a relatively new feature of Python in the past few years
that tells Python to actually format stuff in the string in a special way.
And the symbol via what you do this is a little weird,
but this is what the world chose.
If you put an F at the beginning of the string,
right before the first quote mark, that's a clue to Python that, ooh,
this is a special string.
Let me format this in a special way for you.
Let me now rerun the program.
Python Hello.py, Enter.
D-A-V-I-D, Enter.
And now we see the goal this whole time--
Hello, David.
We don't start with this way, because I think if we did this the first way,
you'd be like, why are we doing this?
What are all these magical symbols?
But this is just yet another way to solve the same problem.
But let me propose that we consider now yet
other things we can do with strings.
And it turns out that even as we've been doing some relatively simple operations
here, we've generally been trusting that the user is going to cooperate
and that is to say that they're going to actually type in what we want them
to type.
Now just because they type a string, though, doesn't mean
it's going to look the way we want.
You and I, honestly, as humans are actually
in the habit-- on websites and apps-- of like accidentally hitting
the spacebar a lot, either at the beginning of our input
or at the end, maybe because the space bar tends to be so big.
It's pretty common to get accidental spaces
before or after some users' input.
You and I are definitely in the habit of not necessarily capitalizing words
like we should.
If we're sending text messages, we're probably being a little quick
and just sending everything in lowercase,
for instance, if that's your style, if your phone's not fixing it for you.
Maybe in a formal letter you would capitalize things properly.
But you and I as humans, we can't really be trusted to type things in a nice way
necessarily when using some piece of software,
be it an app or a website or something else.
But it turns out that strings themselves come
with a lot of built-in functionality.
You can see all of that in Python's own documentation here.
The string data type that we've been talking about
comes with a lot of functionality built in that
means that we can manipulate the user's input to do more than just
join it with something else like Hello, we can actually clean it up
or reformat it in a way that hopefully looks a little better for us.
So let me go back to my code here and let me just
demonstrate what might happen if a user doesn't cooperate.
If I go ahead here and run Python of Hello.py, Enter.
Let me just sloppily hit the spacebar a few too many times.
Why?
I just wasn't paying attention.
And now I'm going to type in my name D-A-V-I-D and, I don't know,
I hit the spacebar a couple more times.
Like it's kind of a mess.
It's all lowercase.
That's not going to necessarily look grammatically right.
It's got spaces here and here.
The program is going to print exactly that
and that looks really bad, at least if we're
prioritizing aesthetics and grammar.
Like, why are there so many spaces after the comma?
This is not a very nice way to greet your users.
But we can clean this up.
It turns out that built into strings, which, again, is this data type, so
to speak, this type of data in Python--
is the ability to actually do things to that string.