Skip to main content

Python | Data Structure Or Data Type (Tuple)

Data Type Of Python

 In this session we will see last data type of python which is Tuple.

5) Tuple:- Tuple is a ordered collection of immutable datatype, tuples are quite similar to lists but difference is list are mutable where as tuple are immutable.
  • Empty tuple:- 
    t = () or tuple()

  • Tuple with single element:-
    t = (10,)

  • Tuple with multiple element:-
    t1 = ( 10, 20, 30, 'Hello' )

  • Nested tuple:- 
    t1 = ( 10, 20, 30, 'Hello' )
    p = t2, 30, 40, 50
    print(p) => ((10, 20, 30, 'Hello'), 30, 40, 50)

Methods of Tuple:- 
  1. count(value):- It return the count of value if the value is present in the tuple else it return 0.
    For example:-
    t = (10, 20, 30, 'Hello')
    t.count(10) => 1 
    t.count(50) => 0

  2. index(value,start,end):- It return the index number if the value is present in the tuple, otherwise it raised value error.
    For example:-
    t = (10, 20, 30, 'Hello')
    t.index(10) => 0
    It return the index number of value 10.


Comments

Popular posts from this blog

Python | Introduction

Python is developed by Guido van Rossum . Guido van Rossum started implementing python in 1989. Python is very simple programming language so if you are new to programming, you can learn python without facing any issue. Features of Python Free and Open Source -                                                                                      Python is a open source language. We can easily install python.It is freely available at python's official website . High level language -                                                                                     ...

Python | Recursive Function

Python - Recursive Function Recursive function means the function call themselves within the function body until a certain condition is matched then this function is recursive function. For example:-  def fact(n):      if n==0 or n==1:           return 1      else:           return n*fact(n-1) print(fact(8)) Output:- 40320 This is the example of recursive function. In this program we create a function named as fact and we call this function within the same function. How this program works? We give the value of n is 8 so n=8. In the first if condition, 8 is not equal to 0 and 1 so it goes to the else part in the else part the sentence is return n*fact(n-1) that means 8*fact(7) now here value of n become 7 to find fact(7) again same procedure repeat up to fact(1).   Because fact(1)=1   we give this condition. Then  fact(2) => 2*fact(1) =>  2*1 =>  2   ...

Python | Control Flow Tools

Control Flow Tools In this session we will see about control flow tools of python. 1) if Statement:- In the programming language if statement is important statement. Now we see how to write if statement in the python program. For Example:- >>> x = int(input("Please Enter an integer")) Please Enter an integer 50 >>> if x<0:      x=0      print("Negative change to zero") elif x==0: print("Zero") elif x==1: print("One") else: print("More") More >>>  Here is the program of if statement. You can add one or more elif part in the program. else part is the optional.      In the program we take the value of x is 50. In the if statement we write x<0 condition which is false because 50 is not less than 0 so it will go to the next part of the code which is first elif part, here we give condition x==0 this condition is also false so it will go to the second elif part which is x==1 this condition is also f...