3-1-2-27. Tuples
Consider this example involving latitude and longitude.
Tuples are similar to lists in that they store
an ordered collection of objects which can be accessed by their indices.
For example, location zero and location one.
Unlike lists however, tuples are immutable.
You cannot add or remove items from tuples or sort them in place.
Why do we have tuples if they’re like lists with fewer features?
Tuples are useful when you have two or more values that are so
closely related that they will always be used together,
like latitude and longitude coordinates.
Tuples can also be used to assign multiple variables in a compact way.
Notice that the values assigned to the tuple dimensions
aren’t surrounded with parentheses as previous examples were.
The parentheses are optional when making tuples and
programmers frequently omit them if parentheses don’t clarify the code.
In the second line,
three variables are assigned from the content of the tuple dimensions,
this is called tuple unpacking.
You can use tuple unpacking to assign the information from a tuple
into multiple variables without having to access them one by one,
and make multiple assignment statements.
In this example, if we won’t need to use dimensions directly,
we could shorten those two lines of code into
a single line that assigns three variables in one go.
Python은 또 다른 유용한 컨테이너인 튜플을 제공합니다.
튜플은 관련 정보를 저장하는 데 사용됩니다.
튜플은 요소의 변경 불가능한 순서화된 시퀀스인 Python의 데이터 구조입니다.
위도와 경도를 포함하는 이 예를 고려하십시오.
튜플은 저장한다는 점에서 목록과 유사합니다.
인덱스로 액세스할 수 있는 정렬된 개체 컬렉션입니다.
예를 들어 위치 0과 위치 1입니다.
그러나 목록과 달리 튜플은 변경할 수 없습니다.
튜플에서 항목을 추가 또는 제거하거나 제자리에서 정렬할 수 없습니다.
기능이 더 적은 목록과 같은 경우 튜플이 있는 이유는 무엇입니까?
튜플은 다음과 같은 두 개 이상의 값이 있을 때 유용합니다.
그들은 항상 함께 사용될 것이므로 밀접하게 관련되어 있습니다.
위도와 경도 좌표처럼.
튜플은 또한 간결한 방식으로 여러 변수를 할당하는 데 사용할 수 있습니다.
튜플 차원에 할당된 값이
이전 예와 같이 괄호로 묶이지 않습니다.
튜플을 만들 때 괄호는 선택 사항이며
괄호가 코드를 명확히 하지 않으면 프로그래머는 자주 생략합니다.
두 번째 줄에서,
튜플 차원의 내용에서 세 개의 변수가 할당됩니다.
이것을 튜플 언패킹이라고 합니다.
튜플 압축 풀기를 사용하여 튜플에서 정보를 할당할 수 있습니다.
하나씩 액세스하지 않고도 여러 변수에
여러 할당문을 작성합니다.
이 예에서 차원을 직접 사용할 필요가 없다면
두 줄의 코드를 다음으로 줄일 수 있습니다.
한 번에 세 개의 변수를 할당하는 단일 라인.
Tuples
A tuple is another useful container. It’s a data type for immutable ordered sequences of elements. They are often used to store related pieces of information. Consider this example involving latitude and longitude:
location = (13.4125, 103.866667) print("Latitude:", location[0]) print("Longitude:", location[1])
Tuples are similar to lists in that they store an ordered collection of objects which can be accessed by their indices. Unlike lists, however, tuples are immutable – you can’t add and remove items from tuples, or sort them in place.
Tuples can also be used to assign multiple variables in a compact way.
dimensions = 52, 40, 100 length, width, height = dimensions print("The dimensions are {} x {} x {}".format(length, width, height))
The parentheses are optional when defining tuples, and programmers frequently omit them if parentheses don’t clarify the code.
In the second line, three variables are assigned from the content of the tuple dimensions. This is called tuple unpacking. You can use tuple unpacking to assign the information from a tuple into multiple variables without having to access them one by one and make multiple assignment statements.
If we won’t need to use dimensions
directly, we could shorten those two lines of code into a single line that assigns three variables in one go!
length, width, height = 52, 40, 100 print("The dimensions are {} x {} x {}".format(length, width, height))
댓글을 달려면 로그인해야 합니다.