In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "introduction to basic data types of python language". Friends who are interested may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the introduction to the basic data types of python language.
3.1 numeric type
Numeric types: integer type, floating point type, plural type.
Integer types: decimal, binary, octal and hexadecimal.
# you can directly operate between integers with different bases > 0x3F2/10101.0 > (0x3F2+1010) / 0o17622.0
Floating point type: must have a decimal part, which can be 0, for example, 1010.0. The methods of representation are general representation (only decimal) and scientific counting. The numerical range and decimal accuracy are limited by different computer systems, and it is generally believed that there is no limit to the range. There is an uncertain Mantissa.
# Scientific counting format: e = axi10 to the b power (or E) > > print (1010) 1010.0 > > print (- 1.01E-3)-0.00101 # floating point Mantissa > 0.1 + 0.20 300000000000004 > > 0.1 + 0.2 = = 0.3False # use the round () function to round off the Mantissa to retain the Mantissa > round (0.1 + 0.2) = 0.3True
Plural type: a number that contains an imaginary unit (j). Can be regarded as a binary ordered real number pair (a, b), expressed as: a + bj,an is the real part, referred to as the real part, b is the imaginary part, referred to as the imaginary part. When b is 1, 1 cannot be omitted, 1J represents the plural, and j represents a variable in the Python program.
# get real number part > > (1.23e4+5.64e4j). Real12300.0 # get imaginary number part > (1.23e4+5.64e4j) .imag56400.0 >
3.2 Operation of numeric types
Nine basic numeric operators:
> > 2-5-5-3 > > 2-5-5-3 > > 2-5-5-3 > > 2-5-5-3 > 2-5-5-3 > 2-5-5-3-2-5-5-3-2-5-5-3-3-5-5-5-3-2-5-5-3-2-5-5-5-3-2-5-5-3-3-5-5-3-3-5-5-3-3-5-5-3 > 2-5-5-3 > 2-5-5-3 > 2-5-5-3 > 2-5-5-3 > 2-5-5-3 > 2-5-5-3 > 2-5-5-3 > 2-5-5-3 > 2-5-5-3
The result of integer and floating-point operation is floating-point number, the result of integer / integer operation is floating-point number, and the result of integer or floating-point number and complex operation is complex.
Enhanced assignment operators: + =,-=, * =, / =, / / =,% =, * * =
> Xerox 3 > Xbox 3 # is equivalent to x = x birthday 3 > print (x) 27
Numeric operation function:
# absolute value > abs (- 666) 666 # returns the two tuples (x _ Accord) x% y, that is, (integer quotient, remainder by division) > divmod (10 ~ (666)) (3,1) # x to the power of y That is to say, the y power% z of pow 27 # x (modular operation and power operation are faster at the same time) > pow) 4375 # rounded > > round (6.6) 7 # keep decimal Mantissa rounded > round (3.1415926Cool 3) 3.142 # take the maximum value > > max (1Min 2min 3pm 4min 50.1) 5 # take the minimum > min
3.3 string types and formatting
A single-line string can be represented by a pair of single quotation marks (') or double quotation marks (") as boundaries, and single and double quotation marks have the same effect.
A multiline string can be represented by a pair of three single quotation marks ('') or three pairs of quotation marks ("") as boundaries, both of which have the same effect.
> print ('this is "single-line string") this is "single-line string" > print ("this is' single-line string'") this is' single-line string'> print ("this is the first line of a multiline string and the second line of a multiline string") this is the first line of a multiline string, and this is the second line of a multiline string > > print (this is a multiline string. The first line of the string this is the second line of the multiline string) this is the first line of the multiline string and the second line of the multiline string.
Language escape character:\. For example:\ n for newline,\\ for backslash,\ 'for single quotes,\ "for double quotes,\ t for tabs (TAB), and so on.
> print ("requires both 'single quotation marks' and\" double quotation marks\ ") requires both 'single quotation marks' and 'double quotation marks'
Index of the string:
> [- 5] 'you' > > s = "Qingqingzi, leisurely in my heart." > > s [5] 'you'
Slicing of string:
> [8:4]'> > "Qingqingzi, leisurely in my heart." [: 4] 'Qingqingzi' > > 'Qingqingzi' > > "Qingqingzi" is leisurely in my heart. " [5:] 'take care of my heart.' > [5:]) take care of my heart.
The format () method uses: "string template containing {}" .format (comma-delimited parameter 1mem2re3.) Parameters are numbered from 0, and {} represents slots.
"{} said: learn and learn from time to time, not also {}." ("Confucius", "Shuo Hu") Confucius said, "it is not right to learn and learn from time to time." {1}: learn and learn from time to time, not {0}. " Confucius said: if you learn and learn from time to time, you should also say that it is not true.
The slot of the template string in the format () method can include format control information in addition to the parameter sequence number. {:}
> s = "Grade examination" > > "{: 25}" .format (s) # left alignment Default 'Grade exam' >'{: ^ 25} '.format (s) # Center' Grade exam'>'{: > 25} '.format (s) # right alignment' Grade exam'>'{: * ^ 25} '.format (s) # is centered and filled with the * sign' * Grade exam *'> > "{: + ^ 25}". Format (s) # is centered and filled with the + sign'+ grade exam +'> > "{: ten ^ 25}" .format (s) # is aligned and filled with the Chinese characters "ten", "ten grade exam ten" > "{: ^ 1}" .format (s) # z specifies a width of 1 Width of insufficient variable s: grade examination'> > "{: .2f}" .format (12345.67890) '12345.68' > "{: > 25.3f}" .format (12345.67890) '12345.679' > > "{: .5}" .format (National computer Grade examination) 'National computer' > "{: B}, {: C}, {: d}, {: O}, {: X}, {: X}" .format (425) '110101001 425, 651, 1a9rect 1A9'> "{: e}, {: e}, {: F}, {:%}" .format (3.14) '3.140000eLife00meme 3.140000Eong00000.140000314.000000%' > > "{: .2e}, {: .2e}, {: .2f}" {: .2%} ".format (3.14) # output the format control information of the commonly used format () method >" {: .2f} ".format (3.1415926) # output two digits after the decimal place '3.14' > >" {: X} ".format (1010) # output integer in the form of sixteen mechanism '3f2' >" {: .5} ".format "this is a very long string") # the first five bits of the output string "this is a very'>" {:-^ 10} ".format (" PYTHON ") # centered and populated with'--PYTHON--'
3.4 string type operation
String operator:
# concatenate two strings x and y > > "PHP" + "is the best language in the world"PHP is the best language in the world" # copy the string x > "lws" * 3roomlwslwslwslws' # if x is a substring of s, return True, otherwise return False > > name = "lws" > > l in name > "l" in nameTrue
String handling function:
# returns length of string x > len ("lws") 3 # returns string form corresponding to any type x > str (0x3f)'63'# returns single character corresponding to Unicode code x > chr (10000) '✐' # returns Unicode code represented by single character x > > ord ("and") 21644 # returns string in lowercase form of integer x corresponding to hexadecimal number > hex (1010) '0x3f2' # returns Integer x corresponds to the lowercase string of octal numbers > oct (- 255)'- 0o377'
String processing method: called in the form of .func (x). Method only acts on the leading object
# str.split (sep) separates the string str according to sep, and the split content is returned as a list type. > "Python is an excellent language." .split () ['Python',' is', 'an',' excellent', 'language.'] > "Python is an excellent language." .split (' a') ['Python is',' n excellent l', 'ngu',' ge.'] # str.count (sub) method returns the number of times sub appears in the string str, where sub is a string. The "Python is an excellent language." .count ('a') 3 # str.replace (old, new) method replaces the old string that appears in the string str with the new string. The lengths of old and new can be different. > "Python is an excellent language.". Replace ('Python',') 'Python is # n excellent lumbago.' > "Python is an excellent language.". Replace ('Python',' C')'C is an excellent language.' The str.center (width, fillchar) method returns a string of length width, where str is in the center of the new string, new characters are filled with fillchar on both sides, and str is returned when width is less than the length of the string. Where fillchar is a single character. > "Python" .center (20, "=")'= Python=' > "Python" .center (2, "=") 'Python'
3.5 Type judgment and conversion between types
Python language provides type (x) function to judge the type of variable x, which is suitable for any data type.
N = eval (input ('Please enter a number:') if type (n) = = type (123): print ("the number entered is an integer.") Elif type (n) = type (11.3): print ("the number entered is a floating point number.") Else:print ("unable to determine input type.")
Through the built-in numeric type conversion function:
Conversion of floating-point numbers or strings to integers > int (10.01) 10 # integers or strings to floating-point numbers > float (10) 10.0 # integers or floating-point numbers into strings > str (10.0) '10.0' to this point, I believe you have a deeper understanding of the "python language basic data types introduction", might as well come to the actual operation of it. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.