Python Basic Operation II

Source: Internet
Author: User

Dictionary method:

DIC = {"    Beijing": {"        Changping District": ["Tian Tong Yuan", "Shahe", "Huilongguan"]    }}abc = {}

 

Fromkeys (Seq,value): Creates a new dictionary that is the key of the dictionary for the elements in the sequence SEQ, and value is the initial value corresponding to all the keys of the dictionary

Parameters:

SEQ: List of dictionary keys

Value: can omit parameter, default value per key, default is None

Example:

>>> Abc.fromkeys ([' xiaoming ', ' Xiaomhong '], ' Nicai ') {' Xiaomhong ': ' Nicai ', ' xiaoming ': ' Nicai '}

  

Get (Self,key,default): Returns the value of the specified key if the value does not return the default value in the dictionary

Parameters:

Key: Specify the key to get

Default: Defaults to the specified key, default to None

Example:

>>>dic.get (' Beijing ', ' Fafaefa ') {' Changping District ': [' Tian Tong Yuan ', ' shahe ', ' Huilongguan ']}>>> dic.get (' Beijing ', ' faef ') ' FAEF '

  

Items (): Returns the first-level key and value as a list

>>> Dic.items () dict_items ([' Beijing ', {' Changping District ': [' Tian Tong Yuan ', ' shahe ', ' Huilongguan ']})

  

VALUES (): Returns the value of the first layer as a list

>>> dic.values () dict_values ([{' Changping District ': [' Tian Tong Yuan ', ' shahe ', ' Huilongguan ']}])

  

Ii. Common operation of documents

Open (' name ', ' mode '): The commonly used parameter is name and mode, which returns a

Parameters:

Name: FileName Mode: file operation ' R '       open for reading (default) ' W '       open for writing, truncating the file first ' x '       create a NE W file and open it for writing ' a '       open for writing, appending to the end of the file if it exists ' B '       binary mode ' t '       text mode (default) ' + '       open a disk file for updating (reading and writing) ' U '       Universal newline mode (for BA Ckwards compatibility; Unneeded for          new code)

Read (n): reads how many characters, read all by default

Parameters:

N: Number of characters, reading n characters altogether

ReadLine (limit): reads the limit characters in a row, all by default. return string

Parameters:

Limit: number of characters read

ReadLines (): reads all the rows in the file, returns the list

Write (s): Write File method

Parameters:

S: A string or variable that needs to be written

Json.load (FP): reads the JSON string from the file, noting that all of the files are in JSON format

Common parameters:

FP: File descriptor

Usage:

Json_file = open (' Things.txt ', ' R ') print (Json.load (json_file)) or print (Json.load (' Things.txt ', ' r '))

 

Json.dump (OBJ,FP): Writes a string to a file in JSON format

Parameters:

OBJ: Data to be written, strings, variables, dictionaries, lists, etc.

Usage:

Json.dump (' Faefaf ', open (' Abc.txt ', ' W '))

  

Basic usage of the function:

1. Define a function:

def function name ():

function body

Example:

Def Print_hello ():    print (' Hello ')

2. Call a function:

Function name ()

3. function parameter list:

A) def Print_hello (b), A and B are variables within the function, called formal parameters here, and virtual print (A, b  ) is called when called: Print_hello (' xiaoming ', ' Xiaohong ')  #这里的 ' xiaoming ', ' Xiaohong ' is called the argument, instead of a A, a, a, a, B is equivalent to two variables, a= ' xiaoming ', b= ' Xiaohong '

  

b) def Print_hello (*args) #多个参数组合成一个元组传入函数  print (args) Print_hello (' xiaoming ', ' Xiaohong ') (' xiaoming ', ' Xiaohong ') ‘)

  

c) def Print_hello (**kwargs) #将参数组成字典传入函数内  print (Kwargs) Print_hello (a= ' xiaoming ', b= ' Xiaohong ') {' B ': ' Xiaohong ' , ' a ': ' Xiaoming '}

  

  

Python Basic Operation 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.