Introduction

Setup

Operators

variable

String

Number

Dtype & Comment

Lists

Tuples

Sets

Dictionaries

if else statement

For Loop

While Loop

iterator & iterable

Pre-Built functions

Function

Decorators

generators

Types of errors

Exceptions Handling

OOP

Regular expression

Create module

OS module

Date module

Random module

Math

Statistics module

Exercise 1

Exercise 2

Exercise 3

Learn Data Structure & Algorithm

Learn Numpy

Learn Pandas

Learn Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Python tuple

What is tuple?

A tuple is used to store multiple collections of values in one variable. We can store too many values in a tuple and perform a lot of work on it. We can store string, number, or both at the same time in a Tuple. We can use capital letters and small letters in a Tuple. We can store duplicate values in a tuple. Once we create a tuple, we can't change or update or remove the values of the tuple.
In list we can do change, update or remove values or data but in tuple after creating the list we can't remove or delete or change any value. So it is a difference between list and tuple.

What is index?

Index means the position number of each value which are stored in the tuple. In python, the index starts from 0. If there are 5 values in the tuple then its first index is 0 and the last index =(n-1) =(5-1)=4. Index numbers can be used in various ways. It's a very important thing. We can get tuple values using an index number. Suppose a tuple contains ten values or element. Now if we want to get first five element or if we want to get a particular value like number eight position value then we can do that by using index number.

How to create a tuple?

We write tuple inside a first bracket.
So at first give the first brackets().
The string must be written inside a single or double quotation ('',") and for numbers no need to give a single or double quote.
Give a comma between two values.
If we write numbers in the quotation then it will act as a string and we won't be able to perform mathematical work on that tuple element. If we write numbers and letters or words together then we must use quotations. Because if we write number and letters together then it no longer number it wil become a string. That's why we have to give double or singe quotation if we write number and text together.

Example:
Input
x=('A','b','C')
y=("1","2","3")
z=("A","2","c")
s=(1,2,3)
v=("1A","2b",3)
u=(1,"2b",3,"C")
print(x)
print(y)
print(z)
print(s)
print(n)
print(u)
Output
('A','b','C')
("1","2","3")
("A","2","c")
(1,2,3)
("1A","2b",3)
(1,"2b",3,"C")

In the example we can see six different tuple. The process to creating the tuples is same but we put different data types of value in each tuple. In the first tuple we have all the string value, second tuple contain number as string, fourth contain number values, fifth and sixth contain both number and string value together.

Tuple functions

in

To check that a value is present in the tuple or not "in" is used. If the value is present in the tuple then it will return true otherwise it will return false.

Example:
Input
x=("a","b","c","d")
print("a" in x)
Output
True

In the example we are checking if the x tuple containing a as an element or not. Because the tuple containing a as an element.

Count() function

Count() function shows that how many times a specified number appears in the tuple.

Example:
Input
thistuple = (1, 2, 7, 4, 7, 4, 4, 6, 8, 5)
x = thistuple.count(4)
print(x)
Output
3

In the example the tuple containing the value 4 three times. That's why we got the output 3.

index() function

If we pass a value that is present in a tuple then the index function will show the index number of that value. If that value appears more than one time then this function selects that value which it gets the first time.

Example:
Input
thistuple = (1, 1, 7, 4, 7, 4, 4, 6, 8, 5)
x = thistuple.coindexunt(1)
print(x)
Output
0

Look in the example 1 is in first and second position but the index function took the first 1 for work and return it index number 0.

Tuple Slicing

By index number, you can get a single value or a set of single values from the tuple and this is called tuple slicing. Negative indexing means starting from the end of the tuple.

Example:1
Input
x=("a","b","c","d","e")
y=x[2]
z=x[-2]
print(y)
print(z)
Output
c
d

In the example, the tuple 2 index position value is c. So we get the output c. -2 means the selection will start from the end. Now if we go to the end of the tuple then we will see the last second value is d. So we got the output is d.

Example:2
Input
x=("a","b","c","d","e")
y=x[2:]
z=x[:-1]
print(y)
print(z)
Output
('c', 'd', 'e')
('a', 'b', 'c', 'd')
Example:3
Input
x=("a","b","c","d","e","w","q","z","m")
y=x[2:5]
z=x[-5:-1]
print(y)
print(z)
Output
('c', 'd', 'e')
('e', 'w', 'q', 'z')

How to Join tuples?

We can join multiple tuple and create one tuple.

Example:
Input
x=("a","b","c","d")
y=(1,2,3,4)
z=x+y
print(z)
Output
("a","b","c","d",1,2,3,4)

Here we joined two different tuples and created a single tuple

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.