About Python variable types

  1. Variables

  Value of the variable is stored in memory

  This means that will open up a space in memory when you create a variable

  Based on the data type of the variable, the interpreter allocates specified memory, and decide what data can be stored in memory

  Thus, the variable can specify different data types, these variables may be stored an integer, decimal, or other characters

  2. Assignment

  Python does not need to type in the variable assignment statement

  Each variable created in memory, including all the variables ID, name and data information

  Each variable must be assigned before use variable assignment after the variable will be created

  Equal sign (=) is used to assign values ​​to variables

  The left side of the equal sign (=) operator is a variable name

  The right side of the equal sign (=) operator is the value stored in the variable

  counter = 100 # integer variable assignment

  Python allows simultaneous assignment to multiple variables

  a = b = c = 1

  a, b, c = 1, 1.0, "1"

  3. Standard

  The data stored in memory may be various types

  For example, a person's age can be stored digitally, and his name can be used to store characters

  Python defines a number of standard types, for storing various types of data.

  There are five standard Python data types

  Numbers (Digital)

  Type for storing the digital data value

  Immutable data types

  This means changing the numeric data type will be assigned a new object

  When you specify a value, Number object is created:

  var1 = 1

  Del statement can also be used to remove some references to objects

  of var1

  It supports three different types of numbers:

  int (signed integer)

  float (float)

  complex (plural)

  E.g:

  int  float  complex

  10 10.0 (10 + 0j)

  String (String)

  Strings or string (String) is a string of characters consisting of numbers, letters, the underscore

  It is the programming language indicates the data type of text

  s = "a1a2···an" # (n>=0)

  There are two kinds of string list of values ​​in this order:

  The default start index 0 from left to right, the maximum range is a little string length

  Index of default from right to left at -1, the maximum range is the beginning of a string

  

Here Insert Picture Description


  s = 'joveH-H'

  print (s) # complete output string

  print (s [0]) # output of the first character in the string

  # joveH-H

  # j

  If you want to achieve access for a sub-string from a string

  You can use [superscript head: Tail index] to intercept the corresponding string

  Wherein the subscript is counted from 0, may be positive or negative, the subscript can represent an empty take head or tail

  Get a character string containing the subject head, but not the end of the target character

  print (s [2: 5]) # string output string between the third to fifth

  print (s [2:]) # string output start from the third character

  # veH

  # veH-H

  May receive a third parameter, the parameter is taken action steps

  The following examples are for the index to the index 2 and 5 is set to 2 steps (spaced position) to intercept the string

  print (s [2: 5: 2]) spacing between string # 2 output string third to fifth

  # Articles

  List (list)

  You can complete the list data structure to achieve most of the collection classes

  Support characters, numbers, strings may even contain a list (ie, nested)

  List with [] identified python is the most common complex data type

  l = ['jove', 10086, 2.23, 'H-H', 20.3]

  print(l)

  # ['jove', 10086, 2.23, 'H-H', 20.3]

  Cutting list values ​​may be used in variable [head superscript: Tail index], the corresponding list will be taken

  From left to right index default 0, from right to left index default at -1

  Subscripts may take the tail head is empty or represents

  The last list element # output print (l [-1])

  print (l [1: 3]) # output of the second to the third element

  print (l [2:]) # output from the start to the end of the third list of all of the elements

  print (l [1: 5: 2]) interval between the elements 2 # output string second to fifth

  # 20.3

  # [10086, 2.23]

  # [2.23, 'H-H', 20.3]

  # [10086, 'H-H']

  Tuple (tuple)

  Tuple is another data type, similar List (list)

  Identified by tuple (). Internal element separated by commas

  But tuple elements can not be secondary assignment, equivalent to the read-only list

  t = ('jove', 10086, 2.23, 'H-H', 20.3)

  t [2] = 1000 # tuple is illegal application

  # TypeError: 'tuple' object does not support item assignment

  Set (collection)

  Collection (set) is an unordered sequence of elements will not be repeated

  Braces {} may be used or a set () function creates a collection

  s1 = {'g', 'o', 'o', 'g', 'l', 'e'}

  s2 = { 'g', 'the', 'of', 'g'}

  print(s1, s2, s1 - s2)

  # { 'And', 'or', 'l', 'g'} { 'or', 'g'} { 'l', 'and'}

  Note: Create an empty set must be set () instead of {}, {} as it is used to create an empty dictionary

  Dictionary (dictionary)

  Dictionary (dictionary) python is in addition to being outside the list of the most flexible type of built-in data structures

  List is an ordered collection of objects, dictionaries are unordered collections of objects

  The difference between the two is that: among the elements of the dictionary is accessed by a key, rather than by shifting access

  Dictionaries with "{}" logo. A dictionary index (key) value and a value corresponding to its composition

  d = {'jove': 10086, 2.23: 'H-H'}

  print(d[2.23])

  print(d.keys())

  print(d.values())

  # H-H

  # dict_keys(['jove', 2.23])

  # dict_values([10086, 'H-H'])

  4. Conversion

  Sometimes the need for built-in data type conversion, simply as a function of the type of data you can name

  Several built-in functions can perform conversion between data types

  The function returns a new object that represents the converted value

  The integer x is converted to a

  class int (x, base = 10) # x: string or a number, base: hexadecimal, decimal default

  print(int(12.0))

  print (int ( "2e", 16)) # but if it is, then base-band parameter, x should be entered as a string

  # 12

  # 46

  The transition to a floating point x

  class float ([x]) # x: string or number

  print(float(-12))

  print(float("20.3"))

  # -12.0

  # 20.3

  Creating a complex

  class complex ([real [, imag]]) # real: int, float or string, imag: int, float

  print(complex(1, 2))

  print(complex("1+2j"))

  # (1 + 2j)

  # (1 + 2j)

  The object is converted to a string x

  class str (object = '') # object: Object

  str([10, 0, 86])

  # '[10, 0, 86]'

  The string object is converted to an expression x

  class repr (object) # object: Object

  repr (str ([10, 0, 86]))

  # "'[10, 0, 86]'"

  Python expression for calculating effective in the string, and returns an object

  class eval(expression[, globals[, locals]])

  # Expression: expression

  # Globals: variable scope, global namespace, if provided, it must be a dictionary object

  # Locals: variable scope, local namespace, if provided, it can be any mapping object

  x = 7

  print(eval('3 * x'))

  # 21 Wuxi flow of the hospital http://xmobile.wxbhnk120.com/

  Converting the sequence s is a tuple

  class tuple (seq) # seq: tuples to be converted to a sequence of

  print(tuple([1, 2, 3]))

  # (1, 2, 3)

  Converting the sequence s is a list of

  class list (tup) # tup: to convert to the list of tuples

  print(list((1, 2, 3)))

  # [1, 2, 3]

  Converted into a set of variable

  class set ([iterable]) # iterable: Object iterables

  print(set('google'))

  # { 'L', 'o', 'g', 'and'}

  Create a dictionary

  class dict (** kwarg) # ** kwargs: Keyword

  Container element: class dict (mapping, ** kwarg) # mapping

  class dict (iterable, ** kwarg) # iterable: iterables

  # Must be a sequence of (key, value) tuple

  print (dict (a = 'a', b = 'b', t = 't')) # incoming keyword

  print (dict (zip ([ 'one', 'two', 'three'], [1, 2, 3]))) # mapping function constructed embodiment Dictionary

  print (dict ([( 'one', 1), ( 'two', 2), ( 'three', 3)])) # iterables embodiment constructed Dictionary

  # {'a': 'a', 'b': 'b', 't': 't'}

  # {'one': 1, 'two': 2, 'three': 3}

  # {'one': 1, 'two': 2, 'three': 3}

  Is converted to a set of immutable

  class frozenset ([iterable]) # iterable: objects can be iterative, such as lists, dictionaries, and the like tuple

  print(frozenset('google'))

  # frozenset({'g', 'l', 'e', 'o'})

  If you do not provide any parameters, the default will generate an empty set

  Convert an integer to a character

  class chr(i) # i:数字

  print(chr(48), chr(0x31), chr(0o62))

  # 0 1 2

  A character into its integer value

  class ord (c) # c: Character

  print (words ( '6'), words ( 'A'), word ( 'a'))

  # 54 65 97

  Convert an integer to a hexadecimal string

  class hex (x) # x: integer

  print(hex(128))

  # 0x80

  Convert an integer to an octal string

  class oct (x) # x: integer

  print(oct(1024))

  # 0o2000

  Thank you!


Guess you like

Origin blog.51cto.com/14335413/2424547