June 7, 2017 14:59:27
Task:
Read the basics of Python
1. As the name implies, computers can do mathematical calculations, so computer programs can naturally handle a variety of values. However, the computer can handle far more than the numerical value, but also can deal with text, graphics, audio, video, web and other kinds of data, different data, need to define different data types.
2. Because of the feelings of the matter, his mood is really big bad, bad to explode, can not afford to learn, rely on.
3. Integers and floating-point numbers are stored inside the computer in different ways, and integer operations are always accurate (is division accurate?). Yes! ), and the floating-point operation may have rounding errors.
4. The string is any text enclosed in ' or ', such as ' abc ', ' XYZ ', and so on. Note that the ' or ' itself is only a representation, not a part of the string, so the string ' abc ' only a,b,c these 3 characters. If ' itself is also a character, then you can use "", such as "I m OK" contains the characters are I, ', M, space, o,k these 6 characters.
5. Escape character \ Can escape many characters, such as \ n for line break, \ t for tab, character \ itself to escape, so \ \ means the character is \
6. Boolean values
The Boolean value is exactly the same as the Boolean algebra, with a Boolean value of only true, false two, or true, or false, in Python, which can be used to indicate a Boolean value directly with True, false (note case), or by Boolean operations:
7. Boolean values are often used in conditional judgments.
8. Null value
The null value is a special value in Python, denoted by none. None cannot be understood as 0, because 0 is meaningful, and none is a special null value.
In addition, Python provides a variety of data types, such as lists, dictionaries, etc., and also allows you to create custom data types.
9. Variables are represented by a variable name in the program, and the variable name must be a combination of uppercase and lowercase English, numeric, and _, and cannot begin with a number
10.Py is a dynamic language, the type of the variable can change at any time, but Java is a static language, and the variable must be specified as a fixed type.
1. Constants
So-called constants are immutable variables, such as the usual mathematical constants π is a constant. In Python, constants are typically represented in all uppercase variable names:
2. Integer operation results are always accurate
3.Python supports a variety of data types, within the computer, any data can be regarded as an "object", and variables are used in the program to point to these data objects, the variable is assigned to the data and variables to associate.
4. Strings are also a type of data, but a special string is a coding problem
5. To deal with Chinese obviously a byte is not enough, requires at least two bytes, and does not conflict with ASCII encoding, so China has developed a GB2312 code, used to put Chinese into.
The 6.ASCII encoding is 1 bytes, while the Unicode encoding is usually 2 bytes.
7. In computer memory, Unicode encoding is used uniformly, and is converted to UTF-8 encoding when it needs to be saved to the hard disk or when it needs to be transferred.
The 8.Python provides the Ord () and Chr () functions to convert letters and corresponding numbers to each other:
9.Python later added support for Unicode, the string in Unicode represented by U ' ... '
10. Convert u ' xxx ' to UTF-8 encoded ' xxx ' with encode (' Utf-8 ') method:
1. In turn, convert the UTF-8 encoded string ' xxx ' to the Unicode string U ' xxx ' using the decode (' Utf-8 ') method:
2. In Python, the format used is consistent with the C language and is implemented in%
The 3.% operator is used to format a string. Inside the string,%s is replaced with a string,%d is replaced with an integer, there are several% placeholder, followed by a number of variables or values, the order to correspond well. If there is only one%, the parentheses can be omitted.
Common placeholders are:
%d integers
%f floating Point
%s string
%x hexadecimal integer
where formatted integers and floating-point numbers can also specify whether to complement 0 and the number of digits of integers and decimals:
4. Keep in mind the Chinese to add U:
name = raw_input (U "Please enter your name:")
Summary: Read the basic tutorial on the knowledge has a new consolidation, refueling.
5.list
One of the data types built into Python is the list: lists. A list is an ordered set of elements that can be added and removed at any time.
The list is a mutable, ordered table, so you can append an element to the end of the list:
The data types of the elements inside the IST can also be different
The list element can also be another list,
Summary: The data types in the list do not have to be the same, and the list can contain list,list that are ordered, can be added, or can be deleted.
If a list does not have an element, it is an empty list with a length of 0
Append () is the addition of elements such as append (' a ') at the end of a list or tuple, which means adding a ' a ' element to the last position of the list or tuple insert ()
is to insert an element, such as insert (1, ' a ') anywhere within the index range of a list or tuple, by adding the ' a ' element to the second index of a list or tuple
6.tuple
Another ordered list is called a tuple: a tuple. Tuple and list are very similar, but once the tuple is initialized, it cannot be modified, for example, the name of the classmate is also listed:
What is the meaning of immutable tuple? Because the tuple is immutable, the code is more secure. If possible, you can use a tuple instead of a list as much as possible.
A list can be nested in a tuple tuple, where the list is mutable.
The so-called "invariant" of a tuple is that each element of a tuple, pointing to never change.
List and tuple are Python's built-in ordered set, one variable, one immutable. Choose to use them as needed.
Conditional Judgment and circulation
1.if < condition judgment 1>:
< Executive 1>
Elif < condition judgment 2>:
< Executive 2>
Elif < condition judgment 3>:
< Executive 3>
Else
< Executive 4>
2. Cycle
There are two types of Python loops, one is the for...in loop, and each of the elements in the list or tuple is iterated in turn to see an example:
names = [' Michael ', ' Bob ', ' Tracy ']
For name in Names:
Print Name
3. So for x in ... The loop is to take each element into the variable X
4. If you want to calculate the sum of 1-100 of integers, from 1 to 100 is a bit difficult, fortunately, Python provides a range () function, you can generate a sequence of integers, such as range (5) The resulting sequence is less than 0 from the beginning of the integer
5. The second loop is the while loop, which, as long as the condition is satisfied, loops continuously and exits the loop when the condition is not satisfied. For example, we want to calculate the sum of all the odd numbers within 100, which can be implemented with a while loop:
6.raw_input () reads the content is always returned as a string, the string and integer comparison will not get the expected results, you must first use INT () to convert the string to the integer we want:
7. The loop is an effective way to make the computer do repetitive tasks, sometimes, if the code is written with a problem, it will let the program into a "dead loop", that is, forever cycle. You can then use CTRL + C to exit the program, or force the end of the Python process.
8.dict
Python built-in dictionary: dict support, Dict full name dictionary, in other languages also known as map, using key-value (Key-value) storage, with a very fast search speed.
The longer the list, the more time it takes
Why does Dict look so fast? Because the implementation principle of dict and look dictionary is the same. Suppose the dictionary contains 10,000 characters, we need to look up a word, one way is to turn the dictionary back from the first page, until we find the word we want, this method is to find the element in the list method, the larger the list, the slower the lookup.
The second method is to search the dictionary index table (such as the radical table) the corresponding page number, and then directly to the page, find the word, regardless of which word, this search speed is very fast, not with the increase in the dictionary size and slow.
Dict is the second implementation, given a name, such as ' Michael ', Dict can directly calculate Michael's corresponding storage Score "page number", that is, 95 of the memory address of the number stored, directly out, so the speed is very fast.
As you can guess, this key-value storage method, when put in, must be based on the key to calculate the storage location of value, so that the time can be obtained by key directly to the value.
To delete a key, the corresponding value is also removed from dict using the Pop (key) method:
Compared with list, Dict has the following features:
The speed of finding and inserting is very fast and will not increase with the increase of key;
It takes a lot of memory, and it wastes a lot of memory.
And the list is the opposite:
The time to find and insert increases as the element increases;
Small footprint and little wasted memory.
So, Dict is a way of exchanging space for time.
Dict can be used in many places where high-speed lookups are needed, almost everywhere in Python code, it is important to use dict correctly, and the first thing to keep in mind is that the Dict key must be an immutable object.
This is because Dict calculates the storage location of value based on key, and if each calculation of the same key results in a different result, the dict interior is completely chaotic. The algorithm for calculating the position by key is called the hash Algorithm (hash).
To ensure the correctness of the hash, the object as a key can not be changed. In Python, strings, integers, and so on are immutable, so you can safely use them as keys. The list is mutable and cannot be a key:
9.set
Set is similar to Dict and is a set of keys, but does not store value. Because key cannot be duplicated, there is no duplicate key in set.
To create a set, you need to provide a list as the input collection:
You can add elements to the set by using the Add (key) method, and you can add them repeatedly, but without effect:
You can delete an element by using the Remove (key) method:
The only difference between set and dict is that it does not store the corresponding value, but the set principle is the same as the dict, so it is also not possible to put mutable objects, because it is not possible to determine whether the two Mutable objects are equal, and there is no guarantee that there will be no duplicate elements inside the set. Try putting the list in set to see if it will give an error.
The content of object A is ' abc ', but in fact it means that a is itself a variable, and the content of the object it points to is ' ABC '
Therefore, for an immutable object, any method that invokes the object itself does not change the contents of the object itself. Instead, these methods create a new object and return it, ensuring that the immutable object itself is always immutable.
-------------------------------------Split Line-----------------------------------
Summarize:
More than the content of the course online, more content, like the teacher this style, refueling. The next step is to look at the function.
Python Basics-------python2.7 Tutorial Learn "Liao Xuefeng Edition" (ii)