Day 3 python learning Notes

Simple to use 1 pycharm of

  1.1 Open pycharm program

  Select create a new project 1.2

  1.3 Select pure python items named in the location in

  1.4 python found in the pre-compiled to run in files, you can create a new project

  1.5 Right-click the project name, you can create python file

Table 2 Operation dictionary

  2.1 Establish dictionary table has two ways, one with braces {}, separated by commas key pair, there is a constructor argument format: dict (name = 'TOM', age = 32)

  2.2 Access dictionary table element has two ways, one format: name dictionary table [index], there is a format: name dictionary table .get (index), except that when the index is not present, a second embodiment not being given, a further second way is to give the default value when the index does not exist in the format: name dictionary table .get (index, default value)

3 sequence management table of elements in the dictionary

  3.1 The dictionary table is converted into a list of keys, and then apply .sort ranking function list (), according to certain sequential access key

  3.2 use global function sorted ()

Statement 4-tuple

  4.1 Use parentheses, separated by commas between elements

  4.2 Statement multiple elements of a tuple without brackets can also be inter-element separated by commas

  4.3 tuple define a single element, with or without braces, are required in the element after a comma

Simple to use 5-tuple named

  5.1 The first step, import library, from collections import namedtuple

  5.2 format defined: Employee = namedtuple ( 'Employee', [ 'name', 'age', 'salary']), the inside of the key list Note that quoted

          jerry = Employee ( 'jerry', age = 20, salary = 9000), Note that only the first key with the one quoted

  5.3 Access named value tuples:. Employee key

Create a file in 6 python, reading and writing

  6.1 files are created: file = open ( 'filename', mode)

    mode 类型: r, w, a, b, +

  6.2 to write the contents of the file:

    file.write (content), writing a single value, by way of numerical format, such as the value written to x, y, z to file f: f.write ( '{}, {}, {}' .format (x, y, z)); writing list, need to be converted to a string, such as writing to the listing file f l: f.write (str (l))

  6.3 Reading the contents of the file:

    file.read (), read the entire contents

    file.readline (), read the contents of a row

    file.readlines (), read the contents of the list into a row

  Read and write data according to the format 6.4

    At this point you need to use the pickle module.

      If we want to dictionary { 'name': 'liming', 'age': 20} is written into the text

      import pickle

      d1={'name':'liming','age':20}

      f1 = open ( 'data.pkl', 'wb')

      pickle.dump(d1,f1)

      If we need to read the dictionary table, we can deal with this:

      f2=open('data.pkl','rb')

      d2=pickle.load(f2)

Guess you like

Origin www.cnblogs.com/zhuome/p/11297276.html