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 | 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   ...

Basic Knowledge of Python part 2

Strings using python - Other than numbers python can also handle strings which can be expressed by several ways. In programming a string is a contiguous sequence of symbols or values such as a character string or digit string. Strings is declared in a '....' (single quotes) or "...." (double quotes).     By using  '\n'  you can insert the string to the new line by using  print()  command. For example:  We can concatenate string by using + operator. Concatenate means we can merge two or more separate strings by using + operator. For example: We see another example of multiple concatenate. Here 3*'py' means the string 'py' repeats 3 times and same for the 'thon' string. Now we another method to concatenate two or more strings. In this method you have write strings next to each other without any operator. For example: In the python string is stored in indexes. The index is start from 0 (zero). In python indexes also be calculated from reve...

Python | Data Structures or Data Types (List)

Data Type of Python In Python there are two types of data types which are Mutable and Immutable. Mutable data type contains List and Dictionary data types. And Immutable data type contains String,Tuple and Sets data types. Now we see what is Mutable and Immutable data type? The main object get changed permanently after applying methods of data type this data type is called as Mutable data type. And the main object will not be changed permanently after applying methods of data type this data type is called as Immutable data types. Now we see data types one by one! 1) List - List is an ordered collection of mutable datatype. Lists are hashable, iterable. List contain any kind of data. List are declared in square braces[]. Empty List-  L = [ ] List with multiple elements -  L1 = [10, 15.10, 'Python', [10,20] ] In the above list you can add any data type as shown above. In list L1 there is Integer, float,String,list data types. To add string data type you must have to declared in ...