Python Beginner Knowledge Points Summary

Source: Internet
Author: User

The beginning of the file needs to write the declaration "#!/usr/bin/env Python3" (Do not write 3, the default execution of the existing computer 2.7 version)

After you create a new. py file, enter Python xx.py in the shell to run, or enter chmod a+x xx.py carriage return, and then run through./xx.py

The most intuitive feeling is not to write a semicolon, there is no "{}", with indentation represented, this is often due to incorrect indentation and error.

The amount of indentation used for the error. (resulting in "indentationerror:unexpected indent", "Indentationerror:unindent does not match any outer indetation level" and " Indentationerror:expected an indented block ")

Forget to add at the end of the IF, elif, else, for, while, class, Def declarations: (resulting in "syntaxerror:invalid syntax") Note: Elif is the meaning of else if, For,while can use E Lse

Single-line Comment • Front plus "#", Multi-line comments in ' ' ' as: ' ' Comment out the content '

print--Printing, input--input

int (num), type (num)//convert num to an integer to view the data type of num

a,b,c=1,2,3//to A,b,c, respectively, to the value of

The power of 2 is 5 times: 2**5

' Really '. Encode (' Utf-8 ') where the parentheses are not written, and the default is ' Utf-8 ', which is the transcoding with Utf-8

' xxxxx '. Decode (' Utf-8 ') This is decoding, other ibid.

Ternary Operation D=a If a>b else C-and when a is greater than B assigns a to D otherwise assigns C to D

There are three ways to display a variable in a string:

a) info=‘‘‘------------ info of %s ------------Name:%sAge:%dSalary:%s‘‘‘%(name,name,age,Salary)b) info2=‘‘‘------------ info of {_name} ------------Name:{_name}Age:{_age}Salary:{_Salary}‘‘‘.format(_name=name,_age=age,_Salary=Salary)c) info3=‘‘‘------------ info of {0} ------------Name:{0}Age:{1}Salary:{2}‘‘‘.format(name,ageSalary)

Import file Imports, will be found in the local, and the location of the Python file to find the address, so the name of the file itself is the same name, easy to cause errors.

Password not visible when entering the password to be introduced when "Getpass" is used: Password=getpass.getpass ("Password:")--Display as: Password: (need to enter content)

Names2=names.copy (), names2=list (names), names2=names[:] are all shallow clones, to be introduced "copy" for deep cloning, when used: names2=copy.deepcopy (names), After the introduction of copy, you can also use copy for shallow cloning, such as: names2=copy.copy (names)

There are two types of lists:

a) list=["xx", "xx", "xx", "XX"]--> can be operated by Append,insert,remove,del,pop,clear,reverse,sort,count,index etc.

List.append ("xx") = + adds "xx" to the last one in the list

List.insert (n, "xx") = + adds "xx" to the position labeled N in the list listing,

List.remove ("xx") = = Remove "xx" from the list,

Del List[n],list.pop (n) + removes the value labeled N in the list listing, where the last one is removed if the value is not written after the pop parenthesis.

List.clear (), List.reverse (), list.sort () = clear, flip, sort,

List.count (' xx '), List.index (' xx ') \ = Query ' xx ' position in list,

b) list= ("xx", "xx", "xx", "XX")--the values in the list are immutable and can only be manipulated by Count,index.

List also has a way to operate: List[start:end:n]->start,end,n can be omitted, are omitted after the list[:] That is, the front of the shallow clone, start,end with the head does not contain the tail, n is the interval n-1 values-- Eg.list=[' A ', ' B ', ' C ', ' d '], after List[::2] operation, the result is =>[' a ', ' C ']

It is similar to the for loop, such as:

For I in Range (1,10,2):

Print (i)

The result is: 1,3,5,7,9

\033

Print ("\033[31;1mxxxxxxx\033[0m") ==>31 is font red, 32 is font green, 33 is font yellow, 41 is font background red, 42 is font background green, 43 is font background yellow

Python Beginner Knowledge Points 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.