Python learns the sequence of data types, and python Data Types

Source: Internet
Author: User

Python learns the sequence of data types, and python Data Types

I. Sequence (this article uses python3.5)
######################################## ####################
# List and tuples are all Sequences
# Features:
#1. You can obtain specific elements through indexes.
#2. You can obtain a small sequence through the slice operation.
# Basic operations
Str = "abc ";
Str1 = "efg ";
Print (len (str); # obtain the sequence length
Print (str + str1); # concatenate Sequences
Print (str * 5); # repeat the sequence for five times abcabcabcabcabc
Print ('A' in str); # determine whether an element exists in the sequence
Print (max (str); # obtain the largest element in the sequence
Print (min (str); # obtain the smallest element in the sequence
# The cmp (x, y) function is used to compare two objects. If x <y returns-1, if x = y, 0 is returned. If x> y, 1 is returned. This function is canceled in python3.
Ii. String Sequence
# String is a sequence type data each character has its own bucket string cannot be changed str4 = "abcdefgh"; print (str4 [0]); # a obtain print (str4 [1] + str4 [2]) through the index; # bcstr5 = str4 []; # print (str5) is not included after the first entry ); # bcdeprint (str4 [: 4]); # abcd intercepts 0-4 strings (excluding [4]) print (str4 [4:]); # efg intercepts all characters from subscript 4 to the End of print (str4 [: 1]); # each step is set to abcdefuplint (str4 [: 2]); # aceg # negative subscript print (str4 [-1]); # hprint (str4 [-4:-1]); # efg does not include str4 [-1]

3. tuple)

Feature: the Python tuples are similar to the list. The difference is that the elements of the tuples cannot be modified, and the tuples use parentheses.

######################################## ############################## ################ Features of tuples: the value of the tuples cannot be changed. The tuples have all the operations in sequence # the declaration of the tuples arr = (); # The empty tuples arr1 = (2 ,); # single element in the tuples # operation arr2 = name, age, gender = ("Zhang San", 20, "male"); print (name); # Zhang San

Iv. List)

A list is a data structure that processes a group of Ordered items. A list is a variable type of data that has all the operations of a sequence.
 
# The list is the data structure that processes a group of Ordered items, and the list is a variable type of data with all the operations of the sequence # list declaration list = []; # Empty list list1 = [1, 2, 3]; list2 = ["", 10]; # assign a value to the list element (modify the value) print (list2 [3]); list2 [3] = 50; print (list2 [3]); # add data list2.append ("zhangsi") to the list "); # Add print (list2) after list2; # ['zhangzhang ', 'zhang2', 'zhangsan', 50, 20, 10, 'zhang si'] list2.remove (list2 [6]); # The print (list2) elements of the list are deleted; # ['zhang zhang', 'zhang 2', 'zhang san ', 50, 20, 10] del (list2 [5]); # Delete the specified Element print (list2) in the list; # ['zhang zhang', 'zhang 2', 'zhang san ', 50, 20]
 

V. Dictionary (dict)

# Dictionary ing type hash table dictionary objects can be changed. Key values must use immutable objects. Key value types can be different.
 
# Dictionary ing type hash table dictionary object is variable, key value must use an immutable object, key value type can be different # method: # keys () Return key class table values () return Value List # items () returns the tuples containing key-value pairs # create map of the dictionary ={}; # empty dictionary map1 = {'name': "Lee ", 'age': "12"}; map2 = {'name': "Zhang San", 'age': "23", 'tel ': 12345678901 }; # obtain the corresponding valueprint (map2 ['name']) based on the key; # Zhang San print ("get =" + map2.get ("name ")); print ("getNull =" + map2.get ("name_1", 'null'); # return null if the Name does not exist # traverse the self-dictionary for k in map2: print (k ); # key value print (map2 [k]); # values value # Add Value map2 ["addrss"] = "Beijing"; print (map2 ["addrss"]); # Beijing # modify the value map2 ["tel"] = 13456727890123; print (map2 ["tel"]); # Delete del (map2 ["tel"]); # Delete the specified Element map2.pop ("name"); # Remove the specified Element and return the value of this element map2.clear (); # Clear the dictionary # destroy the entire dictionary del (map2 ); # Use the factory production dictionary dict ()
 

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.