Furious Python-Article 2-types and operations

Source: Internet
Author: User
Tags python list
Python supports five data types, including numeric, string, list, tuple, and dictionary ). As a dynamic type language, you do not need to declare the type of a Identifier. the type of the identifier is automatically determined when it is used. Python supports five data types, including numeric, string, list, tuple, and dictionary ). As a dynamic type language, you do not need to declare the type of a Identifier. the type of the identifier is automatically determined when it is used.

If you do not use the defined variable, you can use del to delete it to release the occupied resources, as shown in figure

Var = 10del var

Python numeric (numbers): Supports int, long, float, and complex, which is relatively simple and does not provide an example.

Python string (string): As mentioned above, it can be defined by a variety of quotation marks. it supports [p] (get the characters at the p position), [l: r] (get the string starting from l to the end of r), * t (get the string that t is linked together), and + s (get the string connected to s) method (methods, why is the method used? it is related to ruby. methods can display all the operations that can be executed, so I also started to call an operation that can be executed by an object as a method), as shown in the following example:

S = 'This is a test case. 'print s [0] # output s's first character 't'print s [0: 4] # output s's first to fourth character string 'C' PRint s * 2 # print s + 'good! '# Output s and 'good! 'Link string

Python list: it is defined by []. it uses a vector container like C ++, but this container can hold different types of values and can be nested, A list contains another list. the method is similar to that of a string. The following is an example.

L = ['wyp', 1, 3.14, [2, 'CC'] print l # output the entire lprint l [0] # output the first element of l 'wyp' print l [3] [0] # output the fourth element of l the first element 2 print l [0: 3] # output the first element of l to the third element print l * 2 # output the list of two l linked together print l + ['. '] # output l and list ['. '] linked list

Python tuple: defined by (). The method is similar to the list. The only difference is that a value cannot be assigned twice, so no example is provided.

Python dictionary (dictionary): defined by {}. The definition method is like json. it is used as a C ++ container map. the method is shown in the following example.

D = {1: 'wyp', 'two': 'Is, 3.0: 'coder '}
D ['Four '] = 'like 'd [5] = 'Farmer 'print d # output the entire dictionary dprint d [1] # output key 1 value 'wyp' print d. keys () # output all the keys print d. values () # output all values

Python type conversion: not used in visual testing. The following is an example.

Int (x) # Convert x to int type and take the integer directly, regardless of the ending number long (x) # Convert x to long type and take the integer directly, no matter the ending number float (x) # Convert x to float complex (x, y) # create an x to be the real part, and y to be the plural chr (x) of the virtual part) # convert to the character ord (c) corresponding to ascii code x # convert to the ascii code unichr (x) corresponding to c # Convert x to the unicode character hex (x) # convert x to its hexadecimal string oct (x) # convert x to its octal String str (x) # Convert x to the string eval (str) # calculate the value of a string expression 'str'. tuple (s) # convert the value of string-type structure into a list of dict (s) # Convert the s of a sequential structure and each element is a binary group into a dictionary

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------

Python's operation methods are very similar to C ++, which can be divided into arithmetic operations, comparison operations, logical operations, and bit operations. On this basis, member operations and identity operations are added, the following is an example of a discrepancy with C ++.

Arithmetic operation: the multiple operators include ** and //. Therefore, the ** = and // = operators are added to give an example.

A ** B # that is, a ^ B. the B power of a is a/B # first a/B and then directly integer

Comparison: The multiple operators are "<>", and "! = "The function is the same, so no example is given.

Logical operation: here it is a little different. all logical operations are expressed in English, that is, they are similar to (and), or (or), and they are not (not ).

Bitwise operation: same as C ++

Member operation: the operators are in and not in. take in as an example. If an element is true in the sequence, otherwise, false, and inverse is used, the following is an example.

T = [1, 2, 3, 4] 1 in t # returns true1 not in t # returns false0 in t # returns false0 not in t # returns true

Identity calculation: the operators are "is" and "not is", that is, to determine whether two variables are in the same storage unit. here we find a small python detail, its 0 ~ 256 all these integers are stored in the memory, while others are directly allocated to the new memory when defined. the following example is provided.

A =-1b =-1a is B # The value is false. here, we find that-1 is not the + = 1b + = 1 while (a is B) pre-stored in the memory of python ): # true before 256
A + = 1
B + = 1 print str (a) + ',' + str (B) + '\ n'

The above is the content of the furious Python-Article 2-type and computation. For more information, see PHP Chinese network (www.php1.cn )!

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.