tuple in python | learn Python

By | January 30, 2020
Spread the love

tuple in python it is a type in Python.Tuple means ordered collection.

example = (‘one’,’two’,’three’);
print(example)

the output will be one, two, three

print(example[1])

output be will

example[0] = 1 error tuple cannot be changed and the list can be changed.

tuple in python

difference between tuple and lists in python:

In list’s element can be changed but in a tuple, it cannot be changed.

In tuple, you can make a loop and can make mixed tuple,

examples of a tuple in python:

Example 1:


mixed = (1,2,3,4.9,'Asad')



 for i in mixed:
print(i)



the result will be 1 2 3 4.9, Asad

2nd Example of tuple in python:

mixed = (1,2,3,4.9)

print(len(mixed)

the result will be 4 because len function will print the length of items.

i=0;
while i<len(mixed):

print(i)

i+=1

it will print 0 1 2 3 4

because length of mixed is 5 so it will only print 5 items because it will be greater than the length of mixed so loop will be stop.

number = (1)

print(type(number)

it will print the datatype of number

result of this tuple will be int

name = (‘Rashid’,)

print(type(name)

it will print tuple if you remove comma then it will print string.

Python_class = ‘ Umer’,’Aslam’, 23,
print(type(Python_class))

it will print tuple

tuples are faster than list. In tuple you cannot change the data.

you can also define tuple like this

This will assign one tuple value to other tuple value

Ik, Do, Teen = (example)

print(Ik,Do,Teen)

it will print one two three

List_Within_tuple = (21,4.50,’Rashid’,[‘this’,2.90,’Python_class’])

print(List_Within_tuple)

pop()
apppend()
range()
list() it will create list e.g nums = list(nums)

nums1 = str(num)

it will convert number into string.

s

max = 1,2,3,4.9

print(min(mixed))
print(max(mixed))

print(sum(mixed))

  •  
    78
    Shares
  • 76
  •  
  •  
  •  
  • 2
  •  
  •  
  •   
  •   
  •   
  •   
  •  

Leave a Reply

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