Home >Backend Development >Python Tutorial >How to use python tuple

How to use python tuple

藏色散人
藏色散人Original
2019-06-26 09:38:464083browse

Python is a widely used interpreted, high-level programming, general-purpose programming language created by Guido van Rossum. The first version was released in 1991. It can be regarded as an improved LISP. Python's design philosophy emphasizes code readability and concise syntax. Python allows developers to express ideas in less code than C or Java. Whether it is a small or large program, the language attempts to make the structure of the program clear and unambiguous.

How to use python tuple

How to use python tuple?

Python tuple tuple() function converts a list into a tuple.

tuple() method syntax:

tuple( seq )

Parameters

seq -- The sequence to be converted to a tuple.

Return value

Return tuple.

Example

The following example shows how to use the tuple() function:

Example 1

>>>tuple([1,2,3,4])
 
(1, 2, 3, 4)
 
>>> tuple({1:2,3:4})    #针对字典 会返回字典的key组成的tuple
 
(1, 3)
 
>>> tuple((1,2,3,4))    #元组会返回元组自身
 
(1, 2, 3, 4)

Example 2

#!/usr/bin/python
 
aList = [123, 'xyz', 'zara', 'abc'];
aTuple = tuple(aList)
 
print "Tuple elements : ", aTuple

Above The output result of the example is:

Tuple elements :  (123, 'xyz', 'zara', 'abc')

Related recommendations: "Python Tutorial"

The above is the detailed content of How to use python tuple. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn