Python BASICS (1), python BASICS (
The reason for selecting py transaction is as follows: 1. python is a glue language (cross-platform); 2. python is omnipotent (except for the underlying layer ), 3. Easy to write python (notepad ++ and other text editors can do things). 4. Many scripts for penetration are written in py (and google's website is also py)
Install and run the interactive Interpreter
In most linux and UNIX systems (including Mac OS X), the Python interpreter already exists. We can enter the python command at the prompt for verification (the author's environment is 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 the above information is displayed in the python command, it indicates that the python environment has been installed.
If Python is not installed in ubuntu/Debin linux, run the following command to install Python:
$ apt-get install python
If the installation is in windows and the installation of the source code package is downloaded in linux, it is believed that such problems can be easily found and will not be explained here. It is much easier than configuring the java environment for the first time.
Enter the command to check whether it can run normally.
>>> print "hello,world!" hello,world!
Note: To launch an interactive interpreter, press ctrl + D.
What is an algorithm?
Before programming carefully, let's talk about what computer programming is, simply put, computer programming is to tell the computer what to do. Computers can do a lot of things, but they don't think about it themselves. We need to tell it the details and tell it the algorithm in a language that computers can understand. "Algorithms" are just another sweet saying of "Programs" or "Recipes"-a detailed description of how to do something.
Bean curd, bean curd,
First, get some tofu; (declare a variable)
Then add the Scallions to the bean curd. (Add two variables)
If you need special spicy tofu. (Consider specific conditions)
Cook --- check every 10 minutes. (Run this command again)
Recipes and algorithms contain elements (objects and items) and instructions (statements ). "Green onion" and "Tofu" are elements. Instructions include "adding scallions to Tofu.
Numbers and expressions:
Interactive python interpreter can be used as a powerful Calculator
>>> 1-2 -1 >>> 1+2 3 >>> 32784*13 426192 >>> 1/2 0
What is the situation? Shouldn't Division 1 and Division 2 be 0.5? How is it 0? "/" Is used to take an integer. If it is not enough for division, it must be 0. Try floating point number.
>>> 7/2 3>>> 1.0/2.0 0.5
Try "%" to get the 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 the difference between the Division (/) and the remainder (%.
6/3 is divisible by 2, so the remainder (%) is 0, 13/9 can only get 1 9, and the remainder is 4.
Next, try the "**" Power Operation (multiplication operator ).
>>> 2*2*2 8 >>> 2**3 8 >>> 2**6 64 >>> -3**2 -9 >>> (-3)**2 9
The 3rd power of 2 can be represented by a multiplication character (**), which is quite convenient.
The multiplication operator has a higher priority than the inverse (unary subtraction operator). Therefore,-3 ** 2 is equivalent to-(3 ** 2). If you want to calculate (-3) ** 2. It must be explicitly stated.
Long Integer
Python can handle very large numbers.
>>> 100000000000000000000000 100000000000000000000000L >>> 100000000000000000000000L 100000000000000000000000L
The normal integer is between 2 147 483 647 and-2 147 483 647. If the value is exceeded, it can be expressed as a long integer (L ), we can also convert the integer into an integer (Add "L" at the end of the input ")
Variable
This is no different from other programming languages. If you have never touched any program, it is easy to understand.
>>> x = 3 >>> x*2 6>>> x = 5 >>> x * 2 1
Assign 3 to x, and the current value of the x variable is 3. Multiply x by 2 and the result is 6. Then, I assign 5 to x, then the result multiplied by 2 must be 10.
Rule for defining variables: a variable can contain letters, numbers, and underscores, but cannot start with a number.
Statement
In fact, at the beginning, we have already mentioned that print is the print output statement.
>>> 2*2 4 >>> print 2*2 4
Although the results are the same, the above is the expression, and the following is the statement.
Get user input
User input is required for running the program, and execution continues based on user input.
>>> Input ("input expression:") input expression: 2*2 4
The second line of the program is waiting for user input. 2*2 is input by the user. Next, let's look at something more interesting.
>>> x=input("x:") x:15 >>> y=input("y:") y:32 >>> print x*y 480
Ah ~! Don't be silly. It's interesting to work with me! I promise you will not understand the following!
>>> if 1==2 : print 'one equals two' ... >>> if 1==1 : print 'one equals one' ... one equals one
If 1 is equal to (=) 2 and the condition is false (that is, it is not true), print 'one equals two' will not be executed.
If 1 is equal to (=) 1 and the condition is true (true), print one equals will be executed.
Function
We have discussed how to use the power operator (**) to calculate the multiplication operator. In fact, you can use a function to replace the operator. This function is pow:
>>> 2**38>>> pow(2,3)8
Functions are like small programs that can be used to implement specific functions. Many functions of python do amazing things. You can also customize functions. Therefore, we usually call functions provided by pow and other systems as 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. The abs () function can obtain the absolute values of several people. The round function rounds the floating point number to the nearest integer.
Module
You can think of a module as importing it to python to enhance its function extension. You need to use the special command import to import the module.
>>> import math>>> math.floor(32.9)32.0>>> int(math.floor(32.9))32
The floor function is an integer function. Because the floor () function belongs to the math module, before using it, you must first import the module (import math) and then use the function of this module in the format of (module. function.
If you want to convert 32.9 to an integer (32) instead of a floating point (32.0), you can use the int function.
If you do not want to import multiple functions with the same name (imported from different modules), you may not want to write the module name every time you call a function. You can use another form of the import command:
>>> from math import sqrt>>> sqrt(9)3.0>>> sqrt(8)2.8284271247461903
The sqrt () function is used to calculate the square root of a number.
Save and execute the program
I am learning the process in ubuntu, and a python command enters the interactive interpreter. The interactive interpreter is one of the strengths of python and makes it a reality to test the solution in real time and experience the language. However, all input in the interactive interpreter is lost when it exits. What we really want to do is to write programs that can run by ourselves and others.
Next we create a file root @ fnngj-H24X:/hzh/python # touch hell. pyroot @ fnngj-H24X:/hzh/python # lshell. pyroot @ fnngj-H24X:/hzh/python # vi hell. py input: print "hello, world!" Save and exit. Run root @ fnngj-H24X:/hzh/python # python hell. py hello, world!
Look! This is a program that can run all the time. Okay! The above method is used to create an interesting program.
Open the file and enter the program name = raw_input ("what is your name? ") Print" hello "+ name + "! "Save and exit. Run! Root @ fnngj-H24X:/hzh/python # python hell. py what is your name? Huhu // the user is required to enter his/her name hello huhu!
Python basic tutorial (version 2) is recommended for the python getting started tutorial. If there is no basic knowledge, python is recommended for the basic learning. For the advanced tutorial, python core programming is recommended. if you cannot find resources, you can trust me!