Python-learn Note (2)

Source: Internet
Author: User

Python Basic data Types

integers, floating-point numbers (floating-point numbers, which are decimals, are referred to as floating-point numbers because the decimal position of a floating-point number is mutable when represented by scientific notation), a string (any text in or around a string ‘‘ "" ), a Boolean value, a null value (NULL is a special value in Python, With the None expression)

Print statement

The print statement can also keep up with multiple strings, separated by commas ",", and can be connected to a string of outputs, and a comma "," will output a space; "+" stitching does not create spaces

Python comments

Python comments begin with the # text followed by a comment until the end of the line

Python variables

Variables are represented by a variable name, and the variable name must be a combination of uppercase and lowercase English, numeric, and underscore (_) and cannot begin with a number;

In Python, an equal sign = is an assignment statement that can assign any data type to a variable, the same variable can be repeatedly assigned, and can be a variable of different types, which is called a dynamic language by the type of the variable itself;

     A = ' Imooc ' # A becomes a string
Raw strings and multiline strings in Python

Prefix the string to r indicate that it is a raw string, and that the characters inside it do not need to be escaped.

However r‘...‘ , the notation cannot represent multiple lines of string, nor can it represent " a string containing and

A multiline string that can be ‘‘‘...‘‘‘ represented by:

"' Line 1 '
Line 2
Line 3 "

Add this multiline string to a raw string before adding it in front of a multi-line string r

Unicode strings in Python
String encoding problem: Because the computer can only handle numbers, if you want to process the text, you must first convert the text to a number before processing (byte-encoded table).
A string expressed in Unicode with U ' ... ', for example: Print U ' Chinese '

In addition to a single Unicode string u , the escape character, raw string, and multiline notation are still valid, with no difference to the normal string;

If the Chinese string encounters unicodedecodeerror in a Python environment, this is because there is a problem with the format saved by the. py file. You can add comments on the first line #-*-Coding:utf-8-*-

The purpose is to tell the Python interpreter to read the source code with UTF-8 encoding.

Boolean type in Python

Boolean type operation: with Operation (and): Only two Boolean values are true when the result of the calculation is true.

or arithmetic (OR): evaluates to true as long as there is a Boolean value of true.

Non-arithmetic ( not ): Turns true to false, or false to true.

Boolean types can also be used with other data types for and, or, and not operations, Python will, 0 空字符串‘‘ and as None False, other numeric and non-empty strings are considered true;

and and the or An important rule of operation: short-circuit calculation

    1. When calculating a and b , if A is false, the whole result must be false, according to the algorithm, and therefore return a; If A is True, the entire result must depend on B and therefore return B.
    2. In the calculation a or b , if A is true, the whole result must be true, according to the or algorithm, and therefore return a; If A is False, the whole result must depend on B, so it returns B.
Python lists: List

A list is an ordered set of elements that are arranged in order and can be added and removed at any time.

Just by  enclosing all the elements of the list is a list object.
Because Python is a dynamic language, the elements contained in the list do not have to be the same data type, and we can include a variety of data in the list: L = [' Michael ', +, True]
Gets the specified element in the list by index. such as: L[0]
Reverse index: We can use the 1 index to represent the last element: L[-1]

Add a new element: The first way is to use the list append() method to append the new classmate to the end of the list;

The second method is to use the list insert() method, which accepts two parameters, the first parameter is the index number, the second parameter is a new element to be added;

The pop () method deletes the last element of the list, and thePop (index) method deletes the element of the specified index in the list, and it also returns the element;

To assign a value to an index in a list, you can replace the original element with the new element directly, and the list contains the same number of elements.

Python tuple: Tuple

A tuple is another ordered list. Tuple and list are very similar, however, once a tuple is created, it cannot be modified.

The only difference between creating a tuple and creating a list is the ( ) substitution [ ] .

t = (1,) also automatically adds a "," in order to tell you more clearly that this is a tuple when printing a cell element tuple. The multivariate tuple plus does not add this extra "," effect is the same:

The so-called " invariant " of a tuple is that each element of a tuple, pointing to never change. That point ' a ', it cannot be changed to point to ' B ', pointing to a list, cannot be changed to point to other objects, but the list itself is variable!

The condition judgment of Python

The IF statement is followed by an expression, and then begins with the : representation code block.

If ... else ... Statement, else there is a ":"

If ... Multiple elif ... else ... Structure, write all the rules at once;

The cycle of Python

The For loop can then iterate over each element of the list or tuple in turn:

L = [' Adam ', ' Lisa ', ' Bart ']
For name in L:
Print Name

While loop, while loop does not iterate over the elements of a list or tuple, but instead determines whether the loop ends with an expression.

N = 10
x = 0
While x < N:
Print X
x = x + 1

Break statement Exit Loop

Continue skips the subsequent loop code and proceeds to the next loop.

The dict of Python

The curly braces {} indicate that this is a dict, and then follow the key:valueand write them out. The last comma of a key:value can be omitted. Use the form of D[key] to find the corresponding value;

determine if key exists: use the in operator ' Paul ' in D

using a get method provided by the dict itself, when Key does not exist, returns None

Dict The first feature is the fast search speed, regardless Dict have a Ten elements, or Ten all elements, find the same speed . The search speed of the list decreases as the element increases. The disadvantage is that the memory is large, but also waste a lot of content ;

Dict The second feature is the storage Key-value sequence pairs are not sequential;

Dict The third feature is that as a Key elements must be immutable , the basic types of python, such as strings, integers, floating

The points are immutable and can be used as key. But the list is mutable and cannot be a key.

Set of Python collections

The way to create a set is to call set () and pass in a list,list element as the set element:

s = set ([' A ', ' B ', ' C '])

The elements stored inside the set are unordered . Set cannot contain duplicate elements and will automatically remove duplicate elements;

Accessing an element in a set actually determines whether an element is in the set. (judged by the in operator);

When adding an element, use the Add () method of set: If the added element already exists in set, add () will not error;

When you delete an element in a set, remove () uses the Remove () method of set, and if the element does not exist in the set, removing () will error.

Functions of Python

Define a function to use the def statement, write down the function name, parentheses, the arguments in parentheses, and the colon: and then, in the indent block, write the function body, and The return value of the function is returned with a return statement. Return none can be shortened to return.

Python Returns a tuple, in syntax, returns a tuple that can omit parentheses;

To allow a function to accept any parameter, we can define a mutable parameter: The variable parameter has an * number in front of the name, and we can pass in 0, one or more parameters to the variable parameter: DEF fn (*args):

Python-learn Note (2)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.