Python data type (ii)

Source: Internet
Author: User

    • List of Python data types

      A list is a collection of data defended in square brackets "[]", with different members separated by ",". The list can contain any data type, or it can contain other lists, which can be accessed by the list by ordinal. You can sort, add, delete, and so on in a script, and you can change the value of any member in the list.

      • Common list Operations


List operations Describe
List.append Append member
List.count (x) Count the number of occurrences of the parameter X in the list
List.extend (L) Append another list to the list L
List.index (x) Get the position of the parameter X in the list
List.insert () Inserting data into the list
List.pop ()
Deletes the last value in the list and returns the deleted value
List.remove ()
Remove a member from a list
List.reverse ()
Reverses the order of the members in the list
List.sort ()

Sort members in a list

    • Python Data type: Dictionary

A dictionary is a special kind of data type in Python and is a collection of data that encloses {} with braces. The biggest difference between a dictionary and a list is that the dictionary is unordered, and the members are accessed through keys in the dictionary.


Usually the operation of a dictionary

Dictionary operations Describe
Dic.clear () Empty dictionary
Dic.copy Copy Dictionary
Dic.get (k) Get the value of the key K
Dic.has_key (k)
Whether to include the key K
Dic.items () Get a list of keys and values
Dic.keys () Get a list of keys
Dic.pop (k) Delete key k
Dic.update () With new members
Dic.values () Get a list of worthwhile
Python data type: A file file can also be considered a data type in Python, and a file object is returned when a file is opened with Python's built-in function open
Open (filename,mode,bufsize) fiename the file to be opened mode optional, file Open, R, read-only w overwrite write a append bufsize optional parameter, buffer size E G:user_file=open ("User.log", "R") print (user_file) E:\Python34\python.exe e:/one/day1/day1homework1.py<_io. Textiowrapper name= ' user.log ' mode= ' r ' encoding= ' cp936 ' >process finished with exit code 0
  • Basic operation of the file


    File operations Describe
    File.read () Reads the entire file into a string
    File.readline () Read a line of a file into a string
    File.readlines () Read the entire file into the list by line
    File.write () Writing a string to a file
    File.writelines () Write a list to a file
    File.close () Close an open file


      • File.read

User_file=open ("User.log", "R") User_line=user_file.read () print (user_line) E:\Python34\python.exe e:/one/day1/ Day1homework1.pyduqiu,123wangming,123
    • File.readline

User_file=open ("User.log", "R") User_line=user_file.readline () print (user_line) E:\Python34\python.exe e:/one/day1/ Day1homework1.pyduqiu,123process finished with exit code 0
    • File.readlines

User_file=open ("User.log", "R") User_line=user_file.readlines () print (user_line) E:\Python34\python.exe e:/one/day1 /day1homework1.py[' duqiu,123\n ', ' wangming,123 ']process finished with exit code 0


This article is from the "Ancient Capital Road" blog, please be sure to keep this source http://7157581.blog.51cto.com/7147581/1790683

Python data type (ii)

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.