Python learning experience-basic knowledge (iv)

Source: Internet
Author: User

Data Type One, String (str)1. Make the string lowercase into uppercase a= ' Hello ' B=a.upper () 2, make the string uppercase a= ' Hello ' b=a.lower () 3, a quick view of the object's class there are methods A= ' abc ' B=dir (a) that is, a quick view of the string class there are methods 4. What are the detailed methods for viewing the object's class a= ' abc ' B=help (Type (a)) Second, integer (int)1, Bit_length () gets the number converted to binary can represent the shortest number of digits a=4 binary: 00000100a.bit_length () The result is 3 Three, List 1, inserting content at the end of the list--appenda=[1,2,3,4,5,6]a.append (7) Results: a=[1,2,3,4,5,6,7] Note: List contents can be  2 for any type, insert content in the list pointing position-- Inserta=[1,2,3,4,5,6,7]a.insert (3,5) Results: a=[1,2,3,5,4,5,6,7] 3, add elements of a list to the end of the list--extenda=[1,2,3,5,4,5,6,7] A.extend ([i]) results: a=[1,2,3,5,4,5,6,7,1,2,3] Note: Extend () accepts a list parameter, adds the element of the argument list to the end of the list, and append () takes an object parameter, Add an object to the end of the list eg:a.append ([three-way]) results: a=[1,2,3,5,4,5,6,7,1,2,3,[1,2,3]] 4, a list of elements with several identical--counta=[ 1,2,3,5,4,5,6,7,1,2,3,[1,2,3]]a.count (3) Results: 2, indicating that the ' 3 ' element has 2  5 in the list, the position of the first occurrence of an element in the list in the list--indexa=[ 1,2,3,5,4,5,6,7,1,2,3,[1,2,3]]a.index (5) Results: 3, indicating that the first occurrence of ' 5 ' in the list is 3 6, directly deleting an element in the list (the first occurrence of the element)--removea=[ 1,2,3,5,4,5,6,7,1,2,3,[1,2,3]]a.remove (5) Result: A=[1,2,3,4,5,6,7,1,2,3,[1,2,3]] 7 the element of a ' position ' in the list and prints out the deleted element-- Popa=[1,2,3,4,5,6,7,1,2,3,[1,2,3]]a.pop (3) Results: 4, which represents the deletion of elements on the list position ' 3 ', the deleted element is ' 4 ' a=[1,2,3,5,6,7,1,2,3,[1,2,3]] 8, Elements in the reverse list--reverseb=[1,2,5,4,9,7,3,6]b.reverse () Result: b=[6,3,7,9,4,5,2,1] 9, sort the original list--sortb=[1,2,5,4,9,7,3,6] B.sort () Results:b=[1,2,3,4,5,6,7,9]  Iv. Ganso (tuple)  1. Index a= (1,2,3,4,5) b=a[0]print (b) Results: 1 2, Slice a= (1,2,3,4,5) b=a[:3]print (b) Result: (a) 3, cyclic a= (1,2,3,4,5) for B in A:print (b) Result: elements are all printed out 4, length a= (1,2,3,4,5) B=len (a) print (b) Results: 5 5, including a= (1,2,3,4,5) 2 in a results: true note: Yuan zu v. Dictionary (dict)  1. Index a={' name ': ' Hloyun ', ' age ': 30}for b in A:print (b) Result: Age Name 2, delete a={' name ': ' Hloyun ', ' Age ': 30} Clear all content--a. Clear () 3, get the key, value, key value pair a={' name ': ' Haoyun ', ' Age ': 30} Gets the key--a.keys () Gets the value--a.values () gets the specified value, if the specified key does not exist, specifies a value, and does not error--a.get (' Name ') Gets the key value pair--a.items () 4, loop default output keya={' name ': ' Hloyun ', ' age ': 30}for b in A:print (b) Result: Age name 5, get length a={' Nam E ': ' Hloyun ', ' Age ': 30}len (a) Results: 2 Loop statement and other One, for loop statementsFunction: Iterate through the contents of an object in order 1, sample A=[1,2,3,4,5]for B in A:print (b) Result: All elements in the list are printed note: Note the role of break\continue in the FOR Loop statement second, the use of enumerateFunction: Automatically generate a column, default starting from 0 1, sample a=[' computer ', ' mouse ', ' yacht ']for k,v in Enumerate (a): print (k,v) Result: 0 pc 1 Mouse 2 yacht Iii. use of range and xrangeOrange is the 2 version of the usage, equivalent to the 3 version of the range effect: generate the corresponding number 1 by the specified range, in 1 units increments for a in range (1,5): print (a) result: [1,2,3,4,5] 2, increments of n units for a in ran GE (1,10,2): print (a) result: [1,3,5,7,9] 3, descending by n units for a in range (10,1,-2): print (a) result: [10,8,6,4,2] Iv. Code Conversion1, characters converted to bytes, with bytes (' String ', encoding= ' encoding method ') Utf-8 accounted for 3 bytes gbk accounted for 2 bytes name= ' Good luck '
For I in Name:
Print (i) bytes_list=bytes (i,encoding= ' Utf-8 ') print (bytes_list) #默认每一个字节都是16进制表示 for B in Bytes_list:prin T (b) #默认每一个字节都是10进制表示结果: Good b ' \xe5\xa5\xbd ' 229165189 op B ' \xe8\xbf\x90 ' 232191144 2, Byte converted to character # convert string to Bytea= ' Good luck 'b1=bytes (a,encoding= ' utf-8 ')Print (B1)b2=bytes (a,encoding= ' GBK ')Print (B2) #将字节转换才字符串c1=str (b1,encoding= ' utf-8 ')c2=str (b2,encoding= ' GBK ')3, 10 binary numbers converted to 2 binary lenidbin (10 binary digits)n=10Bin (N) Result: ' 0b1010 '

Python learning experience-basic knowledge (iv)

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.