Basic Python content: data type

Source: Internet
Author: User
I. variables: Rule for variable definition: 1. variable names can only be any combination of letters, numbers, or underscores 2. the first character of the variable name cannot be a number. the following keywords cannot be declared as variable names [and, as, assert, break, class, continue, def, del, elif, else, except T, exec, finally, for, from, global, i. variables: Rule for variable definition: 1. variable names can only be any combination of letters, numbers, or underscores
2. the first character of the variable name cannot be a number.
3. the following keywords cannot be declared as variable names
['And', 'as', 'assert ',
'Break', 'class', 'contine', 'Def ', 'Del', 'elif', 'else', 'sort t', 'exec ',
'Finally ', 'For', 'from', 'global', 'if', 'import', 'in', 'is, 'lambda ',
'Not ', 'or', 'pass', 'print', 'raise ', 'Return', 'try', 'While ', 'with ', 'yield '] Data Type: 2. number type: int (integer) on a 32-bit machine. the integer length ranges from-2 ** 31 to 32 ~ 2 ** 31-1, I .e.-2147483648 ~ 2147483647 in a 64-bit system, the number of digits of an integer is 64 bits, and the value range is-2 ** 63 ~ 2 ** 63-1, that is,-9223372036854775808 ~ 9223372036854775807 long (long integer) is different from the C language. the length integer of Python does not specify the bit width, that is, Python does not limit the size of the long integer, but in fact because the machine memory is limited, the long integer value we use cannot be infinitely large. Note: Since Python2.2, if an integer overflows, Python will automatically convert the integer data to a long integer. Therefore, without the letter L after the long integer data, it will not cause serious consequences. Float (float type) floating point number is used to process real numbers, that is, numbers with decimal places. Similar to the double type in C, it occupies 8 bytes (64-bit), where 52 bits represent the bottom, 11 bits represent the index, and the remaining one represent the symbol. The complex number consists of a real number and a virtual number. the common form is x + yj, where x is the real number of a complex number, and y is the virtual number of a complex number, here, both x and y are real numbers. Note: There is a small number pool in Python:-5 ~
257 III. Boolean value True, False1, or 0 (all values except 0 are True) how to view the bool type of the variable? >>> Bool (0) 4. string concatenation: strings in python are represented as a character array in c. each time you create a string, you need to open a continuous empty block in the memory, in addition, if you need to modify the string, you need to open up space again. every time the "+" symbol appears, a space will be re-opened from it. Simply put, the plus sign is used to concatenate a string to waste resources. the string is formatted with name = 'Ian 'age = 12 print (' % s is % d years
Old. '% (name, age) # string is % s; integer % d; floating point % f string common features: str = 'Ian is 12 !! '# Remove blank # this method will remove the leading space and the trailing \ n linefeed str. strip () # split () will put the split field into a list, separated by spaces by default, str. split (',') separated by commas (,) s = str. split () print (type (s) print (s [1]) # Length print (len (str) # Index # returns a string that can output any character, if the index is negative, it is equivalent to the number from the forward. Print (str [10]) print (str [-4]) # slice # The slice is to extract part of the content from the given string print (str [0: 3]) print (str [: 3]) 5. list creation list: list1 =
['Apple', 'pear ', 'peach'] or list1 =
List (['apple', 'pear ', 'peach']) common functions of the list: list =
['Apple', 'pear ', 'peach', 66] # index print (list [0]) # slice, same string print (list [0: 2]) # append list. append ('banana ') print (list) # DELETE # remove method, delete element, no return value # pop method, delete element, return element value, delete list from the back by default. remove ('bana') print (list) print ('* 20) a = list. pop () print (a) print (list. pop (2) # delete peach, or use pop (-2) # Length to display the number of list elements print (len (list) # How to loop through a list? X = 0 # add the sequence number for I in list: x + = 1 print (x, I) # include if 'Apple' in list: print ('in ') else: print ('out') 6. create a Ancestor: tuple1 =
('Apple', 'pear ', 'peach') or tuple1 =
Tuple ('apple', 'pear ', 'peach') common operations of the ancestor: tuple1 =
('Apple', 'pear ', 'peach') # The ancestor and list are basically the same, but cannot be modified after the ancestor is created. the list can be modified # index print (tuple1 [1]) # slice print (tuple1 []) # Loop x = 0for I in tuple1: x + = 1 print (x, I) # Length print (len (tuple1 )) # if 'Apple' in tuple1: print ('in') else: print ('out') 7. the dictionary is unordered !!!! Create a dictionary: dic =
{'K1 ': 'v1', 'K2': 'V2'} or dic =
Common operations for dict ({'k1 ': 'v1', 'K2': 'V2'}) dictionaries: dic =
{'K1 ': 'v1', 'K2': 'V2'} # Index # The dictionary index uses the key as the key sub-print (dic ['k1 ']). # Add dic ['k3 '] = 'v3 'print (dic) # DELETE # del is the same as remove in the list and pop () is the same, delete the value and return del dic ['k3 '] print (dic) del_key = dic. pop ('K2') print (del_key) print (dic) # print ('* 20) dic =
{'K1 ': 'v1', 'K2': 'V2', 'k3 ': 'v3'} print (dic. keys () # Only display keyprint (dic. values () # Only show valueprint (dic. items () # Display key and value # loop for I in dic: # The default value is. keys (), which can be dic. values () or dic. items () print (I) # Length print (len (dic) 8. Loop/range/break/continue # Loop # A simple for loop is as follows: for I in, 3]: print (I) # range function> range (1, 5)
# Represents from 1 to 5 (excluding 5) [1, 2, 3, 4] >>>
Range (, 2) # represents from 1 to 5, interval 2 (not including 5) [1, 3]> range (5)
# Indicates the display method from 0 to 5 (excluding 5) [0, 1, 2, 3, 4] ps: The above is 2.0, which is not applicable in 3.0, in 3.0, the # breakbreak statement can be used in the for loop and while loop statements. Simply put, the break statement immediately exits the loop, and the loop body behind it is not executed. # The continuecontinue statement is also used in the for loop and while loop statements. With continue, you can skip this loop. the unfinished loop body does not loop, but directly performs the next loop.

The above is the basic content of Python: details of data types. For more information, see other related articles in the first PHP community!

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: 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.