Data types for Python

Source: Internet
Author: User

Basic data type:

>> Numbers and strings

List:

There is no concept of arrays in >>python, the closest concept to arrays is lists and Ganso, and lists are containers for storing a series of elements

>>> str = [' AB ', ' CD ', ' EF ', ' G ']>>> print (str[3]) g

>> Modify the contents of the list

>>> str = [' AB ', ' CD ', ' EF ', ' g ']>>> str[3] = ' gh ' >>> print (str) [' AB ', ' CD ', ' EF ', ' gh ']

Ganso:There is no concept of arrays in Python, the concept closest to arrays is the list and Ganso, Ganso is a container for storing a series of elements

>>> str = (' AB ', ' CD ', ' EF ', ' g ') >>> print (str[1]) CD

>> Read only cannot be modified

>>> str = (' AB ', ' CD ', ' EF ', ' g ') >>> ABC = str[2]>>> print (ABC) ef>>> str[0] = ' a ' Traceback (most recent):  File "<pyshell#13>", line 1, in <module>    str[0] = ' A ' TypeError: ' Tu Ple ' object does not support item assignment

Collection:

There are two main functions in the >>python collection:

>> Building Relationships

>>> a = set (' abcdef ') >>> B = Set (' abc ') >>> x = a&b #交集 >>> print (x) {' C ', ' A ', ' B '} >>> y = a|b #并集 >>> print (y) {' B ', ' e ', ' f ', ' C ', ' a ', ' d '}>>> z = A-a #差集 >>> print (z) {' F ', ' d ', ' e '}

>> Eliminate duplicate elements

>>> a = (' Abcdabcabc ') >>> new = Set (a) >>> print (new) {' C ', ' B ', ' A ', ' d '}

Dictionary:

Also known as an associative array, stored in the form of a key-value pair in {}

>>> dic = {' name ': ' Xiaoxiao ', ' age ': 1, ' City ': ' Beijing '}>>> print (DIC) {' Age ': 1, ' City ': ' Beijing ', ' Name ': ' Xiaoxiao '}>>> print (dic[' name ') Xiaoxiao

>> Adding a modified dictionary

>>> dic = {' name ': ' Xiaoxiao ', ' Age ':1}>>> print (DIC) {' Age ': 1, ' name ': ' Xiaoxiao '}>>> dic[' City "] = ' Beijing ' >>> print (DIC) {' Age ': 1, ' City ': ' Beijing ', ' name ': ' Xiaoxiao '}>>> dic[' age '] = 0> >> Print (DIC) {' age ': 0, ' city ': ' Beijing ', ' name ': ' Xiaoxiao '}

  

Data types for Python

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.