tuples

Lists are great for storing data sets that can change while a program is running. Lists can be modified; however, sometimes you need to create a series of elements that cannot be modified, and tuples can fulfill this requirement. Python calls values that cannot be modified immutable, and immutable lists are called tuples.

Define a tuple

Tuples look like lists, but are identified with parentheses instead of square brackets. Once a tuple is defined, its elements can be accessed using indexes, just like list elements.

For example, if you have a rectangle whose size should not change, you can store its length and width in a tuple to ensure that they cannot be modified.

dimensions = (200.50)
print(dimensions[0])
print(dimensions[1])
Copy the code

Start by defining a tuple, dimensions, using parentheses instead of square brackets. The individual elements of the tuple are then printed separately using the same syntax used to access list elements.

The running result is as follows:

200
50
Copy the code

As mentioned earlier, elements in a tuple are not modifiable. What can go wrong if we deliberately modify the value of a tuple?

dimensions = (200.50)
dimensions[0] = 410
print(dimensions)
Copy the code

The running result is as follows:

Traceback (most recent call last): File "D :/github/Python_base/ code/demo11.py", line 2, in <module> dimensions[0] = 410 TypeError: 'tuple' object does not support item assignmentCopy the code

Since attempting to modify a tuple is prohibited, Python states that you cannot assign values to elements of a tuple.

Note: strictly speaking, tuples are identified by commas, parentheses just make the tuple look cleaner. If you need to define a tuple that contains only one element, you must put a comma after the element.

Iterate over all the values in a tuple

Like a list, you can also use a for loop to iterate over all the values in a tuple:

dimensions = (200.50)
for dimension in dimensions:
    print(dimension)
Copy the code

Modify a tuple variable

Although you cannot modify elements of a tuple, you can assign values to variables that store tuples. Therefore, if you want to modify the elements of a tuple, you need to redefine the tuple.

dimensions = (200.50)

print('Raw data')
for dimension in dimensions:
    print(dimension)

print('Modified data')
dimensions = (400.100)
for dimension in dimensions:
    print(dimension)

Copy the code

The running result is as follows:

Original data 200 50 Modified data 400 100Copy the code

Tuples are simpler data structures than lists, and can be used if you need to store a set of values that will remain constant throughout the life of the program.

Formatting code

As you write longer programs, it’s important to understand some code formatting conventions. Take the time to make your code as easy to read as possible. This will help you understand what the program does, and it will help others understand the code you write.

To ensure that everyone is writing code with the same format, Python programmers follow a few formatting conventions. Once you have learned to write clean Python, you can understand the overall structure of Python code written by others.

This is the beginning of becoming a qualified programmer.

Format Setting Guide

To suggest changes to the Python language, you need to write Python improvement proposals. PEP8, one of the oldest PEPs, provides guidelines for formatting code for Python programmers.

The indentation

PEP8 uses four Spaces for each level of indentation, which improves readability while leaving enough room for multiple levels of indentation.

governor

Professional programmers often need to open multiple files on a screen, and PEP8 recommends that comments be no longer than 72 characters long in order to keep each line of code in the programmer’s line of sight.

A blank line

To separate different parts of the program, use blank lines. You should use blank lines to organize your program file, but don’t abuse them. For example, if you have five lines of code, the first three lines are used to create lists, and the last two lines are used to process lists, you can use one blank line to separate the two sections instead of three or four blank lines.

The PEP8 format is particularly rich, and this article will not be complete until we move into more advanced Python structures.

The last

Nothing can be accomplished overnight, so is life, so is learning!

So what’s a three-day, seven-day crash?

Only insist, can succeed!

Biting books says:

Every word of the article is my heart to knock out, only hope to live up to every attention to my people. Click “like” at the end of the article to let me know that you are also working hard for your study.

The way ahead is so long without ending, yet high and low I’ll search with my will unbending.

I am book-learning, a person who concentrates on learning. The more you know, the more you don’t know. See you next time for more exciting content!