Python dictionary and Function parameters __ function

Source: Internet
Author: User

1. Dictionary

1.1. Definition of Dictionary:

In tuples and lists, the element is accessed by number, but sometimes we use the name for the data and even the structure, so we introduce the dictionary

A dictionary is a data structure that is referenced by a name or keyword, and its key can be a number, a string, a tuple, and this type of structure is also called a mapping. Mapping is actually a set of key and value and the mapping function between, which is characterized by: key uniqueness, key and value of a one-to-many mapping.

1.2. The method of the dictionary

Dict.clear () Delete all elements in a dictionary
Dict.copy () Returns a copy of a dictionary (shallow copy)
DICT.FROMKEYSC (Seq,val=none) Creates and returns a new dictionary, which is the key of the dictionary in Seq, and Val does the initial value for all keys in the dictionary (none is default if this value is not provided)
Dict.get (Key,default=none) Returns the value of the key in the dictionary dict, and returns the value of default if this key is not present in the dictionary (note that the default value for parameter default is None)
Dict.has_key (Key) Returns true if the key is present in the dictionary, or false. After the Python2.2 version is introduced in and not, this method is almost obsolete, but still provides a working interface.
Dict.items () Returns a list containing the dictionary (key, value) to tuple
Dict.keys () Returns a list containing the keys in the dictionary
Dict.values () Returns a list containing all the values in the dictionary
Dict.iter () Methods Iteritems (), Iterkeys (), itervalues () are the same as their corresponding non iterative methods, but they return an iteration rather than a list.
Dict.pop (key[, default]) Similar to the method get (), if the key key in the dictionary exists, deletes and returns Dict[key], and throws a Keyerror exception if the key does not exist and the value of default is not given.
Dict.setdefault (Key,default=none) Similar to the method set (), if the key is not present in the dictionary, it is assigned a value by Dict[key]=default.
Dict.setdefault (Key,default=none) Similar to the method set (), if the key is not present in the dictionary, it is assigned a value by Dict[key]=default.

Practice:

Shopping Cart Program requirements:
   1. After starting the program, let the user enter the salary, and then print the list of goods;
   2. Allow users to purchase goods according to the commodity number
   3. After the user selects the goods, the test balance is sufficient, enough direct deduction, not enough on the reminder;
   4. Free to quit Print purchased goods and balances when exiting

Create a user Login system: Save the user's username and password
when creating a user, to determine whether the user exists, if there is an error log in the
system, to ensure that the user name exists in the system, password errors have three opportunities, more than three opportunities will be
the error When landing to determine the time difference between the landing and the last landing, if time<4h, show you have  


2. Function:

2.1. function declaration The function in Python is declared in the following form:
def function name ([parameter 1, Parameter 2, Parameter 3 ...]):
EXECUTE statement

2.2 Keyword return and pass

Once the function has encountered return during execution, the function completes and returns the result.
The return value none is returned when the function is not encountered during execution.
If you want to define an empty function that does nothing, you can use the PASS statement as a placeholder for the code to run first.

2.3. Parameters of function

Order of parameter definitions: Required parameters > Default parameters > Variable parameters > Keyword parameters

parameter checking of functions
function returns multiple values, essentially returning a tuple tuple, which can omit parentheses when returned.
When a function call receives a return value, it assigns a value to the variable by position.

Default parameters, when the required parameters exist in the function, be sure to put the required parameters in front of the default parameters, set the default parameters, the big changes in the parameters of the front, change the small parameters can be set to the default parameters. The default parameters are generally immutable data types

Variable parameters

When a function is defined, the formal parameter can be *args, so that the function can receive multiple parameters and the data type received is a tuple;
If you want to pass a list or tuple into a function, you can pass the arguments in the function by *li or *t.

*args, variable parameters, args received is tuple

Exercise: Returns the first character of a variable argument


Keyword parameters

**kwargs, keyword parameters, Kwargs receive a dictionary




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.