Python section III

Source: Internet
Author: User
Tags variable scope

This time we learned to include tuples, dictionaries, and collections.

  1. Tuple (tuple): Enclose with parentheses

    If there is only one element, add a comma behind it --tup=

    Tuples can be added

    (1,2,3)+(4,5,6)--(1,2,3,4,5,6)

    * indicates a few copies

    Convert tuples:tuple1=tuple (list)

    t= (' A ', ' B ', [' A ', ' B '])

    t[2][0]= ' X '

    t[2][1]= ' Y '

    t= (' A ', ' B ', [' X ', ' Y '])

    Tuples cannot be changed or deleted, but lists in tuples can be

  2. Dictionary: Enclosed in curly braces

    Key-value pairs:

    D={key1:value1,key2:value2}

    Increase:

    By deleting:

    Change:

    Check:

    Keys (): list of all keys

    VALUES (): list of all values

    Items (): key values are

  3. Set collection

    . Union for the set (123456)  set1| Set2

    . Intersection set1&set2

    . Difference

    . Symmetric_difference set1^set2

    Set1 (1,2,3,4)

    Set2 (3,4,5,6)

  4. functions

    Strings,tuples,numbers immutable ( immutable )

    List, Set variable ( mutable "

    keyword parameter ( **xx

    default parameter: Default parameter must be placed on the last side

    indeterminate length parameter ( *xx tuple

  5. Variable scope

    Global variables are scoped differently from local variables

    Different life cycle

    Global Variables declared with globals

    6. anonymous function:lambda[arg1[,arg2,..... argn]]:expression

    Sum=lambda a,b:a+b

Python section III

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.