Comptt

Comptt
  • Home
  • Features
  • _Multi DropDown
  • __Dropdown 1
  • __Dropdown 2
  • __Dropdown 3
  • _ShortCodes
  • _Sitemap
  • _Error Page
  • Documentation
  • _Web Doc
  • _Video Doc
  • Download This Template
February 05, 2023

 Tuples in Python


What are the Tuple?


Tuples in Python are similar to lists. Tuples can store multiple values of different data types, such as integers, strings, and other data structures like lists or tuples.
Tuples can be used to group values together, making it easier to work with them.

Tuple properties

Tuple can be enclosed with Parentheses or Round Brackets, ( ) and the values inside the tuple separated with commas.

 fruits = ('apple', 'banana', 'cherry')
print(fruits)

//Output
('apple', 'banana', 'cherry')

Python Tuple are mutable 

This means that once a tuple is created, its elements cannot be changed, added, or removed. When you try to change the elements of a tuple, you will get a Type Error.

For example:

 fruits = ('apple', 'banana', 'cherry')
fruits[0] = 'pear' # This will raise a TypeError
print(fruits)

//Output
TypeError: 'tuple' object does not support item assignment

Though a tuple can store similar data it is commonly used for storing dissimilar data. 

 a = ()                  # empty tuple
b = (10,) # tuple with one item, after 10 is necessary
c = ("John", 3.5, 10) # tuple with dissimilar items
d = (1, 2, 3, 4, 5) # tuple with similar items
print(a)
print(b)
print(c)
print(d)

//Output
()
(10,)
('John', 3.5, 10)
(1, 2, 3, 4, 5)
While creating the tuple b, if we do not use the comma after 10, b is treated to be of type int.

While initializing a tuple, we may drop ( ).

 a = 'John', 3.5, 10   # tuple with multiple items
print(a) # without bracket also tuple can be execute
print(type(a)) # a is of the type tuple

//Output
('John', 3.5, 10)
<class 'tuple'>

Tuple slicing

In Python, tuple slicing is the process of accessing a sub-tuple from an existing tuple. Tuple slicing is similar to list slicing and is performed using the square bracket notation, just like with lists.

Here's an example of slicing a tuple in Python:

 fruits = ('apple', 'banana', 'cherry', 'orange')
print(fruits[1:3])

//output
('banana', 'cherry')

In this example, the slice 'fruits[1:3]' creates a new tuple that contains the second and third elements (index 1 and 2) of the original tuple 'fruits'.

You can also use negative indices to count from the end of the tuple:

 fruits = ('apple', 'banana', 'cherry', 'orange')
print(fruits[-2:])

//output
('cherry', 'orange')

Additionally, you can specify a step value to skip elements while slicing:

 fruits = ('apple', 'banana', 'cherry', 'orange')
print(fruits[::2])

//output
('apple', 'cherry')
Tuple slicing is a powerful and flexible tool for accessing and manipulating sub-tuples in Python. It can be used to extract and process data in a variety of ways, making it an important part of the Python language.


Tuple methods

Python tuple has two built-in methods that can you use.

Method

Description

count()

Returns the number of times a specified value occurs in a tuple

index()

Searches the tuple for a specified value and returns the position of where it was found




Share This:
Facebook Twitter Pinterest Linkedin Whatsapp Whatsapp
at February 05, 2023
Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest

No comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

Popular

Facebook

Subscribe Us

Tags

Android-Hacking Code HTML Programming Language python
Powered by Blogger.

Exclusive content in your inbox

Do you want to be creative? Subscribe to Our Newsletter!
* We promise that we don't spam !

* We promise that we don't spam !

Follow us By Email

Get new post updates by email
Powered by follow.it

FOLLOW US BY EMAIL

Get new post updates by email
Powered by follow.it

Report Abuse

Comptt

Search This Blog

  • March 20236
  • February 202315
  • January 202313

Social Plugin

  • facebook
  • whatsapp
  • instagram
  • youtube
  • twitter
  • linkedin
  • email
  • github
Educational Collaborator

Educational Collaborator

Zeal Education Society Narhe, Pune
Educational Collaborator

Educational Collaborator

JSPM Narhe Technical Campus, Narhe, Pune
Project Manager

Project Manager

Mr. Ayushman Sanjay Dalvi | Project Manager for Technicals
Special Collaborator

Special Collaborator

Cyber Security Mumbai | Special Collaborator for Cyber Security and Contents
  • Home
  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Courses
  • Join Us
  • Affiliate
  • Home-icon
  • Courses
  • Programming Lanuages
  • _HTML
  • _JavaScript
  • _Python
  • _Java
  • Programming Notes
  • Compilers
  • Ethical Hacking
  • About Site
  • _About US
  • _Privacy Policy
  • _Cookie Policy
  • _Terms & Conditions
  • _Contact US
  • Join Us
  • Certificate

Menu Footer Widget

  • Home
  • About
  • Privacy Policy
  • Terms & Conditions
  • Contact Us
  • Instagram
  • Join Us
  • Affiliate

Social Plugin

Our honorable CEO and Experts

Founder and CEO and All the Expert Team Members

Courses Provided by Us

You'll discover all of the most up-to-date bring innovative here.

Special Topics

  • Programming Language

Founder and CEO

Mr. Rohan Kumar Bhoi
Hello, this is Rohan Kumar Bhoi, CEO at CompTT. "Develop more skills to be self-dependent! Learn Rapidly, Be Unique, Live Stylish" facebook twitter youtube instagram linkedin github
    Pune, Maharashtra, India

    Latest posts

    10/recent/ticker-posts
    Project Manager

    Project Manager

    Mr. Aayan Sattar Mulla | Project Manager for Formating and Editing
    Honorable Founder and CEO

    Honorable Founder and CEO

    Mr. Rohan Kumar Bhoi

    "Learn new Skills with Us, and Be SELF DEPENDENT"

    With our easy-to-follow tips and tricks, you'll be self-sufficient in no time! Plus, we have solutions to all your computer-related pro…
    "Learn new Skills with Us, and Be SELF DEPENDENT"

    Courses

    • HTML
    Crafted with by Computer Tips-Triks || Distributed by Mr.Rohan Kumar Bhoi || © Copyright 2019-2023 Computer Tips-Triks All Right Reseved
    Design by - Free Blogger Templates | Distributed by Blogger Templates
    Comptt

    Made for WORLD by

    computertipstricks.tech and designed by Mr. Rohan Kumar Bhoi and Team. It is a one of the best Educational Hub for everyone looking for Computer Knowledge.
    • Home
    • Compilers
    • About Us
    • Privacy Policy
    • Courses
    • Join Us
    • Blog
    • Study Material
    • Home
    • About
    • Contact us
    • Privacy Policy
    • Cookie Policy
    • T & C

    Recent in Posts

    3/Ethical%20Hacking/post-list

    Facebook

    Telegram

    Telegram Group
    Comptt

    Popular Posts

    January 25, 2023

    Pages

    • Contact
    • About
    Created By Themes | Distributed By Gooyaabi Template