python--(variable)

Source: Internet
Author: User

变量:

Python 是动态类型语言, 也就是说不需要预先声明变量的类型。变量是对象的引用,变量只是将指针指向了对象所在的内存地址。变量的类型和值在赋值那一刻被初始化。变量起名:    1.显式-->通俗易懂    2.nums_of_alex_gf = 19     3.NumsOfAlexGf = 20  驼峰写法    4.中横线不能作为变量的命名字符    5.数字不能作为开头,但可以在中间或者结尾    6.特殊字符不能作为变量名的组成部分    7.不能使用空格    8.关键字不能声明为变量

assigning values to variables

Variables in Python do not need to be declared, and the assignment of variables is the process of declaring and defining variables.

Each variable is created in memory and includes information about the identity, name, and data of the variable.

Each variable must be assigned before it is used, and the variable will not be created until the variable is assigned.

The equals sign (=) is used to assign a value to a variable.

The left side of the equals sign (=) operator is a variable name, and the right side of the equals sign (=) operator is the value stored in the variable.

assigning values to multiple variables

Python allows you to assign values to multiple variables at the same time. For example:

A = b = c = 1

The above example creates an integer object with a value of 1 and three variables allocated to the same memory space.

You can also specify multiple variables for multiple objects. For example:

A, B, C = 1, 2, "John"

For the above example, two integer objects 1 and 2 are assigned to variables A and B, and the string object "John" is assigned to variable C.

Problem:

Now there is a tuple of n elements or a sequence, how to extract the value inside it and assign the value at the same time
Give n variables?

Any sequence (or an iterative object) can be extracted and assigned to multiple objects by a simple assignment statement.
Variable. The only prerequisite is that the number of variables must be the same as the number of elements in the sequence.

>>> p = (4, 5)>>> x, y =P>>>x4>>>y5>>>>>> data = ['ACME', 50, 91.1, (2012, 12, 21) ]>>> name, shares, price, date =Data>>>name'ACME'>>>Date (2012, 12, 21)>>> name, shares, Price, (year, Mon, day) =Data>>>name'ACME'>>> Year2012>>>Mon12>>> Day21>>>
View Code

This decompression assignment can be used on any iterator object, not just a list or tuple.
Includes strings, file objects, iterators, and generators.

Sometimes, you might just want to unzip a part and discard the other values. For this scenario, Python does not mention
for special syntax. But you can use any variable name to take a place, and then lose those variables.

>>> data = [' ACME ', 50, 91.1, (2012, 12, 21)]
>>> _, shares, price, _ = Data
>>> shares
50
>>> Price
91.1

Problem:

If the number of elements of an iterative object exceeds the number of variables, a valueerror is thrown. So
How can I extract n elements out of this iterative object?

The asterisk expression in Python can be used to solve this problem.

>>> record = (' Dave ', ' [email protected] ', ' 773-555-1212 ', ' 847-555-1212 ')
>>> name, email, *phone_numbers = record
>>> Name
' Dave '
>>> Email
' [Email protected] '
>>> phone_numbers
[' 773-555-1212 ', ' 847-555-1212 ']

An asterisk expression can also be used at the beginning of the list. For example, you have a company that sells data for the first 8 months
column, but you want to see the comparison between the last one months of data and the average of the previous 7 months. You can do this:
*trailing_qtrs, current_qtr = Sales_record
Trailing_avg = SUM (trailing_qtrs)/len (trailing_qtrs)
Return Avg_comparison (Trailing_avg, Current_qtr)
The following results are performed in the Python interpreter:
>>> *trailing, current = [10, 8, 7, 1, 9, 5, 10, 3]
>>> trailing
[10, 8, 7, 1, 9, 5, 10]
>>> Current
3

It is important to note that an asterisk expression is useful when iterating over a sequence of elements that are variable-length tuples. Like what
The following is a list of tuples with labels:
Records = [
(' foo ', 1, 2),
(' Bar ', ' hello '),
(' foo ', 3, 4),
]
def do_foo (x, y):
Print (' foo ', X, y)
def Do_bar (s):
Print (' bar ', s)
For tags, *args in records:
if tag = = ' Foo ':
Do_foo (*args)
elif tag = = ' Bar ':
Do_bar (*args)

Foo 1 2
Bar Hello
Foo 3 4

The asterisk decompression syntax can also be useful for string manipulation, such as String segmentation.

code example:
>>> line = ' nobody:*:-2:-2:unprivileged user:/var/empty:/usr/bin/false '
>>> uname, *fields, homedir, sh = line.split (': ')
>>> uname
' Nobody '
>>> Homedir
'/var/empty '
>>> SH
'/usr/bin/false '

Sometimes you want to extract some elements and discard them, you can't simply use *, but you can use a
An ordinary obsolete name, such as or IGN.
code example:
>>> record = (' ACME ', 50, 123.45, (12, 18, 2012))
>>> name, *_, (*_, year) = record
>>> Name
' ACME '
>>> year

2012

In many functional languages, the asterisk decompression syntax has a lot in common with list processing. For example, if you have
A list, you can easily split it into two parts:
>>> items = [1, 10, 7, 4, 5, 9]
>>> Head, *tail = items
>>> Head
1
>>> Tail
[10, 7, 4, 5, 9]
>>>
If you are smart enough, you can use this segmentation syntax to skillfully implement recursive algorithms. Like what:
>>> def sum (items):
... head, *tail = items
... return head + sum (tail) if tail else head
...
>>> sum (items)
36

python--(variable)

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.