Python variable type

Source: Internet
Author: User

Python Variable Type

The value that the variable is stored in memory. This means that there is a space in memory when creating variables.

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

Therefore, variables can specify different data types, which can store integers, decimals, or characters.


  • Python variable Assignment

    Variables in Python do not need to be declared, and the assignment of variables is the process of declaring and defining variables.

    Each variable is created in memory and includes information about the identity, name, and data of the variable.

    Each variable must be assigned before it is used, and the variable will not be created until the variable is assigned.

    The equals sign (=) is used to assign a value to a variable.


  • Data type

python 5 data types


      • Numbers (digital)--->int

      • String (string)--->str

      • List (listing)--->list

      • Dictionary (dictionary)-->dict

      • Tuple (tuple)--->tuple

    • string

A string or series (string) is a string of characters consisting of numbers, letters, and underscores.

It is the data type that represents the text in the programming language.

The Python string list has 2 order of values:

    • Left-to-right index starts at default 0, with a maximum range of 1 less string lengths

    • Right-to-left index starts with default-1, the maximum range is the beginning of the string

  • List

    1. Slicing

    The list is the most frequently used data type in Python.

    A list can accomplish the data structure implementation of most collection classes. It supports characters, numbers, and even strings that can contain lists (so-called nesting).

    The list is identified by []. Is the most common composite data type of Python.

The list slice is left open and closed (subscript starting from 0)

>>> list = [' ABCD ', 786, 2.23, ' John ', 70.2]>>> list[0:2] #取前两个数据 [' ABCD ', 786]>>> lis T[:2] #默认从0开始 [' ABCD ', 786]>>> List[::2] #后面的2是代表, every other element, take one [' ABCD ', 2.23, 70.2]>>> list[4 :] #取最后一个, cannot write -1[70.2]

2. Increase

>>> name.append ("E") #添加到最后 >>> name[' A ', ' C ', ' B ', ' d ', ' E ']>>> name.insert (1, "F") #添 Add to specified position >>> name[' a ', ' F ', ' C ', ' B ', ' d ', ' e ']

3. Delete

>>> del name[2] #删除列表位置 >>> name[' A ', ' F ', ' B ', ' d ', ' e '] >>> name.remove ("E") #删除列表内容 >>> name[' A ', ' F ', ' B ', ' d ']>>> name.pop () #删除最后一个 ' d ' >>> name[' a ', ' F ', ' B ']

4. Change

>>> name[' A ', ' F ', ' B ']>>> name[2] = "E" >>> name[' a ', ' f ', ' e ']

5. Append

>>> name[' A ', ' f ', ' E ']>>> name.append ("D") >>> name[' A ', ' f ', ' e ', ' d ']

6. Expansion

>>> name[' A ', ' f ', ' e ', ' d ']>>> num[1, 2, 3]>>> name.extend (num) >>> name[' A ', ' f ', ' E ', ' d ', 1, 2, 3]>>> num[1, 2, 3]

7. Get subscript

[' A ', ' f ', ' e ', ' d ', 1, 2, 3]>>> name.index (1) 4
    • dictionary

      Dictionary (dictionary) is the most flexible built-in data structure type in Python except for lists. A list is an ordered combination of objects, and a dictionary is a collection of unordered objects.

Features of the dictionary:

Dict is disordered.

Key must be unique and so is inherently heavy

>>> dict = {' dept ': ' Sales ', ' Code ': 6734, ' name ': ' John '}>>> for I in Dict: ... print (i,dict[i]) ... c Ode 6734dept Salesname john>>> for i,v in Dict.items (): #先把dict转成list, data in the big time mo ... print (i,v) ... code 6734dept s Alesname John

This article is from the "Patrick" blog, so be sure to keep this source http://patrick0715.blog.51cto.com/3681259/1834468

Python variable type

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.