Basic Data Types for getting started with python

Source: Internet
Author: User

Basic Data Type:

1. Value: mainly includes int, float, bool, and other types.

2, string

Taking str as one type is because str has some container properties, it contains characters (although python does not have the character type) and can randomly access some of the elements, run dir (string) in the script parser to view all the methods and attributes of the string. Several common methods are introduced.

Python code

 
 
  1. >>> string='string,String,STRING' 
  2. >>> string[3:9]  
  3. 'ing,St' 
  4. >>> string.startswith('str')  
  5. True 
  6. >>> string.split(',')  
  7. ['string', 'String', 'STRING']  
  8. >>> string  
  9. 'string,String,STRING' 
  10. >>> string.islower()  
  11. False 
  12. >>> string.lower()  
  13. 'string,string,string' 
  14. >>> string.upper()  
  15. 'STRING,STRING,STRING' 

Container data structure: 

1, list:

A list is a data structure that processes a group of ordered projects. You can store a series project in a list. Once you create a list, you can add, delete, or search for projects in the list. Since you can add or delete a project, we say that the list is a variable data type, that is, this type can be changed.

Several list methods are introduced.

Insert (I, x) ---- insert an item at a specified position. The first independent variable is inserted before the element and represents it by subscript, for example, list. insert (0, x) before the list, list. insert (len (list), x) is equivalent to list. append (x ).

Append (x) ---- equivalent to list. insert (len (a), x)

Index (x) ---- search for the value x in the list and return the subscript of the first element with the value x. An error occurred while not found.

Remove (x) ---- Delete the first element with the value of x from the list. An error occurred when the element cannot be found.

Sort () ---- sort list elements in the original position. Note that this method changes the list instead of returning the sorted list.

Reverse () ---- sorts list elements in reverse order. Change the list.

Count (x) ---- returns the number of times x appears in the list.

Python code

 
 
  1. >>> list = ['a','c','g','e','t','b','f','d','g']  
  2. >>> list.insert(3,'d')  
  3. >>> list  
  4. ['a', 'c', 'g', 'd', 'e', 't', 'b', 'f', 'd', 'g']  
  5. >>> list.append('c')  
  6. >>> list  
  7. ['a', 'c', 'g', 'd', 'e', 't', 'b', 'f', 'd', 'g', 'c']  
  8. >>> list.index('c')  
  9. >>> list.remove('c')  
  10. >>> list.index('c')  
  11. >>> list.count('d')  
  12. >>> list.sort()  
  13. >>> list  
  14. ['a', 'b', 'c', 'd', 'd', 'e', 'f', 'g', 'g', 't']  
  15. >>> new_list = ['a','c','g','d','b']  
  16. >>> new_list.reverse()  
  17. >>> new_list  
  18. ['b', 'd', 'g', 'c', 'a'] 

You can use help (list) to obtain all the methods of list objects.

2. tuples:

The tuples are similar to the list, except that the tuples and strings are immutable, that is, you cannot modify the tuples. Tuples are usually used to securely use a group of values for statements or user-defined functions. That is, the values of the used tuples do not change.

Example: Python code

 
 
  1. >>> zoo = ('wolf', 'elephant', 'penguin')  
  2. >>> print len(zoo)  
  3. >>> new_zoo = ('monkey', 'dolphin', zoo)  
  4. >>> print len(new_zoo)  
  5. >>> print new_zoo  
  6. ('monkey', 'dolphin', ('wolf', 'elephant', 'penguin'))  
  7. >>> print new_zoo[2]  
  8. ('wolf', 'elephant', 'penguin')  
  9. >>> print new_zoo[2][1]  
  10. elephant 

The differences between the list and the group can be seen from the code.

3. dictionary:

A dictionary is a collection of unordered "key values: values". The key values in the same dictionary are different from each other. The main operation of the dictionary is to save a value for a key value, and find the corresponding value after the key value is given. You can also use del to delete a key-value pair. If you save a value with an existing defined key value, the original vegetation is forgotten. An error occurred when searching with a nonexistent key value.

Unlike sequences, a dictionary is not used to index numeric subscripts within a range. Instead, it is used to index key values. The key value can be of any immutable type. String and value can always be used as key values. If the tuples only contain strings, values, or tuples, The tuples can also be used as key values, because the tuples cannot be changed. The list cannot be used as a key value, because the list can be changed locally using its append () method.

The keys () method of the dictionary object returns a list of all key values in the dictionary. The order is random. When sorting is required, you only need to use the sort () method for the returned key value list. To check whether a key value is in the dictionary, use the has_key () method of the dictionary.

Dictionary example:

Python code

 
 
  1. >>> employee_id = {'Sammy':1, 'david':2, 'shirley':3,'jack':4,'guido':5}   
  2. >>> employee_id['shirley']   
  3. 3   
  4. >>> employee_id.has_key('fly')   
  5. False   
  6. >>> employee_id.keys()   
  7. ['shirley', 'Sammy', 'guido', 'jack', 'david']   
  8. >>> employee_id   
  9. {'shirley': 3, 'Sammy': 1, 'guido': 5, 'jack': 4, 'david': 2}   
  10. >>> del employee_id['david']   
  11. >>> employee_id   
  12. {'shirley': 3, 'Sammy': 1, 'guido': 5, 'jack': 4}   
  13. >>> for name, id in employee_id.items():   
  14. .... print 'employee %s id is %d' % (name, id)   
  15. ....   
  16. employee shirley id is 3   
  17. employee Sammy id is 1   
  18. employee guido id is 5   
  19. employee jack id is 4   
  20. >>> if 'shirley' in employee_id or employee_id.has_key('shirley'):   
  21. .... print employee_id.get('shirley')   
  22. ....   
  23. 3  

Read this article to help you.

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.