This article is a record of reading notes for Python core programming.
Python is an explanatory language, and all statements are interpreted directly by the interpreter (interpreter), but it is also a high-level language, which makes it possible to directly interact with the machine when writing code. Very good
Print " Hello world! "
Directly on the interpreter, a Hello world! is displayed directly on the screen. The >>> symbol is the prompt, which is the interpreter prompt for the wait flag. Input two kinds of things can be responded to, one is a statement is an expression. Statements are made up of keywords, such as the print "Hello world!". The expression is not a keyword, such as a function or a mathematical formula.
Python's print statements, like the C language, can be used in conjunction with the string operator (%) to implement string substitution functionality. Such as:
Print " %s is number%d! " % ("programming", 1) is number 1!
The% here is a character replacement, and the following letter represents the replacement format. The usual is%s%d%f. The C-language printf () statement expresses the above notation as
printf ("%s is number%d", "Programming", 1);
There is a slight grammatical difference.
>>> num = Int (raw_input (""))
Raw_input is a text input function that reads a string from the standard input and then deletes the line break at the end of the string. The input is stored as a string type, and then the content is converted to an integer using int ()
Comments
# One comment Print ' Hello world! ' # another comment
Python uses # as a single-line comment character. #后的内容作为注释
In addition, Python has a special note for the document string that adds a string to the beginning of the module, class, or function, which is the function of the online document, such as:
def foo (): " This is a doc string " Retrun True
The phrase "This is a doc string" is equivalent to a comment. Unlike regular annotations, however, a document string can be used to automatically generate documents (?) at run time. )
+ - * / // % **
Subtraction to take the power of surplus
where//is floating-point division (rounding the result)
With regard to variables, Python acts as a dynamic type language without prejudging the variable type, and the type and value of the variable are initialized at the moment of assignment. The assignment number is =
Python does not support self-increment and decrement in C. The self-increment 1 and the self minus 1 of n are written
n + =1-= 1
For types, Python has five basic numeric types, signed integer, Long Integer, Boolean, float, and plural
Python's long integer differs from the long type of C in that the size of the long integer depends on the total number of user virtual memory, similar to Java's BigInteger. Complex types are suitable for scientific operations
String that supports the use of paired single or double quotes, with three quotation marks to contain special characters. The index operator [] and the slice operator [:] can get substrings. In a string index, the first index is 0, and the last is-1.
+ can be used for strings of connections, * can be used for string repetition
Lists and tuples, which are data structures in Python, can be seen as ordinary "arrays". Save any number and any type of Python object. The index element is also starting from 0. However, lists and tuples can store different types of objects.
The list element is wrapped in brackets [], and the number and value of the elements can be changed. Tuple elements are wrapped in parentheses () and cannot be changed (but their contents can be changed). Tuples can be viewed as read-only lists, and subsets can be obtained by slicing operations ([] and [:]).
List:
>>> alist = [1, 2, 3, 4]>>> alist[1, 2, 3, 4]>>> alist[0]1 >>> alist[2: [3, 4]>>> alist[:3[1, 2, 3]>>> alist[1] = 5>>> alist[1, 5, 3, 4]
Meta-group:
>>> atuple = ('Robots', 77, 93,'Try')>>>Atuple ('Robots', 77, 93,'Try')>>> Atuple[:3]('Robots', 77, 93)>>> Atuple[1] = 5Traceback (innermost last): File"<stdin>", Line 1,inch ? Typeerror:object doesn'T Support item assignment
A dictionary consists of a key-value (Key-value). Almost all Python objects can be used as keys, and numbers and strings are the most common.
The value can be any type of Python to the next, and the dictionary is wrapped in curly braces ({}). As follows:
>>> adict = {'Host':'Earth'}#Create Dict>>> adict['Port'] = 80#Add to Dict>>>adict{'Host':'Earth','Port': 80}>>>Adict.keys () ['Host','Port']>>> adict['Host']'Earth'>>> forKeyinchadict: ...Printkey, Adict[key]...host earthport80
Code blocks express logic by indenting. Do not use parentheses. The readability is indeed more concise. But it may require more control over the code hierarchy. Individuals are still not accustomed to this way for the time being. But the way to get used to it.
2015/8/10 python basic usage (1)