I. Installation of python and the first program in life
To write a python program, you must install python! I thought it was a direct write, dizzy. I still want to see why python should be installed first. I have learned through the query that there are several types of computer programs, one being compiled and the other being interpreted. The compiled type is like C ++, And the interpreted type is like python perl ruby! But I still don't quite understand it. It's just a bit of concept! No! As you may know, the compiler must first write the source code and then compile the link to run it. The interpreter is equivalent to a computer translation and can be executed one by one.
I will install the python translation first! Google found the official python website, dizzy! There are two faction installation files, no matter! 2.7 should be simpler than 3.2. Just install 2.7 (2 points installed on the teaching material ). Because I have installed games before, hey, I know how to install them. In a short time, the installation is complete. Then I opened the standard python interpretation environment IDLE. I wrote a line of code in the book:
- print "Hello, world!"
When I press enter again, a line appears:
Hello, world!
I know a little English. I know what print means. Haha! A line of code is written so quickly. Haha!
Hey, the computer is still very smart! Quick response! I am randomly typing something to see how it reacts! Dizzy! No, I have output a few strange lines of things. I don't need to worry about it, or I should strictly follow the instructions in the book!
The book says that you can choose many types of IDE (integrated development environment) for writing python code. It turns out that this IDE has many more tools to help you write code. What code is automatically completed and highlighted! Not busy! Just like using standard IDLE.
Ii. Significance of Algorithms
Oh! Algorithm! I have to take a closer look at what this is! Oh! The algorithm is like a detailed description of what you have arranged every day. For example, whether to brush your teeth or wash your face in the morning! Or drag the ground! It needs to be dragged every day. Oh! I understand it!
3. curiosity and a powerful IDE Installation
I saw so many ides in the book just now. I think I am still looking for one, so I feel that I am working very hard to explore the knowledge. I have seen so many IDE introductions that I really don't know which one to choose. So I searched the internet and decided to use Eclipse recommended by many people. However, I saw a sentence in the search process. There are three types of programmers: emacs, vim, and others. Dizzy. I further searched, and emacs is the God's editor. VIM is the god of the editor. I am depressed. It doesn't matter to other types of editor. I have carefully reviewed myself, at the beginning, I wrote the first line of code, and the distance from God is out of reach, so I should use eclipse first. I will try again later on the things about God. I found an article to introduce how to build the eclipse python environment. It took 10 minutes to get it done. It was nice to open it and use it. When you press pr, you will automatically prompt print to execute each line of code and press F9. Well, it's good.
Iii. Numbers and expressions
Oh! Python can implement some functions that begin with computers! I will try it. I enter
- print 2 + 2
Press F9 to output 4. Hey hey, it's really impatient. You can add numbers. Try Division again
- print 1 / 2
Dizzy, why output 0! In the past, when python used integer division, the fractional part would be truncated. Well, how can we keep the novel? The method is to use a real number, and one of the two numbers involved in Division takes a number as a floating point number. Try me
- >>>1.0 / 2.0
- 0.5>>>1 / 2.00.5
- >>>1.0 / 2
- 0.5>>>1 / 2.0.5
Haha good! Besides, it indicates a real number. It can only be followed by one point after an integer. Haha! However, this is sometimes complicated. What if you want python to only execute normal division! There is a way to add a line of code before the Code:
- from __future__ import division
- print 1 / 2
- print 1 // 2
In this way, the integer division will not be integer division, but if you still want to divide the integer division, you can use two //. Think about it. In addition to (addition, subtraction, multiplication, division), we should also be able to take the remainder, multiplication, and I will try:
- print 1 % 2
- print 10 / 3
- print 10 % 3
- print 9 % 3
- print 2.34 % 0.5
- ________________________________
- 1
- 3
- 1
- 0
- 0.34
Haha good! There is no problem at all, but where is the remainder operation useful. Good. For example, if you need to drag the site once every 10 minutes, you can check whether the result of 10% of the time is 0 for determination. Haha! I think this is really the case. I can apply the remainder in this way.
Dizzy! Just now, click release! Now, let's continue learning, long integer! A long integer is a long and long integer. I tried it in the interactive environment, mainly because I still can't write a long piece of code, and it is a waste of time to use eclipse (alas! Farther away from God !), Let me try it. It's a long and long integer. Let's see how python responds!
- >>> 999999999999999999999999999999999999999
- 999999999999999999999999999999999999999L
Oh! The response is correct, but why is there an L later! Oh, it turns out that a normal integer can only be within the same range (2147483647-2147483648). Wow! The number I wrote just now exceeds the normal integer! So there is an L later. Oh, fortunately, it's capital L. If it's lowercase, it's very similar to 1. I have to remember that it must be capital L later.
Hexadecimal and octalWhat is hexadecimal! I have queried the relevant information! Now you know, the original hexadecimal system is to enter 16, and the octal system is to enter 8. Oh, in decimal format, it is commonly used in life. When it reaches 10, it will enter 1 digit! Alas! Why do we need to use decimal digits? If we use sixteen or eight, we can see that it is faster! Unfortunately, there are only 10 fingers! Hey!
VariableOh! The variable should be like this, it is equivalent to a box, and then something can be put in it! A variable must have a name. It must be nice and easy to recognize. It is very important to have a variable name in the book! Variables can start with letters, numbers, and underscores, but cannot start with numbers. Oh, remember this.
StatementOh! So far! What I have been learning is expression, just like a dish in a recipe. Then, what I want to learn now is the statement, that is, how to cook these dishes. Hey, it's fun. Now I want to cook it! Alas! No, I seem to have prepared some dishes! I recall 1:
Print is the command statement for cooking, and 2*2 is the dish. Then m = 2 is to put dish 2 in the box of m. Yes, you have already done "dishes. Hey.
Get user input. Yes! I remember. General programs require users to enter some "dishes", and then programmers can arrange how to make "dishes. All I wrote just now are similar to the program-written dishes. To obtain user input, you need to use one function input! What is a function? I have to search for related knowledge! By searching, I know that a function is actually a reusable code block! Oh, it turns out to be like this. It's like a knife. You can use it to cut dishes at any time ". If you want to switch, you can switch. But where is the input function? How does python know it? I am depressed. If I don't know it, no matter what it is. I can imagine python already knows it. I will try this function in the notebook now.
- >>>input("the meaning of life:")
- the meaning of life:25
- 25
Haha! Real Input and Output! However, it is useless to do this. Just show me the user input values. Dizzy! No. Actually, this is useless! I suddenly remembered the "box", and I had to put it in a "box. So that I can make good use of it. I will try:
- >>>x = input("x:")
- x: 23
- >>>y = input("y:")
- y: 32
- >>>print x * y
- 736
Haha! Good! This is a lot of work! I put the "Dish" in the "box", and I also calculated the product of two numbers.
If. if statement. Oh! This should be what it means. That is to say, if so, what will happen! Well! Well, the lines of code I wrote previously are all executed one by one! Now you can do it. What if you do? haha! I feel like shopping on the streets! If I buy only 2.5 a kilo, I will not buy it if I have more, hey! I will try this. I will give a try on the synthesis of the previous study. Hey hey:
- >>> m = 10
- >>> if m > 20
- SyntaxError: invalid syntax
- >>> if m > 20:
- print "OK" >>> if m > 2:
- print "OK"
-
- OK
Hey! Yes, I still encountered a problem in the middle. I still need to enter the number after the if statement. Dizzy! Remember!
Iv. Functions
If the function is used, I have already queried relevant information, which is actually a small segment that can be reused. I don't know how python knows the input function just now. Now I know, it turns out to be like this. Input is a standard function, also known as a built-in function! So what python knows is that one person must know whether there is a mouth or not, and his ears are the same. In fact, the multiplication party just now can be implemented using the built-in function pow:
- >>> 2 ** 3
- 8
- >>> pow(2,3)
- 8
In fact, this behavior is called a function call! A function call behavior requires a function parameter. The number of parameters is related to different function implementations. In the above pow example, two parameters are provided, and then the function returns one value, which is a power operation! Because it is an interactive environment, the return value of the function is printed directly! Haha, I learned something again! Alas! There are still many built-in functions! I have to remember! Practice such as abs, round, and so on!
V. Module
Ah! What is a module. Oh! The original module is an extension to enhance the python function. This is equivalent to buying a "Bumblebee" so that I can go further and farther! Haha. The module has many extended functions, which are equivalent to the "Bumblebee", and the built-in functions are equivalent to the mouth and ears of its own history. Haha! For example, the floor function can be rounded down. However, this function is not in the built-in function, but in other modules (in the math module), you need to use a command import to import the module! I will try:
- >>> import math
- >>> math.floor(32.9)
- 32.0
Haha, good! Successful! After importing one module, you only need to use the module name. Function Format to use the functions in this module. But I noticed one problem. After floor is rounded down, the result is a floating point number. What if I want to program it as an integer! Fortunately, there is a way to wrap it with the int function. Try:
- >>> int(math.floor(32.9))
- 32
Haha! Successful! Good. However, every time you use a function in the math module, you need to add the module name. prefix, which is still very troublesome. What should I do! There is a solution. Use the form module import function. In this way, you can use functions directly without adding the module name prefix. Try:
- >>> from math import sqrt
- >>> sqrt(9)
- 3.0
Well, that's great!
How to double-click and execute the python program in WINDOWS. If you want to double-click the program in windows like a normal program, pay attention to a few issues! Hidden, so you have to change the default hidden suffix to display .) Another is to add the directory where the python interpreter is located to the System path Environment Variable so that windows can find the python interpreter. I want to pay attention to the following code.
- print "hello world!"
Save the file ended with. py. After the path environment variable is set, double-click the file and the program will flash through! A line of code must be added here, as shown below:
- print "helloworld!"raw_input("Press ")
Haha, that's all! You can see the program running result! Good! Haha!
Note. This comment is very important. I have queried the relevant materials and said that writing a comment is not only convenient for me to read my code, but also very helpful for others to read your code. More comments, but don't write anything as simple as an idiot's code line. Hey hey!
7. String.
Print "Hello, world! "Hey hey! In this program, what is "Hello, world"? This is a string. In python, strings are actually the same as numbers! It's also a value! Let's look at a string:
- >>> "Hello,world!"
- 'Hello,world!'
- >>> "Let's go!
- ""Let's go!"
- >>> 'Let's go!'
- SyntaxError: invalid syntax
- >>> 'Let\'s go!
- '"Let's go!"
Hey, no! In python, single quotes and double quotation marks are the same results, but they are used when single quotes and double quotes are mixed. The backslash can be used to escape! In this way, you can use only one quotation mark in a string!
String concatenationFor String concatenation, "+" is used, for example:
- >>> "Hello," + "world!"
- 'Hello,world!'
- >>> x = "Hello,"
- >>> y = "world!"
- >>> x + y
- 'Hello,world!'
Haha, that's good. The plus sign also plays a role!
String representation. str and repr. python have two mechanisms for string processing. What is a mechanism! Just think of the methods and practices! I am not very familiar with it for the moment !. One is through the str function, which converts the value to a string in a reasonable form to make it easy to understand; the other is through the repr function, it indicates the value in the string format of a valid python expression, as follows:
- >>> print repr("Hello,world!")
- 'Hello,world!'
- >>> print repr(10000L)
- 10000L
- >>> print str("Hello,world!")
- Hello,world!
- >>> print str(10000L)
- 10000
Hey, you understand. This makes sense. See the following example:
- >>> temp = 42
- >>> print "The temperature is" + temp
- Traceback (most recent call last):
- File "<pyshell#36>", line 1, in <module>
- print "The temperature is" + temp
- TypeError: cannot concatenate 'str' and 'int' objects
- >>> print "The temperature is" + repr(temp)
- The temperature is42
Haha! The first print statement is incorrect because it cannot add strings and numbers! The second is correct because the temp variable has been converted to the string form through repr! Anyway! Str and repr are two methods to convert python into strings! The str function makes the string easier to read !, Repr converts the result string to a valid python expression.
Differences between input and raw_inputI have used input for user input before, but why do we need raw_input? Is there any reason? Let me write a code to see it:
- Name = input ("What is your name? ")
- Print "Hello." + name + "s"
- ''''''''''''''''''''''''
- 'What is your name? Dodolook
- Error !!!!!
Dizzy! This Code seems to have no problem. Why is it wrong! Oh! The original problem is that the input will assume that the user is comfortable with a legitimate python expression. If the string is used as the input name, this program will not be used, however, it is too much to ask the user to enter a name with a quotation mark! What should we do? We need to use the raw_input function at this time. It regards the input as the original data and then puts it in the string. Hey, so there is no special need. Use raw_input. Use input when needed, alas! No matter now!
Long String. When writing a very long string, for example, when it needs to span multiple lines, you can use three quotation marks. In addition, you can use single quotation marks and double quotation marks between strings without the need to use backslashes!
Link: http://www.cnblogs.com/dodolook/archive/2011/05/19/2051038.html