Python assigns values to multiple variables at the same time

Source: Internet
Author: User
Assigning multiple values at the same time

Here's a cool programming shortcut: In Python, you can use tuples to

Assigns multiple values.

>>> v = (' A ', 2, True)

>>> (x, y, z) = V①

>>> x

A

>>> y

2

>>> Z

True

1. V is a ternary tuple, and (x, Y, z) is a meta with three variables

Group. Assigning one of the values to another will assign each value in V in order

to each variable. This feature has many uses. Suppose you need to specify a name for a specific range of

Value. You can use the built-in range () function for multi-variable assignment to quickly

The row continuous variable assignment value.

>>> (MONDAY, Tuesday, Wednesday, Thursday, FRIDAY,

SATURDAY, SUNDAY) = range (7) ①

>>> MONDAY

Ii

0

>>> Tuesday

1

>>> SUNDAY

6

1. The built-in range () function constructs an integer sequence. (From Technology

says that the range () function returns neither a list nor a tuple, but an iterative

But you will learn the difference later. ) MONDAY, Tuesday,

Wednesday, Thursday, FRIDAY, SATURDAY and SUNDAY are what you decide

The variable that is justified. (This example comes from the Calendar module, which is a short and interesting module

Print a calendar, a bit like a UNIX program Cal. The Calendar module is the number of weeks

An integer constant is defined.

2. Now, each variable has its value: MONDAY is 0, Tuesday is 1,

So the analogy.

You can also use a multivariate assignment to create a function that returns multiple values by simply returning a package

A tuple with all values. The caller can treat the return value as a simple tuple, or

Assign it to a different variable.

  • 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.