Skip to main content

Python tuple positive indexing or forward indexing and native indexing or backward indexing

Python tuple positive indexing or forward indexing  and native indexing or  backward indexing 

Positive index      0      1        2       3        4       5
    
                               90     50     30     20     99    10

Native index        -6      -5      -4      -3      -2      -1 

#accessing elements in python tuple indexing number and slicing

#python tuple positive indexing or forward indexing stat with 0 end with n-1

tuple=(40,70,12,23,78,"ram",45.77,78)

print(tuple)           #print all elements in tuple

print(tuple[3])      #print 3rd index element in tuple

print(tuple[1:4])  #print 1st index element to 4rd index element

print(tuple[:5])    #print 0 index elements to 5th index element

print(tuple[2:])    #print 2rd index elements to end of the tuple elements


#python tuple backward indexing or native indexing

print(tuple[-1]) #

print(tuple[-5:-1])

      

#iterate a tuple using positive indexing

pytuple=('d','j','a','n','g','o')

print(pytuple) #print all elements in tuple

for x in range(4):#range function stat with 0 to end 4

    print(pytuple[x])#x is iterate varaible it will print every time singlevalue


# iterate a tuple using negative indexing

pytuple=('d','j','a','n','g','o')

for x in range(-6,0): #print native indexing -6 index to before 0 index

    print(pytuple[x])


#how to check if an item exists or not in tuple elements

x=(45,89,34,55,'ram',90)

print(34 in x) #it is only for True or False 

#or

if(34 in x):

    print("34 is in x element")

else:

    print("34 is not in x element")

   

#python tuple length function

tuple=(56,23,44,89,12,'welcome')

print(tuple)

print(len(tuple))  

Comments

Popular posts from this blog

remove specific element in python list - remove index based element

  #remove specific element in python list list=list([34,55,34,66,77,45.6,23,34,]) print(list) list.remove(66) print(list) output: [34, 55, 34, 66, 77, 45.6, 23, 34] [34, 55, 34, 77, 45.6, 23, 34] #Remove all occurrence of a specific python repeated element mylist = list([26, 4, 26, 26, 8, 12]) for item in mylist:     mylist.remove(26) print(mylist) output :  [4,8,12] #remove item present at given index mylist = list([2, 4, 6, 8, 10, 12]) mylist.pop(2)  #remove 2 index based element print(mylist) output:  [2, 4, 8, 10, 12] # remove item without passing index number(last value remove) using pop() mylist = list([2, 4, 6, 8, 10, 12]) mylist.pop()   print(mylist) output : [2, 4, 6, 8, 10] [2, 4, 8, 10] #remove the range of elements python list with slice: mylist = list([2, 4, 6, 8, 10, 12]) #remove item from index 2 to 5 del mylist[2:5] print(mylist) output: [2, 4, 12] #remove all items starting from index 2 mylist=list([23,44,55,34,53,78,55]) del myl...

Python tuple packing and unpacking - Python coding

Python tuple packing and unpacking python tuple can also be created without using a tuple() class or enclosing the items inside the parentheses. it is called the variable packing #python tuple packing into tuple tuple=23,44,66,'django' print(tuple) print(type(tuple)) #unpacking tuple into variable a,b,c,d=tuple print("a is =",a) print("b is =",b) print("c is =",c) print("d is =",d) print(type(a)) print(type(b)) print(type(c)) print(type(d)) output :  (23, 44, 66, 'django') <class 'tuple'> a is = 23 b is = 44 c is = 66 d is = django <class 'int'> <class 'int'> <class 'int'> <class 'str'>

Python Complex Datatypes

Python Complex Datatypes    A complex number is a number with a real and an imaginary component represented as a+bj where a and b contain integers or floating-point values. # both values are int type a = 9 + 8j           print(a)      print(type(a)) # one int and one float values b = 10 + 4.5j        print(b)       print(type(b)) # both values are float type c = 11.2 + 1.2j      print(c) print(type(c))