Furious Python-Article 2-type and operation, Article 2 python operation

Source: Internet
Author: User

Furious Python-Article 2-type and operation, Article 2 python operation

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

PythonNumber (numbers): Int, long, float, and complex are supported, which is relatively simple.

PythonString): As mentioned above, it can be defined by multiple quotation marks. It supports [p] (get the characters at the p position) and [l: r] (get the string starting from l to the end of r), * t (get t strings that are linked together with this string), and + s (get the string connected to the s-link) method (methods, why is it related to ruby, all objects. 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

PythonList): Defined by []. It uses a vector container like C ++, but this container can hold different types of values and can be nested. One list contains another list, the method is similar to that of string, as shown in the following 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

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

PythonDictionary): Defined by {}, the definition method is json, and the container map like C ++ is used. 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] # value of the output key as 1 'wyp' print d. keys () # output all the keys print d. values () # output all values

PythonType 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 Operations: The extra operators include ** and //. Therefore, the extra operators ** = and // = are used as examples.

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 (and), or (or) and not (not). Examples are not given in the same usage.

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 a + = 1 B + = 1 print str (a) + ',' + str (B) + '\ n' before 256'

 

 

 


Python operations

1/2 python calculates an integer (because both are integers). 1/2 = 0.5. 0. 3/2 = 1.5. 1.

1% 2 Calculate the remainder 1% 2 = 1
If you want your calculation to be greater than 5, you can return true (1.0/2) * 100> 5.

Just do more, and this document should be introduced.

Logic Operations in python

Logical operations are not limited to boolean variables/values.

The key is whether a variable/value is "equivalent to" True or False.
None, False, empty string "", 0, empty list [], empty dictionary {}, empty tuples (), all are equivalent to False. All others are equivalent to True.

Let's talk about A or B first.
If A is equal to True, A is returned. Otherwise, return B.
Therefore, if both A and B are False, A and B are also False (False or False = False ).

In addition to boolean values, C = A or B is usually used to assign B to C as an alternative when you are not sure whether A is null.

A and B are opposite. If a is equal to True, B is returned. Otherwise, return.
Therefore, A and B are True (True and True = True) only when both A and B are True ).

The above is my own, original.

Related Article

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.