Learn about your own Python content sharing (more http://www.yibencezi.com/notes/9073 to learn):
0, Environment installation (self-Baidu)
Online Environment: http://pythonfiddle.com/
1, my first python:
2, simple print:
Rint, Chinese means printing, in Python It is not printed on paper, but printed on the command line, or terminal, the console. Print is a very basic and common operation in Python, and its operand is a string (what is a string, hold the table here, and wait for a slow path). The basic format is:
Print the things you want printed
Or
Print (What you're going to be printing)
Here must be the English characters of the parentheses, all the symbols appear in the program must be the English character, pay attention to not be your input method pits.
Note: If you are a Python3 and a later version, you must use print (), or you will get an error.
Example Demo:
After printing, add a paragraph of text to output, you need to add double quotation marks or single quotation marks. It is found that print, in addition to printing text, can also output a variety of numbers, calculation results, comparison results and so on. You try to print something else, see what is successful, what will fail, and if you are interested, guess the reason for the failure.
Note: Python3 Remember to add in the back ()
In fact, under the Python command line, print can be omitted, and the result of each command is output by default. Just like this:
>>> ' Your yida! '
' Your yida! '
>>> 2+13+250
265
>>> 5<50
True
#======== Short-term goal ========#
A short-term goal is a very simple, mentally retarded game:
Com:guess what I think?
5
Com:your answer is too small.
12
Com:your answer is too large.
9
Com:your answer is too small.
10
Com:bingo!!!
Explain: First the computer will pinch a count in the heart, meditation a number, and then ask you to guess. You guessed the answer, the computer will be kind enough to tell you big or small, until you finally determined to guess.
3,ide
What is an IDE? English is called integrated development environment, Chinese is integrated development environment. Well, it doesn't say.
To make an inappropriate analogy, if the code is to make a handicraft, then the IDE is the machine tool. Another inappropriate analogy, PS is the picture of the Ide,word is the doc document Ide,powerpoint is the PPT file of the IDE. Python also has its own IDE, and there are many more.
Python comes with an IDE called Idle. Once you've installed the Windows,windows, you'll find it in Python 2.7, Start menu, programs, and more. After opening it is much like the command line we used before. Yes, it is, just print it inside and try it out. I do not know the previous use of the command line of the students have noticed that the command line to lose a line of command will return the results, and before the print so much, after the switch does not know where to go. So it's not going to meet our big plans to write retarded games. We need to use a new method!
Clicking on the "File", "new Window" in the menu bar above the window will hit a new, much more like one, but there is nothing in it. This is a text editor, where we can write our Python program. Continue print a few lines, this time you can print a bit more:
print ' Hello '
print ' IDE '
print ' Here I am. '
Now is the time to witness the miracle! Click Run Module, or press the shortcut key F5 directly. You will be prompted to save the file, just take a name, such as "lesson3.py". (. py is the type of Python code file, although not specified. PY is also possible, but the recommendations are still in accordance with the specification), the previous console window will output the results you want once.
Later you want to edit or run the code just now, as long as you select "File", "Open ..." in the idle.
The Idle on Mac is pre-installed, enter "idle" in "terminal" to start, use the same method as Windows. You can also find the idle in the folder/usr/bin. If it is a re-download installed Python, it seems that can be found in the "Application" idle, Mac classmate can be verified. (There is feedback from students that the default Python on Mac is not IDLE, you need to download and install Python after the inside only)
In addition to the official IDLE, there are some very useful third-party IDE, the file directory, text editor, command line are all integrated together, but also added a lot of auxiliary functions, configured after the use should be more convenient than IDLE. This one of the first tweets Pycharm, which was previously paid for software, is now available in the free version, enough for general learning and development use. Interested students can also find out to try.
In addition, there is a third-party free IDE under Windows, called Pyscripter, Address: http://sourceforge.net/projects/pyscripter/. However, pyscripter encoding settings can sometimes lead to strange problems, but also to note that the location of its installation and. py file can not be saved in Chinese, in addition to its official maintenance is not to force, so now is not very recommended.
Today's content is a bit long. Configure the development environment This is the most troublesome thing, we have to be patient, after all, one investment, long-term benefit. Our course will be in the IDE in the future, basically no longer directly to the command line to knock the code.
Finally, there are a lot of Python programmers who don't use any ides. As for the reason, it may be like good craftsmen don't use machine tools to process artwork.
4, enter
Pre-review:
1. command line, including the console (CMD) under Win and the terminal under Mac (Terminal).
It can help us confirm that python on our computer is not normal. But this approach is hard to help us achieve the goal of writing a complete small program.
2. IDE, including Python's own idle and other third-party Ides.
Do not know whether everyone is successful, and can successfully save and open the py file? In the future, you can do all the content in our course.
3. Online editor, Compileonline or Pythonfiddle.
They also include the Code Editor (where the code is written) and the console (where the results are output) in two parts. So our operations in the local IDE can be implemented in it. Just save the file will be a bit complicated, compileonline is click Download files package download, Pythonfiddle need to register a bit. Of course, you can also directly copy the code you write, save it locally, next time paste it up and then write.
#======== into today's topic ========#
Before print so much, is the program in the "output" to the screen. That has come to, have borrowed to have also, have to eat ... Cough cough! Well, we have to have a way of "inputting" information to the program, so that we can talk to the program and make the so-called "human-computer interaction".
Python has a method that receives input from the command line:
Input ()
Note that unlike print, this time we have to add (), and we have to have the parentheses of the English characters.
Okay, finally we can move out of that retarded little game, yes! In the game we need to talk to the program one, so let's talk first.
Open our Python editor, whether it's an idle, online editor, or another IDE. Enter the following lines of code in the Code Editor:
Print "Who does you think I am?"
Input ()
Print "Oh, yes!"
Then, run! (Forrest run! You will see in the command line that the program outputs the first sentence and then stops, which is input waiting for you to enter.
Enter your answer and return. You will see the program answer. Attention! Quotes!! Another quote!!! As with print, if you lose a string of text, enclose it in quotation marks "or", if it is a number.
(In a word, Python also has an input method: Raw_input (), it put all the input directly as a string of characters, so you can not add quotation marks, interested students can try to experience the difference between the two. We'll talk about this tangled quote later. )
It looks good, oh, it seems to be the right thing to say. Do you feel a big step forward from the completion of the mini-game? But everyone found out, even if you say "idiot! "The program will still answer calmly," Oh, yes!. Because it goes in the right ear and out of the ear, it doesn't even listen to what we say. So how can we make it go down and listen? Snapped! and listen to tell.
PS: Today is a good mood, to tell you a very cold programmer jokes.
A programmer is very interested in calligraphy and decided to make a contribution in this regard after retirement. So he spent a fortune to buy the finest four treasures. One day, after the sudden birth of yaxing, a grind the ink paper, and point on a good sandalwood, quite Wang Xizhi demeanor, and a Yan zhenqing momentum, moments of calm, splash brush solemnly write the next line of words:
Hello World!
5, variable
#======== variable ========#
The god of Python says, "There are variables!" Then there is the variable.
Variable, words too literally, is the amount of change. The way to create a variable in Python is simple, give it a name, and give it a value. Lift several chestnuts:
name = ' Crossin '
MyVar = 123
Price = 5.99
Visible = True
The function of "=" is to assign the right value to the left variable.
Here's another concept called "data type", above 4 chestnuts representing four basic types that are more common in Python:
String-Represents a string of characters that need to be caused by "or" "
Integer
Floating-point number-is a decimal
BOOL-This is a special type that is used to indicate that the logic is "non" and that it has only two values, true and false. (Note that there is no quotation marks, the quotation marks become a string)
Again, we used the familiar print. This time, we have upgraded, to print a "variable" with printing:
name = ' Crossin '
Print Name
Did you see the results? No output "name", no error, but output "Crossin". Now is not able to understand some, why before print a text without quotation marks will be error, and print a number is no problem?
It's called a variable, that's what can change. So after an "assignment" operation, you can continue to give it a new value, and it can be a different type of value.
A = 123
Print a
A = ' Hi '
Print a
The right side of "=" can also be more complex, such as a computed value:
Value = 3 * 4
Print value
Value = 2 < 5
Print value
Even, it can be input ():
name = input ()
Print Name
So, we can evolve our little game again. Change the contents of the last write slightly, plus the variables:
Print "Who does you think I am?"
you = input ()
Print "Oh, yes! I am A "
Print You
It seems that the program already knows our input. Next, let it learn to make judgments about different answers. Let's save this for the next time.
Instance:
Small Game Advanced:
6,bool (Boolean)
Yesterday I talked about a few basic types in python, strings, integers, floating-point numbers are good to understand, about the remaining bool (Boolean) I would like to say a little more.
Logical judgment is very important in programming. A large number of complex programs are fundamentally based on the basic logic of "true" and "false". and bool is the most simple and essential True/false, true and false, and non-.
Take a look at the following example:
A = 1 < 3
Print a
b = 1
c = 3
Print B > C
By comparing two values with ">" <, we get a bool value. The true or false of this bool value depends on the result of the comparison.
Method
">" "<" is a logical operator in a programming language, and common logical operators include:
: Greater Than
<: Less than
>=: greater than or equal to
<=: Less than or equal to
= =: equals. Compares two values for equality. The reason for using the two equals sign is to differentiate it from a variable assignment.
! =: Unequal to
Not: Logical "not". If x is true, then not X is False
And: Logical "and". If x is true and Y is true, then X and Y are true
Or: Logical "or". If at least one of X, Y is true, then x or Y is true
The bool and the logical operations are far more than that, but now we're not going to think about it so much that we don't get around to find the north. The most basic is greater than, less than, equals already enough for us to use first.
Try adding a bool to our mini-game:
num = 10
print ' Guess What I think? '
Answer = input ()
result = Answer<num
print ' too small? '
Print result
result = Answer>num
print ' too big? '
Print result
result = Answer==num
print ' equal? '
Print result
The code is a little bit more than before, explain.
First code: Create a variable with a value of 10 num, output a hint, and then enter a value to the variable answer.
The second code: Calculate the result of the answer<num, record in result, output prompt, and then output the result.
The third and fourth paragraphs are similar to the second paragraph, except that the content of the comparison is not the same.
See if the results are consistent with your expectations? It seems silly, but it's a step closer to the goal.
Now count the tools you have: input, output, variables to record values, and logical operations that can compare numerical sizes. Use them to toss in your python.
Instance:
7,if
#======== if ========#
Continue with the last program. We've been able to get the program to judge the value we've entered, but it's still a little bit, but it's going to have to say three times anyway. Because so far, our programs have been executed sequentially from top to bottom in a row. A classmate sent a question: how can it be based on the results of our input to choose the implementation? The answer is--if
Take a look at a picture (pure hand-painted, slag quality)
To explain, the sequence of the program is executed down when encountering the IF statement, it will judge the true and false conditions.
"If" is true, the next content is executed. "If" is false, skip.
The syntax is:
If condition:
SELECT statement to execute
Special note: The colon after the condition cannot be less, and must also be an English character.
Special NOTE: If internal statements need to have a uniform indentation, generally with 4 spaces. Python replaces {} In many other programming languages in this way. You can also choose 1/2/3 ... a space or press the TAB key, but it must be unified throughout the file. Do not use tab and space to mix, otherwise there will be a variety of inexplicable errors. So the recommendation is to use 4 spaces directly.
Shangli son:
Thisislove = input ()
If Thisislove:
Print "Then turn around and be brave to stay"
Try? Enter true and you will be answered. Enter false and nothing. (If you have problems exporting Chinese, please change to English)
So, our game can be rewritten like this:
num = 10
print ' Guess What I think? '
Answer = input ()
If Answer<num:
print ' Too small! '
If Answer>num:
print ' Too big! '
If Answer==num:
print ' bingo! '
If is referred to as a "control flow statement" in a programming language, it is used to control the order of execution. There are other control flow statements that we will use later.
#======== homework after class ========#
A lot of students are strongly asked to decorate their homework. All right, meet you guys. Remember that "Who do you think I am?" "The program?" (Please reply 4 if you don't remember.) )
Rewrite it, only when you answer some nice things, the program will be oh yes. None of the others will ignore you. Even when you say certain words, it will contradict you.
As for which words to use which conditions, you set it yourself.
8,while
#======== Comment ========#
Let's start with a new thing: comments.
In Python, text that begins with "#" is not considered executable code.
print "Hello World"
And
print "Hello World" #输出一行字
is the same effect. But the latter can help developers to better understand the code.
In the next lesson, I'll often use annotations to explain the code.
#======== while ========#
After we have improved our mini-game with If, the function has been basically implemented. Many students have said after the completion of each time can only guess once, after the end of the run again, feel good trouble. Can there be a way for the player to keep guessing until the guess is reached? The answer is obviously, if this small problem can not be solved, then Python will be weak explosion.
The simplest solution is the while.
As with if, while is also a control flow statement, and it is also called a looping statement. Continue to look at the slag quality hand-painted flowchart:
When the program executes to the while, when the condition is true, it executes the code inside the while, and skips when the condition is false.
The syntax is:
While condition:
Loop-Executed statements
As with if, note the colon, and note the indentation.
Today's chestnuts:
A = 1 #先a设为1
While a! = 0: #a不等于0就一直做
print "Please input"
A = input ()
Print "Over"
Think about how to use while to improve a little game? There are many ways of writing, we think about it, I do not explain more. Gives a method.
Note that there is a two-layer indentation here, which keeps the number of spaces indented for each layer the same.
So far, the mini-game has been basically shaped. But it seems to be very close: every time I know the answer, this play with God horse meaning.
Tomorrow, how to let you don't know the answer to the computer.
Python departure--first article