Python Learning log 02_ common data types

Source: Internet
Author: User

list :[4.0, ' string ', True], can be changed, ordered , L[n] represents the n+1 element; L[-n] denotes the penultimate nth, E.g.l[-2] It means the second last one.

Increase: l.append (' xxx ') can add xxx to the end of list, L.insert (n, ' xxx ') can add xxx to the list index n position;

Delete: Del l[n] You can remove an element with an index of n in the list; L.pop () can delete the last element in the list, L.pop (n) can delete an element with an index of n

Change: Directly re-assign value

Sort: L.sort ()


tuple tuples:(4.0, ' string ', True), cannot be changed, ordered , T[n] represents the n+1 element; T[-n] represents the nth of the penultimate, Tuples are commonly used in print statements

To eliminate () as precedence ambiguity, create a cell tuple is to add a comma, e.g.t = (1,) represents the cell tuple, t= (1) represents the integer 1

age = 22; name = ' Swaroop ';
Print '%s is%d years old '% (name, age)
Print ' Why are%s playing with that python? '% name


dictionary dict:d={key1:value1, key2:value2 }, unordered, key immutable value variable , can be through if KeyN in D See if a key exists

Increase: D[keyn]=valuen or D.setdefault (Keyn,valuen)

Delete: Del D[keyn] Remove the element with key KeyN, del d Delete dictionary, d.clear () Empty dictionary

Change: Del D[key]

Use a For loop for a dictionary if Keyn,valuen in D: | | Xxxx


Set Set: ([4.0, ' String ', True]), unordered, cannot contain duplicate elements , supports cross-and-poor set operations, iterates for X in S, adds S.add (), deletes S.remove ()


sequences : Lists, Ganso, and strings are sequences that can be used for indexing operations and slicing operations.

The list slice: L[0:3] can take the first three elements of the list, L[1:3] remove the second and third, that is, l[a,b] remove l[a] to l[b-1] elements, a=0 can be omitted.

L[:] means from beginning to end, L[::2] The third parameter indicates that each n takes one, the above l[a:b:c] from l[a] to l[b-1], every c element out of one.

Slicing the string: ' ABCDEFG ' [-3:]--> ' EFG '


Python Learning log 02_ common 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.