Python Foundation 3: Basic data types

Source: Internet
Author: User

Basic data Type one, int, shaping A, creation method

N1 = 123

N2 = Int (123)

b, int internal optimization

The default number of -5~257 is shared with one address

= = = ID (object memory address) = ID (object or variable name)

c, Length limit

32-2**31~2**31-1

-2**63~2**63-1

There's no limit in py3.

Second, str, string A, how to create

S1 = "Alex"

S1 = str ("Alex")

B, Unique features

SL = "Alex"

1, both ends go blank: s1.strip

2. To ... Beginning: S1.startswith ()

3, find sub-series "", "H": S1.find ()

4. Replace a subsequence in a string with the specified value: S1.replace ()

5. Capitalization: S1.upper ()

6, is ... : S1.isalphe ()

7. Join what is connected in what form:

Li = ["Alex", ' SB ']

L1 = "_". Join (LI)

L1 the corresponding value ALEX_SB

C, Public function

Index: Only one element can be taken

Slices: Fetching multiple elements

Len: Calculate element length

3.5 is represented by characters.

2.7 Inside is byte

For

3.5 is represented by characters.

    

Encoding, for:
Name = "Lu Li"
For I in Name:
Print (i)
bytes_list = (Bytes (i,encoding= ' utf-8 '))
Print (bytes_list)
For B in Bytes_list:
1, 3.5 for loop, every element of the loop is a "character"


2, character = = "Byte:
bytes_list = Bytes ("string", encoding= "Utf-8")

Utf-8 = "3 bytes
GBF = "2 bytes
Print (bytes_list) #默认每一个字节都是16进制表示
For B in Bytes_list:
Print (b) #默认没一个字节都是10进制表示
  
3, 10 binary digits = = "2 binary
Len
Id

Bin (plus a 10-digit number, which becomes 2 binary)

    

D, Byte, and string conversions: bytes and str

     

A ="Lu Li"            #Convert a string to bytesB1 = bytes (a,encoding="Utf-8")            Print(B1) B2= Bytes (a,encoding='GBK')            Print(B2)#Convert bytes to StringsNewal = str (b1,encoding="Utf-8")            Print(Newal) newa2= STR (b2,encoding="GBK")            Print(NEWA2)

list, listing A, how to create

Li = [11,22,33,44]

Li = List ()

Li = List ([11,22,33,44])

B, Conversion

1. Convert strings into lists

" Lu Li " # for, the character ==> can iterate li = List (s1)#forLoop, which will loop through each element as a list of elements #["Li" " Lu"]  Print(li)# result: [' Lee ', ' Lucy ']

2. Tuples converted to lists

# tuples converted to list t2 = ("Acker""seven""  Laolanhai"= list (t2)print(L2)# result: [' Acker ', ' seven ', ' Laolanhai ']

3. Dictionary conversion to List

#dictionaries converted to listsDIC = {"K1":"Alex","K2":"Seven"}l3=list (Dic.items ())Print(L3)#results: [(' K1 ', ' Alex '), (' K2 ', ' Seven ')]#The dictionary consists of key and values#string, tuple, dictionary ==> list
C, Unique features

Li = [111,22,3]

1, Append: Li.append ()

2, clear: Li.clear ()

3, flip, inside the byte element flip: Li.reverse ()

4. Insert the specified element into the specified position: Li.insert (1, "X")

5, go to Space: Li.strip ()

6.

# expands bytes to the inside of a byte with another iterated object # str,list,dic,tupleli = [111,22,3" lu Li "li.extend (s)print  (LI)# results: [111, 22, 3, ' Li ', ' Lulu ']
D. Public functions

Li = ["Alex", "Aric", "Sevev", "123"]

Index: li[2]

Slice: li[2:3]:::*** What kind of the original is what type, the list is also a list of tiles * * *

For

Len

del# Delete

Example of an index nesting: Remove the 2nd "123":

Li = ["Alex", 123, {"K1": "V1", "K2": {"VV": (One, one, 123), "II": 456}}]

LI[2]-->{"K1": "V1", "K2": {"VV": (One, one, 123), "II": 456}}

li[2][' K2 '-->{"VV": (One, one, 123), "II": 456}

li[2][' K2 ' ["VV"]--(11, 22, 123)

li[2][' K2 ' ["VV"][2]-->123

Iv. tuple A, creation and conversion

t = (11,22,33)

t = tuple ((11,22,33))

t = tuple ([]) #字符串, list, dictionary

# tuples are converted to lists t2 = ("Acker","seven","  Laolanhai"= list (t2)print(L2)# result: [' Acker ', ' seven ', ' Laolanhai ']

B. Unique methods
#add content to tuples#tuplet = (11,22,["Alex",{"K1":"v1"}]) t[2][1]['K2'] = 123Print(t)#results: (one, one, [' Alex ', {' K1 ': ' v1 ', ' K2 ': 123}])T[2][1].update ({'K3': 123})Print(t)#results: (one, one, [' Alex ', {' K1 ': ' v1 ', ' K2 ': 123, ' K3 ': 123}])

Count

Index

C, nesting (get Element)

     

T = (one, one, ["Alex", {"K1", "v1 ")  = t[2][1]["K1"]print(L1)# Results: v1
D, tuples, characteristics:

The son cannot be modified.

E, collation:

Generic string, performs a function, generates a new content, the original content is unchanged list,tuple,dict,

Perform a function that changes itself

V. Dictionary a, creating

A = {"K1": 123}

A = Dict ()

#dictionaries converted to listsDIC = {"K1":"Alex","K2":"Seven"}l3=list (Dic.items ())Print(L3)#results: [(' K1 ', ' Alex '), (' K2 ', ' Seven ')]#The dictionary consists of key and values#string, tuple, dictionary ==> list

b, the use of enumerate

Converts a list to a dictionary {key:10 sliding scale, value: the element in the table}

li = [one, A, dict (enumerate (li,10))#Enumerate can automatically add a column of keys to form the dictionary print (new_dict) # Result: {10:11, 11:22, 12:33}
   C, add content in the dictionary:
 #   dictionary add content  dic = {  " k1   ": "  v1   "  " #   Method 1  #   Dic.update ({' K2 ': 123})  #   print (DIC)  #   Method 2  dic[ "  K2   "] = 123print   (DIC)  #   result: {' K1 ': ' v1 ', ' K2 ': 123} 

    

# a handy way to add a dictionary d ={"K1":'v1'}# d.update ({' K2 ': 123})d['K2'] = 123# if replaced by: d[' k1 '] = 123 is the V1 can be updated print(d)# result: {' K1 ': ' v1 ', ' K2 ': 123}

D, the use of Fromkeys
#How to get K1,K2, the default value is AlexDIC = {'K1': 123,'K2': 456,'K4': 111}n= Dic.fromkeys (['K1','K2'],'Alex')Print(n)#result: {' K1 ': ' Alex ', ' K2 ': ' Alex '}

    

    






   

  

    

Python Foundation 3: Basic data types

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.