Install and run the interactive interpreter
In the vast majority linux and UNIX System installation （ Include Mac OS X）,Python The interpreter already exists . We can type... At the prompt python Command to verify （ The author's environment ubuntu）
fnngj@fnngj-H24X:~$ python Python 2.7.3 (default, Sep 26 2012, 21:53:58) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
Ok ! If you type in python When the above information appears in the command , Indicates that we have installed python Environmental Science .
If not installed , stay ubuntu /Debin linux , Then you can use the following command to install Python:
$ apt-get install python
If it is window Installation below , as well as linux Download the source package installation , I believe this kind of person's problem is easy to find , I won't explain it in detail here . I believe it's better than the first configuration java The environment is much simpler .
Type in the command , See if it works .
>>> print "hello,world!" hello,world!
Be careful ： If you want to launch an interactive interpreter , Sure ctrl+D
What is the algorithm ？
Before serious programming , Let's talk about computer programming first , In short , Computer programming is to tell the computer what to do . Computers can do a lot of things , But it doesn't think for itself , We need to tell it the details , You need to tell it the algorithm in a language that the computer can understand .“ Algorithm ” But is “ Program ” perhaps “ The recipe ” Another pleasant way of saying ---- Describe in detail how to do sth .
Tofu with shallot 、 tofu 、
First , Take some tofu ;（ Declare a variable ）
Then add shallots to the tofu .（ Add two variables ）
If you need special spicy tofu .（ Consider specific conditions ）
Cooked --- Every time 10 Check every minute .（ Repeat this command ）
Recipes and algorithms contain some elements （ object 、 goods ）, And instructions （ sentence ）.“ Shallot ” and “ tofu ” It's the elements . The instructions include “ Add shallots to tofu ” etc. .
Numbers and expressions ：
Interactive python The interpreter can be used as a powerful calculator
>>> 1-2 -1 >>> 1+2 3 >>> 32784*13 426192 >>> 1/2 0
What circumstance ？1 except 2 It's not supposed to be equal to 0.5 Well ？ How is 0 Well ？ “/” Used for rounding , It's not divisible enough , Must be 0 了 , Try floating point numbers
>>> 7/2 3>>> 1.0/2.0 0.5
try “%” Remainder
>>> 6 % 3 0 >>> 6 / 3 2 >>> 6 % 3 0 >>> 7 / 3 2 >>> 7 % 3 1 >>> 13 % 9 4>>> 0.75 % 0.5 0.25
See, Division （/） And surplus （%） It's different .
6 / 3 It happened to be 2 to be divisible by , So the remainder （%） yes 0 , 13 / 9 I can only bring 1 individual 9 , The remainder is 4 .
Now try again “ ** ” Power operation （ chengfang ） operator
>>> 2*2*2 8 >>> 2**3 8 >>> 2**6 64 >>> -3**2 -9 >>> (-3)**2 9
2 Of 3 The power can be a power symbol （**） Express , It's quite convenient .
The ratio of the power sign is negated （ Unary minus operator ） High priority , therefore -3**2 Equate to -（3**2）, If you want to calculate （-3）**2 , You need to explicitly state .
python Can handle very large numbers
>>> 100000000000000000000000 100000000000000000000000L >>> 100000000000000000000000L 100000000000000000000000L
Ordinary integers in 2 147 483 647 to -2 147 483 647 Between , Excess can be represented by long shaping （L） , We can also transform integers into integers by ourselves （ Add... At the end of the input “L”）
This is no different from other programming languages , If you've never been exposed to a program , I believe it's easy for you to understand .
>>> x = 3 >>> x*2 6>>> x = 5 >>> x * 2 1
hold 3 Assign a value to x , that x The current value of the variable is 3, x multiply 2 The result is 6 , I have put the 5 Assigned to x , Then with 2 The result of multiplication must be 10 了 .
Define variable rules ： Cannot start with a number , It can contain letters 、 Numbers and underscores .
In fact, we've talked about the sentence at the beginning print It's a printout statement .
>>> 2*2 4 >>> print 2*2 4
Although the result is the same , The expression above , Here's the sentence .
I've talked about using the power operator （**） To calculate the power . in fact , You can use functions instead of operators , The function is pow :
>>> 2**38>>> pow(2,3)8
Functions are like small programs that can be used to perform specific functions .python Many of our functions do wonderful things . We can also customize functions . therefore , We usually put pow The function provided by the system is called Built-in functions .
>>> 10+pow(2, 3*5)/3.010932.666666666666>>> abs(-10)10>>> 18/131>>> round(18.4/13.7)1.0
We can use function calls and operators to create more complex expressions ; abs() Function can get the absolute value of several people ,round The function rounds the floating-point number to the nearest integer value .
Think of modules as importing into python To enhance the expansion of its functions . You need to use a special person's command import To import modules .
>>> import math>>> math.floor(32.9)32.0>>> int(math.floor(32.9))32
floor The function is round . because floor() Function belongs to math modular , So import the module before using it （import math）, And then according to （ modular . function ） The format uses the functions of this module .
If you want to put the 32.9 Convert to integer （32） Not floating-point numbers （32.0）, have access to int function .
When making sure you don't import multiple functions with the same name （ Import from different modules ） Under the circumstances , You may not want to call a function every time , Write the name of the module . that , have access to import Another form of command ：
>>> from math import sqrt>>> sqrt(9)3.0>>> sqrt(8)2.8284271247461903
sqrt() Function is used to calculate the square root of a number .