One, Python number type
1, numeric types have integer type, floating point type and some of the more uncommon types, numeric types support mathematical operations
subtraction in [1]: + 45out[1]: 68In [2]: 1.7 + 2out[2]: 3.7In [3]: 2 * 10out[3]: 20In [4]: 10/2out[4]: 5In [5]: 23-45ou T[5]: -22in [6]: & 7out[6]: 4
2, the math module of Python math
Properties and methods of In [7]: import mathmath in [8]: math.math.acos math.cos math.factorial math.ldexp math.sinmath.acosh math.cosh math.floor math.lgamma math.sinhmath.asin math.degrees math.fmod math.log math.sqrtmath.asinh math.e math.frexp math.log10 math.tanmath.atan math.erf math.fsum math.log1p math.tanhmath.atan2 math.erfc math.gamma math.modf math.truncmath.atanh Math.exp math.hypot math.pi math.ceil math.expm1 math.isinf math.pow math.copysign math.fabs math.isnan math.radians common function factorial: in [13]: math.factorial (out[13]: ) 230843697339241380472092742683027581083278564571807941132288000000000000L Open Square In [14]: math.sqrT (out[14]: 7.3484692283495345) constant piin [15]: math.piout[15]: 3.141592653589793
3. Random number Module
in [+]: import random1. Generate random number in [+]: Random.random () out[17]: 0.101435172235644362. Given random number generation range in []: Random.uniform ( 2, 3) out[18]: 2.8343477251262748In []: Random.uniform (100,3) out[19]: 84.883344129396963. Generates a random integer in [20] for a given range: Random.randint (3,4) out[20]: 34. Random selector in []: Random.choice ([1, ' a ', 3.4]) out[23]: 1In []: Random.choice ([1, ' a ', 3.4]) OUT[24]: 3.4In [+]: Random.choice ([1, ' a ', 3.4]) out[25]: ' A ' 5. Random reordering, in-situ modification in []: a = [1,2,3,4,5]in]: Random.shuffle ( a) in [aout[28]: [3, 1, 5, 2, 4]6. Random slices, without affecting the original sequence in [the]: Random.sample (a,2) out[34]: [1, 2]in []: aout[35]: [1, 2, 3, 4 , 5]
Two, Python string
A string is a sequence of strings of a single character that is immutable
1. String sequence manipulation
1. Define the string in [P]: a = ' Apache ' 2. String index (starting from 0, right starting from 1) in [PNS]: a[0]out[37]: ' A ' in []: a[1]out[38]: ' P ' in [All]: a[-1]out[39] : ' E ' 3. String slice in [max]: a[1:3]out[40]: ' pa ' in [P]: a[2:]out[41]: ' ache ' in [P]: a[:2]out[42]: ' ap ' in [Max]: a[:-1]out[43]: ' A Pach ' in [[]: a[-3:-1]out[44]: ' ch ' 4. String copy in [[]: B = a[:]in []: bout[46]: ' Apache ' 5. The character of the string is invariant in [h]: A + ' Web server ' out[47]: ' Apache Web server ' in [[]: aout[48]: ' Apache ' in [$]: A * 3out[49]: ' Apacheapacheapache ' in [[]: aout[50]: ' APA Che
2. String method
The usual string methods are as follows 1. Count occurrences of characters in a string in [Si]: a.count (' a ') out[54]: 2In [s]: A.count (' P ') out[55]: 12. Find the offset of the character in the string in [page]: A.find (' a ') OUT[57]: 0In []: A.find (' P ') out[58]: 13. Character substitution, do not modify metadata in [the]: A.replace (' A ', ' B ') out[59]: ' Bpbche ' in []: aout[60]: ' Apache ' 4. Uppercase conversion in [a]: A.upper () out[61]: ' Apache ' 5. Test string in [+]: A.isalpha () out[62]: True6. String split in [[]: b = "11:22:33:44 in [+]: B.split (': ') out[64]: [' 11 ', ' 22 ', ' 33 ', ' 44 ']7. Go to string trailing space in [the]: c = ' apache\n ' in [the]: c = C.rstrip () in []: CO UT[70]: ' Apache ' 8.python3.0 new feature: Replace in []: ' {0},2,{1},4 '. Format (' 1 ', ' 3 ') out[71]: ' 1,2,3,4 '
This article is from the "Linux Sailing" blog, make sure to keep this source http://jiayimeng.blog.51cto.com/10604001/1896157
Python Learning Diary First article