Python study notes (c) of the Python variable type

1. The standard data types

  • Numbers (Digital)
  • String (character)
  • List (list)
  • Tuple (tuple)
  • Dictionary (dictionary)

2. Number (digital)

  • Data types are not allowed to change, if you want to change the value of the Number data type, you will need to re-distribute content space.
  • delStatement can be used to delete references to a number of Number, Example: del var var can delete a Number of reference; when del var1, var2 plurality removed references var1 var2 and the like, the separation between the reference comma.
  • Number There are four different types of values:
    • Integer (int)
    • Long integer (Long) - unlimited size integers. The last integer is an uppercase or lowercase L. (Long integer deprecated in Python3 in)
    • Float (float)
    • Complex (complex): may be represented by a + bj, or complex (a, b). The real part and the imaginary part b is a float .
  • Conversion Number Type Value:
    • int (x, base =?) - x is a string, base (can be omitted) represent binary number x. This function is used to decimal conversion of x.
    • long (x, base =?) - as above, but you can type long infinity
    • float (x) - x string into a float
    • complex (real, imag) - creates a complex, which can be omitted imag
    • str (x) - x into a string object
    • eval (x) - the type of the passed, returns a formatting object
    • chr (x) and ord (x) with the use, can be used to calculate a number of ASCII code and the ASCII code may be converted to the form of characters. chr (x) is converted to ASCII character form, ord (x) sucked into ASCII character codes.
    • hex (x) - an integer to hexadecimal strings
    • oct (x) - tell a Integer to octet string
  • Mathematical Functions (math)
    • abs (x) - computing the absolute value
    • exp (x) - e power of x
    • fabs (x) - the absolute value of
    • sqrt (x) - square root
    • pow (x, y) - i.e. x ** y, x power of y
  • Trigonometric functions (math)
    • sin (x), cos (x), tan (x) cosine, tangent
    • asin (x), acos (x), atan (x) anyway / cosine, arctangent radians.
  • A random number function (Random)
    • random () randomly generate a range of [0,1) real number
    • seed (x) - to change the random number seed
    • choice (seq) - a randomly selected number from the sequence. Example: random.choice (range (10)) represented by a random number taken from 0 to 9.
    • uniform (x, y) - generating a random real number in the range [x, y]
  • Mathematical Constants
    • math.pi pi π
    • math.e constants of nature

3. String (String)

  • Creating a string 'or "strings enclosed content, it is to be noted that to correspond to each other end to end.
  • Three 'or three "can represent a string of multiple lines.
  • Python is no single character type , but also as a single-character strings.
  • Square brackets may be used to intercept the string . Example: var = "Hello"; var [0] can be taken to the 'H' the character, var [0: 2] may be taken "He".
  • It may be used +for string concatenation . Example:; "! Hello World" var = "Hello" var1 = var + "! World", the string may be assembled into form by this example.
  • Python used \this to escape the backslash character. Example: \\represents a single anti-diagonals, \rrepresents a carriage return and the like.
  • String operators
    • +For string concatenation
    • *For assignment string. Example: a = "Hello"; a * 2 == "HelloHello"
    • in and not in indicating whether a string in a string sequence
    • r/RBefore writing string that represents the original string. Example: r '\ n' it is output out \ n, is output.
    • uFor defining a Unicode string. (Unicode unify all the language set encodings, there will be no garbage problem.)
  • String built-in functions
    • Sequence occurs in the number of sub string str.count (sub, start, end) is calculated. Wherein the start and end values ​​may be omitted.
    • str.find (sub, start, end) in sequence to find sub string, returns the position to find the first occurrence of the sub, can not find -1
    • str.lower () will all lowercase string str
    • str.upper () will be fully converted to uppercase string str
    • str.strip () you remove the spaces before and after the string str. Similarly: lstrip () and rstrip () respectively to remove the spaces left and right spaces.
    • len (str) Returns the string length
    • str.format () format string ( the Python the format formatting function )

4. List (List)

  • List of data items does not necessarily need the same properties. Example: list1 = [ 'a', 1, 222, "bbb"]
  • A list of basic operations:
    • List created : list2 = [1, 2, 3]
    • List Access : list2 [0] = 1; list2 [0: 2] = [1, 2]
    • List added : Using append, namely list2.append (4)
    • List Delete : Use del, namely del list2 [0]
  • A list of scripted actions:
    • len () can be calculated length of the list
    • Compositions: [2,3] + [4,5] can be obtained [1,2,3,4,5]
    • Repeat: [ "Hello"] * 2 can be [ "Hello", "Hello"]
    • Whether the elements in the list. Example: 3 in [1,2,3] can be True
    • Traversing: for x in [1,2,3]: print (x)
  • List interception:
    • list3 [2] The third element of the access list
    • list3 [-2] ACL penultimate element
    • list3 [2:], taken from the back of the third element start all list elements
  • method:
    • count the number of occurrences of an element is calculated
    • pop (index =?) default to remove the last element
    • remove (obj) Removes the first occurrence
    • reverse () Reverse List
    • sort(cmp = None, key = None, reverse = False
      • cmp - represents Sort
      • key - an element of the specified object to sort
      • reverse - True for descending, False for ascending

5. Tuple (tuple)

  • Tuples and lists similar, the difference between them is that tuple elements can not be amended .
  • Basic operations and scripted operations are basically the same list of tuples, with reference to the above list of operations.
  • Although the tuple elements can not be amended, but tuples can be spliced (by +splicing, and to be assigned to the new tuple) and delete the entire tuple.

  • 元组形式: tup = (1, 2, 3, "a", "b")

  • Unsigned any object, separated by commas, default tuples.

6. Dictionary (dictionary)

  • Dictionary form: d = {key1: value1, key2: value2}
  • Unique, if repeated, will be covered when the key.
  • Value may take any number, but the key is immutable.
  • Dictionary basic operations:
    • Dictionary 创建: dict = { 'name': ' John Doe', 'age': 12}
    • Dictionary 访问: dict [ 'name'] output Zhang
    • Dictionary 修改: dict [ 'class'] = 'First', if the key already exists in the dictionary, then the cover; if not increase, compared with the new key-value pair.
    • Dictionary 删除: del dict [ 'name'] Delete 'name' of this entry; del dict deletes the entire dictionary
  • The basic guidelines keys:
    • A dictionary is not allowed key appears twice , is assigned the same key twice, the latter will be remembered.
    • Key must be immutable , it may be numbers, strings or tuples act as, but not with the list .

Guess you like

Origin www.cnblogs.com/vanishzeng/p/12185398.html