Placeholder Image

字幕列表 影片播放

  • That's enough for the interactive shell for now. Let's write our first program.

  • In IDLE, click on File > New File.

  • This makes the file editor window appear.

  • Enter the following code: I'll ex[plain what it does afterwards.

  • # This program says hello

  • and asks for

  • my name. Add a blank line, followed by

  • print('Hello world!')

  • print('What is your name?')

  • # ask for their name

  • myName = input(), print(

  • 'It is good to meet you, ' + myName

  • print('The length of your name is:')

  • print(

  • len(myName))

  • print('What is your age?')

  • # ask for their age

  • myAge = input()

  • print('You will be ' +

  • str(int(myAge))

  • + 1 +

  • ' in a year.' Save this program as hello.py.

  • Python programs are just text files that have the .py

  • file extension. Then click on Run >

  • Run Module or just press F5 to run the program.

  • "Hello world! What is your name?" Al.

  • "It is good to meet you Al. The length of your name is

  • 2. What is your age?" Thirt-

  • 26. Yeeeah.

  • I am 26. You will be 27 in a year."

  • Ah, I remember when that was true.

  • Okay, that's the entire program. One thing to keep in mind

  • is that Python will start at the first instruction at the top of the program,

  • and then move downwards executing each instruction entered

  • Think of it as putting your finger on the top instruction

  • and as each instruction is executed moving your finger

  • down to the next instruction. This is just like how you might move your finger

  • along the lines of a book that you're reading.

  • Your finger is called the execution. The execution

  • in Python starts at the top and moves down.

  • So the first line with the

  • pound sign is called a "comment". Python ignores comments.

  • You can use them to write notes to yourself or reminders

  • about what the code is trying to do or what the entire program does.

  • Blank lines are completely ignored by Python. You can just use these to space

  • out your code.

  • In fact, let's add a couple more blank line right here

  • just to group together the

  • code that's relevant to asking for your name, and

  • the code that ask your age. The

  • print() function displays the string value inside the parentheses

  • on the screen. So the line print('Hello world!')

  • means print out the text in the string 'Hello World!'.

  • Functions in Python are like mini-programs in your program.

  • They contain code that you various things and for your convenience Python

  • comes with a lot of them.

  • In code, a function is its name,

  • followed by parentheses and optionally

  • sometimes there are values passed

  • to the function inside the parentheses.

  • In this context these values are called

  • arguments. But really, values and arguments are the exact same thing.

  • This is called calling the print() function.

  • We have code like this here. So the next instruction is also a call

  • the print() function. This time it's passing the 'What is your name?'

  • string to the print() function. There's also a comment at the very end of the

  • line. You can always add comments

  • to the end of an instruction since everything after that

  • pound sign will just be ignored by Python. When the input function

  • is called, Python waits for the user to type some text on a keyboard and

  • press Enter.

  • Just like a variable will evaluate to the value it contains,

  • calls to the input() function evaluate to the string value

  • of what the user typed. So if I typed in

  • "Al" when input() was called, this

  • myName variable assignment statement would just evaluate to 'Al'.

  • And then the string will get stored inside myName.

  • Remember, expressions can always evaluate down to a single value.

  • So if 'Al' is stored in myName

  • and this is what that call to the print() function looks like

  • first my name evaluates to the value inside it

  • and then, this is just a string concatenation,

  • so the next step looks just like this:

  • This is the string that gets passed to the print() function call.

  • This next line introduces a new function called

  • len(). What len() does is it takes a string

  • argument and then evaluates

  • to the integer value of the length of the string,

  • which is the number characters in a string. Let's just experiment with this

  • function in the interactive shell for a little bit.

  • so I can call the len() function and pass it

  • 'Al' and it returns 2.

  • Or 'Albert' to return 6. Or

  • length... the blank string and that returns 0.

  • and then I can just use these function calls to len() anywhere

  • I could using integer. So I could say, you know,

  • type out my name ten times, it's length would be 20.

  • So then we have some more print() and input() calls.

  • This time we're saving whatever the user types

  • into the variable myAge. And then we have this line,

  • which introduces the str()and int() functions.

  • Now this seems really complicated, so we'll just take this one step at a time

  • The str() and int() functions return string and integer

  • values of whatever you pass them. This is really handy if you need to convert

  • between data types. So in the interactive shell

  • you can have something like str()

  • of the value 26, and that returns a string value

  • with 26 in it. And if you have to go the opposite route,

  • you can call the int() function and pass it

  • a string value like '1234' and it will return in integer value.

  • There's also a float() function that you can use

  • to convert something to a floating-point value.

  • So you can pass a string to it, it'll return a float value.

  • Or if you pass an integer value to it, it will return

  • a floating-point version value of that

  • integer. Notice it has the decimal point and .0.

  • This may seem pointless but remember that the input() function always returns a

  • string value, even if the string value something like

  • '26'. We can't do math on a string.

  • That returns an error. First we have to get in integer value

  • of it. So that's why we passed it to the

  • int()function. And just like you can't do addition on strings, you can't do string

  • concatenation on integers.

  • So we're going to have to call the str() function

  • to convert that into a string value. This is What the

  • entire last line

  • looks like when it- when Python evaluates it. First

  • the myAge variable will evaluate to the

  • value that's inside of it. Then that gets passed to the

  • int() function, which returns an integer form of it.

  • We add 1 to that integer. We have to convert that

  • back to a string value, because we want to concatenate

  • the other streets next to it. And then after that, it's just the string

  • concatenation.

  • And so

  • all this just reduces down

  • to this string, which we pass to the print() function call,

  • which then gets it displayed on the screen. That's how the entire program works.

  • Just to recap, the file editor is where you type in code for a program.

  • The execution starts at the top and then proceeds

  • down, executing each instruction in turn.

  • Comments begin with a pound sign

  • and are just notes that are ignored by Python. They're just notes for the

  • programmer.

  • Functions are kind of like mini programs that your program can run

  • to execute some code that does some specialized thing. In this case

  • the print() function care has code for making strings appear on the screen.

  • There's a print() function for

  • making text appear on screen. There's an input() function for getting text from the

  • keyboard.

  • And there's also a str(), int(),

  • and a float() function that returns an integer, string,

  • or float version the values that we passed them.

That's enough for the interactive shell for now. Let's write our first program.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級 美國腔

第3課--Python編程 (用Python自動完成無聊的事情) (Lesson 3 - Python Programming (Automate the Boring Stuff with Python))

  • 39 2
    熊敏 發佈於 2021 年 01 月 14 日
影片單字