Data type
A computer is a machine that can do mathematical calculations as the name implies, so a computer program can handle a variety of values. However, the computer can handle far more than the numerical value, but also can deal with text, graphics, audio, video, web and other kinds of data, different data, need to define different data types. In Python, there are several types of data that can be processed directly:
A number (numeric)1.1 Creation of numeric types
a=10b=ab=666 print (a) #10print (b) #666
Note that this differs from C:
#include <stdio.h>void main (void) { int a = 1; int b = A; printf ("a:adr:%p,val:%d,b:adr:%p,val:%d\n", &a,a,&b,b); A = 3; printf ("a:adr:%p,val:%d,b:adr:%p,val:%d\n", &a,a,&b,b); }//Print result: [email protected]:~$ gcc test.c[email protected]:~$./a.outa:adr:0x7fff343a069c,val:1b:adr:0x7fff343a0698, Val:1a:adr:0x7fff343a069c,val:3b:adr:0x7fff343a0698,val:1
1.2 Number type Conversion
Var1=3.14var2=5var3=int (var1) var4=float (var2) print (VAR3,VAR4)
ABS (x) returns the absolute value of the number, such as ABS (-10) returns 10#ceil (x) returns the top integer of a number, such as Math.ceil (4.1) returns 5#cmp (x, y) if x < y returns 1, if x = = y returns 0, if x > y returns 1#exp (x) returns the x power of E (ex), such as MATH.EXP (1) Returning 2.718281828459045#fabs (x) returns the absolute value of a number, such as Math.fabs (-10) to return 10.0#Floor (x) returns the lower integer of the number, such as Math.floor (4.9) returns 4#log (x) as Math.log (MATH.E) returns 1.0,math.log (100,10) return 2.0#log10 (x) returns the logarithm of x with a base of 10, as MATH.LOG10 (100) returns 2.0#Max (x1, x2,...) Returns the maximum value of a given parameter, which can be a sequence. #min (x1, x2,...) Returns the minimum value for a given parameter, which can be a sequence. #MODF (x) returns the integer part and fractional part of X, the two-part numeric symbol is the same as x, and the integer part is represented by a floating-point type. #pow (x, y) the value after the x**y operation. #round (x [, N]) returns the rounding value of the floating-point number x, given the n value, which represents the digits rounded to the decimal point. #sqrt (x) returns the square root of the number x, the number can be negative, and the return type is real, such as MATH.SQRT (4) returns 2+0Jpy built-in math functions
Two string types (string)
A string is ‘ any text enclosed in single or double quotation marks " , such as ‘abc‘ , and "123" so on.
Note that ‘‘ or "" itself is just a representation, not part of a string, so the string is ‘abc‘ only a , b c this 3 characters. If it ‘ is also a character, it can be "" enclosed, such as the character that contains it,,, the space, the "I‘m OK" I ‘ m O K 6 characters.
2.1 Creating a String:
var1 = ' Hello world! ' VAR2 = "Python Luchuan"
Corresponding operation:
# 1 * Repeat output string print (' Hello ' * * *) # 2 [], [:] Get the character in the string by index, here and the slice operation of the list is the same, the concrete content See list print (' HelloWorld ' [2:]) # 3 in Member operator-if the string contains the given character return Trueprint (' el ' in ' Hello ') # 4 format string print (' Alex is a good teacher ') print ('%s is a good TEAC Her '% ' Alex ') # 5 + string stitching a= ' 123 ' b= ' abc ' c= ' 789 ' d1=a+b+cprint (D1) # + inefficient, use joind2= '. Join ([a,b,c]) print (D2)
Python's built-in approach
#string.capitalize () capitalizes the first character of a string#string.center (width) returns the center of the original string and fills the new string with a space of length width#string.count (str, beg=0, End=len (String)) returns the number of occurrences of STR in a string, if beg or end specifies the number of occurrences of STR in the specified range#String.decode (encoding= ' UTF-8 ', errors= ' strict ') decodes a string in encoding specified encoding format, if an error defaults to a ValueError exception unless err ORS specifies ' ignore ' or ' replace '#String.encode (encoding= ' UTF-8 ', errors= ' strict ') encodes a string in encoding specified encoding format, and if an error defaults to a ValueError exception, unless errors specifies Is ' ignore ' or ' replace '#string.endswith (obj, beg=0, End=len (string)) checks whether the string ends with obj, if beg or end specifies whether the specified range ends with obj, if yes, returns TRUE, no False is returned.#string.expandtabs (tabsize=8) Turns the tab symbol in string strings to a space, and the default number of spaces in the tab symbol is 8. #string.find (str, beg=0, End=len (string)) detects if STR is contained in a string, and if beg and end specify a range, then the check is contained within the specified range if it is the index at which the start is returned Value, otherwise return-1#string.index (str, beg=0, End=len (String)) is the same as the Find () method, except that STR does not report an exception if it is not in a string.#String.isalnum () returns True if the string has at least one character and all characters are letters or numbers, otherwise False#String.isalpha () returns True if the string has at least one character and all characters are letters, otherwise False#String.isdecimal () returns True if the string contains only a decimal number, otherwise False is returned.#string.isdigit () returns True if the string contains only a number, otherwise False.#String.islower () returns True if the string contains at least one case-sensitive character, and all of these (case-sensitive) characters are lowercase, otherwise Fal Se#string.isnumeric () returns True if the string contains only numeric characters, otherwise False#string.isspace () returns True if the string contains only spaces, otherwise False.#String.istitle () returns True if string is heading (see Title ()), otherwise False#String.isupper () returns True if the string contains at least one case-sensitive character, and all of these (case-sensitive) characters are uppercase, otherwise Fal Se#string.join (seq) merges all the elements in the SEQ (the string representation) into a new string, using string as a delimiter#string.ljust (width) returns the left alignment of an original string and fills the new string with a space of length width#String.Lower () converts all uppercase characters in a string to lowercase.#String.lstrip () truncates the left space of a string#String.maketrans (Intab, Outtab]) the Maketrans () method is used to create a conversion table of character mappings, for the simplest invocation of two parameters, the first argument is a string representing the character that needs to be converted , the second parameter is also the target of the string representation transformation. #Max (str) returns the largest letter in the string str. #min (str) returns the smallest letter in the string str. #string.partition (str) is a bit like the combination of find () and split (), which, starting from the first position in Str, divides the string into a 3 The tuple of elements (STRING_PRE_STR,STR,STRING_POST_STR), if the string does not contain str, then string_pre_str = = String.#string.replace (str1, str2, Num=string.count (STR1)) replaces str1 in string with STR2, and if NUM is specified, the substitution is not more than num times.#string.rfind (str, Beg=0,end=len (string)) is similar to the Find () function, but looks up from the right.#string.rindex (str, Beg=0,end=len (string)) is similar to index (), but starts from the right.#string.rjust (width) returns the right alignment of the original string and fills the new string with a space of length width#string.rpartition (str) is similar to the partition () function, but looks up from the right.#String.rstrip () deletes a space at the end of a string string.#String.Split (str= "", Num=string.count (str)) slices a string with the Str delimiter, and if NUM has a specified value, only the NUM substring is delimited#string.splitlines (num=string.count (' \ n ')) is separated by rows, returns a list containing the rows as elements, and if num specifies only a slice of num rows.#string.startswith (obj, Beg=0,end=len (string)) checks whether the string starts with obj, returns True, or False. If beg and end specify a value, the check is within the specified range.#String.strip ([obj]) executes Lstrip () and Rstrip () on string#string.swapcase () flips the casing in a string#String.title () returns a string that is "heading", meaning that all words start with uppercase and the remaining letters are lowercase (see istitle ())#string.translate (str, del= "") converts string characters according to the table given by STR (contains 256 characters), and the characters to be filtered out into the del parameter#string.upper () converts lowercase letters in string to uppercase
Python-based data types