Python (ii) supplements

Source: Internet
Author: User

1, int

Automatic execution within the system

A=123>>>>a=int (123) >>>>a=_init_ (123)

External calls



B=a.bit_length () calculates the minimum number of digits in an int (integral type) that are converted to binary

A=123print (A.bit_length ()) 7

B=len () View string, list, meta-ancestor, dictionary group length    

A= "123" Print (Len (a)

Python internal optimization (range is -5~257)

When n1=123


n3=123. Oython internal automatic optimization n1,n2,n3.,,, common use to listen to a memory address

Unique features within a string

1, Si.sartwith () Start with XXX

2, Si.strip () Remove both ends of the space

3, Si.find ("xx", A, b) find the sub-sequence position, not found return-1

4, Si.repace (0ld,new, replace a few) replace

5, Si.upper () all uppercase

6, si.isxxxx is XXX?

In 2.7, the string is looped in bytes, and in 3.0 it is looped as a character.

bytes can convert characters to bytes (binary string in 16 binary notation ) )

Use a For loop to output each byte in decimal notation

A= "Lu Xiaojun" for I in A:    print (i)    print (bytes (i,encoding= "Utf-8")) Lou B ' \xe5\x8d\xa2 ' Xiao B ' \xe6\x99\x93 ' Army b ' \xe5\x86\ x9b '

Characters converted to byte classes can also be converted to characters

Turn byte p=bytes ("in", encoding= "Utf-8")

Turn character d=str (p,encoding= "Utf-8")

A=str () >>>>>>1. Creating a String 2. Converting to a string

A list or a tuple followed (can be iterated (with A For loop))

Unique features of the list

1, append (li) means append (can append string, list, meta-ancestor, dictionary)

2, Extend () to expand their own, expand the content and self-integration with the dictionary inside the update ()

3. Clear () Erase all content

4, reverse () reversal

5. Insert (position, insert object) Inserts the object into the specified position


The Ganso element itself cannot be modified, but the element can be modified inside (Ganso's son cannot change, grandson variable)


Use enumerate when converting a list to a dictionary

How to extend a dictionary

1, the use of Updata expansion

2, li={"K1": "v1"} dic["K2"]= "v2" >>>>>>li={"K1": "V1", "K2": "V2"}


Generic string performs a function to generate a new content that does not change the original content

list, meta-ancestor, dictionary perform a new function, change itself

A= "Alex"

Print (A.partition ("E"))
[' Al ', ' X ']
(' Al ', ' e ', ' X ')

ASCII one byte eight bit
Unicode minimum of two bytes
Utf-8 Cubic bytes Chinese
GBK two bytes Chinese

When strings are numbers, they can be turned into integers.

List to Dictionary
For i,k in Enumrate (LI):
Print (DICT)
A=dict (Enumrate (LI))

Python (ii) supplements

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: 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.