Python learning 2-python simple data types

Source: Internet
Author: User

Python Simple data type

  1. List

    list creation, using []

    A_list = [' A ', ' B ', ' C ']

    Print A_list

    Print A_list[0] #a

    If you go to the last element of the list, in addition to calculating the index location, you can also use-1

    Print A_list[-1] #c

    And so on, you can get the bottom 2nd, the bottom 3rd one.

    Print A_list[-2] #b

    Print A_list[-3] #a

    The addition of the list element

    A_list.append (' d ') #增加到末尾

    A_list.insert (1, ' e ') #增加到指定位置

    A_list.extend ([' F ', ' g ', ' h ') #将两个个对象的元素加到该list的末尾

    A_list*3 #使用乘法扩展list

    Delete of list element

    Del A_list[1] #删除指定元素

    Del a_list #删除整个list

    A_list.pop () #删除最后一个元素. To delete the specified index position element, pop (i)

    A_list.remove (' a ') #删除首次出现的指定元素

    Substitution of list elements

    A_list[1] = ' Z ' #直接赋值给对应索引位置

    The data type of the list element can be different

    A_list = [' Apple ', ' 123 ', True] #True和False首字母要大写

    The list element can also be another list

    A_list = [' Apple ', ' orange ', [' Python ', 123], 321]

    Print Len (a_list) #4

    List element Access and Count

    accessing directly using indexed locations

    A_LIST[1]

    Gets the index position of the first occurrence of the specified element

    A_list.index (' a ')

    Gets the number of occurrences of the specified element in the list

    A_list.count (' a ')

    For Yuanju, string, and range

    Range (Ten). Count (3) #1

    (2,2,2,4). Count (2) #3

    (' A ', ' B ', ' C ', ' a '). Count (' a ') #2

    Determines whether the specified element is in the list

    ' A ' in a_list

    Slicing operations

    x = Range (10,100)

    x[::] #全取

    X[::-1] #倒着全取

    X[::2] #从头每两个取一个

    X[::-2] #倒着每两个取一个

    X[1::2] #从位置1每两个取一个

    X[3:6] #取指定范围

    X[:10:2] #前10个中每两个取一个

    List sort

    A_list = [1,2,3,4,5,6]

    Import Random

    Random.shuffle (a_list) #打乱顺序

    A_list.sort () #默认升序

    A_list.sort (reverse = True) #降序排序

    You can also use sorted () to sort, unlike the sort () directly for list modifications, sorted () returns a new list and does not manipulate the original list

    b = Sorted (a_list)

    Reverse the list element

    A_list.reverse ()

    You can also sort by reversed (), unlike reverse (), which changes directly to the list, reversed () returns a new list and does not manipulate the original list

    Newa = Reversed (a)

  1. tuple

    Tuple and list are very similar, but the tuple cannot be modified once initialized, he does not have append (), insert () method. The method to get the element is the same as list, but cannot be assigned to another element

    Creating a tuple ()

    a_tuple = (' A ', ' B ', ' C ')

    b_tuple = (' A ',)

    < Span style= "font-size:10pt" Use the tuple () function to convert other types of sequences to Ganso

    a = tuple (' ABCDEFG ')

    Alist = [1,2,3,4, ' a ']

    tuple (alist)

    &NBSP;

  2. Dict

    The dictionary is an unordered, variable sequence of key-value pairs, and each element in the dictionary contains two parts: Key and value, using curly braces {}.

    Creation of dictionaries

    D = {' Michael ': Up, ' Bob ': +, ' Tracy ': 85}

    Print d[' Michael '] #95

Determine if key exists

print ' Bob ' in D #True

print ' Bob ' in D #False

Read from Dict

d[' Michael '] #95

can also be obtained by means of a GET, if key does not exist, returns none or the value specified by itself

D.get (' Michael ') #95

D.get (' Mi ',-1) #-1

Dict items () can return the dictionary's "Key-value" list

For item in D.items ():

Print (item)

For key in D:

Print (key)

For key, value in D.items ():

Print (key, value)

Increase of dict elements

When the key is subscript, if the keys exist, then modify the value, if not present, add a heap of key-value

d[' Michael '] = #将Michael的值从95修改为96

d[' Jack '] = #增加一对key-value

Dict Delete

To delete a pair of key values, use POP (key)

D.pop (' Michael ')

  1. Set

    Each element of the set is unique, using curly braces {}

    Creation of Set

    A ={1,2}

    Print a #set ([+])

    Creating a set can also use the set () function to convert the list, Ganso to set, and if duplicate elements exist in the original data, only one

    A_set =set (Range (8,14))

    Print A_set

    Increase of Set

    A.add (3)

    Print a #set ([+])

    Operation of Set

    S1 = set ([+])

    S2 = set ([2,3,4])

    Print S1&s2 #交集

    Print S1|s2 #并集

Python learning 2-python simple data types

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.