Placeholder Image

字幕表 動画を再生する

  • All right.

  • Hello, world.

  • This is CS 50 Live.

  • My name is Colton.

  • Argument.

  • Today we're joined by the man himself.

  • David Malin.

  • Hello, everyone.

  • Good to see everyone on stream and not just in the chat today.

  • Um, we're doing something pretty cool today.

  • Pretty fun.

  • We're doing a from scratch of python tutorial.

  • And my understanding is that you have actually brought some questions and topics that challenges e.

  • Oh, I haven't seen we're gonna implement, and I'm going to be a proxy to somebody who maybe is learning for the first time.

  • Indeed.

  • So I'm gonna pretty much assume that Colton may or may not actually know Python.

  • And so along the way, we'll try to solve some problems actively, by a coating, will pull things up by Google or stack overflow as we go.

  • And frankly, try to approximate the experience that Colton and I arguably do every day when we're writing code to sort of figure out problems that we've got it solved.

  • Sure.

  • I guess maybe we should start off first off.

  • But maybe explaining what Python is, folks, we've advertised.

  • This is sort of an experience for people who may not have even programmed at least have not programmed in python before.

  • Yeah, so why don't we assume that no one here is really that familiar with Python?

  • But if you are, that's fine.

  • Hopefully, we can fill in some gaps in your knowledge, stop us at any point by the chat.

  • If you have a question, there's no dumb questions here.

  • If we taken for granted that you know what some concept is or some function is, or some feature just chime in and say, Hey, everyone would What did they mean by that?

  • So that we can answer here on stream.

  • So python is very popular programming language.

  • It's, ah, little different from languages like C and C plus plus and a few others that you would traditionally compile.

  • Recall that if you've ever programmed in one of those languages, you have to actually run some program called a compiler to convert your source code, the text that you write thio machine code, the zeros and ones that the computer actually understands.

  • Nice thing about Python is that we don't have to do that.

  • What is you want to characterize python for folks to distinguish it from that process?

  • Yeah, no I mean, pipe bombs.

  • Nice.

  • It's essentially it runs almost in this.

  • It's called a virtual machine, but it's almost like having a computer inside a computer.

  • And it executes these with a traditional programming language.

  • You're writing text, and I'll switch over to my computer here.

  • Just that we could maybe frame this visually a little bit better, but not chrome invesco.

  • By the way, this is my text editor of choice just V s code you need in order to write any source code, some sort of text editor application, you could even use something like text edit, for example, if I pull it text edit on my Mac, it's not sure that email is there, but I think that might have been from another stream.

  • But essentially what you do is you.

  • You have some program that you can write text into, and usually you don't want to write it in what's called rich texture.

  • You have formatting you rented in plain text like this, and you write your instructions to the computer a bunch of lines.

  • No, this isn't quite nice this year.

  • Unless you don't know how to write good variable names, things might look This might be some people's Python code, but you essentially go through and you write a set of instructions for the computer like this top down and the computer will execute them.

  • But computers don't understand arbitrary textual information.

  • The locator can only speak in certain languages.

  • Computer law, specifically insulin machine coaches finery, which we talked about his 51 another, more underlying principles of that.

  • David has an awesome series of lectures on our YouTube channel.

  • But long story short, somewhere along the line you have to convert all your instructions in tow something that the computer can understand.

  • And with a traditional language that concluded Before you do this with the help of a compiler, it'll take this text and translating it into, you know, they basically bits, which is the computer's native sort of tongue.

  • But how's Python different here?

  • It is different in that it doesn't actually perform this step explicitly along the line is still actually happens, but it basically translates all the lines that you've written when you just load the script into a program that is itself called Python.

  • Yeah, you can think of Python, then is an interpreter.

  • This sort of middle man.

  • That translates between what you rights and what the computer actually understands.

  • So you can think of pipe on.

  • The program, which will run in just a couple of minutes, is a program that reads your own code top to bottom left to right and just does what you say.

  • You don't have to actually use something like visual studio or GCC or clang or any number of other compilers.

  • Explicitly, all of that sort of happens for you automatically, thanks to the interpretation process along the way, and you can even fire up.

  • Let's see if if you have it installed on your machine, for example, here I have a pipe on three.

  • There's two different versions of Python three.

  • A long story short Python three is the new way.

  • It's the way that is going to be the official way very shortly as 2020.

  • But if I type pipe on three and my command line, if you have it installed, you can see that I actually have this sort of prompt here these three arrows, which and I can actually type in instructions and it will immediately execute things.

  • For example, if I wanted to say, uh, print David J.

  • Malin like like this is to me that I type it correctly.

  • It'll actually literally do it without needing to do that intermediary compilation stuff.

  • Yeah, relation.

  • This is an interactive interpreter, so we'll just do what you say.

  • The downside, of course, is that we're not really saving any of our work along the way.

  • So it's better generally to write your programs in an actual text editor like the S code or sublime, or Adam or something else, so that you can save your code and let the interpreter run it and re run it again and again.

  • So why don't we go ahead and do that?

  • You want to go ahead and open up a blank editor window?

  • So for those of you following along at home, if you want to go ahead and Google the S code or visual studio code, you're welcome to download the same program for free Adam Sublime text in no pad plus plus and a bunch of others are very popular on Macs and PCs and Lennox machines.

  • But odds are, if you're tuning in, you probably have some form of text editor, but if not feel free to try one of those, and then you should also do.

  • If you don't have it already is, make sure your computer, if you do want to follow long, has python installed.

  • So again, Python is not only a language, it's an interpreter, a program that will understand your use of that language.

  • And if you go to python dot org's slash downloads, you'll find yourself at the official website, where you can download the Python Version three installer to make sure that you have that on your machine.

  • Depending on how your machine was configured, you may have Python already as cold and said, but it might be an older version.

  • Python, too, that's officially gonna be end of life.

  • Soon, that is, it's being phased out in favor.

  • Finally, of Python three.

  • So everything we do today, it's Python three specific, But beware because when you're Googling when you're looking at stack overflow, there's a lot of good answers to old pipe on code out there so you don't want to be led.

  • A stray serial has a sweet comment, says David.

  • Because of you, I stopped playing video games all day.

  • Learn to code.

  • I now have a real job at an E.

  • Come, come.

  • Wow, that's amazing.

  • I see Andres taking exception to the fact that we haven't promoted Idol.

  • There's a few other I.

  • D Ys and editors as well, this is not an exhaustive list.

  • What's nice about things like V s code is that they are first and foremost a text editor and you can layer more features on top of them.

  • Somebody asked what he s code Am I using?

  • I think I'm using Dracula right now.

  • It's just that at the end of the last stream, but I actually prefer the noctis theme so real quick, just the important stuff out of the way.

  • First, this is nothing to do with python switch to knock this color.

  • And that's the one that I recently have liked.

  • But they all a lot of the defaults look very nice.

  • And you want to let's go ahead and get rid of the Explorer for now.

  • But let's just go ahead and save.

  • This program is hello dot pie.

  • So hello is gonna be the name of the program.

  • Pie indicates that this is a python program.

  • You don't need number three anywhere in there.

  • You still just use dot pie.

  • So our first program, Well, let's keep it super simple and literally write the program that everyone in the world seems to start writing code in, which is just hello world.

  • So instead of David J.

  • Mainland, how would you go ahead and python and just write a program that prints out literally Hello, world?

  • I would I would use the print function which comes with Python.

  • And actually, the nice thing about V s code is that it gives you a lot of this sort of built in intelligence, depending on how you like to develop or it'll actually tell you when you write functions, you know, sort of what the function of what the function expects, what it does.

  • So this is a nice thing about modern text editors and integrated development environments uses.

  • Andi said, Hello world.

  • So I'm just gonna type in the string Hello world and that's it.

  • Yeah, but some and you'll notice now that you get syntax highlighting with programs like this.

  • I see there's a shoutout thio pie charm as well, which is a popular editor for python as well.

  • Yes, we do know that I don't might come with the default installation, but we're just We're just focusing on Python today.

  • You can use any text editor out there that you would like it.

  • It's amazing folks in the chatter actually typing, typing some responses to what you said, and it's actually it's kind of interesting because Gaurav Birds Wall actually says print hello world.

  • And if I'm if I'm reading that correctly he actually said are here She actually said, Hello, world.

  • Now if we try to do you think we should try and run this in?

  • Oh, I don't know.

  • I don't like the squiggly s that suggests that there's a mistake of some sorts.

  • Okay, Undefined variable.

  • How low and this says unexpected token world.

  • So let's go.

  • Let's come back to that, though, because I think we should run the program first, cause while we have arguably written a program now, we haven't actually run it.

  • Previously, when Colton ran the program called Python three, he was able to write a lot of code hit enter, and immediately it was executed.

  • But typically, the paradigm, of course, for writing code is to write one or more lines of code save it in a file and then somehow execute that file.

  • So I think to execute this file hello dot pie we're gonna need a terminal window.

  • Sure.

  • How do we do that?

  • So I already have one.

  • So this is the Mac native terminal application.

  • Windows has the same thing with C and D Lennox.

  • Actually, it's called analytics, which called Olympics, Terminal Terminal or Ex term or a bunch of other things on DSO.

  • I'm actually I'm still in the pipeline show.

  • I'm gonna clear my screen.

  • And then this is assuming that I'm actually in the directory.

  • We're doing this code.

  • So right now we're at my belief.

  • Dev is the pwd that tells you where you're working.

  • Yeah.

  • So this is for President Working directory.

  • I'm here.

  • Users J Harbor.

  • Deb, I want actually go into where I just created a file, which is streams slash python tutorial.

  • I believe that is where I created it.

  • And you'll see I hit l s for list.

  • And then if I type in Python three like we did before to open up the interactive show.

  • But instead I say hello dot pie after that.

  • Well, see that it actually outputs exactly what we wrote in the source files.

  • And while trivial, go ahead.

  • If you could go back to the terminal window literally run by a python three hello dot pie again, this should go without saying, but because the code is saved and file, you can, of course, repeat your steps.

  • You can improve on the program.

  • You can fix bugs again and again.

  • Can't do that as easily in just the interactive interrupt.

  • You lose all that state that you've created a scene as you exit that the interpreter in that context.

  • So the place we usually start when introducing Python other students is to actually now make the program actually interact like a program that just prints out Hello world.

  • While sort of nice to be part of that history for posterity, it's not particularly interactive at all beyond just running it.

  • So how would in python you go about asking a user for his or her name and then say hello?

  • So and so instead of just hello world all the time.

  • Well, it's common in a lot of languages that you get some sort of input function of Java has one system dot was it?

  • You create a scanner at system got in?

  • Um, see youse a scan if and in pipe on it's actually pretty easy.

  • All you have to do is just say, Let's just say I want to store the user's name in a variable called name and I'll sign it whatever the return value of this function input is.

  • And it noticed that I'm still passing in this string.

  • Hello, world, because when I asked him at the command line, maybe a presumably want prompt them for something Or maybe say, in this case, not hello world.

  • But please enter your name colon space.

  • Okay.

  • And then if I wrote what was tried out, let's go ahead and run it.

  • So he notice Colton's using some keyboard shortcuts.

  • He's sitting up on his keyboard, which on a lot of computers, will actually rerun the previous command.

  • And you can keep hitting up, up, up of up to go to previous commands further.

  • So I'm already realizing there is gonna be a slight issue with this.

  • So please enter your name.

  • Let's say my name is Colton and the program is over.

  • Nice.

  • Which is expected, Yeah.

  • I mean, you only want one line of code that is asking user for input, prompting them for their name and then storing that value over on the other hand, side of the other side in the name variable.

  • So what do you want to do next?

  • Well, now, since I have name actually stored somewhere I've set name equals or against the value of input, please into your name.

  • I actually reuse that variables, Aiken say print hello space.

  • And then there's multiple ways to do this, which might have time to look at.

  • But all for now, we'll just say hello.

  • And then I'll actually add name to that string like that.

  • Yeah.

  • So what's this?

  • Plus, I mean, we're not doing addition here?

  • No.

  • So this is common in a lot of languages.

  • If this is a term called concatenation, where you're not actually adding numbers, you're actually adding strings.

  • You're basically taking one string and sticking another string right at the end of it in the common operator for that use case on, there are multiple operators across different languages.

  • But common is the plus just cause it's kind of intuitive.

  • Okay.

  • All right, well, let's let's go ahead and run this.

  • It's now two lines of code Colin has saved.

  • This file is going back to the interpreter and running it on this file, and we say hello comes to work now.

  • It's kind of it's kind of cold.

  • Hello.

  • If I wanted, I could probably add maybe a exclamation point at the end of it just to make it a little bit more.

  • You know, uh, I guess inviting or friendly or unhappy face to see you.

  • Oh, true.

  • I could say I get Say hello, Colton.

  • Uh, now, unfortunately, that's gonna print since my unhappy face for everyone, not just Colton, but even me and for you.

  • So why don't we go out and change this?

  • I feel like in a lot of programming languages that we've seen that folks out there might have seen.

  • You can do things like conditions.

  • You can do something.

  • If something is true, else you can do this other thing.

  • So could we modify this program in Python to maybe be happy to see anyone except you?

  • In which case you should see the unhappy face.

  • Well, this feels like a depressing.

  • Hey, where this is This is what the audience is requested.

  • Oh, is it?

  • Okay, so we'll say.

  • Okay.

  • So, Well, uh, we're going to get the name, and then I want to basically check to see what the value of the name is because you said we want the frown on Lee appear whenever it's me and otherwise.

  • Exclamation point.

  • I'm assuming anything else.

  • Really?

  • Anything.

  • Okay, so we'll say we'll keep it simple.

  • So I'll say if name is equal to Colton Colon.

  • So this is a python thing.

  • If you want to do it.

  • If statement you cultivate cold.

  • Yeah, Golden Colin, Then you say it will say print Hello on Will do.

  • Plus name.

  • Plus.

  • And I guess in this case, we could even hard code the name, since it's specific to this use case, and then we'll do the frown.

  • Put a space in front of the smiley face because it was getting put right on the let's make sure we see how unhappy we are.

  • Yeah, exactly.

  • And then we'll just do this other, you know, thing.

  • If that if statement doesn't turn out to be the case, if the name is David or anybody else literally in the entire universe, then We'll just say else would do what we did before, which is just print the name.

  • And then, in this case that we do want.

  • We do want the program to be happy to see who it is, so we'll do the next right.

  • So let's run this and then let's come back.

  • We'll tease apart some of the syntax that we've just added.

  • Sure, So we'll say.

  • First of all, we'll say, David.

  • Hello, David.

  • So he was happy to see you.

  • Very nice and polite.

  • How about can we do another one?

  • Can we?

  • D'oh!

  • About Richard.

  • Hello, Richard.

  • Richard.

  • Okay, so please don't you named Richard.

  • Okay.

  • Delightful to see you as well.

  • You see, Richard, now let's let's test to see if our functionality is correct.

  • R r r, I guess one off use case and we'll use my name.

  • Oh, perfect.

  • This is This is a good program.

  • So far, we have a smart, smart program that knows the appropriate waiter.

  • Just sort of, though, can you run it one more time?

  • And it's like an angry person.

  • Can you type your name in all caps this time?

  • Oh, sure.

  • Absolutely.

  • Sell.

  • Say, Colton.

  • Oh, so it is happy to see me.

  • Oh, no, this is a bug.

  • This is objectively a bug.

  • So maybe we're going to take a look at a couple of their syntactic features, but especially if you're a little new to programming, but you have the right intuition.

  • Hear it?

  • Folks in the chat wouldn't mind proposing what techniques we could use to actually solve.

  • This problem will go back in and fix it in just a minute or 200 sets a feature, by the way, that no, that's a buck by.

  • But let's go ahead and back to the code for just a moment.

  • Well, folks proposed solutions here, perhaps, and there's a few questions I had.

  • For instance, if I'm coming, a python knew what's with the Coghlan's like I'm used to the indentation, but the Coghlan's When we're the curly braces, the so the nice thing about Python is there are no curly braces.

  • The interesting thing about Python is that everything is based on white space eso indentation blocks from, and this is something that folks may or may not be familiar with, but did, noting that something essentially is inside of something else is done with indentation.

  • So this if statements like body of code that executes when it's true we don't use curly brace braces like we do in other languages.

  • We can't wait.

  • We can in line it.

  • But you would instead of doing something like this and then having, uh and actually, I'm not 100% sure.

  • I think this, I think, technically isn't technically possible to, you know, it's not.

  • I'm getting that conflated with I think something else, but you don't know another stream entirely, but moving on you don't need these things.

  • You you've done all you just need to do is make sure this is indented a certain level within this after the same statement here and without opening a can of worms.

  • What's your preference?

  • You like tabs?

  • You like spaces.

  • I like tabs they could convert it into space is okay.

  • I'll take that as an acceptable answer.

  • Indeed, in the Python community, unlike most languages, there is an accepted norm, or at least a recommended norm, which is to use spaces.

  • So let's put an end to that debate today.

  • We're gonna use spaces.

  • Four space is generally would be pretty common, but I like that You don't have to do the the curly braces, as you know, but I noticed you're also using single quotes, and I feel like in different languages.

  • And even in python, I sometimes the single quotes sometimes double quotes.

  • How do I think about that?

  • So there are for the for the large part interchangeable.

  • You can make strings with double quotes.

  • You could make strings with single quotes.

  • Now, if you're coming from a background like C or Java, you can only use double points for strings.

  • But you have to use single quotes for single characters.

  • Okay, Python.

  • There's no difference.

  • There are no there is not really a by default single character versus string when you're using, you know, this apostrophe character or the double quote character.

  • So how would people go about deciding whether on a single quote kind of person or a double quote kind of person?

  • Well, I think that's a matter of taste.

  • But if you are use if your string, for example, contains, let's say, uh, Colton's favorite food pizza, we can see that there's an issue in that different things that we see the green that we've seen before with strings.

  • After this apostrophe.

  • Up until this apostrophe, where we're actually using it for grammar direction, using it as a part of the string itself on there's a couple ways of dealing with this.

  • One way is we could use the backslash like this, and that will fix it.

  • So now we've actually said, Ignore this apostrophe.

  • This is part of the actual string Gen.

  • Gen X Ax notes that as well.

  • Nice.

  • But let's say I don't like the way that looks.

  • I don't like having a backslash start my strings all over the place.

  • Instead, I could decide in any case, where I do have a single quote inside the string itself.

  • All use double quotes on the outside.

  • And because this doesn't match up with this, it doesn't cause an error in parsing the strength.

  • Nice.

  • Yeah.

  • I mean, I'm kind of going through an existential crisis.

  • I've been using double quotes for like 20 years, and frankly, I'm tired of holding this shift down, and I do think you're seeing and more modern Lee popular languages python and javascript among them.

  • I'm starting to see frankly, I think single quotes more often, but you got to beware because of the stupid grammar issues, like so I don't know.

  • Just be consistent.

  • I think is the point or resort to one or the other when you have to contextually personally big single quotes, so as well.

  • Yeah, yeah, I know too much shift.

  • 20 years of hating the shift key is enough.

  • Somebody proposed an interesting way of getting around the problem.

  • Yeah, let's come back to that.

  • I heard someone proposed that we use lower, so forced the whole string to lower case.

  • I see Richard proposed using an array or a list in python where we could check for little Colton or Big Colton Orr alternating caps.

  • Colton, I think the downside there's that you're gonna have inner knowing number of permutations of Colton's, if you will, with the sea is capitalized, or the O or the L or multiple ones and so forth.

  • So I like this lower idea.

  • This is actually pretty conventional in a lot of programming languages, especially python.

  • When you want to compare something for equality, you can Ah, Nikolai zit so to speak.

  • You force it to all lower case or all lower all lower his are all upper case so that you know definitively what you're comparing against, but I don't want to do it destructively.

  • You want to do it essentially temporarily on a copy of the very bright.

  • Exactly.

  • So how could we do that here to make sure that we're unhappy to see you.

  • Well, we could say essentially, here's here's what happens.

  • I guess we should demonstrate this in the terminal before we before we do anything else.

  • So I'm gonna pull up pipe on three.

  • And this is a nice thing, actually.

  • First, what I'm gonna do, I'm gonna clear my seal I and then I'm gonna pull a python three.

  • If you're ever curious about using a function and python, this is the down.

  • This is the upside of being able to use it in an interpreter context.

  • You can actually fire your interpreter and just test variables test functions right away.

  • Andi, I happen to know that there's a function called don't lower.

  • It works on strings, but let's actually experiment with it.

  • So if I say, uh, let's do, for example, the string Colton and we'll say lower parentheses and will hit.

  • Enter and we'll see that indeed brings it all down.

  • Now.

  • This is weird.

  • Why do you have a period after a string?

  • Don't you need to do with that on, like a variable or something?

  • So this is then this gets into kind of the idea of what's called object oriented programming, which won't dive into today.

  • But essentially every string in Python has a series of functions that it has its own reference to and the function when you when you call it, using this dot which essentially means go inside of the thing to the left and then execute this thing that belongs to that thing on the left.

  • In this case, the lower function sort of belongs to this string.

  • Colton.

  • It will run this function and sort of implicitly pass in this as a parameter to this function.

  • Yeah.

  • So what you really have is that Colton is indeed a string in this case, in the string is an actual object, technically a class in python.

  • So it has all of these phone features built into it.

  • Just one of which is this this lower function.

  • So I see ah Hassan Ali proposed.

  • I think what's actually a perfect solution on and that's a little simpler and less code to write than something like a list based approach.

  • So she wanted deployed Hassan's approach.

  • Sure, so first, before we do that, let's just test it again.

  • So we have the version where it's my name.

  • Tuck.

  • Normally, let's tight.

  • Let's test it with the version of my name and all upper case, which, uh, does also go to the name Colton.

  • And then just for just for kicks, Well d'oh, well do old MySpace style text, and then we'll showing your There we go, and then we'll see that no matter what permutation of capitalization we use were unhappy to see you.

  • Yes, exactly.

  • Coming back down to the same basic case.

  • Someone exit python and then we'll adopt What was Assan said, which was, if name lower is equal to two.

  • Not not to Colton with a capital C, because that would that won't work.

  • There will never be in capital letters, right, I think.

  • Well, Andre caught it there, but and I oversee misstated.

  • We do have to make one tweak, which is Just replace that with a lower case, C dude.

  • And if you run this, let's go back here and we'll do pipe on three of hello hi again, which I could have just hit up, probably.

  • And well, first, we'll try it with my name like this.

  • Okay.

  • Hello?

  • Cold?

  • It's a It's a frown.

  • Okay, so that was the behavior that was happening before.

  • Let's try it again.

  • But this time, let's let's do just the first few couple letters capitalized.

  • Oh, but here's the interesting thing is actually okay, Never mind.

  • It's still printing out.

  • CEO.

  • A nice feature would be to have it print out the name with just a capital letter and not well, funny, you should say, because our next challenge is gonna be exactly that.

  • So your user does miss type his or her name in some stupid way, like holding a capital C Capital.

  • He'll win classic Colton.

  • We can, at least with some probability, fix this in code.

  • Now I say some probability because at least in English, there's a lot of names that have a mix of uppercase and lower case characters.

  • O'Reilly, for instance, is the last name, also the name on a very popular series of computer science books.

  • You might know, so we could get this wrong.

  • But let's assume that we have very simple names in this case, like Colton Capital.

  • C O.

  • How in Python could you force the user's input to be Nietzscheans?

  • Not all lower case, but Capital C and then lower case.

  • Colton.

  • Well, my first instinct would be probably to use this name the lower as our starting point.

  • We'll bring everything well, Kanon ical eyes all the names back, all lower case like this, and it's actually let's actually start with that.

  • So we'll go named Lower, actually other way around.

  • Well, actually, no.

  • We'll do in both cases will do in both cases.

  • So now you'll notice that I'm printing name dot lower in the actual output for both situations and where we've typed in the name.

  • So that way, even if we type in capital C capital o l T o n.

  • It's still gonna give us the economical ized strengths and not the best design.

  • But we'll get there.

  • Well, let's try this out.

  • So we'll say this plays into your name and we'll try Colton like that.

  • It doesn't say hello, Colton.

  • With everything lower case, we're halfway there.

  • Will have to now do the very first string first character in the first character in the string.

  • So I actually off hand don't know what function strings might have to capitalize the first letter, but the chip those chat happened.

  • But no looking at looking at How would you do this if you were home alone with that alive twitch stream to answer these questions for sure.

  • So I would probably say Python capitalize first letter strings.

  • So have the name of the languages.

  • The first word?

  • That's probably gonna be a major thing that Google optimizes for Andi.

  • Just literally kind of the main words that I care about in the query for what I'm Looking at, which capitalized not, You know, we specify what the first letter in a string, and this is something you learn over time.

  • And odds are most everyone in the chat room is one of the authority you don't have to ask Google were being or whatever use full sentences.

  • You can drop the words that really aren't adding anything to the query, like how and and and it and is and really just focus on the words like Colton has here that really have more information content for the search engines that do that query and the Google is amazing, the first result says.

  • In Python, the capitalized method converts the first character of a string to capital letter uppercase letter.

  • If the string has first character as capital returns the original string, yeah, and I don't use this particular site too often myself.

  • Geeks for Geeks stop or but it seems on point.

  • And just for kicks, can you scroll down just so we can see what some of the other search results are?

  • Sure, so we're seeing Stack.

  • Overflow is number two stuck Overflows Number three.

  • A blogger is number four programming.

  • Uh, program is dot com is the next, so you'll start to see patterns and search results.

  • If you're learning programming or teaching yourself, Stack Overflow is a big one.

  • But sometimes Google thinks that it's pretty sure what it knows what the answer is.

  • So it'll just bubble it up special.

  • So the top art to try this but shout out to those of you in the chat job on three and Davis to 16 all the phone got this and major drug facts.

  • If I'm pronouncing that right also had a clever solution, but arguably, you're reinventing the wheel.

  • And I would propose that in Python you really try to reuse some of the wheels that have been given.

  • But odds are underneath the hood capitalizes doing something quite like that, but probably with some error checking just in case.

  • Name is is short.

  • Indeed.

  • So we'll say name dot lower.

  • Now, this is an interesting thing.

  • So when we call this method dot lower, it's actually returning a string and new straight on.

  • Ben, that's essentially what we're doing here.

  • We're returning a new string opening into the output were returning a new string and comparing it to the string Colton.

  • So the cool thing is that you can actually change these methods onto themselves, so I can say name dot lower.

  • Which gives me the name Colton in all lower case or whatever name in all lower case letters.

  • But I can then d'oh dot capitalize like so.

  • And what this will do is this will say Okay, give me the thing.

  • Give me the dot Lower method of the thing to the left.

  • Okay.

  • And return it so this will give whatever the name is all lower case letters, and then this becomes essentially that.

  • Then when you called dot capital lies, it's doing the same thing as saying Give me the value to the left and then capitalize its first letter.

  • That thing to the left is now that lower case string.

  • Now when you hover over, capitalize.

  • Based on the definition of this function that GS code is feeding us here.

  • Do we need to force things the lower case first?

  • And how would you go about figuring that out returning capitalized version of the string more specifically, make the first character have upper case and the rest lower case.

  • Ah, that's interesting, actually realize that that was part of the signature.

  • Well, let's try it then.

  • In that case, So let's say if name dot capitalize named are capitalized.

  • And in this case, I mean, we could even we could even do it here and just say, Colton and then dot capitalize stride out.

  • So let's say the name of Colton first.

  • Okay.

  • Hello, Colton.

  • Let's try.

  • C o l t o n.

  • Not a man.

  • We have a whole new problem.

  • How do we fix that problem?

  • Uh, we get better.

  • Is in the terminal.

  • Oh, it turns out that that actually worked, so I looked a few steps ahead of us, right?

  • So besides trial and error like we just did to you concerning read the official documentation.

  • In fact, just so folks concede if you've never seen Pythons documentation, it's so so like all the most of the information is there.

  • But it's not always that easy to find.

  • What you're looking for is not a huge fan of the Pythons.

  • Didn't know it could be much more user friendly.

  • S o Can you go ahead and find us the documentation for the stirring classes capitalize function So we'll go to S t R, which is the string class and python a string object type and weigh what went right here.

  • String capitalized and it looks like it's almost the verbatim.

  • So yes, go is probably getting its its instructions right from here.

  • But this is good that we have the documentation up and notice how it's ordered.

  • You have on the left indented all of left aligned.

  • You have all of the function names in that class boldface, no less capitalized case fold center Someone in the chat just a moment ago.

  • Proposed title.

  • The title function.

  • Is there a title function inside the string class?

  • Let's go ahead and look.

  • There's is Title string, not title yet.

  • So is this title case version of the string or words start with an uppercase character, and the remaining characters are lower case.

  • So this would be great for for full names.

  • Yeah, assuming no weirdness would like lower case letters needing to be lower case.

  • Still sure, but yeah, absolutely.

  • So we could maybe even experiment with that.

  • So if I were to say Let's go back to GS code, let's say I'm a specifically Maybe there's a Colton Jackson that we that we'd like to see.

  • But Colton Ogden is the one we don't want to see.

  • So why don't we go ahead and test that out?

  • So we'll say named Title is equal to because, remember, title doesn't capitalized as but to multiple words in a string.

  • So in this case, we don't have just one word, actually have two words separated by space.

  • So let's go ahead and say, named a title and named title, and there is a better way to do this, which we should probably also take a look at in just a second as well.

  • So we'll go ahead and run this code, and I'm gonna enter my name as Colton Ogden.

  • But all lower case letters still don't like him.

  • Oh, it looks like it figured it out night.

  • And actually, our programming This is this artificial intelligence.

  • And this is the very This is the seed for artificial intelligence.

  • Six lines.

  • State of the art.

  • No, I would propose here too.

  • We are using online three title to compare against Colton Aubin Capital C Capital O.

  • I admit I don't love that.

  • Maybe you can convince him otherwise.

  • It just feels weird to not just choose something super simple.

  • Like all lower case are all upper case.

  • Because this assumes that now I have the sophistication or my colleagues know what title does and therefore this just seems a little more fragile.

  • Would you agree or disagree?

  • I think I would learn.

  • They agree.

  • I think I would say I think I would say What I don't like the most is that we're using name dot title dot the DOT title meant that specifically in three places.

  • That for sure, but I don't like title for formatting, but for quality testing personally, I would suggest you something that's absolutely robust.

  • All lower case, all upper case or another technique altogether.

  • But we'll get there in just a moment.

  • Another something good up my sleeve for you.

  • Yeah, well dot lower lower.

  • Probably be better change.

  • That's adult lower, just a dot lower.

  • And then here's a nice little short kind of using V s code in your highlight something.

  • Let's say I want to edit this and this.

  • What's this?

  • At the same time, it's actually highlight this.

  • If I hit command D or controlled the on a Windows computer, I actually have multiple cursors.

  • Now I can actually go over here and type lower.

  • Amazing in two different places, talking twice as fast as I could have been my day.

  • Here we go.

  • So let's let's take things up a notch if we can, because you know you have a middle name, right?

  • So in a lot of people have middle names, and we want to make sure that we want to be unhappy about all cold and Ogden's not just you, specifically.

  • So how could we go about adapting this program in such a way that it is unhappy to see Colton something Ogden or Colton?

  • Something else.

  • Ogden.

  • So that there's a Colton at the beginning and ogle at the end.

  • We don't care what's in the middle, but there can be something in the middle.

  • How would you go about solving that problem?

  • A middle name.

  • Middle name?

  • Yeah, but one name, two names, arbitrary number of middle age.

  • I just don't want to see any Colton's over here or Ogden's over here.

  • Okay, Sure.

  • So, um, in this case, what we can probably dio is figure out what?

  • First of all, we're going to get input in a string of some kind.

  • It's gonna be this presumably space separated names.

  • Okay, I'm sure.

  • So they're in pipe on a nice thing that you can do is you can actually do this thing called Split.

  • You can split a string.

  • Okay, so it's going to the end of the terminal here and actually pull up the interpreter again, like you just want to play.

  • Yeah, exactly.

  • Let's just get a sense of what the function looks like.

  • And so and indeed answered the questions on the chat Yes, we're essentially at the moment, just trying to get the first word in the last word.

  • Yeah, so we'll say.

  • Let's say I have Colton Ogden here in a string, and I want to split it into its constituents because so I'll just you got split, which is another method inside of the string cloud.

  • Exactly.

  • And well, we have some interesting syntax here, but it looks like it did find my first name and it separated out my last name.

  • And what are these square brackets around you?

  • Why am I seeing so these square brackets are indicative of what you might have seen in job or C in array.

  • But in Python, it's called a list.

  • And the nice thing about listen pipe on is that they can be arbitrary size that can grow dynamically.

  • You don't have to allocate memory for them if you're coming from or statically based background.

  • And I'm smiling because the sandwich in the chat here is proposing that your sample middle name be Colton.

  • Make love in Ogden, which is a great reference to a great movie question here about.

  • Can we use the operator from Richard?

  • Because if the name is double bear bear double barrel that wouldn't work.

  • So in just tests for the presence of a sub string anywhere in the string beginning, middle end or anywhere else in between.

  • So it doesn't feel like the right fit.

  • We deliberately want to check the beginning and the end.

  • So I think the road we're going down is it just one approach that we can take?

  • Someone's has stirred up find as well.

  • But again, I think that would also do lead us down the same track.

  • Something stirred out find just literally finds whether and not a vision asked, What if someone has to plus middle names?

  • That too, we want to be sensitive to.

  • So how?

  • Let's let's go ahead.

  • And, uh, well, I don't I don't just like the approach you're taking just splitting on the strings.

  • But how do I go about checking for Colton at the beginning and Ogden today?

  • And it can we try and, like, make love in and maybe a two word middle name, just to see what the result looks like.

  • You know what to code for calling the 11 which is fantastic.

  • Next, you have three.

  • You have David J.

  • Malin is your name will use that as well.

  • So let's split that so again, it worked just like it did before.

  • Even though we have three names.

  • That too, it's split on spaces.

  • Can I see your names?

  • Will see David J.

  • Mc Love and Malin eyes.

  • That's a mouthful, and we'll split that.

  • Okay, so every string from the original string ends up inside of the list.

  • It does.

  • So really, we want to check the beginning of the list and the end of the list for Colton and Ogden and Unhappiness, we d'oh and lists are nice and that you can just reference.

  • If you want a specific spot in the list, you can say Let's make a list here, which has five numbers in it.

  • Well, actually for confusion.

  • Let's not do that.

  • Let's say I have apple, orange and banana as a list, and we could excuse our names as well.

  • But if I want to get a specific element from the list, I can use what's called an index and and this is a numerical indexes.

  • So list start, they're ordering of their elements at zero.

  • So, for example, in this case, L at index zero is Apple wise one if it's the first element because, uh, computer scientists decided that zero is how they like to start numbering things.

  • And that's actually think Lou is one indexed scratches wanted next to presumably for user friendliness.

  • Maybe because Louis I know Lou.

  • It was originally designed as a conflict language for less technical people who scratches a programming environment for people that are fresh programming and computer science.

  • But ironically, just ends up confusing because then they eventually have to change the transition to see or to Java or whatever.

  • Now, what's this?

  • Is here?

  • Is that a number one on the left?

  • No, that's an L.

  • And it l is just a short hand in this case for a variable for list l for listen is typically frowned upon.

  • You don't want to use a letter as a variable name for most circumstances.

  • Outside of maybe some context.

  • Well, we'll get to a little bit later, but in this case, I just wanted a quick list.

  • So I'm gonna just use l again and one l looks like a one in a model space.

  • Fun.

  • If I use these brackets just like we did for the actual Thio actually show the list itself.

  • But I say l at index zero.

  • That's how you can almost like almost like a box if we're thinking about it like there's little boxes, little cubbies for each individual spot in our list.

  • If we do this, we'll see that l at index zero returns.

  • Apple L index one gives us orange Ellen next to gives us banana so everything is sequentially laid out.

  • In what fruit are we gonna see it?

  • L bracket three.

  • That's a good question.

  • Looks like Python is smart enough to know at runtime whether or not we're actually indexing outside of the rain.

  • We've allocated for less so in languages like C N C plus plus, by default, you're typically going to see no error.

  • But you're gonna be touching memory.

  • You shouldn't or the program could even crash.

  • If you do something like this, this is course.

  • It is an exception.

  • We'll talk about that perhaps in another stream, But it's a way of handling errors.

  • And similarly, what?

  • What is L bracket Negative one, for instance, can go to the left.

  • Oh, yeah, absolutely.

  • So l bracket negative one now this this would cause chaos.

  • I think if you get really worried about this is gonna break the computer.

  • Oh, interesting.

  • So, Nana, l negative one gave us.

  • It looks like banana.

  • The last element of our list.

  • Interesting.

  • No.

  • Why's that?

  • Well, it looks like it turns out that in py

All right.

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

B1 中級

PYTHON TUTORIAL!- CS50ライブ、EP.63 (PYTHON TUTORIAL! - CS50 Live, EP. 63)

  • 1 0
    林宜悉 に公開 2021 年 01 月 14 日
動画の中の単語