Data type of Python review supplements

Source: Internet
Author: User

One, operator

in# who is in ... In

' Hello ' in ' sdfjsdfjsdlkfjsd '

"Li" in [' Li ', ' OK ']

II. Basic data types

int

A. How to create

N1 = 123 #根据int类, an object was created

N2 = Int (123) #根据int类, an object was created

b. int internal optimization

1, N1 and N2 memory addresses are the same

N1 = 123

N2 = N1

2, supposedly N1 and N2 memory address is not the same

That

N1= 123

N2 = 123

But Python's internal optimizations, -5~257 within Python, are all pointing to the same memory when written in 2.

N1 = 123

N2 = 123

Besides, the memory addresses of N1 and N2 are not the same.

N1 = 123123

N2 = 123123

3, ID view the memory address of the object

n1= 123

I1 = ID (n1)

Print (I1)

c, int length limit

32-bit system-**31~2**31-1

64-bit system -2**63~2**63-1 After python2.2, after the maximum length of int is exceeded, it is automatically converted to long type, long type has no length limit

After Python3, int is limited to its own length

Str.

A. How to create

S1 = "Alex"                S1 = str (' Alex ')                s1 = str (' Alex ')

  


B. Unique Features
Remove spaces at both ends
S1.strip ()

To.. Beginning
S1.startswith ()

Find the sub-sequence "", "H"
S1.find ()

Replaces a subsequence in a string with the specified value
S1.replace ()

Change capitalization

S1.upper ()

be ... It?
S1.isalpha ()

c. Public functions
index: Only one element can be taken
slices: Fetching multiple elements
Len:
Len ("Lilou")
3.5 = = "character
For :
3.5 = = "character

encoding, for:
         

    Name = "Lilou" for          i in Name:           print (i)          bytes_list = bytes (i, encoding= ' utf-8 ')          print (bytes_list) for          b In bytes_list:          1, the 3.5 for loop, each element of the loop is "character"          2, character = "byte          bytes_list = bytes (" string ", encoding= ' Utf-8 ')

  


# utf-8 3 bytes
# GBK-"2 bytes
print (bytes_list) # Default Every byte is a 16 binary representation
For B in bytes_list:
print (b) # Default Every byte is a 10 binary representation

3, 10 binary digits = = "2 binary
Len
ID
bin (10 binary digits)
d, Bytes and str conversions
a = "Lilou"
# Convert strings to bytes

           B1 = Bytes (A, encoding= ' Utf-8 ')           print (B1)           b2 = bytes (A, encoding= ' GBK ')           print (b2)           # Convert bytes to string           NEWA1 = str (B1, encoding= "Utf-8")           print (NEWA1)           newa2 = str (b2, encoding= ' GBK ')           print (NEWA2)

  


###########
x = str ()
Create a string
Convert to String, byte, encode
m = bytes ()
Create byte
Converts bytes to bytes, strings, what encoding type to program

List
The "collection" of mutable elements

-----------
STR--Create a string, or convert the other into a string
------------------------------------------
List --Create lists, convert other elements to lists

A. Creating and converting
1. Create
li = [11,22,33,4]
li = List ()
li = List ([11,22,33,4])
2. Conversion
           

            S1 = "Lilou"            #for, character ==> can iterate            l1 = list (S1) # FPR loop, each element of the loop will be looped as a list of elements            ["Lee", "Dew"]            print (l1) S1            = "Lilou" 
   for, the character ==> can iterate            l1 = list (S1) # FPR loop, will loop through each element, as a list of elements            ["Lee", "Dew"]            print (l1)            t2 = ("Alex", " Laonanhai "," seven ")            L2 = list (t2)            print (L2)            # dictionary            dic = {' K1 ':" Alex "," K2 ": ' Seven '}            L3 = List ( Dic.items ())             print (L3)

  

string, tuple, dictionary = "list

B. List-specific features
Append
li.append ()
Clear
li.clear ()
extend yourself, with another iteration of the object, to expand into your own internal
Str,list,dict,tuple
s = "Lilou"
Li.extend (s)
Print (LI)
Flip and flip your own internal elements
Li.reverse ()
inserts the specified element into the specified position
Li.insert (1, "X")
c. Public functions
li = ["Alex", "Eric", ' Seven ', 123]
index: li[2]
Slice: li[2:3]

           For           Len           D.           Li = ["Alex", "Eric", ' Seven ', 123]           li = ["Alex", 123, {"K1": "V1", "K2": {"VV": (11,22,123), "II": 456}}]          li[2] -{"K1": "V1", "K2": {"VV": 123, "II": 456}}          li[2][' K2 '] ==> {"VV": 123, "II": 456}          li[2][' K2 ' ["VV"] ==&G T (11,22,123)          li[2][' K2 ' ["VV"][2]

  



Meta-group
A. Creating and converting
t = (11,22,33)
t = tuple ((11,22,33))
t = tuple ([]) # string, list, dictionary
B. Specific methods
Count
Index
c. Nesting (elements cannot be modified)
t = (11,22,33)
t = (11,22,["Alex", {"K1": "V1"}])


            e. Properties of tuples, non-modifiable, who cannot be modified
             tuples, son cannot change
             tuples, son can't change, grandson, ...

             organizing:
             General string, perform a function, generate a new content, the original content is unchanged
            list,tuple,dict, performing a function that changes itself


             dictionary:
            1, creating
           

A = {"K1": 123}            A = Dict ()            li = [11,22,33]            new_dict = dict (Enumerate (LI))            print (new_dict)

  


2. Dictionary internal function
Keys ()
VALUES ()
Items ()
Pop




Data type of Python review 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: 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.