Python variables and data types

Source: Internet
Author: User

Almost all programming languages have ' variables ', such as a = 2, which refers to the number 2 with a variable, and in Python everything is object, so when the variable is assigned it is actually a memory space object that stores the contents of the variable in memory.

An object can specify a different data type, and the variable name points to the memory space where the variable's contents are, so you can actually duplicate the same variable name, which in the Python kernel points back to the newly opened memory space, the Python interpreter has a garbage collection mechanism, Every once in a while to check if there is a zone in the memory area that is not referenced, and when the reference is 0, the interpreter clears it as garbage.

There are five standard data types in Python:

  • Numbers (numeric type)
    • int (signed integral type)
    • Long (integer)
    • Float (float type)
    • Complex (plural)
  • String (String)
    • Python internally actually treats the string as a list object, such as a = ' Peter ', you can use A[2:4] to remove the ete character, you can use the ' + ' sign to stitch the string
  • List (lists) uses [] to represent
    • The list list is the most frequently used data type in Python, and lists can accomplish the data structure implementation of most collection classes. Lists support characters, numbers, and even nesting
      • #!/usr/bin/python #-*-Coding:utf-8-*- list = [ "runoob 7862.23john70.2 ] tinylist = [123 ' john "]
      • < Span class= "Hl-code" > < Span class= "hl-brackets" >< Span class= "hl-quotes" > print list # output full list
      • < Span class= "hl-brackets" >< Span class= "hl-quotes" > Span class= "hl-reserved" >print list[ 0] # the first element of the output list
      • < Span class= "Hl-code" > < Span class= "Hl-identifier" >print list[1< Span class= "Hl-code" >:3] # outputs the second to third element
      • < Span class= "Hl-code" >< Span class= "Hl-code" > print list[2 :] # output all elements from the third start to the end of the list
      • < Span class= "hl-reserved" >< Span class= "Hl-code" >print tinylist< Span class= "Hl-code" > * 2 # output list two times print list tinylist # List of print combinations
  • Tuple (tuple): use () to indicate:
      • Tuple-like lists, the main difference is that the list can be modified and tuples cannot, and the use of tuples is often due to security considerations because of its immutable nature.
    Dictionary (dictionary): use {} to indicate
      • The dictionary is the most flexible built-in data structure type in Python outside the list, and the list is an ordered combination of objects, and the dictionary is an unordered combination of objects, the difference is mainly in the dictionary with the key to take the value, and the list by the subscript offset value.

Python variables and data types

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.