About Tuple in Python with examples | For loop and tuple | count , index len function slicing | Nested lists are obtained using nested indexing


About Tuple in Python with examples | For loop and tuple | count , index len function slicing | Nested lists are obtained using nested indexing
In this article, you will learn all about Python tuples. More specifically, what are tuples, how to make them, when to use them and the different ways you should be familiar. A tuple in Python is similar to a list. The difference between the two is that we cannot change the elements of a tuple as it is assigned whereas, in a list, the elements can be changed.

Tuple in python

tuple is a collection which is ordered and unchangeable. In Python tuples … You can access tuple items by referring to the index number, inside square 

# Tuple data structure

# tuple can store any data type # most important tuples are immutable ,

# one tuple is created you cann't update# data inside tuple
example = ('one','two','three','four')
# no append ,no insert ,no pop , no remove
days = ('monday','tuesday')

# tuple are faster then lists 

Method

count , index
len function
slicing
example[0]=1  # tuple do no assine 


number = (1,2,3,4,5,6,7.0)
 

Nested lists are obtained using nested indexing.


my_list = ['p', 'r', 'o', 'b', 'e']
# Output: P
Print (my_list [0])
# Output: o
Print (my_list [2])
# Output: E
Print (my_list [4])
# Error! Only integers can be used for indexing
# my_list [4.0]
# Nested list
n_list = ["Happy", [2,0,1,5]]
# Nested indexing
# Output: A
Print (n_list [0] [1])
# Output: 5
Print (n_list [1] [3])
 

# For loop and tuple


A  for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

This is less like the for keyword in other programming languages, and works more like an iterator method as found in other object-orientated programming languages.

With the for loop we can execute a set of statements,

 once for each item in a list, tuple, set etc.

for i in number:
    print(i)

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

Domain & free Hosting

Categories

You have successfully subscribed to myprogrammingschool

There was an error while trying to send your request. Please try again.

My Programming School will use the information you provide on this form to be in touch with you and to provide updates and marketing.