Python Learning (i)--data type

Source: Internet
Author: User

Learn a little python at the university, only learn the grammar, about the actual application but not how to use. Now use some Python's script to simulate the webservices, very useful. This language, still want to study hard.

Currently looking at the textbook, this textbook, it seems that the same year, "Python Study Manual (fourth edition)", but now with the eclipse of the integration environment, writing Python is also a lot of convenience.

Examples of mathematical parts:

Import Math Print Math.PI Print math.sqrt (5)import  randomprint random.random ()

Example of a string part:

s='Spam'PrintLen (S)PrintS[0]PrintS[1:3]PrintS+'Abds'PrintS*8PrintS.find ('a')PrintS.replace ('PA','xx')Printsline='aaa,bbb,ccc,ddd'PrintLine.split (',')

Example of a column:

L=[123,'SPM', 1.23]PrintLen (L)PrintL[0]PrintL[:-1]Printl+[4,5,6,7]l.append ("NI")#AppendPrintLl.pop (3)#RemovePrintLPrint '==============='M=['AA','BB','cc']m.sort ()PrintMm.reverse ()PrintM

List parsing Examples:

m=[[1,2,3],   [4,5,6],   [7,8,9]   ]PrintMPrintM[1]PrintM[2]PrintM[1][2]Print '=============='col2=[ROW[1] forRowinchM]Printcol2col3=[ROW[1] forRowinchMifrow[1]%2==0]PrintCol3dialog=[m[i][i] forIinch[0,1,2]]PrintDialogdoubles=[c*2 forCinch 'spam']PrintDoublesPrint '====================='G= (sum (row) forRowinchM)PrintNext (G)PrintNext (G)PrintNext (G)PrintList (map (sum,m))

Dictionary Example:

d={' Food':'spam','quanlity': 4,'Color':'Pink'}Printd[' Food']d['quanlity']+=1PrintDPrint '======================'N={}n['name']='Job'n[' Age']=40PrintNPrint '======================'Rec={'name':{' First':'Bob',' Last':'Smith'},'Job':['Dev','Mgr'],' Age'74]}Printrec['name']Printrec['name'][' First']rec['Job'].append ('Pro')PrintRec

This dictionary data is similar to JSON.

Example for and while ordering:

d={'a': 1,'b': 2,'C': 3}ks=list (D.keys ())PrintKsks.sort ()PrintKs forKeyinchKs:PrintKey"=", D[key]Print '=========for=============='E={'a': 1,'b': 2,'C': 3} forKeyinchSorted (E):PrintKey,'=', E[key]Print '==========for==================' forCinch 'spam':    Print(C.upper ())Print '=========while=================='x=4 whileX>0:Print('spam!'*x) x-=1;

Tuple Example:

" "@author: Administrator" "T= (1,2,3,4)Printlen (t) t+ (5,6)PrintTPrintT[0]PrintT.index (4)PrintT.count (4)Print '======================='T=('spam', 3.0,[11,22,33])PrintT[0]PrintT[2][1]

File Example:

" "@author: Administrator" "F=open ('Data.txt','W') F.write ('hello\n') F.write ('world\n') F.close ()Print '======================='F=open ('Data.txt') Text=F.read ()PrinttextPrintText.split ()

This will generate a data.txt file in the current directory.

There is also a set type:

" "@author: Administrator" "X=set ('spam') Y={'h','a','m'}Printx, yPrintx&YPrintX|YPrintX-YPrint{x*2 forXinch[1,2,3,4]}Print '============ number ==========='Print1/3Print(1/3) + (2/3)Print1>2

The above types are almost all involved, and the string and other language formats are the same. A list is identified by the [] symbol, which can be a number, a string, and so on. The dictionary is identified by the {}, which is a key-value pair, and the map in Java is similar, but its value is extensible, taking a list, assigning multiple values. Tuples are () identified, and it is important that elements in a tuple are immutable. Then there is a set type, also identified with the {}, and the dictionary is different, this is not a key-value pair structure. The file types are quite different from theirs and are easy to understand.

Finally there is the object-oriented class, because it is python, so for the first time to see, feel not fit.

" "@author: Administrator" "classWorker:def __init__(self,name,pay): Self.name=name Self.pay= PaydefLastName (self):returnSelf.name.split () [-1]    defgiveraise (self,percent): Self.pay*= (1.0+percent) #调用bob=worker ('Bob', 5000) LastName=Bob.lastname ()PrintLastnamebob.giveraise (10)PrintBob.pay

Python Learning (i)--data type

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.