Table of Contents
Use tuple literals
Example
Output
Use another list
Create tuple from string
in conclusion
Home Backend Development Python Tutorial Create a Python program using tuple literals

Create a Python program using tuple literals

Sep 10, 2023 am 11:09 AM
python program tuple Literal value

Create a Python program using tuple literals

In Python, a tuple is an immutable sequence type typically used to store a collection of items.

Python tuples are very similar to Python lists in terms of nesting, indexing, and repetition, but one difference is that tuples are immutable, while lists are mutable, which means we can change the elements of the list, but not Do the same with tuples. Another difference is that lists are declared using square brackets [] while tuples are declared using simple brackets ().

In this article, we will discuss how to create tuples in Python using tuple literals. We'll introduce the syntax for defining tuples and provide examples of creating tuples using elements of different types.

We can declare a tuple in the following way -

1

2

3

tup1 = ("this" , "is" , “a” , "tuple")

tup2 = (1, 2, 3, 4, 5 )

tup3 = ("a", "b", "c", "d")

Copy after login

There are many ways to create tuples, but in this article, we will see how to create tuples using tuple literals.

Use tuple literals

Before using tuple literals to create tuple literals, let us first understand the meaning of literals in Python. The data we assign to variables and constants when we write code is called literals.

The values ​​of these literals will not change during the entire execution of the program. There are many types of literal in Python, but if we have to refer to a collection type object, such as a list, tuple or dictionary, then we have to use a so-called "< i>Literal Collection".

A tuple literal is an example of a collection of literals that can be used to create a tuple object in Python.

Example

In the following example, we will create a tuple using parentheses and comma declarations.

1

2

tuple1 = ( 1 , 2 , 3 , 4 , 5 , 6 , 7 )

print("the tuple is", tuple1 )

Copy after login

Output

The output of the above code is as follows -

1

the tuple is ( 1 , 2 , 3 , 4 , 5 , 6 , 7 )

Copy after login
Copy after login

Use another list

We will see how to create a tuple using another list. This method is also called type conversion. Essentially, what we do is create an object of type list and convert it to an object of tuple type. Using this method is an unconventional way to create a list, but is mentioned in this article as an alternative in case you need it.

Example

In the example below, we will create a list from another list. The tuple created will have the same elements as the list, but cannot be changed. We can do this by using the tuple() method, which is a built-in method provided by python to convert the data type in it to a tuple.

1

2

3

List1 = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]

tuple1 = tuple(List1)

print("the tuple is", tuple1 )

Copy after login

Output

The output of the above program is as follows -

1

the tuple is ( 1 , 2 , 3 , 4 , 5 , 6 , 7 )

Copy after login
Copy after login

Create tuple from string

We will see how to create tuples using form strings . With this approach, all characters of the string will be distinct elements of the created tuple. We can do this by using the tuple() function, which will convert the data type in it to a tuple.

Example

In the example below, we will create a list from strings . With this method, all characters in String will become elements of the tuple in the order in which they appear in String.

1

2

3

string1 = "This is a tuple"

tuple1 = tuple(string1)

print("the tuple is" , tuple1 )

Copy after login

Output

The output of the above program is as follows -

1

the tuple is ( ‘T’ , ‘h’ , ‘i’ , ‘s’ , ‘ ’ , ‘i’ , ‘s’ , ‘ ’ , ‘a’ , ‘ ’ , ‘t’ , ‘u’ , ‘p’ , ‘l’ , ‘e’ )

Copy after login

in conclusion

In this article, we discussed how tuples are created and their properties. We saw 3 different ways of creating tuples. In the first method, we create a tuple using parentheses with commas separating the elements. In the second method, we create a tuple from the list, in this method all the elements of the tuple are the same as the elements of the list from which it was created. In the third method, we create tuples from strings. All characters of a string are elements of a tuple.

The above is the detailed content of Create a Python program using tuple literals. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run python program in notepad++ How to run python program in notepad++ Apr 08, 2024 am 03:24 AM

Using Notepad++ to run a Python program requires the following steps: 1. Install the Python plug-in; 2. Create a Python file; 3. Set the run options; 4. Run the program.

Absolute tuple sum in Python Absolute tuple sum in Python Sep 12, 2023 pm 07:37 PM

In Python, tuples are immutable sequences that can store multiple elements of different types. They are often used to represent collections of related values. Tuple summation involves adding the corresponding elements of two or more tuples to produce a new tuple. However, in some scenarios, it may be necessary to calculate the absolute sum of elements instead of the traditional sum. In this blog post, we will explore how to perform absolute tuple sums in Python. Traditional Tuple Sum Before we delve into absolute tuple sum, let’s first understand how to do traditional tuple sum. Given two tuples of the same length, we can use a simple Python loop or list comprehension to calculate the sum of the corresponding elements −deftuple_sum(t1,t2):

PyCharm usage tutorial: guide you in detail to run the operation PyCharm usage tutorial: guide you in detail to run the operation Feb 26, 2024 pm 05:51 PM

PyCharm is a very popular Python integrated development environment (IDE). It provides a wealth of functions and tools to make Python development more efficient and convenient. This article will introduce you to the basic operation methods of PyCharm and provide specific code examples to help readers quickly get started and become proficient in operating the tool. 1. Download and install PyCharm First, we need to go to the PyCharm official website (https://www.jetbrains.com/pyc

Does PyCharm Community Edition support enough plugins? Does PyCharm Community Edition support enough plugins? Feb 20, 2024 pm 04:42 PM

Does PyCharm Community Edition support enough plugins? Need specific code examples As the Python language becomes more and more widely used in the field of software development, PyCharm, as a professional Python integrated development environment (IDE), is favored by developers. PyCharm is divided into two versions: professional version and community version. The community version is provided for free, but its plug-in support is limited compared to the professional version. So the question is, does PyCharm Community Edition support enough plug-ins? This article will use specific code examples to

PyCharm Advanced Tutorial: Use PyInstaller to package code into EXE format PyCharm Advanced Tutorial: Use PyInstaller to package code into EXE format Feb 20, 2024 am 09:34 AM

PyCharm is a powerful Python integrated development environment that provides a wealth of functions and tools to help developers improve efficiency. Among them, PyInstaller is a commonly used tool that can package Python code into an executable file (EXE format) to facilitate running on machines without a Python environment. In this article, we will introduce how to use PyInstaller in PyCharm to package Python code into EXE format, and provide specific

python program development process python program development process Apr 20, 2024 pm 09:22 PM

The Python program development process includes the following steps: Requirements analysis: clarify business needs and project goals. Design: Determine architecture and data structures, draw flowcharts or use design patterns. Writing code: Program in Python, following coding conventions and documentation comments. Testing: Writing unit and integration tests, conducting manual testing. Review and Refactor: Review code to find flaws and improve readability. Deploy: Deploy the code to the target environment. Maintenance: Fix bugs, improve functionality, and monitor updates.

Flask installation and configuration tutorial: a tool to easily build Python web applications Flask installation and configuration tutorial: a tool to easily build Python web applications Feb 20, 2024 pm 11:12 PM

Flask installation and configuration tutorial: A tool to easily build Python Web applications, specific code examples are required. Introduction: With the increasing popularity of Python, Web development has become one of the necessary skills for Python programmers. To carry out web development in Python, we need to choose a suitable web framework. Among the many Python Web frameworks, Flask is a simple, easy-to-use and flexible framework that is favored by developers. This article will introduce the installation of Flask framework,

How to run a python program after writing it How to run a python program after writing it Apr 20, 2024 pm 08:59 PM

There are two ways to run a program in Python: 1. Using the Python interpreter; 2. Calling a script directly. Specific steps include: ① Create a Python script file; ② Open a terminal or command prompt; ③ Navigate to the script directory; ④ Use the corresponding method to run the script; ⑤ View the output.

See all articles