Python Learning Summary

Source: Internet
Author: User

One, type

1. Variable has no type, data has type

Example: num = 1---->num is no type, 1 is of type int

Second, formatted output

2.name = "Zhang San"

Age = 18

Print ("I am" + name + ", this year" + Age + "old") #这样会报错, when stitching with +, positive numbers and strings cannot be spliced

3. Placeholder

%c character

%s string

%d has a compliant decimal integer

%u unsigned decimal integer

%o Eight-binary integers

%x hexadecimal integer (Small letter 0x)

%x hexadecimal integer (capital letter 0X)

%f floating Point

%e Scientific counting method (lowercase ' e ')

%E Scientific counting method (lowercase ' E ')

Shorthand for%g%f and%e

Shorthand for%G%f and%e

Third, operator

1.

/except that the result is a floating-point number

Rounding is not rounded is the first integer that is smaller than the result

% take-up

* * Index

2. Compound operator ( when doing a compliance operation, in any case, the first is the right side of the equation )

+=

-=

*=

/=

**=

//=

3. Common data type conversions (input () is a string )

int (x) converts x to an integer

Float (x) converts x to a floating-point number

Complex (Real,[,imag]) creates a complex number, real is the real part, and Imag is the imaginary part.

STR (x) converts x to a string

REPR (x) converts X into an expression string

Eval (str) is used to calculate a valid Python expression in a string and returns an object

Tuple (s) converts a sequence s into a tuple

List (s) converts the sequence s into a list

Chr (x) converts an integer to a Unicode character

Ord (x) turns a character into an ASCII integer value

Hex (x) turns an integer into a hexadecimal string

4. Slicing

Name = "ABCdef"

Print (Name[0:3]) Remove the character labeled 0-2

Print (Name[0:5]) Remove the character labeled 0-4

Print (Name[3:5]) Remove the character labeled 3-4

Print (name[2:]) Remove the character after the Mark 2 to the last

Print (Name[1:-1]) to remove the character from the beginning to the last second of the Mark 1

5. Common Operations for strings

Find (Str,0,len (mystr)) finds whether the STR characters appear in the string, starting with the subscript 0 to the last; printing without finding-1

Index (Str,0,len (MYSTR)) is the same as the Find () method, except that if STR does not report an exception in MyStr.

COUNT () returns the number of times that STR appears in the mystr between start and end

Replace () replaces the str1 in mystr with the str2, and if Count is specified, the substitution does not exceed count times.

Nystr.solit (str,2) a str-delimited slice mystr,2 means splitting only a few strings

Capitalize capitalize the first character of a string

Title capitalize the first letter of each word of the string

StartsWith checks if the string starts with "str" and returns True

EndsWith checks if the string ends with "str" and returns True

Lower convert all uppercase characters to lowercase

Upper converts all lowercase in characters to uppercase

Ljust returns the left alignment of an original string and fills it with a space to the remaining length of the new string

Rjust returns the right alignment of the original string and fills the new string with the remaining length with a space

Center returns an original string centered and fills the new string with a space width of length

Lstrip Remove the white space character to the left of STR

Rstrip Remove the white space character to the left of STR

Strip Delete white space characters at both ends of the mystr string rfind from right to find almost

Rindex is similar to index (), but starts from the right.

Partition divides mystr into three parts, str, str and STR

Rpartition is similar to the partition () function, but starts from the right. Splitlines returns a list that contains each row as an element, separated by rows Isalpha if mystr All characters are letters returns TRUE, otherwise returns FALSEISD Igit returns True if the mystr contains only a number otherwise false.isalnum returns true if all characters are mystr or numbers, otherwise returns true if Falseisspace contains only spaces, Otherwise, the False.join mystr is returned after each string inserted into Str to form a new string

6. List of common operations ( list is ordered, can be repeated, variable )

To add an action:

List_1.append ("ASD") is added directly to the last

List_1.extend ([up]) adds elements from another collection to the list one by one

List_1,insert (index,obj) index means added to the specified subscript, and obj represents the content to be added

Change operation:

LIST_1[1] Modify according to the corresponding position

Check:

In (present), if present, the result is true, otherwise false

Not in (does not exist) if it does not exist then the result is true, otherwise false

Index and Count are the same as used in the string A.index (' a ', 1,4) find where a appears (the index of the principle) by index 1 to index 4

Delete operation:

Del: Delete according to subscript

Pop: Delete last element

Remove: Delete based on the value of the element

Sort by: Sort reverse

Sort is to arrange the list in a specific order by the parameter reverse=true can be changed to reverse, from large to small,

Reberse is to reverse the list

7: Tuple (element of tuple cannot be modified)

Index and Count are the same as in string and list usage

8: Dictionary (unordered variable) only immutable data type can be used as a dictionary key

If you access a nonexistent key value, you will get an error.

You can use the Get method when we are not sure if a key exists in the dictionary and want to get its value, and you can set the default value

To add an action:

1. Access to non-existent elements according to the index error

2. If the "key" is not present in the dictionary when using the variable name [' key '] = data, then this element will be added

Demo: Add a new element

Delete operation:

Del

Del info["name"]

Clear () Clears the entire dictionary, but the dictionary itself still exists

The number of key-value pairs in the Len (DIC) Measurement dictionary Dic.keys Returns a list that contains all keys for the dictionary dic.values returns a list that contains all the values of the dictionary Dic.items returns a list containing all (key, value) Ganso

Dic.has_key Dict.has_key (Key) returns True if key is in the dictionary, otherwise false

Iterating through the elements of a dictionary

Dict = {"Name": "Zhangsan", "Sex": "Nan"}

For item in Dict.items ()

Print Item

>> (' name ', ' Zhangsan ')

>> (' Sex ', ' nan ')

Traverse all key-value pairs

Dict = {"Name": "Zhangsan", "Sex": "Nan"}

For k,v in Dict.items ()

Print (K,V)

>>name,zhangsan

>>sex,nan

Implementing indexed traversal with subscript

>>> chars = [‘a‘, ‘b‘, ‘c‘, ‘d‘]>>> for i, chr in enumerate(chars):... print i, chr...0 a1 b2 c3 d

Cmp? Only in Python.

CMP (ITEM1, ITEM2) Comparison of two values

About references:

Use 1: Immutable type, modify parameters, do not affect arguments

Use 2: Variable type as argument modification change argument

When using 3: a mutable type as an argument, re-copying the parameter in the function

Result: The mutable type is the argument, and if the parameter is re-copied, the modified parameter does not affect the actual argument

Python Learning Summary

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.