Python (3)

Source: Internet
Author: User
Tags shallow copy variable scope

---restore content starts---

(1): meta-group

A Python tuple is similar to a list, except that the elements of a tuple cannot be modified.

Tuples use parentheses (), and the list uses []. Tuple name Tup

Note : When you include only one element in a tuple, you need to add a comma after the element

element values in tuples are not allowed to be deleted and changed, but you can use Del to delete tuples

Built-in functions for tuples:

Len () length

Max () max value

Min () Min value

Tuple () Convert list type to Narimoto group type

(2): dictionary , dictionary name Dict

A dictionary is another mutable container model and can store any type of object.

Each key value of the dictionary (Key=>value) is split with a colon (:), each pair is separated by a comma (,), and the entire dictionary is included in curly braces {}

Key is unique, but value can be arbitrarily

Built-in functions for dictionaries

Len (): Calculates the number of fields, that is, the total number of keys

STR (): Output dictionary, expressed as a printable string

Type (): Returns the type of the input variable and returns the dictionary type if the variable is a dictionary.

Dictionary built-in methods:

Serial Number Functions and descriptions
1 Radiansdict.clear ()
Delete all elements in a dictionary
2 Radiansdict.copy ()
Returns a shallow copy of a dictionary
3 Radiansdict.fromkeys ()
Create a new dictionary with the keys to the dictionary in sequence seq, Val is the initial value corresponding to all keys in the dictionary
4 Radiansdict.get (Key, Default=none)
Returns the value of the specified key if the value does not return the default value in the dictionary
5 Key in Dict
Returns False if the key returns true in the dictionary Dict
6 Radiansdict.items ()
Returns an array of traversed (key, value) tuples as a list
7 Radiansdict.keys ()
Returns a dictionary of all keys in a list
8 Radiansdict.setdefault (Key, Default=none)
Similar to get (), but if the key does not exist in the dictionary, the key is added and the value is set to default
9 Radiansdict.update (DICT2)
Update the key/value pairs of the dictionary dict2 to the Dict
10 Radiansdict.values ()
Returns all values in the dictionary as a list
11 Pop (Key[,default])
Deletes the value of the dictionary given key key, and returns the value to be deleted. The key value must be given. Otherwise, the default value is returned.
12 Popitem ()
Randomly returns and deletes a pair of keys and values in the dictionary (typically delete the end pair).

(3) function :

Functions are well-organized, reusable, code snippets for single, or associated functionality

Define a function

The function code block begins with a def keyword followed by the function identifier name and parentheses ().

Any incoming parameters and arguments must be placed in the middle of the parentheses, and the parentheses can be used to define the parameters.

The first line of the function statement can optionally use the document string-for storing the function description.

The function contents begin with a colon and are indented.

return [expression] ends the function, optionally returning a value to the caller. Return without an expression is equivalent to returning Non

Syntax: def function name (argument list): function body

Objects that can be modified and objects that cannot be changed

In Python, strings, tuples, and numbers are objects that cannot be changed, and List,dict are objects that can be modified.

Parameters:

Altogether four parameter types

The required parameters of the common parameter type must pass in the function in the correct order. The number of calls must be the same as when declared.

The default parameter type parameter can be passed or not, will use the default value, passed the value passed after the argument

Variable length parameter type, which encapsulates the Narimoto group () Tup*var_args_tup

The keyword parameter, which is encapsulated into the dictionary type {}dict, passes the value Key=value **var_args_dict

Anonymous functions: Python uses lambda to create anonymous functions.

Lambda Syntax:

Lambda [arg1 [,arg2,..... argn]]:expression

Instance sum = Lambda arg1, arg2: arg1 + arg2

(4) Variable scope:
Global variables are scoped differently from local variables
Different life cycle
Local variables take precedence when global variables and local variables have the same name (priority principle of the program)

(5) Define functions in Python, which can be used in combination with required parameters, default parameters, variable parameters, keyword parameters, and named keyword parameters, except that mutable parameters cannot be mixed with named keyword parameters. Note, however, that the order of the parameter definitions must be: required, default, variable/named keyword, and keyword parameters.

---restore content ends---

Python (3)

Related Article

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.