The second chapter "basic grammar" Section: Python data types and operators

A data type, the operator

type of data

Core Python \ common data types:
numeric, lists, tuples, dictionaries, set, the string

  • Variable value is not directly stored in python, but the stored value or reference to the memory address.
  • In python, without prior declaration variable name and type. Use an assignment statement can create any type of direct variable, the variable type depends on the type of expression value of the right side of the equal sign.
  • When the assignment (such as a = 'abc'), python interpreter to do two things: ① open space in memory to store 'abc' string; ② create a variable named a in memory, and it specifies that the character string.

Digital num

  • Python support: int, float, complex three different numeric types.
    type(a): A return variable data type name;
    isinstance(a,int): determining whether a variable is of type int.
    Added: instance 'example', isinstance Python is a built-in function. The variable is used to determine the type of an object.
  • print(0.4-0.3 == 0.1)Where # ==is the same value determination, it returns a Boolean type value. Value determination is not recommended directly between floating-point, because there is an error.
  • print(math.isclose(0.4-0.3 == 0.1))# Test whether two real close enough. Wherein math.isclosea determination function that returns a Boolean value.
  • Features: immutable type

String

  • Symbols used:
    single and double quotation marks, three marks (three single quotes, three double quotes);
    intermediate may be used \ escape special characters
  • Python source file in UTF-8 encoding default, all strings are Unicode strings.
  • Features: immutable; sequence belongs.
  • Support for multiple operating string concatenation, interception:
    • Stitching: Symbol+
    • Interception: square brackets for 'slice', such as
      a = 'hello'
      print(a[1:4])# 3 taken, i.e., from position 1 to position ell, and output. Intermediate use the index operation, python in all indexes are starting from zero.
    • The above operation belongs to the general procedure sequences, sequence of one string belongs.
  • sequence:
    • Comprising: a string, a list of tuples.
    • Operation: Index, fragmentation, stitching, repeated, measuring length, for loop, transitions between the three types.

List list

  • Symbols: square brackets []with commas between the elements ,separated;
  • Features: Type variable; sequence belongs.
  • operating:
    • sequence list belonging to meet common operating sequence: index, stitching, repeated, slice, measuring length, for loop type conversion;
    • most complete list data structure implemented collection classes;
    • The list may not be the same type of elements, support number, string, list itself (so-called nesting), etc.

Tuple tuple

  • Symbols: Use parentheses (), commas between the elements of ,segmentation;
  • Features: lists and the like, belonging to the sequence, but the list of symbols different and are immutable types, but may contain variable objects, such as list.
  • Note: When defining a tuple 1 only elements must also be coupled with a comma, for example: t1 = <mark>(1,).

Dictionary dict

  • Use the dictionary '键':‘值’in the form of storage, it has a fast search speed.
  • Key must belong to the immutable type, and the same dictionary keys must be unique.
  • A dictionary is unordered collection of objects,

Collection set

  • Dict dictionary and the like set, is disordered, but also a collection of key button, not a set of values, it does not store the value argument.
  • Features also has a key key: You can not repeat, it must be unique, repeating elements in the set, though not wrong, but it will be automatically filtered out.
  • Such as:
    s = set([1,2,3])
    s# results showed {1,2,3}
    s = set([1,1,2,3,3])
    s# {1,2,3} The results are shown
  • Note: set can be seen as a set of unordered and no repeat elements on mathematics, so that both can also be set on the intersection of mathematical sense ( &) and set ( |), difference ( -)

Operators

Arithmetic, relationship, logical, assignment operator, member operator, operator identity

Arithmetic Operators

Includes : + - * /% ** //
Tips:

  • +Wangchuck operator arithmetic operators for addition, it also can be used lists, tuples, strings (i.e., sequence) of the connector.
  • -In addition to integer, real, complex arithmetic subtraction between the contrast and the number may also be calculated difference sets set. And calculates the time between the real number, there may occur an error.
  • *In addition is an arithmetic multiplication between the integer, real, complex, may also be used to multiply the sequence (list, tuple, string) of the object and an integer representing the repeating sequence elements, generate a new sequence. The '123'*2string representing the repeated twice 123 generates a new string '123123'.
  • %Operators that can be used in addition to the number of remainder operation, a string format operation may also be used.

Relational Operators

Include: ==! => <> = <=
Can be determined between the numerical value of the size;
strings, lists, comparing the code size as element ASCⅡ encoding sequence;
between the set according to the relationship comprises comparing;
results are returned True or False.

Assignment operator =

NOTE: Arithmetic operators and assignment operator can be used in combination.

Logical Operators

Contains: and or not.
andBoolean AND. It is all true is true. When the first to False, returns False, the latter operation is not performed. When the first is True, the second one is determined, If False, returns False, True, the second return value.
orBoolean OR. One of which is true is true. When the first one is true, the latter operation is not performed, the result of a returned value; when the first false, the second one is determined, if true, returns the second value; are all false returns False.
notBoolean NOT. If x is True, it returns False; if x is False, returns True.

Member operator

inIf you find the value returned True, False otherwise specified sequence;
not inif not found in the specified sequence value return True, otherwise False.

Supplementary:
Python encountered a new function, to use the help documentation
help (function name)

Identity operator

Comprising: IS, IS Not
isIS is not the two identifiers is determined from a reference object. x is ySimilar id(x) == id(y);
IS and the difference between ==: is a reference variable is determined whether the two objects are the same; == is determined whether the value of the reference variable is equal.
is notAnalyzing two identifiers are not referenced from different object. x is not ySimilarly id(x) != id(y);.
The same simple integer address is the same.


Released five original articles · won praise 2 · Views 1664

Guess you like

Origin blog.csdn.net/GRIT_zhang/article/details/104711021